Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: aiordache <anca.iordache@docker.com>
  • Loading branch information
aiordache committed Oct 8, 2020
1 parent 19e8141 commit 041e654
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 11 deletions.
15 changes: 15 additions & 0 deletions .vscode/launch.json
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"
}
]
}
12 changes: 12 additions & 0 deletions .vscode/settings.json
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
}
2 changes: 1 addition & 1 deletion Makefile
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
Expand Down
8 changes: 1 addition & 7 deletions docker/transport/sshconn.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,7 @@ def connect(self, **kwargs):
stdin=subprocess.PIPE)

def sendall(self, msg):
if not self.proc or self.proc.stdin.closed:
raise Exception('SSH subprocess not initiated.'
'connect() must be called first.')
self.proc.stdin.write(msg)
self.proc.stdin.flush()
self.send(msg)

def send(self, msg):
if not self.proc or self.proc.stdin.closed:
Expand Down Expand Up @@ -153,8 +149,6 @@ def __init__(self, ssh_client=None, timeout=60, maxsize=10, host=None):
self.ssh_transport = ssh_client.get_transport()
self.timeout = timeout
self.host = host

# self.base_url = six.moves.urllib_parse.urlparse(host)
self.port = None
if ':' in host:
self.host, self.port = host.split(':')
Expand Down
123 changes: 123 additions & 0 deletions main.py
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()


7 changes: 4 additions & 3 deletions tests/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
ARG PYTHON_VERSION=3.7

FROM python:${PYTHON_VERSION}

ARG APT_MIRROR
RUN sed -ri "s/(httpredir|deb).debian.org/${APT_MIRROR:-deb.debian.org}/g" /etc/apt/sources.list \
&& sed -ri "s/(security).debian.org/${APT_MIRROR:-security.debian.org}/g" /etc/apt/sources.list

RUN apt-get update && apt-get -y install --no-install-recommends \
gnupg2 \
pass \
curl \
openssh-client
gnupg2 \
openssh-client \
pass

# Add SSH keys and set permissions
COPY tests/ssh-keys /root/.ssh
Expand Down

0 comments on commit 041e654

Please sign in to comment.