/
TestCommon.java
158 lines (139 loc) · 6.27 KB
/
TestCommon.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* Copyright (c) 2018, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or
* https://opensource.org/licenses/BSD-3-Clause
*/
package com.salesforce.dockerfileimageupdate.itest.tests;
import com.salesforce.dockerfileimageupdate.utils.GitHubUtil;
import org.kohsuke.github.GHOrganization;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;
import org.kohsuke.github.PagedIterable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* Created by afalko on 10/19/17.
*/
public class TestCommon {
private static final Logger log = LoggerFactory.getLogger(TestCommon.class);
public static final List<String> ORGS = Arrays.asList(
"dockerfile-image-update-itest", "dockerfile-image-update-itest-2", "dockerfile-image-update-itest-3");
public static void initializeRepos(GHOrganization org, List<String> repos, String image,
List<GHRepository> createdRepos, GitHubUtil gitHubUtil) throws Exception {
for (String repoName : repos) {
GHRepository repo = org.createRepository(repoName)
.description("Delete if this exists. If it exists, then an integration test crashed somewhere.")
.private_(false)
.create();
// Ensure that repository exists
for (int attempts = 0; attempts < 5; attempts++) {
try {
repo = gitHubUtil.getRepo(repo.getFullName());
break;
} catch (Exception e) {
log.info("Waiting for {} to be created", repo.getFullName());
Thread.sleep(TimeUnit.SECONDS.toMillis(1));
}
}
repo.createContent("FROM " + image + ":test", "Integration Testing", "Dockerfile");
createdRepos.add(repo);
log.info("Initializing {}/{}", org.getLogin(), repoName);
gitHubUtil.tryRetrievingContent(repo, "Dockerfile", repo.getDefaultBranch());
}
}
public static void printCollectedExceptionsAndFail(List<Exception> exceptions, boolean exitWithFail) {
for (int i = 0; i < exceptions.size(); i++) {
log.error("Hit exception {}/{} while cleaning up.", i+1, exceptions.size());
log.error("", exceptions.get(i));
}
if (exitWithFail && exceptions.size() > 0) {
throw new RuntimeException(exceptions.get(0));
}
}
public static void cleanAllRepos(List<GHRepository> createdRepos, boolean exitWithFail) throws Exception {
List<Exception> exceptions = new ArrayList<>();
exceptions.addAll(checkAndDelete(createdRepos));
TestCommon.printCollectedExceptionsAndFail(exceptions, false);
}
private static Exception checkAndDelete(GHRepository repo) {
log.info("deleting {}", repo.getFullName());
try {
repo.delete();
} catch (Exception e) {
return e;
}
return null;
}
private static List<Exception> checkAndDelete(List<GHRepository> repos) throws IOException {
List<Exception> exceptions = new ArrayList<>();
for (GHRepository repo : repos) {
PagedIterable<GHRepository> forks;
try {
forks = repo.listForks();
for (GHRepository fork : forks) {
Exception forkDeleteException = checkAndDelete(fork);
if (forkDeleteException != null) {
exceptions.add(forkDeleteException);
}
}
} catch (Exception getForksException) {
log.error("Could not get forks for repo: ", repo.getFullName());
exceptions.add(getForksException);
}
Exception repoDeleteException = checkAndDelete(repo);
if (repoDeleteException != null) {
exceptions.add(repoDeleteException);
}
}
return exceptions;
}
public static void cleanBefore(List<String> repos, List<String> duplicatesCreatedByGithub,
String storeName, GitHub github) throws Exception {
checkAndDeleteBefore(repos, storeName, github);
checkAndDeleteBefore(duplicatesCreatedByGithub, storeName, github);
}
private static void checkAndDeleteBefore(List<String> repoNames, String storeName, GitHub github) throws IOException, InterruptedException {
String user = github.getMyself().getLogin();
for (String repoName : repoNames) {
for (String org : ORGS) {
checkAndDeleteBefore(Paths.get(user, repoName).toString(), github);
checkAndDeleteBefore(Paths.get(org, repoName).toString(), github);
}
}
checkAndDeleteBefore(Paths.get(user, storeName).toString(), github);
}
public static void checkAndDeleteBefore(String repoName, GitHub github) throws IOException, InterruptedException {
GHRepository repo;
try {
repo = github.getRepository(repoName);
} catch (FileNotFoundException fileNotFoundException) {
return;
}
repo.delete();
// Make sure the repo is actually deleted
for (int attempts = 0; attempts < 60; attempts++) {
try {
github.getRepository(repoName);
} catch (FileNotFoundException fileNotFoundException) {
return;
}
log.info("Waiting for {} to fully delete...", repoName);
Thread.sleep(TimeUnit.SECONDS.toMillis(1));
}
throw new FileNotFoundException(String.format("Unable to pre-delete repository %s during pre-test cleanup", repoName));
}
public static void addVersionStoreRepo(GitHub github, List<GHRepository> createdRepos, String storeName) throws IOException {
String login = github.getMyself().getLogin();
GHRepository storeRepo = github.getRepository(Paths.get(login, storeName).toString());
createdRepos.add(storeRepo);
}
}