def get_options(args): """Resolve all the command-line options.""" args, defaults = preprocess_args( args, default_home_path_filename='validate_bom.yml') parser = argparse.ArgumentParser(prog='validate_bom.sh') add_standard_parser_args(parser, defaults) # DEPRECATED - use output_dir instead add_parser_argument(parser, 'log_dir', defaults, './validate_bom_results', help='Path to root directory for report output.') MetricsManager.init_argument_parser(parser, defaults) validate_bom__config.init_argument_parser(parser, defaults) validate_bom__deploy.init_argument_parser(parser, defaults) validate_bom__test.init_argument_parser(parser, defaults) options = parser.parse_args(args) options.program = 'validate_bom' options.command = 'validate_bom' # metrics assumes a "command" value. options.log_dir = options.output_dir # deprecated validate_bom__config.validate_options(options) validate_bom__test.validate_options(options) if not os.path.exists(options.log_dir): os.makedirs(options.log_dir) if options.influxdb_database == 'SpinnakerBuildTool': options.influxdb_database = 'SpinnakerValidate' # Add platform/spinnaker_type to each metric we produce. # We'll use this to distinguish what was being tested. context_labels = 'platform=%s,deployment_type=%s' % ( validate_bom__deploy.determine_deployment_platform(options), options.deploy_spinnaker_type) latest_unvalidated_suffix = '-latest-unvalidated' if options.deploy_version.endswith(latest_unvalidated_suffix): bom_series = options.deploy_version[:-len(latest_unvalidated_suffix)] else: bom_series = options.deploy_version[:options.deploy_version.rfind('-')] context_labels += ',version=%s' % bom_series if options.monitoring_context_labels: context_labels += ',' + options.monitoring_context_labels options.monitoring_context_labels = context_labels return options
def test_new_release_branch_command(self): defaults = { "input_dir": self.options.input_dir, "output_dir": self.options.output_dir, "only_repositories": EXTRA_REPO, "github_owner": "default", "git_branch": EXTRA_REPO + "-branch", "spinnaker_version": "NewSpinnakerVersion", "github_repository_root": self.options.github_repository_root, } registry = {} bomtool_main.add_standard_parser_args(self.parser, defaults) buildtool.spinnaker_commands.register_commands(registry, self.subparsers, defaults) factory = registry["new_release_branch"] factory.init_argparser(self.parser, defaults) options = self.parser.parse_args(["new_release_branch"]) mock_push_tag = self.patch_method(GitRunner, "push_tag_to_origin") mock_push_branch = self.patch_method(GitRunner, "push_branch_to_origin") command = factory.make_command(options) command() base_git_dir = os.path.join(options.input_dir, "new_release_branch") self.assertEqual(os.listdir(base_git_dir), [EXTRA_REPO]) git_dir = os.path.join(base_git_dir, EXTRA_REPO) self.assertEqual( GitRunner(options).query_local_repository_commit_id(git_dir), self.repo_commit_map[EXTRA_REPO][EXTRA_REPO + "-branch"], ) mock_push_branch.assert_called_once_with(git_dir, "NewSpinnakerVersion") self.assertEqual(0, mock_push_tag.call_count)
def test_new_release_branch_command(self): defaults = { 'input_dir': self.options.input_dir, 'output_dir': self.options.output_dir, 'only_repositories': EXTRA_REPO, 'github_owner': 'default', 'git_branch': EXTRA_REPO + '-branch', 'spinnaker_version': 'NewSpinnakerVersion', 'github_filesystem_root': self.options.github_filesystem_root, 'github_hostname': self.options.github_hostname } registry = {} bomtool_main.add_standard_parser_args(self.parser, defaults) buildtool.spinnaker_commands.register_commands(registry, self.subparsers, defaults) factory = registry['new_release_branch'] factory.init_argparser(self.parser, defaults) options = self.parser.parse_args(['new_release_branch']) mock_push_tag = self.patch_method(GitRunner, 'push_tag_to_origin') mock_push_branch = self.patch_method(GitRunner, 'push_branch_to_origin') command = factory.make_command(options) command() base_git_dir = os.path.join(options.input_dir, 'new_release_branch') self.assertEquals(os.listdir(base_git_dir), [EXTRA_REPO]) git_dir = os.path.join(base_git_dir, EXTRA_REPO) self.assertEquals( GitRunner(options).query_local_repository_commit_id(git_dir), self.repo_commit_map[EXTRA_REPO][EXTRA_REPO + '-branch']) mock_push_branch.assert_called_once_with(git_dir, 'NewSpinnakerVersion') self.assertEquals(0, mock_push_tag.call_count)
def test_new_release_branch_command(self): defaults = { 'input_dir': self.options.input_dir, 'output_dir': self.options.output_dir, 'only_repositories': EXTRA_REPO, 'github_owner': 'default', 'git_branch': EXTRA_REPO + '-branch', 'spinnaker_version': 'NewSpinnakerVersion', 'github_repository_root': self.options.github_repository_root, } registry = {} bomtool_main.add_standard_parser_args(self.parser, defaults) buildtool.spinnaker_commands.register_commands( registry, self.subparsers, defaults) factory = registry['new_release_branch'] factory.init_argparser(self.parser, defaults) options = self.parser.parse_args(['new_release_branch']) mock_push_tag = self.patch_method(GitRunner, 'push_tag_to_origin') mock_push_branch = self.patch_method(GitRunner, 'push_branch_to_origin') command = factory.make_command(options) command() base_git_dir = os.path.join(options.input_dir, 'new_release_branch') self.assertEqual(os.listdir(base_git_dir), [EXTRA_REPO]) git_dir = os.path.join(base_git_dir, EXTRA_REPO) self.assertEqual( GitRunner(options).query_local_repository_commit_id(git_dir), self.repo_commit_map[EXTRA_REPO][EXTRA_REPO + '-branch']) mock_push_branch.assert_called_once_with(git_dir, 'NewSpinnakerVersion') self.assertEqual(0, mock_push_tag.call_count)
def test_bom_command(self): """Make sure when we run "build_bom" we actually get what we meant.""" defaults = vars(make_default_options(self.options)) defaults.update({ 'bom_path': 'MY PATH', 'github_owner': 'TestOwner', 'input_dir': 'TestInputRoot' }) defaults.update({ 'bintray_org': 'TestBintrayOrg', 'bintray_debian_repository': 'TestDebianRepo', 'docker_registry': 'TestDockerRegistry', 'publish_gce_image_project': 'TestGceProject' }) del defaults['github_filesystem_root'] parser = argparse.ArgumentParser() registry = bomtool_main.make_registry([buildtool.bom_commands], parser, defaults) bomtool_main.add_standard_parser_args(parser, defaults) options = parser.parse_args(['build_bom']) prefix = 'http://test-domain.com/test-owner' make_fake = self.patch_method # When asked to filter the normal bom repos to determine source_repositories # we'll return our own fake repository as if we configured the original # command for it. This will also make it easier to test just the one # repo rather than all, and that there are no assumptions. mock_filter = make_fake(BuildBomCommand, 'filter_repositories') test_repository = GitRepositorySpec('clouddriver', commit_id='CommitA', origin=prefix + '/TestRepoA') mock_filter.return_value = [test_repository] # When the base command ensures the local repository exists, we'll # intercept that call and do nothing rather than the git checkouts, etc. make_fake(BranchSourceCodeManager, 'ensure_local_repository') # When the base command asks for the repository metadata, we'll return # this hardcoded info, then look for it later in the generated om. mock_lookup = make_fake(BranchSourceCodeManager, 'lookup_source_info') summary = RepositorySummary('CommitA', 'TagA', '9.8.7', '44.55.66', []) source_info = SourceInfo('MyBuildNumber', summary) mock_lookup.return_value = source_info # When asked to write the bom out, do nothing. # We'll verify the bom later when looking at the mock call sequencing. mock_write = self.patch_function( 'buildtool.bom_commands.write_to_path') mock_now = self.patch_function('buildtool.bom_commands.now') mock_now.return_value = datetime.datetime(2018, 1, 2, 3, 4, 5) factory = registry['build_bom'] command = factory.make_command(options) command() # Verify source repositories were filtered self.assertEquals([test_repository], command.source_repositories) # Verify that the filter was called with the original bom repos, # and these repos were coming from the configured github_owner's repo. bom_repo_list = [ GitRepositorySpec(name, git_dir=os.path.join('TestInputRoot', 'build_bom', name), origin='https://%s/TestOwner/%s' % (options.github_hostname, name), upstream='https://github.com/spinnaker/' + name) for name in sorted([ 'clouddriver', 'deck', 'echo', 'fiat', 'front50', 'gate', 'igor', 'orca', 'rosco', 'spinnaker', 'spinnaker-monitoring' ]) ] mock_filter.assert_called_once_with(bom_repo_list) mock_lookup.assert_called_once_with(test_repository) bom_text, bom_path = mock_write.call_args_list[0][0] self.assertEquals(bom_path, 'MY PATH') bom = yaml.load(bom_text) golden_text = textwrap.dedent("""\ artifactSources: debianRepository: https://dl.bintray.com/TestBintrayOrg/TestDebianRepo dockerRegistry: TestDockerRegistry gitPrefix: http://test-domain.com/test-owner googleImageProject: TestGceProject dependencies: services: clouddriver: commit: CommitA version: 9.8.7-MyBuildNumber timestamp: '2018-01-02 03:04:05' version: OptionBranch-OptionBuildNumber """) golden_bom = yaml.load(golden_text) golden_bom['dependencies'] = load_default_bom_dependencies() for key, value in golden_bom.items(): self.assertEquals(value, bom[key])
def test_bom_command(self): """Make sure when we run "build_bom" we actually get what we meant.""" defaults = vars(make_default_options(self.options)) defaults.update({ "bom_path": "MY PATH", "github_owner": "TestOwner", "input_dir": "TestInputRoot", }) del defaults["github_repository_root"] parser = argparse.ArgumentParser() registry = bomtool_main.make_registry([buildtool.bom_commands], parser, defaults) bomtool_main.add_standard_parser_args(parser, defaults) options = parser.parse_args(["build_bom"]) prefix = "http://test-domain.com/test-owner" make_fake = self.patch_method # When asked to filter the normal bom repos to determine source_repositories # we'll return our own fake repository as if we configured the original # command for it. This will also make it easier to test just the one # repo rather than all, and that there are no assumptions. mock_filter = make_fake(BuildBomCommand, "filter_repositories") test_repository = GitRepositorySpec("clouddriver", commit_id="CommitA", origin=prefix + "/TestRepoA") mock_filter.return_value = [test_repository] # When the base command ensures the local repository exists, we'll # intercept that call and do nothing rather than the git checkouts, etc. make_fake(BranchSourceCodeManager, "ensure_local_repository") # When the base command asks for the repository metadata, we'll return # this hardcoded info, then look for it later in the generated om. mock_refresh = make_fake(BranchSourceCodeManager, "refresh_source_info") summary = RepositorySummary("CommitA", "TagA", "9.8.7", []) source_info = SourceInfo("MyBuildNumber", summary) mock_refresh.return_value = source_info # When asked to write the bom out, do nothing. # We'll verify the bom later when looking at the mock call sequencing. mock_write = self.patch_function( "buildtool.bom_commands.write_to_path") mock_now = self.patch_function("buildtool.bom_commands.now") mock_now.return_value = datetime.datetime(2018, 1, 2, 3, 4, 5) factory = registry["build_bom"] command = factory.make_command(options) command() # Verify source repositories were filtered self.assertEqual([test_repository], command.source_repositories) # Verify that the filter was called with the original bom repos, # and these repos were coming from the configured github_owner's repo. bom_repo_list = [ GitRepositorySpec( name, git_dir=os.path.join("TestInputRoot", "build_bom", name), origin="https://%s/TestOwner/%s" % (options.github_hostname, name), upstream="https://github.com/spinnaker/" + name, ) for name in sorted([ "clouddriver", "deck", "echo", "fiat", "front50", "gate", "igor", "kayenta", "orca", "rosco", "spinnaker-monitoring", ]) ] mock_filter.assert_called_once_with(bom_repo_list) mock_refresh.assert_called_once_with(test_repository, "OptionBuildNumber") bom_text, bom_path = mock_write.call_args_list[0][0] self.assertEqual(bom_path, "MY PATH") bom = yaml.safe_load(bom_text) golden_text = (textwrap.dedent("""\ artifactSources: gitPrefix: http://test-domain.com/test-owner debianRepository: %s dockerRegistry: %s googleImageProject: %s dependencies: services: clouddriver: commit: CommitA version: 9.8.7 timestamp: '2018-01-02 03:04:05' version: OptionBuildNumber """) % ( SPINNAKER_DEBIAN_REPOSITORY, SPINNAKER_DOCKER_REGISTRY, SPINNAKER_GOOGLE_IMAGE_PROJECT, )) golden_bom = yaml.safe_load(golden_text.format()) golden_bom["dependencies"] = load_default_bom_dependencies() for key, value in golden_bom.items(): self.assertEqual(value, bom[key])
def test_bom_command(self): """Make sure when we run "build_bom" we actually get what we meant.""" defaults = vars(make_default_options(self.options)) defaults.update({'bom_path': 'MY PATH', 'github_owner': 'TestOwner', 'input_dir': 'TestInputRoot'}) defaults.update({'bintray_org': 'TestBintrayOrg', 'bintray_debian_repository': 'TestDebianRepo', 'docker_registry': 'TestDockerRegistry', 'publish_gce_image_project': 'TestGceProject'}) del defaults['github_repository_root'] parser = argparse.ArgumentParser() registry = bomtool_main.make_registry([buildtool.bom_commands], parser, defaults) bomtool_main.add_standard_parser_args(parser, defaults) options = parser.parse_args(['build_bom']) prefix = 'http://test-domain.com/test-owner' make_fake = self.patch_method # When asked to filter the normal bom repos to determine source_repositories # we'll return our own fake repository as if we configured the original # command for it. This will also make it easier to test just the one # repo rather than all, and that there are no assumptions. mock_filter = make_fake(BuildBomCommand, 'filter_repositories') test_repository = GitRepositorySpec('clouddriver', commit_id='CommitA', origin=prefix + '/TestRepoA') mock_filter.return_value = [test_repository] # When the base command ensures the local repository exists, we'll # intercept that call and do nothing rather than the git checkouts, etc. make_fake(BranchSourceCodeManager, 'ensure_local_repository') # When the base command asks for the repository metadata, we'll return # this hardcoded info, then look for it later in the generated om. mock_refresh = make_fake(BranchSourceCodeManager, 'refresh_source_info') summary = RepositorySummary('CommitA', 'TagA', '9.8.7', '44.55.66', []) source_info = SourceInfo('MyBuildNumber', summary) mock_refresh.return_value = source_info # When asked to write the bom out, do nothing. # We'll verify the bom later when looking at the mock call sequencing. mock_write = self.patch_function('buildtool.bom_commands.write_to_path') mock_now = self.patch_function('buildtool.bom_commands.now') mock_now.return_value = datetime.datetime(2018, 1, 2, 3, 4, 5) factory = registry['build_bom'] command = factory.make_command(options) command() # Verify source repositories were filtered self.assertEqual([test_repository], command.source_repositories) # Verify that the filter was called with the original bom repos, # and these repos were coming from the configured github_owner's repo. bom_repo_list = [ GitRepositorySpec( name, git_dir=os.path.join('TestInputRoot', 'build_bom', name), origin='https://%s/TestOwner/%s' % (options.github_hostname, name), upstream='https://github.com/spinnaker/' + name) for name in sorted(['clouddriver', 'deck', 'echo', 'fiat', 'front50', 'gate', 'igor', 'kayenta', 'orca', 'rosco', 'spinnaker-monitoring']) ] mock_filter.assert_called_once_with(bom_repo_list) mock_refresh.assert_called_once_with(test_repository, 'OptionBuildNumber') bom_text, bom_path = mock_write.call_args_list[0][0] self.assertEqual(bom_path, 'MY PATH') bom = yaml.safe_load(bom_text) golden_text = textwrap.dedent("""\ artifactSources: debianRepository: https://dl.bintray.com/TestBintrayOrg/TestDebianRepo dockerRegistry: TestDockerRegistry gitPrefix: http://test-domain.com/test-owner googleImageProject: TestGceProject dependencies: services: clouddriver: commit: CommitA version: 9.8.7-MyBuildNumber timestamp: '2018-01-02 03:04:05' version: OptionBranch-OptionBuildNumber """) golden_bom = yaml.safe_load(golden_text) golden_bom['dependencies'] = load_default_bom_dependencies() for key, value in golden_bom.items(): self.assertEqual(value, bom[key])