def test_environments_with_no_default_can_be_modified(self): guy_path = self._create_fake_module('guy', ['qa']) commit_id = get_refs(guy_path + '/.git')['qa'] override = "{0}{1}".format(COMMIT_PREFIX, commit_id) ensure_environment(self.settings, 'test', None, modules=["guy:%s" % override]) self._jens_update() self.assertBare('modules/guy') self.assertClone('modules/guy/.%s' % commit_id, pointsto=commit_id) self.assertEnvironmentLinks("test") self.assertEnvironmentOverride("test", 'modules/guy', override) # ---- Point the override to a different commit destroy_environment(self.settings, 'test') commit_id = get_refs(guy_path + '/.git')['master'] override = "{0}{1}".format(COMMIT_PREFIX, commit_id) ensure_environment(self.settings, 'test', None, modules=["guy:%s" % override]) self._jens_update() self.assertBare('modules/guy') self.assertClone('modules/guy/.%s' % commit_id, pointsto=commit_id) self.assertEnvironmentLinks("test") self.assertEnvironmentOverride("test", 'modules/guy', override)
def test_hint_readded_to_the_queue_if_fetch_fails(self): self.settings.MODE = "ONDEMAND" yi_path = self._create_fake_hostgroup('yi', ['qa']) old_yi_qa = get_refs(yi_path + '/.git')['qa'] yi_path_bare = yi_path.replace('/user/', '/bare/') self._jens_update() self.assertBare('hostgroups/yi') self.assertClone('hostgroups/yi/qa') new_yi_qa = add_commit_to_branch(self.settings, yi_path, 'qa') # ---- Make it temporary unavailable shutil.move("%s/refs" % yi_path_bare, "%s/goat" % yi_path_bare) self.assertEqual(0, count_pending_hints(self.settings)) self._jens_update(hints={'hostgroups': ['yi']}, errorsExpected=True) self.assertEqual(1, count_pending_hints(self.settings)) self.assertBare('hostgroups/yi') self.assertClone('hostgroups/yi/qa', pointsto=old_yi_qa) # ---- Bring it back shutil.move("%s/goat" % yi_path_bare, "%s/refs" % yi_path_bare) self._jens_update(hints={'hostgroups': ['yi']}) self.assertClone('hostgroups/yi/qa', pointsto=new_yi_qa)
def test_environments_are_created_and_known_branches_expanded_regardless_of_update_hints(self): self.settings.MODE = "ONDEMAND" h1_path = self._create_fake_hostgroup('h1', ['qa', 'boom']) old_h1_qa = get_refs(h1_path + '/.git')['qa'] m1_path = self._create_fake_module('m1', ['qa', 'boom']) self._jens_update() ensure_environment(self.settings, 'test', 'master', hostgroups=['h1:boom'], modules=['m1:boom']) new_h1_qa = add_commit_to_branch(self.settings, h1_path, 'qa') self._jens_update(hints={'hostgroups': ['other']}) self.assertClone('hostgroups/h1/qa', pointsto=old_h1_qa) self.assertEnvironmentLinks("test") self.assertEnvironmentOverride("test", 'modules/m1', 'boom') self.assertEnvironmentOverride("test", 'hostgroups/hg_h1', 'boom') self._jens_update(hints={'modules': ['m1'], 'hostgroups': ['h1']}) self.assertClone('hostgroups/h1/qa', pointsto=new_h1_qa) self.assertEnvironmentLinks("test") self.assertEnvironmentOverride("test", 'modules/m1', 'boom') self.assertEnvironmentOverride("test", 'hostgroups/hg_h1', 'boom')
def test_created_if_new_and_removed_if_gone_regardless_of_hints(self): self.settings.MODE = "ONDEMAND" murdock_path = self._create_fake_module('murdock', ['qa']) steve_path = self._create_fake_hostgroup('steve', ['qa']) old_qa = get_refs(murdock_path + '/.git')['qa'] ensure_environment(self.settings, 'test', 'master', modules=["murdock:qa"]) self._jens_update(hints={'hostgroups': ['foo']}) self.assertBare('modules/murdock') self.assertBare('hostgroups/steve') self.assertClone('modules/murdock/master') self.assertClone('modules/murdock/qa', pointsto=old_qa) self.assertClone('hostgroups/steve/qa') self.assertClone('hostgroups/steve/master') self.assertEnvironmentNumberOf("test", "modules", 1) self.assertEnvironmentNumberOf("test", "hostgroups", 1) self.assertEnvironmentOverride("test", 'modules/murdock', 'qa') self.assertEnvironmentOverride("test", 'hostgroups/hg_steve', 'master') del_repository(self.settings, 'modules', 'murdock') repositories_deltas = self._jens_update(hints={'hostgroups': ['foo']}) self.assertTrue('murdock' in repositories_deltas['modules']['deleted']) self.assertEnvironmentLinks("test") self.assertEnvironmentNumberOf("test", "modules", 0) self.assertEnvironmentNumberOf("test", "hostgroups", 1) self.assertEnvironmentOverrideDoesntExist("test", 'modules/murdock') self.assertEnvironmentOverride("test", 'hostgroups/hg_steve', 'master')
def _create_new_repositories(settings, new_repositories, partition, definition, inventory, desired): created = [] for repository in new_repositories: logging.info("Cloning and expanding %s/%s..." % (partition, repository)) bare_path = _compose_bare_repository_path(settings, repository, partition) bare_url = definition['repositories'][partition][repository] try: git.clone(bare_path, bare_url, bare=True) except JensGitError, error: logging.error("Unable to clone '%s' (%s). Skipping." % (repository, error)) if os.path.exists(bare_path): shutil.rmtree(bare_path) continue try: refs = git.get_refs(bare_path).keys() except JensGitError, error: logging.error("Unable to get refs of '%s' (%s). Skipping." % (repository, error)) shutil.rmtree(bare_path) logging.debug("Bare repository %s has been removed" % bare_path) continue
def test_environments_with_no_default_dont_grow(self): guy_path = self._create_fake_module('guy', ['qa']) commit_id = get_refs(guy_path + '/.git')['qa'] override = "{0}{1}".format(COMMIT_PREFIX, commit_id) ensure_environment(self.settings, 'test', None, modules=["guy:%s" % override]) self._jens_update() self.assertBare('modules/guy') self.assertClone('modules/guy/.%s' % commit_id) self.assertEnvironmentLinks("test") self.assertEnvironmentNumberOf("test", "modules", 1) self.assertEnvironmentNumberOf("test", "hostgroups", 0) self.assertEnvironmentOverride("test", 'modules/guy', override) # -- The new module shouldn't be present in 'test' env. self._create_fake_module('newguy', ['qa']) repositories_deltas = self._jens_update() self.assertTrue('newguy' in repositories_deltas['modules']['new']) self.assertEnvironmentOverride('production', 'modules/newguy', 'master') self.assertEnvironmentOverride('qa', 'modules/newguy', 'qa') self.assertEnvironmentLinks("test") self.assertEnvironmentNumberOf("test", "modules", 1) self.assertEnvironmentNumberOf("test", "hostgroups", 0) self.assertEnvironmentOverrideDoesntExist("test", 'modules/newguy')
def test_override_to_branch_and_commit_combined(self): self._jens_update() self._create_fake_module('foo', ['qa', 'bar']) self._create_fake_module('bar', ['qa']) murdock_path = self._create_fake_hostgroup('murdock', ['qa']) commit_id = get_refs(murdock_path + '/.git')['qa'] override = "{0}{1}".format(COMMIT_PREFIX, commit_id) ensure_environment(self.settings, 'test', None, hostgroups=["murdock:%s" % override], modules=['foo:bar']) self._jens_update() self.assertBare('modules/foo') self.assertBare('modules/bar') self.assertBare('hostgroups/murdock') for branch in MANDATORY_BRANCHES: self.assertClone('hostgroups/murdock/%s' % branch) self.assertClone('modules/foo/%s' % branch) self.assertClone('modules/bar/%s' % branch) self.assertClone('hostgroups/murdock/.%s' % commit_id, pointsto=commit_id) self.assertClone('modules/foo/bar') self.assertEnvironmentLinks("test") self.assertEnvironmentNumberOf("test", "modules", 1) self.assertEnvironmentNumberOf("test", "hostgroups", 1) self.assertEnvironmentOverride("test", 'hostgroups/hg_murdock', override) self.assertEnvironmentOverride("test", 'modules/foo', 'bar')
def test_repositories_cleanup_includes_commits(self): self._jens_update() self._create_fake_module('electron', ['qa']) murdock_path = self._create_fake_hostgroup('murdock', ['qa']) commit_id = get_refs(murdock_path + '/.git')['qa'] override = "{0}{1}".format(COMMIT_PREFIX, commit_id) ensure_environment(self.settings, 'test', 'master', hostgroups=["murdock:%s" % override]) repositories_deltas = self._jens_update() self.assertTrue('electron' in repositories_deltas['modules']['new']) self.assertTrue('murdock' in repositories_deltas['hostgroups']['new']) self.assertBare('modules/electron') self.assertBare('hostgroups/murdock') self.assertClone('hostgroups/murdock/.%s' % commit_id, pointsto=commit_id) self.assertEnvironmentOverride("test", 'hostgroups/hg_murdock', override) del_repository(self.settings, 'hostgroups', 'murdock') repositories_deltas = self._jens_update() self.assertTrue('murdock' in repositories_deltas['hostgroups']['deleted']) self.assertNotBare('hostgroups/murdock') self.assertNotClone('hostgroups/murdock/master') self.assertNotClone('hostgroups/murdock/qa') self.assertEnvironmentOverrideDoesntExist('test', 'hostgroups/hg_murdock')
def test_override_to_commit(self): murdock_path = self._create_fake_hostgroup('murdock', ['qa']) commit_id = get_refs(murdock_path + '/.git')['qa'] override = "{0}{1}".format(COMMIT_PREFIX, commit_id) ensure_environment(self.settings, 'test', 'master', hostgroups=["murdock:%s" % override]) self._jens_update() self.assertBare('hostgroups/murdock') for branch in MANDATORY_BRANCHES: self.assertClone('hostgroups/murdock/%s' % branch) self.assertClone('hostgroups/murdock/.%s' % commit_id, pointsto=commit_id) self.assertEnvironmentLinks("test") self.assertEnvironmentOverride("test", 'hostgroups/hg_murdock', override)
def _refresh_repository(data): settings = data['settings'] repository = data['repository'] partition = data['partition'] inventory = data['inventory'] inventory_lock = data['inventory_lock'] desired = data['desired'] logging.debug("Expanding %s/%s..." % (partition, repository)) bare_path = _compose_bare_repository_path(settings, repository, partition) try: old_refs = git.get_refs(bare_path) except JensGitError, error: logging.error("Unable to get old refs of '%s' (%s)" % (repository, error)) return
def test_clones_refreshed_if_bare_in_hints(self): self.settings.MODE = "ONDEMAND" murdock_path = self._create_fake_module('murdock', ['qa']) old_qa = get_refs(murdock_path + '/.git')['qa'] old_site_qa = get_refs(self.site_user + '/.git')['qa'] old_hieradata_qa = get_refs(self.hieradata_user + '/.git')['qa'] ensure_environment(self.settings, 'test', 'master', modules=["murdock:qa"]) self._jens_update() self.assertClone('modules/murdock/qa', pointsto=old_qa) self.assertEnvironmentOverride("test", 'modules/murdock', 'qa') new_qa = add_commit_to_branch(self.settings, murdock_path, 'qa') new_site_qa = add_commit_to_branch(self.settings, self.site_user, 'qa') new_hieradata_qa = add_commit_to_branch(self.settings, self.hieradata_user, 'qa') # Test that it actually intersects existing and hints self._jens_update(hints={'modules': ['foo']}) self.assertClone('modules/murdock/qa', pointsto=old_qa) self.assertClone('common/site/qa', pointsto=old_site_qa) self.assertClone('common/hieradata/qa', pointsto=old_hieradata_qa) self.assertEnvironmentOverride("test", 'modules/murdock', 'qa') self._jens_update(hints= {'modules': ['murdock', 'foo'], 'hostgroups': ['foo'], 'common': ['site']}) self.assertClone('modules/murdock/qa', pointsto=new_qa) self.assertClone('common/site/qa', pointsto=new_site_qa) self.assertClone('common/hieradata/qa', pointsto=old_hieradata_qa) self.assertEnvironmentOverride("test", 'modules/murdock', 'qa') self._jens_update(hints= {'common': ['hieradata']}) self.assertClone('common/hieradata/qa', pointsto=new_hieradata_qa)
def test_prefix_is_case_insensitive(self): murdock_path = self._create_fake_hostgroup('murdock', ['qa']) commit_id = get_refs(murdock_path + '/.git')['qa'] prefix = COMMIT_PREFIX prefix = prefix[0:len(prefix)/2] + \ prefix[len(prefix)/2:len(prefix)].upper() override = "{0}{1}".format(prefix, commit_id) ensure_environment(self.settings, 'test', 'master', hostgroups=["murdock:%s" % override]) self._jens_update() self.assertClone('hostgroups/murdock/.%s' % commit_id, pointsto=commit_id) self.assertEnvironmentLinks("test") self.assertEnvironmentOverride("test", 'hostgroups/hg_murdock', override)
def test_override_not_deleted_if_shared(self): ensure_environment(self.settings, 'test', 'master') self._create_fake_hostgroup('murdock', ['qa', 'aijens_etcd']) sonic_path = self._create_fake_module('sonic', ['qa']) commit_id = get_refs(sonic_path + '/.git')['qa'] override = "{0}{1}".format(COMMIT_PREFIX, commit_id) self._jens_update() self.assertNotClone('hostgroups/murdock/aijens_etcd') # ----- Add two environments needing the same override ensure_environment(self.settings, 'test', 'master', modules=['sonic:%s' % override], hostgroups=['murdock:aijens_etcd']) ensure_environment(self.settings, 'test2', 'master', modules=['sonic:%s' % override], hostgroups=['murdock:aijens_etcd']) self._jens_update() self.assertClone('hostgroups/murdock/aijens_etcd') self.assertClone('modules/sonic/.%s' % commit_id, pointsto=commit_id) self.assertEnvironmentOverride("test", 'hostgroups/hg_murdock', 'aijens_etcd') self.assertEnvironmentOverride("test2", 'hostgroups/hg_murdock', 'aijens_etcd') self.assertEnvironmentOverride("test", 'modules/sonic', override) self.assertEnvironmentOverride("test2", 'modules/sonic', override) self.assertEnvironmentNumberOf("test", "modules", 1) self.assertEnvironmentNumberOf("test", "hostgroups", 1) self.assertEnvironmentNumberOf("test2", "modules", 1) self.assertEnvironmentNumberOf("test2", "hostgroups", 1) # ----- Destroy one of them destroy_environment(self.settings, 'test2') self._jens_update() self.assertClone('hostgroups/murdock/aijens_etcd') self.assertClone('modules/sonic/.%s' % commit_id, pointsto=commit_id) self.assertEnvironmentLinks("test") self.assertEnvironmentDoesntExist("test2") self.assertEnvironmentOverride("test", 'hostgroups/hg_murdock', 'aijens_etcd') self.assertEnvironmentOverride("test", 'modules/sonic', override)
def test_clones_not_refreshed_if_constaints_enabled_but_no_partition_declared(self): self.settings.MODE = "ONDEMAND" murdock_path = self._create_fake_module('murdock', ['qa']) old_qa = get_refs(murdock_path + '/.git')['qa'] ensure_environment(self.settings, 'test', 'master', modules=["murdock:qa"]) self._jens_update() self.assertBare('modules/murdock') self.assertClone('modules/murdock/qa', pointsto=old_qa) self.assertEnvironmentOverride("test", 'modules/murdock', 'qa') new_qa = add_commit_to_branch(self.settings, murdock_path, 'qa') self._jens_update(hints={'hostgroups': ['foo']}) self.assertClone('modules/murdock/qa', pointsto=old_qa)
def test_override_to_commit_is_static(self): murdock_path = self._create_fake_hostgroup('murdock', ['qa']) commit_id = get_refs(murdock_path + '/.git')['qa'] override = "{0}{1}".format(COMMIT_PREFIX, commit_id) ensure_environment(self.settings, 'test', 'master', hostgroups=["murdock:%s" % override]) self._jens_update() # Check that the clone points to commit_id self.assertClone('hostgroups/murdock/qa', pointsto=commit_id) self.assertClone('hostgroups/murdock/.%s' % commit_id, pointsto=commit_id) self.assertEnvironmentOverride("test", 'hostgroups/hg_murdock', override) new_qa = add_commit_to_branch(self.settings, murdock_path, 'qa') self._jens_update() self.assertClone('hostgroups/murdock/qa', pointsto=new_qa) self.assertClone('hostgroups/murdock/.%s' % commit_id, pointsto=commit_id) self.assertEnvironmentOverride("test", 'hostgroups/hg_murdock', override)
def test_clones_not_refreshed_if_bare_not_in_hints(self): self.settings.MODE = "ONDEMAND" murdock_path = self._create_fake_hostgroup('murdock', ['qa']) old_qa = get_refs(murdock_path + '/.git')['qa'] ensure_environment(self.settings, 'test', 'master', hostgroups=["murdock:qa"]) self._jens_update() self.assertClone('hostgroups/murdock/qa', pointsto=old_qa) self.assertEnvironmentOverride("test", 'hostgroups/hg_murdock', 'qa') new_qa = add_commit_to_branch(self.settings, murdock_path, 'qa') self._jens_update(hints={'hostgroups': ['other']}) self.assertClone('hostgroups/murdock/qa', pointsto=old_qa) self.assertEnvironmentOverride("test", 'hostgroups/hg_murdock', 'qa') self._jens_update(hints=None) self.assertClone('hostgroups/murdock/qa', pointsto=new_qa) self.assertEnvironmentOverride("test", 'hostgroups/hg_murdock', 'qa')
(repository, error)) if settings.MODE == "ONDEMAND": try: enqueue_hint(settings, partition, repository) except JensMessagingError, error: logging.error(error) return try: # TODO: Found a corner case where git fetch wiped all # all the branches in the bare repository. That led # this get_refs call to fail, and therefore in the next run # the dual get_refs to obtain old_refs failed as well. # What to do? No idea. # Executing git fetch --prune by hand in the bare repo # brought the branches back. new_refs = git.get_refs(bare_path) except JensGitError, error: logging.error("Unable to get new refs of '%s' (%s)" % (repository, error)) return new, moved, deleted = _compare_refs(settings, old_refs, new_refs, inventory[repository], desired.get(repository, [])) _expand_clones(settings, partition, repository, inventory, inventory_lock, new, moved, deleted) def _purge_repositories(settings, deleted_repositories, partition, inventory): for repository in deleted_repositories: logging.info("Deleting %s/%s..." % (partition, repository)) bare_path = _compose_bare_repository_path(settings, repository,
logging.error("Unable to get old refs of '%s' (%s)" % (repository, error)) return try: git.fetch(bare_path, prune=True, bare=True) except JensGitError, error: logging.error("Unable to fetch '%s' from remote (%s)" % (repository, error)) return try: # TODO: Found a corner case where git fetch wiped all # all the branches in the bare repository. That led # this get_refs call to fail, and therefore in the next run # the dual get_refs to obtain old_refs failed as well. # What to do? No idea. # Executing git fetch --prune by hand in the bare repo # brought the branches back. new_refs = git.get_refs(bare_path) except JensGitError, error: logging.error("Unable to get new refs of '%s' (%s)" % (repository, error)) return new, moved, deleted = _compare_refs(settings, old_refs, new_refs, inventory[repository], desired.get(repository, [])) _expand_clones(settings, partition, repository, inventory, inventory_lock, new, moved, deleted) def _purge_repositories(settings, deleted_repositories, partition, inventory): for repository in deleted_repositories: logging.info("Deleting %s/%s..." % (partition, repository)) bare_path = _compose_bare_repository_path(settings, repository, partition) # Pass a copy as it will be used as interation set