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 / MirrorManagerTest.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.buildTriggers.vcs.git.*;
21 import jetbrains.buildServer.serverSide.ServerPaths;
22 import jetbrains.buildServer.util.FileUtil;
23 import org.eclipse.jgit.transport.URIish;
24 import org.jetbrains.annotations.NotNull;
25 import org.testng.annotations.AfterMethod;
26 import org.testng.annotations.BeforeMethod;
27 import org.testng.annotations.Test;
28
29 import java.io.File;
30 import java.util.Arrays;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
34
35 import static jetbrains.buildServer.buildTriggers.vcs.git.GitServerUtil.getRepository;
36 import static org.testng.AssertJUnit.*;
37
38 /**
39  * @author dmitry.neverov
40  */
41 @Test
42 public class MirrorManagerTest {
43
44   private TempFiles myTempFiles = new TempFiles();
45   private ServerPluginConfig myConfig;
46
47
48   @BeforeMethod
49   public void setUp() throws Exception {
50     ServerPaths paths = new ServerPaths(myTempFiles.createTempDir().getAbsolutePath());
51     myConfig = new PluginConfigBuilder(paths).build();
52   }
53
54
55   @AfterMethod
56   public void tearDown() {
57     myTempFiles.cleanup();
58   }
59
60
61   public void should_handle_clashing_urls() {
62     String clashingUrl1 = "git://some.org/first-clashing-url.git";
63     String clashingUrl2 = "git://some.org/second-clashing-url.git";
64     HashCalculator clashingHash = new ClashingHashCalculator(Arrays.asList(clashingUrl1, clashingUrl2));
65     MirrorManager mirrorManager = new MirrorManagerImpl(myConfig, clashingHash);
66     File dir1 = mirrorManager.getMirrorDir(clashingUrl1);
67     File dir2 = mirrorManager.getMirrorDir(clashingUrl2);
68     assertFalse(dir1.equals(dir2));
69   }
70
71
72   public void should_restore_mapping_from_existing_repositories() throws Exception {
73     //There are 3 existing repositories and no map file (this is the first start after
74     //mirror manager is introduced). After the start map file should be created
75     //and mirrorManager should respect old repositories locations.
76
77     File baseMirrorsDir = myConfig.getCachesDir();
78     Map<String, String> existingRepositories = new HashMap<String, String>() {{
79       put("git://some.org/repository1.git", "git-11111111.git");
80       put("git://some.org/repository2.git", "git-22222222.git");
81       put("git://some.org/repository3.git", "git-33333333.git");
82     }};
83
84     File map = new File(baseMirrorsDir, "map");
85     createRepositories(baseMirrorsDir, existingRepositories);
86     assertFalse(map.exists());
87
88     MirrorManager mirrorManager = new MirrorManagerImpl(myConfig, new HashCalculatorImpl());
89     assertTrue(map.exists());
90     for (Map.Entry<String, String> entry : existingRepositories.entrySet()) {
91       String url = entry.getKey();
92       String dir = entry.getValue();
93       assertEquals(new File(baseMirrorsDir, dir), mirrorManager.getMirrorDir(url));
94     }
95   }
96
97   public void should_ignore_non_existing_directories() throws Exception {
98     File baseMirrorsDir = myConfig.getCachesDir();
99     File map = new File(baseMirrorsDir, "map");
100
101     FileUtil.writeFileAndReportErrors(map, "git://some.org/repository1.git = git-11111111.git\n" +
102                                            "git://some.org/repository2.git = git-22222222.git");
103
104     getRepository(new File(baseMirrorsDir, "git-22222222.git"), new URIish("git://some.org/repository2.git"));
105
106     MirrorManager mirrorManager = new MirrorManagerImpl(myConfig, new HashCalculatorImpl());
107     assertEquals(1, mirrorManager.getMappings().size());
108     assertTrue(mirrorManager.getMappings().containsKey("git://some.org/repository2.git"));
109
110     String mapping = FileUtil.readText(map);
111     assertFalse(mapping.contains("git-11111111.git"));
112   }
113
114
115   public void should_give_different_dirs_for_same_url_if_dir_was_invalidated() {
116     MirrorManager mirrorManager = new MirrorManagerImpl(myConfig, new HashCalculatorImpl());
117     String url = "git://some.org/repository.git";
118     File dir1 = mirrorManager.getMirrorDir(url);
119     mirrorManager.invalidate(dir1);
120     File dir2 = mirrorManager.getMirrorDir(url);
121     assertFalse(dir1.equals(dir2));
122   }
123
124
125   public void should_remember_invalidated_dirs_after_restart() {
126     String url1 = "git://some.org/repository1.git";
127     String url2 = "git://some.org/repository2.git";
128     HashCalculator clashingHash = new ClashingHashCalculator(Arrays.asList(url1, url2));
129     MirrorManager mirrorManager = new MirrorManagerImpl(myConfig, clashingHash);
130     File dir1 = mirrorManager.getMirrorDir(url1);
131     mirrorManager.invalidate(dir1);
132     mirrorManager = new MirrorManagerImpl(myConfig, clashingHash); //restart
133     File dir2 = mirrorManager.getMirrorDir(url2);
134     assertFalse(dir1.equals(dir2));
135   }
136
137
138   private void createRepositories(File baseDir, Map<String, String> url2dir) throws Exception {
139     for (Map.Entry<String, String> entry : url2dir.entrySet()) {
140       String url = entry.getKey();
141       String dir = entry.getValue();
142       getRepository(new File(baseDir, dir), new URIish(url));
143     }
144   }
145
146
147   private static class ClashingHashCalculator implements HashCalculator {
148     private final List<String> myValuesWhenShouldClash;
149
150     ClashingHashCalculator(List<String> valuesWhenShouldClash) {
151       myValuesWhenShouldClash = valuesWhenShouldClash;
152     }
153
154     public long getHash(@NotNull String value) {
155       if (myValuesWhenShouldClash.contains(value))
156         return 42;
157       else
158         return value.hashCode();
159     }
160   }
161 }