TW-57690: now when we load mirrors mapping file we ignore non existing directories...
[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.TempFiles;
20 import jetbrains.buildServer.agent.AgentRunningBuild;
21 import jetbrains.buildServer.agent.DirectoryCleanersProviderContext;
22 import jetbrains.buildServer.agent.DirectoryCleanersRegistry;
23 import jetbrains.buildServer.buildTriggers.vcs.git.MirrorManager;
24 import jetbrains.buildServer.buildTriggers.vcs.git.agent.AgentMirrorCleaner;
25 import jetbrains.buildServer.util.Dates;
26 import jetbrains.buildServer.vcs.CheckoutRules;
27 import jetbrains.buildServer.vcs.VcsRootEntry;
28 import org.jetbrains.annotations.NotNull;
29 import org.jmock.Expectations;
30 import org.jmock.Mockery;
31 import org.testng.annotations.BeforeMethod;
32 import org.testng.annotations.Test;
33
34 import java.io.File;
35 import java.io.IOException;
36 import java.util.ArrayList;
37 import java.util.Date;
38 import java.util.List;
39
40 import static java.util.Arrays.asList;
41 import static jetbrains.buildServer.buildTriggers.vcs.git.tests.VcsRootBuilder.vcsRoot;
42 import static jetbrains.buildServer.util.Util.map;
43
44 @Test
45 public class AgentMirrorCleanerTest {
46   private Mockery myContext = new Mockery();
47   private MirrorManager myMirrorManager;
48   private AgentMirrorCleaner myAgentMirrorCleaner;
49
50   @BeforeMethod
51   public void setUp() {
52     myMirrorManager = myContext.mock(MirrorManager.class);
53     myAgentMirrorCleaner = new AgentMirrorCleaner(myMirrorManager);
54   }
55
56
57   public void should_register_mirrors_not_used_in_current_build() throws IOException {
58     TempFiles tmpFiles = new TempFiles();
59     try {
60       final DirectoryCleanersRegistry registry = myContext.mock(DirectoryCleanersRegistry.class);
61       final File r3mirror = tmpFiles.createTempDir();
62       final File r4mirror = tmpFiles.createTempDir();
63       final Date r3lastAccess = Dates.makeDate(2012, 10, 29);
64       final Date r4lastAccess = Dates.makeDate(2012, 10, 27);
65       List<String> repositoriesInBuild = asList("git://some.org/r1", "git://some.org/r2");
66       myContext.checking(new Expectations() {{
67         one(myMirrorManager).getMappings(); will(returnValue(map("git://some.org/r1", tmpFiles.createTempDir(),
68                                                                  "git://some.org/r2", tmpFiles.createTempDir(),
69                                                                  "git://some.org/r3", r3mirror,
70                                                                  "git://some.org/r4", r4mirror)));
71         one(myMirrorManager).getLastUsedTime(r3mirror); will(returnValue(r3lastAccess.getTime()));
72         one(myMirrorManager).getLastUsedTime(r4mirror); will(returnValue(r4lastAccess.getTime()));
73
74         one(registry).addCleaner(r3mirror, r3lastAccess);
75         one(registry).addCleaner(r4mirror, r4lastAccess);
76       }});
77       myAgentMirrorCleaner.registerDirectoryCleaners(createCleanerContext(repositoriesInBuild), registry);
78     } finally {
79       tmpFiles.cleanup();
80     }
81   }
82
83
84   private DirectoryCleanersProviderContext createCleanerContext(@NotNull final List<String> repositoriesInBuild) {
85     final DirectoryCleanersProviderContext context = myContext.mock(DirectoryCleanersProviderContext.class);
86     final AgentRunningBuild build = myContext.mock(AgentRunningBuild.class);
87     myContext.checking(new Expectations() {{
88       allowing(context).getRunningBuild(); will(returnValue(build));
89       allowing(build).getVcsRootEntries(); will(returnValue(createVcsRootEntries(repositoriesInBuild)));
90     }});
91     return context;
92   }
93
94
95   private List<VcsRootEntry> createVcsRootEntries(@NotNull List<String> repositories) {
96     List<VcsRootEntry> entries = new ArrayList<VcsRootEntry>();
97     for (String r : repositories) {
98       entries.add(new VcsRootEntry(vcsRoot().withFetchUrl(r).build(), CheckoutRules.DEFAULT));
99     }
100     return entries;
101   }
102
103 }