def test_create(self): containers = { "c1": BlockadeContainerConfig("c1", "image"), "c2": BlockadeContainerConfig("c2", "image"), "c3": BlockadeContainerConfig("c3", "image"), } config = BlockadeConfig(containers) self.network.get_container_device.side_effect = lambda dc, y: "veth" + y initialize = lambda x, y: BlockadeState("ourblockadeid", x) self.state_factory.initialize.side_effect = initialize self.state_factory.exists.side_effect = lambda: False self.state_factory.get_blockade_id = mock.MagicMock(return_value="ourblockadeid") self.docker_client.create_container.side_effect = [ {"Id": "container1"}, {"Id": "container2"}, {"Id": "container3"}, ] b = Blockade(config, self.state_factory, self.network, self.docker_client) b.create() self.assertEqual(self.state_factory.initialize.call_count, 1) self.assertEqual(self.docker_client.create_container.call_count, 3)
def test_create(self): containers = { 'c1': BlockadeContainerConfig("c1", "image"), 'c2': BlockadeContainerConfig("c2", "image"), 'c3': BlockadeContainerConfig("c3", "image") } config = BlockadeConfig(containers) self.network.get_container_device.side_effect = lambda dc, y: "veth" + y self.state.exists.side_effect = lambda: False self.state.blockade_id = self.blockade_id self.docker_client.create_container.side_effect = [{ "Id": "container1" }, { "Id": "container2" }, { "Id": "container3" }] b = Blockade(config, state=self.state, network=self.network, docker_client=self.docker_client) b.create() self.assertEqual(self.state.initialize.call_count, 1) self.assertEqual(self.docker_client.create_container.call_count, 3)
def test_create(self): containers = { 'c1': BlockadeContainerConfig("c1", "image"), 'c2': BlockadeContainerConfig("c2", "image"), 'c3': BlockadeContainerConfig("c3", "image") } config = BlockadeConfig(containers) self.network.get_container_device.side_effect = lambda dc, y: "veth" + y initialize = lambda x, y: BlockadeState("ourblockadeid", x) self.state_factory.initialize.side_effect = initialize self.state_factory.exists.side_effect = lambda: False self.state_factory.get_blockade_id = mock.MagicMock( return_value="ourblockadeid") self.docker_client.create_container.side_effect = [{ "Id": "container1" }, { "Id": "container2" }, { "Id": "container3" }] b = Blockade(config, self.state_factory, self.network, self.docker_client) b.create() self.assertEqual(self.state_factory.initialize.call_count, 1) self.assertEqual(self.docker_client.create_container.call_count, 3)
def get_blockade(name): global BLOCKADE_CONFIGS config = BLOCKADE_CONFIGS[name] return Blockade(config, blockade_id=name, state=BlockadeManager.load_state(name), network=BlockadeNetwork(config))
def test_get_container_description_ip_address_info(self): expected_ip = "1.2.3.4" self.docker_client.inspect_container.side_effect = lambda id: { "Id": id, "IPAddress": "", "NetworkSettings": { "Networks": {"TheOnlyNetwork": {"IPAddress": expected_ip}} } } b = Blockade(BlockadeConfig(), state=self.state, network=self.network, docker_client=self.docker_client) container = b._get_container_description("c1") self.assertEqual(expected_ip, container.ip_address)
def test_add_docker_containers(self): containers = ['id1', 'id2', 'id3'] self.docker_client.inspect_container.side_effect = lambda id: {"Id": id} self.network.get_container_device.side_effect = lambda dc, y: "veth"+y self.state.exists.side_effect = lambda: False self.state.container_id.side_effect = lambda name: None self.state.containers = {} self.state.blockade_id = self.blockade_id b = Blockade(BlockadeConfig(), state=self.state, network=self.network, docker_client=self.docker_client) b.add_container(containers) self.assertEqual(self.state.update.call_count, 1) self.assertEqual(len(self.state.containers), 3)
def get_blockade(name): global BLOCKADE_CONFIGS config = BLOCKADE_CONFIGS[name] host_exec = BlockadeManager.host_exec if host_exec is None: raise ValueError("host exec not set") return Blockade(config, blockade_id=name, state=BlockadeManager.load_state(name), network=BlockadeNetwork(config, host_exec))
def test_create(self): containers = [BlockadeContainerConfig("c1", "image"), BlockadeContainerConfig("c2", "image"), BlockadeContainerConfig("c3", "image")] config = BlockadeConfig(containers) self.network.new_veth_device_name.side_effect = ["veth1", "veth2", "veth3"] initialize = lambda x: BlockadeState("ourblockadeid", x) self.state_factory.initialize.side_effect = initialize self.docker_client.create_container.side_effect = [ {"Id": "container1"}, {"Id": "container2"}, {"Id": "container3"}] b = Blockade(config, self.state_factory, self.network, self.docker_client) b.create() self.assertEqual(self.state_factory.initialize.call_count, 1) self.assertEqual(self.docker_client.create_container.call_count, 3)
def test_add_docker_containers(self): containers = ['id1', 'id2', 'id3'] self.docker_client.inspect_container.side_effect = lambda id: { "Id": id } self.network.get_container_device.side_effect = lambda dc, y: "veth" + y self.state.exists.side_effect = lambda: False self.state.container_id.side_effect = lambda name: None self.state.containers = {} self.state.blockade_id = self.blockade_id b = Blockade(BlockadeConfig(), state=self.state, network=self.network, docker_client=self.docker_client) b.add_container(containers) self.assertEqual(self.state.update.call_count, 1) self.assertEqual(len(self.state.containers), 3)
def test_get_container_description_ip_address_info(self): expected_ip = "1.2.3.4" self.docker_client.inspect_container.side_effect = lambda id: { "Id": id, "IPAddress": "", "NetworkSettings": { "Networks": { "TheOnlyNetwork": { "IPAddress": expected_ip } } } } b = Blockade(BlockadeConfig(), state=self.state, network=self.network, docker_client=self.docker_client) container = b._get_container_description("c1") self.assertEqual(expected_ip, container.ip_address)
def test_create(self): containers = [BlockadeContainerConfig("c1", "image"), BlockadeContainerConfig("c2", "image"), BlockadeContainerConfig("c3", "image")] config = BlockadeConfig(containers) self.network.get_container_device.side_effect = lambda dc, x, y: "veth"+y initialize = lambda x, y: BlockadeState("ourblockadeid", x) self.state_factory.initialize.side_effect = initialize self.docker_client.create_container.side_effect = [ {"Id": "container1"}, {"Id": "container2"}, {"Id": "container3"}] b = Blockade(config, self.state_factory, self.network, self.docker_client) b.create() self.assertEqual(self.state_factory.initialize.call_count, 1) self.assertEqual(self.docker_client.create_container.call_count, 3)
def test_create(self): containers = {'c1': BlockadeContainerConfig("c1", "image"), 'c2': BlockadeContainerConfig("c2", "image"), 'c3': BlockadeContainerConfig("c3", "image")} config = BlockadeConfig(containers) self.network.get_container_device.side_effect = lambda dc, y: "veth"+y self.state.exists.side_effect = lambda: False self.state.blockade_id = self.blockade_id self.docker_client.create_container.side_effect = [ {"Id": "container1"}, {"Id": "container2"}, {"Id": "container3"}] b = Blockade(config, state=self.state, network=self.network, docker_client=self.docker_client) b.create() self.assertEqual(self.state.initialize.call_count, 1) self.assertEqual(self.docker_client.create_container.call_count, 3)