ac5a4f590c838dc91dc7c10a1ef3df4e4e43826e
[teamcity/git-plugin.git] / git-tests / src / jetbrains / buildServer / buildTriggers / vcs / git / tests / AgentMirrorCleanerTest.java
1 /*
2  * Copyright 2000-2018 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package jetbrains.buildServer.buildTriggers.vcs.git.tests;
18
19 import jetbrains.buildServer.agent.AgentRunningBuild;
20 import jetbrains.buildServer.agent.DirectoryCleanersProviderContext;
21 import jetbrains.buildServer.agent.DirectoryCleanersRegistry;
22 import jetbrains.buildServer.buildTriggers.vcs.git.MirrorManager;
23 import jetbrains.buildServer.buildTriggers.vcs.git.agent.AgentMirrorCleaner;
24 import jetbrains.buildServer.util.Dates;
25 import jetbrains.buildServer.vcs.CheckoutRules;
26 import jetbrains.buildServer.vcs.VcsRootEntry;
27 import org.jetbrains.annotations.NotNull;
28 import org.jmock.Expectations;
29 import org.jmock.Mockery;
30 import org.testng.annotations.BeforeMethod;
31 import org.testng.annotations.Test;
32
33 import java.io.File;
34 import java.util.ArrayList;
35 import java.util.Date;
36 import java.util.List;
37
38 import static java.util.Arrays.asList;
39 import static jetbrains.buildServer.buildTriggers.vcs.git.tests.VcsRootBuilder.vcsRoot;
40 import static jetbrains.buildServer.util.Util.map;
41
42 @Test
43 public class AgentMirrorCleanerTest {
44   private Mockery myContext = new Mockery();
45   private MirrorManager myMirrorManager;
46   private AgentMirrorCleaner myAgentMirrorCleaner;
47
48   @BeforeMethod
49   public void setUp() {
50     myMirrorManager = myContext.mock(MirrorManager.class);
51     myAgentMirrorCleaner = new AgentMirrorCleaner(myMirrorManager);
52   }
53
54
55   public void should_register_mirrors_not_used_in_current_build() {
56     final DirectoryCleanersRegistry registry = myContext.mock(DirectoryCleanersRegistry.class);
57     final File r3mirror = new File("r3");
58     final File r4mirror = new File("r4");
59     final Date r3lastAccess = Dates.makeDate(2012, 10, 29);
60     final Date r4lastAccess = Dates.makeDate(2012, 10, 27);
61     List<String> repositoriesInBuild = asList("git://some.org/r1", "git://some.org/r2");
62     myContext.checking(new Expectations() {{
63       one(myMirrorManager).getMappings(); will(returnValue(map("git://some.org/r1", new File("r1"),
64                                                                "git://some.org/r2", new File("r2"),
65                                                                "git://some.org/r3", r3mirror,
66                                                                "git://some.org/r4", r4mirror)));
67       one(myMirrorManager).getLastUsedTime(r3mirror); will(returnValue(r3lastAccess.getTime()));
68       one(myMirrorManager).getLastUsedTime(r4mirror); will(returnValue(r4lastAccess.getTime()));
69
70       one(registry).addCleaner(r3mirror, r3lastAccess);
71       one(registry).addCleaner(r4mirror, r4lastAccess);
72     }});
73     myAgentMirrorCleaner.registerDirectoryCleaners(createCleanerContext(repositoriesInBuild), registry);
74   }
75
76
77   private DirectoryCleanersProviderContext createCleanerContext(@NotNull final List<String> repositoriesInBuild) {
78     final DirectoryCleanersProviderContext context = myContext.mock(DirectoryCleanersProviderContext.class);
79     final AgentRunningBuild build = myContext.mock(AgentRunningBuild.class);
80     myContext.checking(new Expectations() {{
81       allowing(context).getRunningBuild(); will(returnValue(build));
82       allowing(build).getVcsRootEntries(); will(returnValue(createVcsRootEntries(repositoriesInBuild)));
83     }});
84     return context;
85   }
86
87
88   private List<VcsRootEntry> createVcsRootEntries(@NotNull List<String> repositories) {
89     List<VcsRootEntry> entries = new ArrayList<VcsRootEntry>();
90     for (String r : repositories) {
91       entries.add(new VcsRootEntry(vcsRoot().withFetchUrl(r).build(), CheckoutRules.DEFAULT));
92     }
93     return entries;
94   }
95
96 }