예제 #1
0
        public async Task DoesNotDeleteOnDryRun()
        {
            Settings.Nondestructive = true;

            var tags    = _fixture.CreateMany <Tag>(10);
            var project = _fixture.Create <Project>();
            var repo    = _fixture.Create <Repository>();

            Rules.Setup(r => r.Load()).Returns(new RuleSet
            {
                DefaultRule = new Rule
                {
                    Project = new Regex(".*"),
                    Repo    = new Regex(".*"),
                    Tag     = new Regex(".*"),
                    Keep    = 3
                }
            }.EnsureDefaults());

            Harbor.Setup(h => h.GetAllProjects()).ReturnsAsync(new[] { project });
            Harbor.Setup(h => h.GetRepositories(It.IsAny <int>())).ReturnsAsync(new[] { repo });
            Harbor.Setup(h => h.GetTags(It.IsAny <string>())).ReturnsAsync(tags);

            await _sut.Process();

            Harbor.Verify(h => h.DeleteTag(It.IsAny <string>(), It.IsAny <string>()), Times.Never);
        }
예제 #2
0
        public async Task KeepsSpecifiedNumberOfTagsViaDefault()
        {
            var tags = _fixture.CreateMany <Tag>(_ruleSet.DefaultRule.Keep + 5);

            Harbor.Setup(h => h.GetTags(It.IsAny <string>())).ReturnsAsync(tags);

            await _sut.Process();

            Harbor.Verify(h => h.DeleteTag(It.IsAny <string>(), It.IsAny <string>()), Times.Exactly(5));
        }
예제 #3
0
        public async Task ExcludesGloballyIgnoredProject(Project p)
        {
            _ruleSet.IgnoreGlobally.Projects = new[] { p.Name };

            Harbor.Setup(h => h.GetAllProjects()).ReturnsAsync(new[] { p });

            await _sut.Process();

            Serilog.Verify(l => l.Verbose("Skipping project {@project}", p.Name), Times.Once);
            Harbor.Verify(h => h.GetRepositories(It.IsAny <int>()), Times.Never);
        }
예제 #4
0
        public async Task ExcludesGloballyIgnoredRepos(Project p, Repository r)
        {
            _ruleSet.IgnoreGlobally.Repos = new[] { r.Name };

            Harbor.Setup(h => h.GetAllProjects()).ReturnsAsync(new[] { p });
            Harbor.Setup(h => h.GetRepositories(It.IsAny <int>())).ReturnsAsync(new[] { r });

            await _sut.Process();

            Serilog.Verify(l => l.Verbose("Skipping repository {@repository}", r), Times.Once);
            Harbor.Verify(h => h.GetTags(It.IsAny <string>()), Times.Never);
        }
예제 #5
0
        public async Task RuleExcludesTag(Tag t)
        {
            _fixture.Inject(new Regex(".*"));
            _ruleSet.Rules.Add(_fixture.Build <Rule>().WithAutoProperties().With(r => r.Ignore, new[] { t.Name }).Create());

            Harbor.Setup(h => h.GetTags(It.IsAny <string>())).ReturnsAsync(new[] { t });

            await _sut.Process();

            Serilog.Verify(l => l.Information("Tag {repo}:{name} skipped because it was found in an ignore list that applies to {repo}", t.Repository, t.Name, _repository.Name), Times.Once);
            Harbor.Verify(h => h.DeleteTag(It.IsAny <string>(), It.IsAny <string>()), Times.Never);
        }
예제 #6
0
        public async Task ExcludesGloballyIgnoredTags(Project p, Repository r, Tag t)
        {
            _ruleSet.IgnoreGlobally.Tags = new[] { t.Name };

            Harbor.Setup(h => h.GetAllProjects()).ReturnsAsync(new[] { p });
            Harbor.Setup(h => h.GetRepositories(It.IsAny <int>())).ReturnsAsync(new[] { r });
            Harbor.Setup(h => h.GetTags(It.IsAny <string>())).ReturnsAsync(new[] { t });

            await _sut.Process();

            Serilog.Verify(l => l.Information("Tag {repo}:{name} skipped due to global ignore rules", t.Repository, t.Name), Times.Once);
            Harbor.Verify(h => h.DeleteTag(It.IsAny <string>(), It.IsAny <string>()), Times.Never);
        }
