def test_resource_no_ownerrefs_adds_ownerref(self): resource = {'metadata': {'ownerReferences': []}} set_resource_ownership(APP_UID, APP_NAME, APP_API_VERSION, resource) self.assertListElementsEqual(resource['metadata']['ownerReferences'], [APP_OWNER_REF])
def main(): parser = ArgumentParser(description=_PROG_HELP) parser.add_argument("--app_name", required=True, help="the name of the application instance") parser.add_argument("--app_uid", required=True, help="the uid of the application instance") parser.add_argument("--app_api_version", required=True, help="apiVersion of the Application CRD") parser.add_argument("--manifests", required=True, help="the configuration for tests") parser.add_argument("--out_manifests", required=True, help="the file to write non-test resources to") parser.add_argument("--out_test_manifests", required=True, help="the file to write test resources to") args = parser.parse_args() if os.path.isfile(args.manifests): resources = load_resources_yaml(args.manifests) else: resources = [] for filename in os.listdir(args.manifests): resources += load_resources_yaml( os.path.join(args.manifests, filename)) test_resources = [] nontest_resources = [] for resource in resources: full_name = "{}/{}".format(resource['kind'], deep_get(resource, 'metadata', 'name')) if deep_get(resource, 'metadata', 'annotations', GOOGLE_CLOUD_TEST) == 'test': print("INFO Tester resource: {}".format(full_name)) set_resource_ownership(app_uid=args.app_uid, app_name=args.app_name, app_api_version=args.app_api_version, resource=resource) test_resources.append(resource) else: print("INFO Prod resource: {}".format(full_name)) nontest_resources.append(resource) if nontest_resources: with open(args.out_manifests, "w") as outfile: yaml.safe_dump_all(nontest_resources, outfile, default_flow_style=False) if test_resources: with open(args.out_test_manifests, "a") as test_outfile: yaml.safe_dump_all(test_resources, test_outfile, default_flow_style=False)
def test_resource_existing_app_ownerref_matching_uid_updates_existing( self): resource = {'metadata': {'ownerReferences': [{'uid': APP_UID}]}} set_resource_ownership(APP_UID, APP_NAME, APP_API_VERSION, 'Application', resource) self.assertListElementsEqual(resource['metadata']['ownerReferences'], [APP_OWNER_REF])
def test_resource_no_ownerrefs_ownerref(self): resource = {'metadata': {'ownerReferences': []}} set_resource_ownership(APP_UID, APP_NAME, APP_API_VERSION, 'Application', resource) self.assertCountEqual(resource['metadata']['ownerReferences'], [APP_OWNER_REF])
def test_resource_existing_ownerref_different_uid_adds_ownerref(self): resource = {'metadata': {'ownerReferences': [{'uid': OTHER_UID}]}} set_resource_ownership(APP_UID, APP_NAME, APP_API_VERSION, 'Application', resource) self.assertListElementsEqual(resource['metadata']['ownerReferences'], [{ 'uid': OTHER_UID }, APP_OWNER_REF])
def dump(outfile, resources, included_kinds, app_name, app_uid, app_api_version): to_be_dumped = [] for resource in resources: if included_kinds is None or resource["kind"] in included_kinds: log("Application '{:s}' owns '{:s}/{:s}'".format( app_name, resource["kind"], resource["metadata"]["name"])) resource = copy.deepcopy(resource) set_resource_ownership(app_uid=app_uid, app_name=app_name, app_api_version=app_api_version, resource=resource) to_be_dumped.append(resource) yaml.safe_dump_all(to_be_dumped, outfile, default_flow_style=False, indent=2)