def create_and_launch_daemon_set_runners(self, apps_api: k8s.client.AppsV1Api, core_api: k8s.client.CoreV1Api): """ Creates DaemonSet for testing pods that already in the cluster. """ supported_cases = [ c for c in self._test_cases if self._hosts_are_in_cluster(c) ] filtered_cases = [ c for c in self._test_cases if c not in supported_cases ] logger.debug("Filtered " + str(len(filtered_cases)) + " test cases: " + str(filtered_cases)) cases_dict = merge_in_dict(supported_cases) logger.debug("Created casesDict: " + str(cases_dict) + " from " + str(supported_cases)) concrete_cases, from_host_mappings, to_host_mappings, port_mappings = \ self._find_or_create_cluster_resources_for_cases(cases_dict, core_api) logger.debug("concreteCases: " + str(concrete_cases)) self._create_project_namespace_if_missing(core_api) config_map_name = PROJECT_PREFIX + "-cases-cfgmap" self._create_or_update_case_config_map(config_map_name, concrete_cases, core_api) service_account_name = PROJECT_PREFIX + "-runner" self._create_missing_service_accounts(core_api, service_account_name, PROJECT_NAMESPACE) self._create_missing_cluster_role() self._create_missing_cluster_role_binding(service_account_name, PROJECT_NAMESPACE) self._create_daemon_set_if_missing(service_account_name, config_map_name, apps_api) return from_host_mappings, to_host_mappings, port_mappings
def test_mergeInDict_twoCasesSameFromHostSameToHost_returnsDict(): case1 = NetworkTestCase(test_host1, test_host2, 80, False) case2 = NetworkTestCase(test_host1, test_host2, 8080, True) expected = { test_host1.to_identifier(): {test_host2.to_identifier(): ["-80", "8080"]} } assert merge_in_dict([case1, case2]) == expected
def test_mergeInDict_twoCasesNoConflicts_returnsDict(): case1 = NetworkTestCase(test_host1, test_host2, 80, False) case2 = NetworkTestCase(test_host2, test_host1, 80, False) expected = { test_host1.to_identifier(): {test_host2.to_identifier(): ["-80"]}, test_host2.to_identifier(): {test_host1.to_identifier(): ["-80"]}, } assert merge_in_dict([case1, case2]) == expected
def generate(outfile: str): """ "Generate and output test cases. """ generator = NetworkTestCaseGenerator(LOGGER) v1net = k8s.client.NetworkingV1Api() orch = NetworkTestOrchestrator([], LOGGER) net_pols = v1net.list_network_policy_for_all_namespaces() cases, _ = generator.generate_test_cases(net_pols.items, orch.current_namespaces) write_formatted(merge_in_dict(cases), outfile)
return [resp] def ensure_cases_are_generated(self, core_api: k8s.client.CoreV1Api): """ Ensures that all required resources for testing are created """ # TODO split up this method supported_cases = [ case for case in self.test_cases if _hosts_are_in_cluster(case) ] filtered_cases = [ case for case in self.test_cases if case not in supported_cases ] self.logger.debug("Filtered %s test cases: %s", len(filtered_cases), filtered_cases) cases_dict = merge_in_dict(supported_cases) self.logger.debug("Created casesDict: %s from %s", cases_dict, supported_cases) ( concrete_cases, from_host_mappings, to_host_mappings, port_mappings, ) = self._find_or_create_cluster_resources_for_cases( cases_dict, core_api) self.logger.debug("concreteCases: %s", concrete_cases) config_map_name = f"{PROJECT_PREFIX}-cases-cfgmap" self._create_or_update_case_config_map(config_map_name, concrete_cases, core_api) return from_host_mappings, to_host_mappings, port_mappings, config_map_name
def test_mergeInDict_emptyList_returnsEmptyDict(): assert merge_in_dict([]) == {}