예제 #7
0
        public async Task UnmatchedTagsAreKeptImplicitly()
        {
            _ruleSet.DefaultRule.Tag = new Regex("^$");

            var tags = _fixture.CreateMany <Tag>(10);

            Harbor.Setup(h => h.GetTags(It.IsAny <string>())).ReturnsAsync(tags);

            await _sut.Process();

            Harbor.Verify(h => h.DeleteTag(It.IsAny <string>(), It.IsAny <string>()), Times.Never);
            Serilog.Verify(l => l.Warning("The default rule did not match all remaining tags for {@repo}. {count} remaining tags will be kept", _repository, 10), Times.Once);
        }
예제 #8
0
        public async Task KeepsTagIgnoredByDefaultRule()
        {
            _ruleSet.DefaultRule.Ignore = new[] { "latest" };

            var tags = _fixture.CreateMany <Tag>(10).Append(_fixture.Build <Tag>().WithAutoProperties().With(t => t.Name, "latest").Create());

            Harbor.Setup(h => h.GetTags(It.IsAny <string>())).ReturnsAsync(tags);

            await _sut.Process();

            Harbor.Verify(h => h.DeleteTag(It.IsAny <string>(), It.IsAny <string>()), Times.Exactly(5));
            Harbor.Verify(h => h.DeleteTag(It.IsAny <string>(), "latest"), Times.Never);
        }
예제 #9
0
        public async Task KeptByRule()
        {
            _fixture.Inject(new Regex(".*"));
            _ruleSet.Rules.Add(_fixture.Build <Rule>().WithAutoProperties().With(r => r.Keep, 5).Without(r => r.Ignore).Create());
            _ruleSet.DefaultRule.Tag = new Regex("^$");

            var tags = _fixture.CreateMany <Tag>(10);

            Harbor.Setup(h => h.GetTags(It.IsAny <string>())).ReturnsAsync(tags);

            await _sut.Process();

            Harbor.Verify(h => h.DeleteTag(It.IsAny <string>(), It.IsAny <string>()), Times.Exactly(5));
        }
예제 #10
0
        public async Task SkipsHarbor14CorruptTags()
        {
            var tag = _fixture.Build <Tag>()
                      .WithAutoProperties()
                      .Without(t => t.Digest)
                      .Create();

            Harbor.Setup(h => h.GetTags(It.IsAny <string>())).ReturnsAsync(new[] { tag });

            await _sut.Process();

            Serilog.Verify(l => l.Warning("Tag {repo}:{name} does not have a digest and was likely corrupted during a delete operation. This tag will be skipped. See https://github.com/vmware/harbor/issues/4214 for details", tag.Repository, tag.Name), Times.Once);
            Harbor.Verify(h => h.DeleteTag(It.IsAny <string>(), It.IsAny <string>()), Times.Never);
        }
예제 #11
0
        public async Task TagsThatHaveTheSameDigestAreKept()
        {
            _ruleSet.IgnoreGlobally.Tags = new[] { "latest" };

            var tags = _fixture.CreateMany <Tag>(10).ToList();

            var digest = _fixture.Create <string>();
            var latest = _fixture.Build <Tag>().WithAutoProperties().With(t => t.Digest, digest).With(t => t.Name, "latest").Create();
            var dup    = _fixture.Build <Tag>().WithAutoProperties().With(t => t.Digest, digest).With(t => t.Name, "dup").With(t => t.CreatedAt, DateTime.MinValue).Create();

            Harbor.Setup(h => h.GetTags(It.IsAny <string>())).ReturnsAsync(tags.Union(new[] { latest, dup }));

            await _sut.Process();

            Harbor.Verify(h => h.DeleteTag(It.IsAny <string>(), It.IsAny <string>()), Times.Exactly(5));
            Harbor.Verify(h => h.DeleteTag(latest.Repository, latest.Name), Times.Never);
            Harbor.Verify(h => h.DeleteTag(dup.Repository, dup.Name), Times.Never);
        }
예제 #12
0
        public async Task DefaultRuleProcessesRemainingTags()
        {
            _fixture.Inject(new Regex("^((?!foo).)*$"));
            _ruleSet.Rules.Add(_fixture.Build <Rule>().WithAutoProperties().With(r => r.Keep, 5).Without(r => r.Ignore).Create());
            _ruleSet.DefaultRule.Tag  = new Regex("^foo.*$");
            _ruleSet.DefaultRule.Keep = 3;

            var tags = _fixture.CreateMany <Tag>(10).ToList();

            foreach (var tag in _fixture.CreateMany <Tag>(10))
            {
                tag.Name = $"foo{tag.Name}";
                tags.Add(tag);
            }

            Harbor.Setup(h => h.GetTags(It.IsAny <string>())).ReturnsAsync(tags);

            await _sut.Process();

            Harbor.Verify(h => h.DeleteTag(It.IsAny <string>(), It.IsAny <string>()), Times.Exactly(12));
        }