mirror of
https://github.com/ansible-collections/community.docker.git
synced 2026-07-29 11:55:04 +00:00
Add docker_context_info module (#1039)
* Vendor parts of the Docker SDK for Python This is a combination of the latest git version (https://github.com/docker/docker-py/tree/db7f8b8bb67e485a7192846906f600a52e0aa623) with some fixes to make it compatible with Python 2.7 and adjusting some imports. * Polishing. * Fix bug that prevents contexts to be found when no Docker config file is present. Ref: https://github.com/docker/docker-py/issues/3190 * Linting. * Fix typos. * Adjust more to behavior of Docker CLI. * Add first iteration of docker_context_info module. * Improvements. * Add basic CI. * Add caveat on contexts[].config result.
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# This code is part of the Ansible collection community.docker, but is an independent component.
|
||||
# This particular file, and this file only, is based on the Docker SDK for Python (https://github.com/docker/docker-py/)
|
||||
#
|
||||
# Copyright (c) 2016-2025 Docker, Inc.
|
||||
#
|
||||
# It is licensed under the Apache 2.0 license (see LICENSES/Apache-2.0.txt in this collection)
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
import unittest
|
||||
|
||||
import pytest
|
||||
|
||||
from ansible_collections.community.docker.plugins.module_utils._api import errors
|
||||
from ansible_collections.community.docker.plugins.module_utils._api.constants import (
|
||||
DEFAULT_NPIPE,
|
||||
DEFAULT_UNIX_SOCKET,
|
||||
IS_WINDOWS_PLATFORM,
|
||||
)
|
||||
from ansible_collections.community.docker.plugins.module_utils._api.context.api import ContextAPI
|
||||
from ansible_collections.community.docker.plugins.module_utils._api.context.context import Context
|
||||
|
||||
|
||||
class BaseContextTest(unittest.TestCase):
|
||||
@pytest.mark.skipif(
|
||||
IS_WINDOWS_PLATFORM, reason='Linux specific path check'
|
||||
)
|
||||
def test_url_compatibility_on_linux(self):
|
||||
c = Context("test")
|
||||
assert c.Host == DEFAULT_UNIX_SOCKET[5:]
|
||||
|
||||
@pytest.mark.skipif(
|
||||
not IS_WINDOWS_PLATFORM, reason='Windows specific path check'
|
||||
)
|
||||
def test_url_compatibility_on_windows(self):
|
||||
c = Context("test")
|
||||
assert c.Host == DEFAULT_NPIPE
|
||||
|
||||
def test_fail_on_default_context_create(self):
|
||||
with pytest.raises(errors.ContextException):
|
||||
ContextAPI.create_context("default")
|
||||
|
||||
def test_default_in_context_list(self):
|
||||
found = False
|
||||
ctx = ContextAPI.contexts()
|
||||
for c in ctx:
|
||||
if c.Name == "default":
|
||||
found = True
|
||||
assert found is True
|
||||
|
||||
def test_get_current_context(self):
|
||||
assert ContextAPI.get_current_context().Name == "default"
|
||||
|
||||
def test_https_host(self):
|
||||
c = Context("test", host="tcp://testdomain:8080", tls=True)
|
||||
assert c.Host == "https://testdomain:8080"
|
||||
|
||||
def test_context_inspect_without_params(self):
|
||||
ctx = ContextAPI.inspect_context()
|
||||
assert ctx["Name"] == "default"
|
||||
assert ctx["Metadata"]["StackOrchestrator"] == "swarm"
|
||||
assert ctx["Endpoints"]["docker"]["Host"] in (
|
||||
DEFAULT_NPIPE,
|
||||
DEFAULT_UNIX_SOCKET[5:],
|
||||
)
|
||||
@@ -255,7 +255,7 @@ class ParseEnvFileTest(unittest.TestCase):
|
||||
class ParseHostTest(unittest.TestCase):
|
||||
def test_parse_host(self):
|
||||
invalid_hosts = [
|
||||
'0.0.0.0',
|
||||
'foo://0.0.0.0',
|
||||
'tcp://',
|
||||
'udp://127.0.0.1',
|
||||
'udp://127.0.0.1:2375',
|
||||
|
||||
Reference in New Issue
Block a user