-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: aiordache <anca.iordache@docker.com>
- Loading branch information
Showing
6 changed files
with
156 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Python: Current File", | ||
"type": "python", | ||
"request": "launch", | ||
"program": "${file}", | ||
"console": "integratedTerminal" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"editor.codeActionsOnSave": { | ||
"source.organizeImports": false | ||
}, | ||
"python.formatting.autopep8Args": [ | ||
"--ignore", | ||
"E402" | ||
], | ||
"python.testing.unittestEnabled": false, | ||
"python.testing.nosetestsEnabled": false, | ||
"python.testing.pytestEnabled": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
TEST_API_VERSION ?= 1.39 | ||
TEST_ENGINE_VERSION ?= 19.03.12 | ||
TEST_ENGINE_VERSION ?= 19.03.13 | ||
|
||
.PHONY: all | ||
all: test | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import json | ||
import io | ||
from docker import APIClient, from_env | ||
from docker import ContextAPI | ||
from docker import TLSConfig | ||
import six | ||
|
||
def test_get_current_context(): | ||
ctx = ContextAPI.get_current_context() | ||
|
||
print(json.dumps(ctx.inspect(), indent = 2)) | ||
|
||
# get list with running containers data | ||
client = APIClient(base_url=ctx.Host, tls=ctx.TLSConfig) | ||
containers = client.containers() | ||
print(ctx.Host) | ||
print("Running container IDs:") | ||
for c in containers: | ||
print("\t{}".format(c['Id'])) | ||
|
||
|
||
def test_create_docker_context(): | ||
""" tls = TLSConfig( | ||
client_cert=("/tmp/certs/cert.pem", "/tmp/certs/key.pem"), | ||
ca_cert="/tmp/certs/ca.pem", | ||
)""" | ||
ctx = ContextAPI.create_context("test", host="tcp://doesnotexist:8000")#, tls_cfg=tls) | ||
print(json.dumps(ctx.inspect(), indent = 2)) | ||
|
||
def test_remove_docker_context(): | ||
ContextAPI.remove_context("test") | ||
|
||
def test_get_docker_context(): | ||
ctx = ContextAPI.get_context("dev") | ||
if not ctx: | ||
return | ||
print(json.dumps(ctx.inspect(), indent = 2)) | ||
|
||
def test_inspect_docker_context(): | ||
inspect = ContextAPI.inspect_context("dev") | ||
print(json.dumps(inspect, indent = 2)) | ||
|
||
def test_set_current_docker_context(): | ||
ContextAPI.set_current_context("dev") | ||
|
||
def test_docker_client(): | ||
ContextAPI.set_current_context("default") | ||
#ctx = ContextAPI.get_context("default") | ||
#client = APIClient(base_url=ctx.Host, tls=ctx.TLSConfig) | ||
client = from_env() | ||
client.containers.run('docker:19.03.3-dind', ports={'2375/tcp': 2375}, privileged = True, environment=["DOCKER_TLS_CERTDIR="], auto_remove = True) | ||
|
||
#client = from_env(environment = {"DOCKER_HOST":"tcp://localhost:2375"}) | ||
#client.containers.run("docker:19.03.3-dind", name = "composecontext-dind", ports={"2375/tcp": 2388}, privileged = True, environment=["DOCKER_TLS_CERTDIR="], auto_remove = True, detach = True) | ||
|
||
|
||
def test_run(): | ||
client = from_env() | ||
#client.containers.run("alpine", "sleep 300", detach=True) | ||
images = client.images.pull("alpine", all_tags=True) | ||
|
||
|
||
def test_ssh_client(): | ||
client = APIClient(base_url='ssh://dind', use_ssh_client=True) | ||
containers = client.containers() | ||
print("Running container IDs:") | ||
for c in containers: | ||
print("\t{}".format(c['Id'])) | ||
print('\n') | ||
return client | ||
|
||
|
||
def run_container(client, *args, **kwargs): | ||
container = client.create_container(*args, **kwargs) | ||
client.start(container) | ||
exitcode = client.wait(container)['StatusCode'] | ||
|
||
if exitcode != 0: | ||
output = client.logs(container) | ||
raise Exception( | ||
"Container exited with code {}:\n{}" | ||
.format(exitcode, output)) | ||
|
||
return container | ||
|
||
|
||
def test_build_with_extra_hosts(): | ||
client = test_ssh_client() | ||
img_name = 'dockerpytest_extrahost_build' | ||
|
||
script = io.BytesIO('\n'.join([ | ||
'FROM busybox', | ||
'RUN ping -c1 hello.world.test', | ||
'RUN ping -c1 extrahost.local.test', | ||
'RUN cp /etc/hosts /hosts-file' | ||
]).encode('ascii')) | ||
|
||
stream = client.build( | ||
fileobj=script, tag=img_name, | ||
extra_hosts={ | ||
'extrahost.local.test': '127.0.0.1', | ||
'hello.world.test': '127.0.0.1', | ||
}, decode=True | ||
) | ||
for chunk in stream: | ||
if 'errorDetail' in chunk: | ||
print("\nerr in Detail\n") | ||
|
||
print(client.inspect_image(img_name)) | ||
ctnr = run_container(client, img_name, 'cat /hosts-file') | ||
logs = client.logs(ctnr) | ||
if six.PY3: | ||
logs = logs.decode('utf-8') | ||
print('127.0.0.1\textrahost.local.test' in logs) | ||
print('127.0.0.1\thello.world.test' in logs) | ||
|
||
|
||
|
||
if __name__ == "__main__": | ||
test_ssh_client() | ||
#test_build_with_extra_hosts() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters