Ejemplo n.º 1
0
def main():
  parser = ArgumentParser()
  parser.add_argument("--manifest", help="the manifest file location to be cleared of tests")
  parser.add_argument("--deploy_tests", action="store_true", help="indicates whether tests should be deployed")
  args = parser.parse_args()

  manifest = args.manifest
  resources = load_resources_yaml(manifest)
  filtered_resources = []
  for resource in resources:
    helm_hook = deep_get(resource, "metadata", "annotations", _HELM_HOOK_KEY)
    if helm_hook is None:
      filtered_resources.append(resource)
    elif helm_hook == _HOOK_SUCCESS:
      if args.deploy_tests:
        annotations = deep_get(resource, "metadata", "annotations")
        del annotations[_HELM_HOOK_KEY]
        annotations[GOOGLE_CLOUD_TEST] = "test"
        filtered_resources.append(resource)
    elif helm_hook == _HOOK_FAILURE:
      if args.deploy_tests:
        raise Exception("Helm hook {} is not supported".format(helm_hook))
    else:
      raise Exception("Helm hook {} is not supported".format(helm_hook))

  with open(manifest, "w") as out:
    yaml.dump_all(filtered_resources, out,
                  default_flow_style=False, explicit_start=True)
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_app_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", encoding='utf-8') as outfile:
            yaml.safe_dump_all(nontest_resources,
                               outfile,
                               default_flow_style=False)

    if test_resources:
        with open(args.out_test_manifests, "a",
                  encoding='utf-8') as test_outfile:
            yaml.safe_dump_all(test_resources,
                               test_outfile,
                               default_flow_style=False)
Ejemplo n.º 3
0
def main():
    parser = ArgumentParser(description=_PROG_HELP)
    parser.add_argument('--namespace')
    parser.add_argument('--manifest')
    parser.add_argument('--timeout', type=int, default=300)
    args = parser.parse_args()

    Command('''
      kubectl apply
      --namespace="{}"
      --filename="{}"
      '''.format(args.namespace, args.manifest),
            print_call=True)

    resources = load_resources_yaml(args.manifest)

    for resource_def in resources:
        full_name = "{}/{}".format(resource_def['kind'],
                                   deep_get(resource_def, 'metadata', 'name'))

        if resource_def['kind'] != 'Pod':
            log("INFO Skip '{}'".format(full_name))
            continue

        start_time = time.time()
        poll_interval = 4
        tester_timeout = args.timeout

        while True:
            try:
                resource = Command('''
          kubectl get "{}"
          --namespace="{}"
          -o=json
          '''.format(full_name, args.namespace),
                                   print_call=True).json()
            except CommandException as ex:
                log(str(ex))
                log("INFO retrying")
                time.sleep(poll_interval)
                continue

            result = deep_get(resource, 'status', 'phase')

            if result == "Failed":
                print_logs(full_name, args.namespace)
                raise Exception("ERROR Tester '{}' failed".format(full_name))

            if result == "Succeeded":
                print_logs(full_name, args.namespace)
                log("INFO Tester '{}' succeeded".format(full_name))
                break

            if time.time() - start_time > tester_timeout:
                print_logs(full_name, args.namespace)
                raise Exception("ERROR Tester '{}' timeout".format(full_name))

            time.sleep(poll_interval)
def main():
    parser = ArgumentParser(description=_PROG_HELP)
    parser.add_argument('--test_schema',
                        help='Test schema file',
                        required=True)
    parser.add_argument('--original_schema',
                        help='Original schema file',
                        required=True)
    parser.add_argument(
        '--output',
        action='append',
        default=[],
        help='Location(s) of the file(s) to output the overlayed schema')
    args = parser.parse_args()

    if os.path.isfile(args.test_schema):
        test_schema = load_yaml(args.test_schema)
    else:
        log.info(
            'Test schema file {} does not exist. '
            'Using the original schema.', args.test_schema)
        test_schema = {}

    output_schema = load_yaml(args.original_schema)
    output_schema['properties'] = output_schema.get('properties', {})
    for prop in test_schema.get('properties', {}):
        test_type = deep_get(test_schema, 'properties', prop,
                             'x-google-marketplace', 'type')
        output_type = deep_get(output_schema, 'properties', prop,
                               'x-google-marketplace', 'type')
        if (test_type != output_type):
            raise Exception(
                'Changing x-google-marketplace type is not allowed. Property: {}',
                prop)
        output_schema['properties'][prop] = test_schema['properties'][prop]

    for output in args.output:
        with open(output, 'w', encoding='utf-8') as f:
            yaml.dump(output_schema, f)
Ejemplo n.º 5
0
 def test_deep_get(self):
     c = {'c': 1}
     b = {'b': c}
     a = {'a': b}
     self.assertEqual(dict_util.deep_get(a, 'a'), b)
     self.assertEqual(dict_util.deep_get(a, 'a', 'b'), c)
     self.assertEqual(dict_util.deep_get(a, 'a', 'b', 'c'), 1)
     self.assertEqual(dict_util.deep_get(a, 'b'), None)
     self.assertEqual(dict_util.deep_get(a, 'a', 'c'), None)
     self.assertEqual(dict_util.deep_get(a, 'a', 'b', 'd'), None)
def main():
    parser = ArgumentParser(description=_PROG_HELP)
    parser.add_argument('--namespace')
    parser.add_argument('--manifest')
    parser.add_argument('--timeout', type=int, default=300)
    args = parser.parse_args()

    try:
        Command('''
        kubectl apply
        --namespace="{}"
        --filename="{}"
        '''.format(args.namespace, args.manifest),
                print_call=True)
    except CommandException as ex:
        log.error("{} Failed to apply tester job. Reason: {}", LOG_SMOKE_TEST,
                  ex.message)
        return

    resources = load_resources_yaml(args.manifest)

    for resource_def in resources:
        full_name = "{}/{}".format(resource_def['kind'],
                                   deep_get(resource_def, 'metadata', 'name'))

        if resource_def['kind'] != 'Pod':
            log.info("Skip '{}'", full_name)
            continue

        start_time = time.time()
        poll_interval = 4
        tester_timeout = args.timeout

        while True:
            try:
                resource = Command('''
          kubectl get "{}"
          --namespace="{}"
          -o=json
          '''.format(full_name, args.namespace),
                                   print_call=True).json()
            except CommandException as ex:
                log.info(str(ex))
                log.info("retrying")
                time.sleep(poll_interval)
                continue

            result = deep_get(resource, 'status', 'phase')

            if result == "Failed":
                print_tester_logs(full_name, args.namespace)
                log.error("{} Tester '{}' failed.", LOG_SMOKE_TEST, full_name)
                break

            if result == "Succeeded":
                print_tester_logs(full_name, args.namespace)
                log.info("{} Tester '{}' succeeded.", LOG_SMOKE_TEST,
                         full_name)
                break

            if time.time() - start_time > tester_timeout:
                print_tester_logs(full_name, args.namespace)
                log.error("{} Tester '{}' timeout.", LOG_SMOKE_TEST, full_name)

            time.sleep(poll_interval)