many tests have been implemented

This commit is contained in:
Peter Yu 2013-11-07 19:49:34 +00:00
parent 97771b1b2b
commit 6c2820835e
3 changed files with 345 additions and 6 deletions

View File

@ -16,7 +16,13 @@ CURRENT_VERSION = 'v1.6'
FAKE_CONTAINER_ID = '3cc2351ab11b'
FAKE_IMAGE_ID = 'e9aa60c60128'
FAKE_IMAGE_NAME = 'test_image'
FAKE_TARBALL_PATH = '/path/to/tarball'
FAKE_REPO_NAME = 'repo'
FAKE_TAG_NAME = 'tag'
FAKE_FILE_NAME = 'file'
FAKE_URL = 'myurl'
FAKE_PATH = '/path'
### Each method is prefixed with HTTP method (get, post...)
### for clarity and readability
@ -51,6 +57,31 @@ def get_fake_images():
return status_code, response
def get_fake_image_history():
status_code = 200
response = [
{
"Id": "b750fe79269d",
"Created": 1364102658,
"CreatedBy": "/bin/bash"
},
{
"Id": "27cf78414709",
"Created": 1364068391,
"CreatedBy": ""
}
]
return status_code, response
def post_fake_import_image():
status_code = 200
response = 'Import messages...'
return status_code, response
def get_fake_containers():
status_code = 200
response = [{
@ -93,6 +124,45 @@ def get_fake_inspect_container():
return status_code, response
def get_fake_inspect_image():
status_code = 200
response = {
'id': FAKE_IMAGE_ID,
'parent': "27cf784147099545",
'created': "2013-03-23T22:24:18.818426-07:00",
'container': FAKE_CONTAINER_ID,
'container_config':
{
"Hostname": "",
"User": "",
"Memory": 0,
"MemorySwap": 0,
"AttachStdin": False,
"AttachStdout": False,
"AttachStderr": False,
"PortSpecs": "",
"Tty": True,
"OpenStdin": True,
"StdinOnce": False,
"Env": "",
"Cmd": ["/bin/bash"],
"Dns": "",
"Image": "base",
"Volumes": "",
"VolumesFrom": "",
"WorkingDir": ""
},
'Size': 6823592
}
return status_code, response
def get_fake_insert_image():
status_code = 200
response = {'StatusCode': 0}
return status_code, response
def get_fake_wait():
status_code = 200
response = {'StatusCode': 0}
@ -111,6 +181,12 @@ def get_fake_diff():
return status_code, response
def get_fake_export():
status_code = 200
response = 'Byte Stream....'
return status_code, response
def post_fake_stop_container():
status_code = 200
response = {'Id': FAKE_CONTAINER_ID}
@ -153,12 +229,24 @@ def post_fake_commit():
return status_code, response
def post_fake_push():
status_code = 200
response = {'Id': FAKE_IMAGE_ID}
return status_code, response
def post_fake_build_container():
status_code = 200
response = {'Id': FAKE_CONTAINER_ID}
return status_code, response
def post_fake_tag_image():
status_code = 200
response = {'Id': FAKE_IMAGE_ID}
return status_code, response
## maps real api url to fake response callback
prefix = 'unix://var/run/docker.sock'
fake_responses = {
@ -170,18 +258,26 @@ fake_responses = {
get_fake_search,
'{1}/{0}/images/json'.format(CURRENT_VERSION, prefix):
get_fake_images,
'{1}/{0}/images/test_image/history'.format(CURRENT_VERSION, prefix):
get_fake_image_history,
'{1}/{0}/images/create'.format(CURRENT_VERSION, prefix):
post_fake_import_image,
'{1}/{0}/containers/json'.format(CURRENT_VERSION, prefix):
get_fake_containers,
'{1}/{0}/containers/3cc2351ab11b/start'.format(CURRENT_VERSION, prefix):
post_fake_start_container,
'{1}/{0}/containers/3cc2351ab11b/json'.format(CURRENT_VERSION, prefix):
get_fake_inspect_container,
'{1}/{0}/images/e9aa60c60128/tag'.format(CURRENT_VERSION, prefix):
post_fake_tag_image,
'{1}/{0}/containers/3cc2351ab11b/wait'.format(CURRENT_VERSION, prefix):
get_fake_wait,
'{1}/{0}/containers/3cc2351ab11b/attach'.format(CURRENT_VERSION, prefix):
get_fake_logs,
'{1}/{0}/containers/3cc2351ab11b/changes'.format(CURRENT_VERSION, prefix):
get_fake_diff,
'{1}/{0}/containers/3cc2351ab11b/export'.format(CURRENT_VERSION, prefix):
get_fake_export,
'{1}/{0}/containers/3cc2351ab11b/stop'.format(CURRENT_VERSION, prefix):
post_fake_stop_container,
'{1}/{0}/containers/3cc2351ab11b/kill'.format(CURRENT_VERSION, prefix):
@ -194,6 +290,12 @@ fake_responses = {
post_fake_image_create,
'{1}/{0}/images/e9aa60c60128'.format(CURRENT_VERSION, prefix):
delete_fake_remove_image,
'{1}/{0}/images/test_image/json'.format(CURRENT_VERSION, prefix):
get_fake_inspect_image,
'{1}/{0}/images/test_image/insert'.format(CURRENT_VERSION, prefix):
get_fake_insert_image,
'{1}/{0}/images/test_image/push'.format(CURRENT_VERSION, prefix):
post_fake_push,
'{1}/{0}/commit'.format(CURRENT_VERSION, prefix):
post_fake_commit,
'{1}/{0}/containers/create'.format(CURRENT_VERSION, prefix):

View File

@ -30,7 +30,7 @@ class BaseTestCase(unittest.TestCase):
tmp_containers = []
def setUp(self):
self.client = docker.Client(version="1.6")
self.client = docker.Client()
self.client.pull('busybox')
self.tmp_imgs = []
self.tmp_containers = []

View File

@ -34,7 +34,7 @@ except ImportError:
# FIXME: missing tests for
# export; history; import_image; insert; port; push; tag
# port;
def response(status_code=200, content='', headers=None, reason=None, elapsed=0,
@ -112,6 +112,16 @@ class DockerClientTest(unittest.TestCase):
params={'filter': None, 'only_ids': 0, 'all': 1}
)
def test_images_quiet(self):
try:
self.client.images(all=True, quiet=True)
except Exception as e:
self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with(
'unix://var/run/docker.sock/v1.6/images/json',
params={'filter': None, 'only_ids': 1, 'all': 1}
)
def test_image_ids(self):
try:
self.client.images(quiet=True)
@ -286,6 +296,42 @@ class DockerClientTest(unittest.TestCase):
headers={'Content-Type': 'application/json'}
)
def test_start_container_with_links(self):
# one link
try:
link_path = 'path'
alias = 'alias'
self.client.start(fake_api.FAKE_CONTAINER_ID,
links={link_path: alias})
except Exception as e:
self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with(
'unix://var/run/docker.sock/v1.6/containers/3cc2351ab11b/start',
'{"Links": ["path:alias"]}',
headers={'Content-Type': 'application/json'}
)
# multiple links
try:
link_path = 'path'
alias = 'alias'
self.client.start(
fake_api.FAKE_CONTAINER_ID,
links=[
{link_path + '1': alias + '1'},
{link_path + '2': alias + '2'},
]
)
except Exception as e:
self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with(
'unix://var/run/docker.sock/v1.6/containers/3cc2351ab11b/start',
'{"Links": ["path1:alias1", "path2:alias2"]}',
headers={'Content-Type': 'application/json'}
)
def test_start_container_with_dict_instead_of_id(self):
try:
self.client.start({'Id': fake_api.FAKE_CONTAINER_ID})
@ -465,6 +511,38 @@ class DockerClientTest(unittest.TestCase):
params={'v': False, 'link': True}
)
def test_export(self):
try:
self.client.export(fake_api.FAKE_CONTAINER_ID)
except Exception as e:
self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with(
'unix://var/run/docker.sock/v1.6/containers/3cc2351ab11b/export',
stream=True
)
def test_export_with_dict_instead_of_id(self):
try:
self.client.export({'Id': fake_api.FAKE_CONTAINER_ID})
except Exception as e:
self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with(
'unix://var/run/docker.sock/v1.6/containers/3cc2351ab11b/export',
stream=True
)
def test_inspect_container(self):
try:
self.client.inspect_container(fake_api.FAKE_CONTAINER_ID)
except Exception as e:
self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with(
'unix://var/run/docker.sock/v1.6/containers/3cc2351ab11b/json'
)
##################
## IMAGES TESTS ##
##################
@ -489,7 +567,7 @@ class DockerClientTest(unittest.TestCase):
self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with(
'unix://var/run/docker.sock/v1.4/images/create',
'unix://var/run/docker.sock/v1.6/images/create',
headers={},
params={'tag': None, 'fromImage': 'joffrey/test001'},
stream=True
@ -524,6 +602,165 @@ class DockerClientTest(unittest.TestCase):
'unix://var/run/docker.sock/v1.6/images/e9aa60c60128'
)
def test_image_history(self):
try:
self.client.history(fake_api.FAKE_IMAGE_NAME)
except Exception as e:
self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with(
'unix://var/run/docker.sock/v1.6/images/test_image/history'
)
def test_import_image(self):
try:
self.client.import_image(
fake_api.FAKE_TARBALL_PATH,
repository=fake_api.FAKE_REPO_NAME,
tag=fake_api.FAKE_TAG_NAME
)
except Exception as e:
self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with(
'unix://var/run/docker.sock/v1.6/images/create',
None,
params={
'repo': fake_api.FAKE_REPO_NAME,
'tag': fake_api.FAKE_TAG_NAME,
'fromSrc': fake_api.FAKE_TARBALL_PATH
}
)
def test_import_image_from_file(self):
buf = tempfile.NamedTemporaryFile(delete=False)
try:
# pretent the buffer is a file
self.client.import_image(
buf.name,
repository=fake_api.FAKE_REPO_NAME,
tag=fake_api.FAKE_TAG_NAME
)
except Exception as e:
self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with(
'unix://var/run/docker.sock/v1.6/images/create',
'',
params={
'repo': fake_api.FAKE_REPO_NAME,
'tag': fake_api.FAKE_TAG_NAME,
'fromSrc': '-'
}
)
buf.close()
os.remove(buf.name)
def test_inspect_image(self):
try:
self.client.inspect_image(fake_api.FAKE_IMAGE_NAME)
except Exception as e:
self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with(
'unix://var/run/docker.sock/v1.6/images/test_image/json'
)
def test_insert_image(self):
try:
self.client.insert(fake_api.FAKE_IMAGE_NAME,
fake_api.FAKE_URL, fake_api.FAKE_PATH)
except Exception as e:
self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with(
'unix://var/run/docker.sock/v1.6/images/test_image/insert',
None,
params={
'url': fake_api.FAKE_URL,
'path': fake_api.FAKE_PATH
}
)
def test_push_image(self):
try:
self.client.push(fake_api.FAKE_IMAGE_NAME)
except Exception as e:
self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with(
'unix://var/run/docker.sock/v1.6/images/test_image/push',
'{}',
headers={'Content-Type': 'application/json'},
stream=False
)
def test_push_image_stream(self):
try:
self.client.push(fake_api.FAKE_IMAGE_NAME, stream=True)
except Exception as e:
self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with(
'unix://var/run/docker.sock/v1.6/images/test_image/push',
'{}',
headers={'Content-Type': 'application/json'},
stream=True
)
def test_tag_image(self):
try:
self.client.tag(fake_api.FAKE_IMAGE_ID, fake_api.FAKE_REPO_NAME)
except Exception as e:
self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with(
'unix://var/run/docker.sock/v1.6/images/e9aa60c60128/tag',
None,
params={
'tag': None,
'repo': 'repo',
'force': 0
}
)
def test_tag_image_tag(self):
try:
self.client.tag(
fake_api.FAKE_IMAGE_ID,
fake_api.FAKE_REPO_NAME,
tag=fake_api.FAKE_TAG_NAME
)
except Exception as e:
self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with(
'unix://var/run/docker.sock/v1.6/images/e9aa60c60128/tag',
None,
params={
'tag': 'tag',
'repo': 'repo',
'force': 0
}
)
def test_tag_image_force(self):
try:
self.client.tag(
fake_api.FAKE_IMAGE_ID, fake_api.FAKE_REPO_NAME, force=True)
except Exception as e:
self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with(
'unix://var/run/docker.sock/v1.6/images/e9aa60c60128/tag',
None,
params={
'tag': None,
'repo': 'repo',
'force': 1
}
)
#################
# BUILDER TESTS #
#################
@ -565,9 +802,9 @@ class DockerClientTest(unittest.TestCase):
cfg = docker.auth.load_config(folder)
self.assertTrue(cfg is not None)
self.assertTrue('Configs' in cfg)
self.assertEquals(cfg['Configs'], {})
self.assertEqual(cfg['Configs'], {})
self.assertTrue('rootPath' in cfg)
self.assertEquals(cfg['rootPath'], folder)
self.assertEqual(cfg['rootPath'], folder)
def test_load_config(self):
folder = tempfile.mkdtemp()