def test_start_with_bindings_and_links(self): t = template_format.parse(template) self.stack = utils.parse_stack(t) definition = self.stack.t.resource_definitions(self.stack)['Blog'] definition['Properties']['port_bindings'] = { '80/tcp': [{ 'HostPort': '80' }] } definition['Properties']['links'] = {'db': 'mysql'} resource = docker_container.DockerContainer('Blog', definition, self.stack) self.m.StubOutWithMock(resource, 'get_client') resource.get_client().MultipleTimes().AndReturn( fakeclient.FakeDockerClient()) self.assertIsNone(resource.validate()) self.m.ReplayAll() scheduler.TaskRunner(resource.create)() self.assertEqual((resource.CREATE, resource.COMPLETE), resource.state) client = resource.get_client() self.assertEqual(['samalba/wordpress'], client.pulled_images) self.assertEqual({'db': 'mysql'}, client.container_start[0]['links']) self.assertEqual({ '80/tcp': [{ 'HostPort': '80' }] }, client.container_start[0]['port_bindings'])
def test_start_with_bindings_and_links(self): t = template_format.parse(template) self.stack = utils.parse_stack(t) definition = self.stack.t.resource_definitions(self.stack)['Blog'] definition['Properties']['port_bindings'] = { '80/tcp': [{ 'HostPort': '80' }] } definition['Properties']['links'] = {'db': 'mysql'} resource = docker_container.DockerContainer('Blog', definition, self.stack) self.m.StubOutWithMock(resource, 'get_client') resource.get_client().MultipleTimes().AndReturn( fakeclient.FakeDockerClient()) self.assertIsNone(resource.validate()) self.m.ReplayAll() scheduler.TaskRunner(resource.create)() self.assertEqual((resource.CREATE, resource.COMPLETE), resource.state) client = resource.get_client() self.assertEqual(['samalba/wordpress'], client.pulled_images) self.assertEqual({'db': 'mysql'}, client.container_start[0]['links']) self.assertEqual({'80/tcp': [{ 'HostPort': '80' }]}, client.container_start[0]['port_bindings'])
def test_create_with_registry_login(self): t = template_format.parse(template) self.stack = utils.parse_stack(t) definition = self.stack.t.resource_definitions(self.stack)['Blog'] props = t['Resources']['Blog']['Properties'].copy() registry = 'my-registry.example.com' username = '******' password = '******' email = '*****@*****.**' props['registry_credentials'] = { 'registry': registry, 'username': username, 'password': password, 'email': email } resource = docker_container.DockerContainer( 'Blog', definition.freeze(properties=props), self.stack) docker_client = docker.Client() self.m.StubOutWithMock(resource, 'get_client') resource.get_client().MultipleTimes().AndReturn( docker_client) self.m.StubOutWithMock(docker_client, 'login') docker_client.login(username=username, password=password, email=email, registry=registry) self.assertIsNone(resource.validate()) self.m.ReplayAll() scheduler.TaskRunner(resource.create)() self.assertEqual((resource.CREATE, resource.COMPLETE), resource.state) client = resource.get_client() self.assertEqual(['samalba/wordpress'], client.pulled_images)
def create_container(self, resource_name): t = template_format.parse(template) self.stack = utils.parse_stack(t) resource = docker_container.DockerContainer( resource_name, self.stack.t.resource_definitions(self.stack)[resource_name], self.stack) self.patchobject(resource, 'get_client', return_value=docker.Client()) self.assertIsNone(resource.validate()) scheduler.TaskRunner(resource.create)() self.assertEqual((resource.CREATE, resource.COMPLETE), resource.state) return resource
def create_container(self, resource_name): t = template_format.parse(template) stack = utils.parse_stack(t) resource = docker_container.DockerContainer( resource_name, t['Resources'][resource_name], stack) self.m.StubOutWithMock(resource, 'get_client') resource.get_client().MultipleTimes().AndReturn(FakeDockerClient()) self.assertIsNone(resource.validate()) self.m.ReplayAll() scheduler.TaskRunner(resource.create)() self.assertEqual((resource.CREATE, resource.COMPLETE), resource.state) return resource
def test_create_with_name(self): t = template_format.parse(template) self.stack = utils.parse_stack(t) definition = self.stack.t.resource_definitions(self.stack)['Blog'] props = t['Resources']['Blog']['Properties'].copy() props['name'] = 'super-blog' resource = docker_container.DockerContainer( 'Blog', definition.freeze(properties=props), self.stack) self.patchobject(resource, 'get_client', return_value=docker.Client()) self.assertIsNone(resource.validate()) scheduler.TaskRunner(resource.create)() self.assertEqual((resource.CREATE, resource.COMPLETE), resource.state) client = resource.get_client() self.assertEqual(['samalba/wordpress'], client.pulled_images) self.assertEqual('super-blog', client.container_create[0]['name'])
def test_start_with_restart_policy_always(self): t = template_format.parse(template) self.stack = utils.parse_stack(t) definition = self.stack.t.resource_definitions(self.stack)["Blog"] props = t["Resources"]["Blog"]["Properties"].copy() props["restart_policy"] = {"Name": "always", "MaximumRetryCount": 0} resource = docker_container.DockerContainer("Blog", definition.freeze(properties=props), self.stack) get_client_mock = self.patchobject(resource, "get_client") get_client_mock.return_value = docker.Client() self.assertIsNone(resource.validate()) scheduler.TaskRunner(resource.create)() self.assertEqual((resource.CREATE, resource.COMPLETE), resource.state) client = resource.get_client() self.assertEqual(["samalba/wordpress"], client.pulled_images) self.assertEqual({"Name": "always", "MaximumRetryCount": 0}, client.container_start[0]["restart_policy"])
def test_start_with_read_only(self): t = template_format.parse(template) stack = utils.parse_stack(t) definition = stack.t.resource_definitions(stack)['Blog'] definition['Properties']['read_only'] = True resource = docker_container.DockerContainer('Blog', definition, stack) get_client_mock = self.patchobject(resource, 'get_client') get_client_mock.return_value = fakeclient.FakeDockerClient() get_client_mock.return_value.set_api_version('1.17') self.assertIsNone(resource.validate()) scheduler.TaskRunner(resource.create)() self.assertEqual((resource.CREATE, resource.COMPLETE), resource.state) client = resource.get_client() self.assertEqual(['samalba/wordpress'], client.pulled_images) self.assertIs(True, client.container_start[0]['read_only'])
def test_create_with_name(self): t = template_format.parse(template) self.stack = utils.parse_stack(t) definition = self.stack.t.resource_definitions(self.stack)["Blog"] props = t["Resources"]["Blog"]["Properties"].copy() props["name"] = "super-blog" resource = docker_container.DockerContainer("Blog", definition.freeze(properties=props), self.stack) self.m.StubOutWithMock(resource, "get_client") resource.get_client().MultipleTimes().AndReturn(docker.Client()) self.assertIsNone(resource.validate()) self.m.ReplayAll() scheduler.TaskRunner(resource.create)() self.assertEqual((resource.CREATE, resource.COMPLETE), resource.state) client = resource.get_client() self.assertEqual(["samalba/wordpress"], client.pulled_images) self.assertEqual("super-blog", client.container_create[0]["name"])
def test_start_with_read_only(self): t = template_format.parse(template) self.stack = utils.parse_stack(t) definition = self.stack.t.resource_definitions(self.stack)["Blog"] props = t["Resources"]["Blog"]["Properties"].copy() props["read_only"] = True resource = docker_container.DockerContainer("Blog", definition.freeze(properties=props), self.stack) get_client_mock = self.patchobject(resource, "get_client") get_client_mock.return_value = docker.Client() get_client_mock.return_value.set_api_version("1.17") self.assertIsNone(resource.validate()) scheduler.TaskRunner(resource.create)() self.assertEqual((resource.CREATE, resource.COMPLETE), resource.state) client = resource.get_client() self.assertEqual(["samalba/wordpress"], client.pulled_images) self.assertIs(True, client.container_start[0]["read_only"])
def test_start_with_read_only(self): t = template_format.parse(template) self.stack = utils.parse_stack(t) definition = self.stack.t.resource_definitions(self.stack)['Blog'] definition['Properties']['read_only'] = True resource = docker_container.DockerContainer('Blog', definition, self.stack) get_client_mock = self.patchobject(resource, 'get_client') get_client_mock.return_value = fakeclient.FakeDockerClient() get_client_mock.return_value.set_api_version('1.17') self.assertIsNone(resource.validate()) scheduler.TaskRunner(resource.create)() self.assertEqual((resource.CREATE, resource.COMPLETE), resource.state) client = resource.get_client() self.assertEqual(['samalba/wordpress'], client.pulled_images) self.assertIs(True, client.container_start[0]['read_only'])
def test_create_with_name(self): t = template_format.parse(template) self.stack = utils.parse_stack(t) definition = self.stack.t.resource_definitions(self.stack)['Blog'] definition['Properties']['name'] = 'super-blog' resource = docker_container.DockerContainer('Blog', definition, self.stack) self.m.StubOutWithMock(resource, 'get_client') resource.get_client().MultipleTimes().AndReturn(docker.Client()) self.assertIsNone(resource.validate()) self.m.ReplayAll() scheduler.TaskRunner(resource.create)() self.assertEqual((resource.CREATE, resource.COMPLETE), resource.state) client = resource.get_client() self.assertEqual(['samalba/wordpress'], client.pulled_images) self.assertEqual('super-blog', client.container_create[0]['name'])
def test_start_with_caps(self): t = template_format.parse(template) stack = utils.parse_stack(t) definition = stack.t.resource_definitions(stack)['Blog'] definition['Properties']['cap_add'] = ['NET_ADMIN'] definition['Properties']['cap_drop'] = ['MKNOD'] resource = docker_container.DockerContainer('Blog', definition, stack) get_client_mock = self.patchobject(resource, 'get_client') get_client_mock.return_value = fakeclient.FakeDockerClient() self.assertIsNone(resource.validate()) scheduler.TaskRunner(resource.create)() self.assertEqual((resource.CREATE, resource.COMPLETE), resource.state) client = resource.get_client() self.assertEqual(['samalba/wordpress'], client.pulled_images) self.assertEqual(['NET_ADMIN'], client.container_start[0]['cap_add']) self.assertEqual(['MKNOD'], client.container_start[0]['cap_drop'])
def test_start_with_caps(self): t = template_format.parse(template) self.stack = utils.parse_stack(t) definition = self.stack.t.resource_definitions(self.stack)['Blog'] definition['Properties']['cap_add'] = ['NET_ADMIN'] definition['Properties']['cap_drop'] = ['MKNOD'] resource = docker_container.DockerContainer('Blog', definition, self.stack) get_client_mock = self.patchobject(resource, 'get_client') get_client_mock.return_value = fakeclient.FakeDockerClient() self.assertIsNone(resource.validate()) scheduler.TaskRunner(resource.create)() self.assertEqual((resource.CREATE, resource.COMPLETE), resource.state) client = resource.get_client() self.assertEqual(['samalba/wordpress'], client.pulled_images) self.assertEqual(['NET_ADMIN'], client.container_start[0]['cap_add']) self.assertEqual(['MKNOD'], client.container_start[0]['cap_drop'])
def test_create_with_name(self): t = template_format.parse(template) stack = utils.parse_stack(t) definition = stack.t.resource_definitions(stack)['Blog'] definition['Properties']['name'] = 'super-blog' resource = docker_container.DockerContainer( 'Blog', definition, stack) self.m.StubOutWithMock(resource, 'get_client') resource.get_client().MultipleTimes().AndReturn(FakeDockerClient()) self.assertIsNone(resource.validate()) self.m.ReplayAll() scheduler.TaskRunner(resource.create)() self.assertEqual((resource.CREATE, resource.COMPLETE), resource.state) client = resource.get_client() self.assertEqual(['samalba/wordpress'], client.pulled_images) self.assertEqual('super-blog', client.container_create[0]['name'])
def test_start_with_restart_policy_always(self): t = template_format.parse(template) self.stack = utils.parse_stack(t) definition = self.stack.t.resource_definitions(self.stack)['Blog'] definition['Properties']['restart_policy'] = { 'Name': 'always', 'MaximumRetryCount': 0} resource = docker_container.DockerContainer( 'Blog', definition, self.stack) get_client_mock = self.patchobject(resource, 'get_client') get_client_mock.return_value = fakeclient.FakeDockerClient() self.assertIsNone(resource.validate()) scheduler.TaskRunner(resource.create)() self.assertEqual((resource.CREATE, resource.COMPLETE), resource.state) client = resource.get_client() self.assertEqual(['samalba/wordpress'], client.pulled_images) self.assertEqual({'Name': 'always', 'MaximumRetryCount': 0}, client.container_start[0]['restart_policy'])
def test_start_with_bindings_and_links(self): t = template_format.parse(template) self.stack = utils.parse_stack(t) definition = self.stack.t.resource_definitions(self.stack)["Blog"] props = t["Resources"]["Blog"]["Properties"].copy() props["port_bindings"] = {"80/tcp": [{"HostPort": "80"}]} props["links"] = {"db": "mysql"} resource = docker_container.DockerContainer("Blog", definition.freeze(properties=props), self.stack) self.m.StubOutWithMock(resource, "get_client") resource.get_client().MultipleTimes().AndReturn(docker.Client()) self.assertIsNone(resource.validate()) self.m.ReplayAll() scheduler.TaskRunner(resource.create)() self.assertEqual((resource.CREATE, resource.COMPLETE), resource.state) client = resource.get_client() self.assertEqual(["samalba/wordpress"], client.pulled_images) self.assertEqual({"db": "mysql"}, client.container_start[0]["links"]) self.assertEqual({"80/tcp": [{"HostPort": "80"}]}, client.container_start[0]["port_bindings"])
def test_start_with_restart_policy_always(self): t = template_format.parse(template) stack = utils.parse_stack(t) definition = stack.t.resource_definitions(stack)['Blog'] definition['Properties']['restart_policy'] = { 'Name': 'always', 'MaximumRetryCount': 0} resource = docker_container.DockerContainer( 'Blog', definition, stack) get_client_mock = self.patchobject(resource, 'get_client') get_client_mock.return_value = fakeclient.FakeDockerClient() self.assertIsNone(resource.validate()) scheduler.TaskRunner(resource.create)() self.assertEqual((resource.CREATE, resource.COMPLETE), resource.state) client = resource.get_client() self.assertEqual(['samalba/wordpress'], client.pulled_images) self.assertEqual({'Name': 'always', 'MaximumRetryCount': 0}, client.container_start[0]['restart_policy'])
def test_start_with_bindings_and_links(self): t = template_format.parse(template) self.stack = utils.parse_stack(t) definition = self.stack.t.resource_definitions(self.stack)['Blog'] props = t['Resources']['Blog']['Properties'].copy() props['port_bindings'] = {'80/tcp': [{'HostPort': '80'}]} props['links'] = {'db': 'mysql'} resource = docker_container.DockerContainer( 'Blog', definition.freeze(properties=props), self.stack) self.patchobject(resource, 'get_client', return_value=docker.Client()) self.assertIsNone(resource.validate()) scheduler.TaskRunner(resource.create)() self.assertEqual((resource.CREATE, resource.COMPLETE), resource.state) client = resource.get_client() self.assertEqual(['samalba/wordpress'], client.pulled_images) self.assertEqual({'db': 'mysql'}, client.container_start[0]['links']) self.assertEqual({'80/tcp': [{ 'HostPort': '80' }]}, client.container_start[0]['port_bindings'])
def test_start_with_restart_policy_on_failure(self): t = template_format.parse(template) self.stack = utils.parse_stack(t) definition = self.stack.t.resource_definitions(self.stack)['Blog'] props = t['Resources']['Blog']['Properties'].copy() props['restart_policy'] = {'Name': 'on-failure', 'MaximumRetryCount': 10} resource = docker_container.DockerContainer( 'Blog', definition.freeze(properties=props), self.stack) get_client_mock = self.patchobject(resource, 'get_client') get_client_mock.return_value = docker.Client() self.assertIsNone(resource.validate()) scheduler.TaskRunner(resource.create)() self.assertEqual((resource.CREATE, resource.COMPLETE), resource.state) client = resource.get_client() self.assertEqual(['samalba/wordpress'], client.pulled_images) self.assertEqual({'Name': 'on-failure', 'MaximumRetryCount': 10}, client.container_start[0]['restart_policy'])
def test_start_with_bindings_and_links(self): t = template_format.parse(template) self.stack = utils.parse_stack(t) definition = self.stack.t.resource_definitions(self.stack)['Blog'] props = t['Resources']['Blog']['Properties'].copy() props['port_bindings'] = {'80/tcp': [{'HostPort': '80'}]} props['links'] = {'db': 'mysql'} resource = docker_container.DockerContainer( 'Blog', definition.freeze(properties=props), self.stack) self.patchobject(resource, 'get_client', return_value=docker.Client()) self.assertIsNone(resource.validate()) scheduler.TaskRunner(resource.create)() self.assertEqual((resource.CREATE, resource.COMPLETE), resource.state) client = resource.get_client() self.assertEqual(['samalba/wordpress'], client.pulled_images) self.assertEqual({'db': 'mysql'}, client.container_start[0]['links']) self.assertEqual( {'80/tcp': [{'HostPort': '80'}]}, client.container_start[0]['port_bindings'])
def assertResourceState(self, resource, ref_id): self.assertIsNone(resource.validate()) self.assertEqual((resource.CREATE, resource.COMPLETE), resource.state) self.assertEqual(ref_id, resource.FnGetRefId())
def assertResourceState(self, resource, ref_id): self.assertEqual(None, resource.validate()) self.assertEqual((resource.CREATE, resource.COMPLETE), resource.state) self.assertEqual(ref_id, resource.FnGetRefId())