diff --git a/tests/test.py b/tests/test.py new file mode 100644 index 00000000..bcc04aa1 --- /dev/null +++ b/tests/test.py @@ -0,0 +1,157 @@ +import unittest + +import docker + +# FIXME: missing tests for +# build; commit; export; history; import_image; insert; inspect_image; +# kill; port; push; remove_container; remove_image; restart; stop; tag; +# kill/stop/start/wait/restart (multi) + +class BaseTestCase(unittest.TestCase): + tmp_imgs = [] + tmp_containers = [] + + def setUp(self): + self.client = docker.Client() + self.client.pull('busybox') + + def tearDown(self): + if len(self.tmp_imgs) > 0: + self.client.remove_image(*self.tmp_imgs) + if len(self.tmp_containers) > 0: + self.client.remove_container(*self.tmp_containers) + +class TestVersion(BaseTestCase): + def runTest(self): + res = self.client.version() + self.assertIn('GoVersion', res) + self.assertIn('Version', res) + self.assertEqual(len(res['Version'].split('.')), 3) + +class TestInfo(BaseTestCase): + def runTest(self): + res = self.client.info() + self.assertIn('Containers', res) + self.assertIn('Images', res) + self.assertIn('Debug', res) + +class TestSearch(BaseTestCase): + def runTest(self): + res = self.client.search('busybox') + self.assertTrue(len(res) >= 1) + base_img = [x for x in res if x['Name'] == 'busybox'] + self.assertEqual(len(base_img), 1) + self.assertIn('Description', base_img[0]) + +class TestPull(BaseTestCase): + def runTest(self): + info = self.client.info() + self.assertIn('Images', info) + # FIXME + +class TestImages(BaseTestCase): + def runTest(self): + res1 = self.client.images() + self.assertEqual(len(res1), self.client.info()['Images']) + res10 = res1[0] + self.assertIn('Created', res10) + self.assertIn('Id', res10) + self.assertIn('Repository', res10) + self.assertIn('Tag', res10) + self.assertEqual(res10['Tag'], 'latest') + self.assertEqual(res10['Repository'], 'busybox') + +class TestImageIds(BaseTestCase): + def runTest(self): + res1 = self.client.images(quiet=True) + self.assertEqual(type(res1[0]), unicode) + +class TestCreateContainer(BaseTestCase): + def runTest(self): + res = self.client.create_container('busybox', 'true') + self.assertIn('Id', res) + self.tmp_containers.append(res['Id']) + +class TestStartContainer(BaseTestCase): + def runTest(self): + res = self.client.create_container('busybox', 'true') + self.assertIn('Id', res) + self.tmp_containers.append(res['Id']) + self.client.start(res['Id']) + inspect = self.client.inspect_container(res['Id']) + self.assertIn('Config', inspect) + self.assertIn('ID', inspect) + self.assertTrue(inspect['ID'].startswith(res['Id'])) + self.assertIn('Image', inspect) + self.assertIn('State', inspect) + self.assertIn('Running', inspect['State']) + if not inspect['State']['Running']: + self.assertIn('ExitCode', inspect['State']) + self.assertEqual(inspect['State']['ExitCode'], 0) + +class TestWait(BaseTestCase): + def runTest(self): + res = self.client.create_container('busybox', ['sleep', '10']) + id = res['Id'] + self.tmp_containers.append(id) + self.client.start(id) + exitcode = self.client.wait(id) + self.assertEqual(exitcode, 0) + inspect = self.client.inspect_container(id) + self.assertIn('Running', inspect['State']) + self.assertEqual(inspect['State']['Running'], False) + self.assertIn('ExitCode', inspect['State']) + self.assertEqual(inspect['State']['ExitCode'], exitcode) + + +class TestListContainers(BaseTestCase): + def runTest(self): + res0 = self.client.containers(all=True) + size = len(res0) + res1 = self.client.create_container('busybox', 'true') + self.assertIn('Id', res1) + self.client.start(res1['Id']) + self.tmp_containers.append(res1['Id']) + res2 = self.client.containers(all=True) + self.assertEqual(size + 1, len(res2)) + retrieved = [x for x in res2 if x['Id'].startswith(res1['Id'])] + self.assertEqual(len(retrieved), 1) + retrieved = retrieved[0] + self.assertIn('Command', retrieved) + self.assertEqual(retrieved['Command'], 'true ') + self.assertIn('Image', retrieved) + self.assertEqual(retrieved['Image'], 'busybox:latest') + self.assertIn('Status', retrieved) + +class TestLogs(BaseTestCase): + def runTest(self): + snippet = 'Flowering Nights (Sakuya Iyazoi)' + container = self.client.create_container('busybox', + ['echo', '-n', '"{0}"'.format(snippet)]) + id = container['Id'] + self.client.start(id) + self.tmp_containers.append(id) + exitcode = self.client.wait(id) + self.assertEqual(exitcode, 0) + logs = self.client.logs(id) + self.assertEqual(logs.read(), snippet) + +class TestDiff(BaseTestCase): + def runTest(self): + container = self.client.create_container('busybox', ['touch', '/test']) + id = container['Id'] + self.client.start(id) + self.tmp_containers.append(id) + exitcode = self.client.wait(id) + self.assertEqual(exitcode, 0) + diff = self.client.diff(id) + test_diff = [x for x in diff if x.get('Path', None) == '/test'] + self.assertEqual(len(test_diff), 1) + self.assertIn('Kind', test_diff[0]) + self.assertEqual(test_diff[0]['Kind'], 1) + # FIXME also test remove/modify + # (need testcommit first) + + +if __name__ == '__main__': + unittest.main() \ No newline at end of file