def test_connect_one_machine_to_link(default_scenario: Lab): default_scenario.connect_machine_to_link("pc1", "A") assert len(default_scenario.machines) == 1 assert default_scenario.machines['pc1'] assert len(default_scenario.links) == 1 assert default_scenario.links['A'] assert default_scenario.machines['pc1'].interfaces[0].name == 'A'
def test_get_num_terms_mix(): # Lab options have a greater priority than machine options lab = Lab('mem_test') lab.add_option("num_terms", "2") kwargs = {"num_terms": "1"} device1 = Machine(lab, "test_machine1", **kwargs) device2 = Machine(lab, "test_machine2") assert device1.get_num_terms() == 2 assert device2.get_num_terms() == 2
def test_deploy_links(mock_deploy_link, docker_link, progress_bar): lab = Lab("Default scenario") link_a = lab.get_or_new_link("A") link_b = lab.get_or_new_link("B") link_c = lab.get_or_new_link("C") docker_link.deploy_links(lab) mock_deploy_link.assert_any_call(None, ("A", link_a)) mock_deploy_link.assert_any_call(None, ("B", link_b)) mock_deploy_link.assert_any_call(None, ("C", link_c)) assert mock_deploy_link.call_count == 3
def test_deploy_machines(mock_deploy_and_start, docker_machine): lab = Lab("Default scenario") lab.get_or_new_machine("pc1", **{'image': 'kathara/test1'}) lab.get_or_new_machine("pc2", **{'image': 'kathara/test2'}) docker_machine.docker_image.check_and_pull_from_list.return_value = None mock_deploy_and_start.return_value = None docker_machine.deploy_machines(lab) docker_machine.docker_image.check_and_pull_from_list.assert_called_once_with( {'kathara/test1', 'kathara/test2'}) assert mock_deploy_and_start.call_count == 2
def test_intersect_machines(default_scenario: Lab): default_scenario.connect_machine_to_link("pc1", "A") default_scenario.connect_machine_to_link("pc2", "A") default_scenario.connect_machine_to_link("pc2", "B") assert len(default_scenario.machines) == 2 links = default_scenario.get_links_from_machines(selected_machines=["pc1"]) assert len(default_scenario.machines) == 2 assert 'pc1' in default_scenario.machines assert 'pc2' in default_scenario.machines assert 'A' in links assert 'B' not in links
def test_deploy_machines(mock_deploy, kubernetes_machine): lab = Lab("Default scenario") lab.get_or_new_machine("pc1", **{'image': 'kathara/test1'}) lab.get_or_new_machine("pc2", **{'image': 'kathara/test2'}) mock_deploy.return_value = None kubernetes_machine.deploy_machines(lab) assert mock_deploy.call_count == 2
def test_undeploy_empty_lab(mock_get_links_by_filters, mock_undeploy_link, docker_link): lab = Lab("Default scenario") mock_get_links_by_filters.return_value = [] docker_link.undeploy("lab_hash") mock_get_links_by_filters.called_once_with(lab.hash) assert not mock_undeploy_link.called
def default_device(mock_docker_container): device = Machine(Lab('Default scenario'), "test_device") device.add_meta("exec", "ls") device.add_meta("mem", "64m") device.add_meta("cpus", "2") device.add_meta("image", "kathara/test") device.add_meta("bridged", False) device.api_object = mock_docker_container return device
def test_pack_data_hidden_files(pack_file_for_tar_mock): lab_path = os.path.join("tests", "model", "hiddenfiles") lab = Lab(None, lab_path) device = lab.get_or_new_machine("test_machine") pack_file_for_tar_mock.return_value = (tarfile.TarInfo(""), None) device.pack_data() pack_file_for_tar_mock.assert_any_call( os.path.join(lab_path, "test_machine", ".hidden"), arc_name="hostlab/" + os.path.join(device.name, ".hidden")) pack_file_for_tar_mock.assert_any_call( os.path.join(lab_path, "test_machine", "nothidden"), arc_name="hostlab/" + os.path.join(device.name, "nothidden")) assert pack_file_for_tar_mock.call_count == 2
def two_device_scenario(): lab = Lab("Default scenario") pc1 = lab.get_or_new_machine("pc1", **{'image': 'kathara/test1'}) pc2 = lab.get_or_new_machine("pc2", **{'image': 'kathara/test2'}) lab.connect_machine_to_link(pc1.name, "A") lab.connect_machine_to_link(pc1.name, "B") lab.connect_machine_to_link(pc2.name, "A") return lab
def default_device(mock_kubernetes_deployment): device = Machine(Lab("default_scenario"), "test_device") device.add_meta("exec", "ls") device.add_meta("mem", "64m") device.add_meta("cpus", "2") device.add_meta("image", "kathara/test") device.add_meta("bridged", False) device.add_meta('real_name', "devprefix-test-device-ec84ad3b") device.api_object = mock_kubernetes_deployment return device
def test_apply_dependencies(default_scenario: Lab): default_scenario.get_or_new_machine("pc1") default_scenario.get_or_new_machine("pc2") default_scenario.get_or_new_machine("pc3") default_scenario.apply_dependencies(["pc3", "pc1", "pc2"]) assert default_scenario.machines.popitem()[0] == "pc2" assert default_scenario.machines.popitem()[0] == "pc1" assert default_scenario.machines.popitem()[0] == "pc3"
def undeploy(mock_get_links_by_filters, mock_undeploy_link, kubernetes_network): lab = Lab("Default scenario") lab.get_or_new_link("A") lab.get_or_new_link("B") lab.get_or_new_link("C") mock_get_links_by_filters.return_value = [kubernetes_network, kubernetes_network, kubernetes_network] kubernetes_link.undeploy("lab_hash") mock_get_links_by_filters.called_once_with(lab.hash) assert mock_undeploy_link.call_count == 3
def test_wipe(mock_get_links_by_filters, mock_net1, mock_net2, mock_net3, mock_undeploy_link, docker_link): lab = Lab("Default scenario") lab.get_or_new_link("A") lab.get_or_new_link("B") lab.get_or_new_link("C") mock_get_links_by_filters.return_value = [mock_net1, mock_net2, mock_net3] docker_link.wipe() mock_get_links_by_filters.called_once_with(lab.hash) assert mock_net1.reload.call_count == 1 assert mock_net2.reload.call_count == 1 assert mock_net3.reload.call_count == 1 assert mock_undeploy_link.call_count == 3
def test_get_or_new_machine_exists(default_scenario: Lab): default_scenario.get_or_new_machine("pc1") default_scenario.get_or_new_machine("pc1") assert len(default_scenario.machines) == 1 assert default_scenario.machines['pc1']
def directory_scenario(temporary_path): Path(os.path.join(temporary_path, "shared.startup")).touch() Path(os.path.join(temporary_path, "shared.shutdown")).touch() return Lab("directory_scenario", path=temporary_path)
def test_create_shared_folder_no_path(default_scenario: Lab): assert default_scenario.create_shared_folder() is None
def test_get_cpu_from_device_meta(): kwargs = {"cpus": "1"} device = Machine(Lab("test_lab"), "test_machine", **kwargs) assert device.get_cpu() == 1
def test_create_shared_folder(directory_scenario: Lab): directory_scenario.create_shared_folder() assert os.path.isdir(os.path.join(directory_scenario.path, 'shared'))
def test_assign_meta_to_machine(default_scenario: Lab): default_scenario.get_or_new_machine("pc1") default_scenario.assign_meta_to_machine("pc1", "test_meta", "test_value") assert "test_meta" in default_scenario.machines['pc1'].meta assert default_scenario.machines['pc1'].meta["test_meta"] == "test_value"
def test_deploy_links_no_link(mock_deploy_link, kubernetes_link, progress_bar): lab = Lab("Default scenario") kubernetes_link.deploy_links(lab) assert not mock_deploy_link.called
def test_get_num_terms_from_lab_options(): lab = Lab('mem_test') lab.add_option("num_terms", "2") device = Machine(lab, "test_machine") assert device.get_num_terms() == 2
def default_scenario(): return Lab("default_scenario")
def test_get_or_new_link_two_cd(default_scenario: Lab): default_scenario.get_or_new_link("A") default_scenario.get_or_new_link("B") assert len(default_scenario.links) == 2 assert default_scenario.links['A'] assert default_scenario.links['B']
def default_link(): from src.Kathara.model.Link import Link return Link(Lab("default_scenario"), "A")
def test_get_num_terms_from_device_meta(): kwargs = {"num_terms": "1"} device = Machine(Lab("test_lab"), "test_machine", **kwargs) assert device.get_num_terms() == 1
def test_get_or_new_machine_two_devices(default_scenario: Lab): default_scenario.get_or_new_machine("pc1") default_scenario.get_or_new_machine("pc2") assert len(default_scenario.machines) == 2 assert default_scenario.machines['pc1'] assert default_scenario.machines['pc2']
def test_assign_meta_to_machine_exception(default_scenario: Lab): default_scenario.get_or_new_machine("pc1") with pytest.raises(MachineOptionError): default_scenario.assign_meta_to_machine("pc1", "port", "value")
def test_get_or_new_link_exists(default_scenario: Lab): default_scenario.get_or_new_link("A") default_scenario.get_or_new_link("A") assert len(default_scenario.links) == 1 assert default_scenario.links['A']
def default_device(): return Machine(Lab("test_lab"), "test_machine")