-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathBomContentTest.java
More file actions
272 lines (241 loc) · 11 KB
/
BomContentTest.java
File metadata and controls
272 lines (241 loc) · 11 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
* Copyright 2019 Google LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud;
import com.google.cloud.tools.opensource.classpath.ClassPathBuilder;
import com.google.cloud.tools.opensource.classpath.ClassPathEntry;
import com.google.cloud.tools.opensource.classpath.ClassPathResult;
import com.google.cloud.tools.opensource.classpath.DependencyMediation;
import com.google.cloud.tools.opensource.dependencies.Artifacts;
import com.google.cloud.tools.opensource.dependencies.Bom;
import com.google.cloud.tools.opensource.dependencies.DependencyPath;
import com.google.common.base.Joiner;
import com.google.common.base.Verify;
import com.google.common.collect.ImmutableList;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.graph.Dependency;
import org.eclipse.aether.util.version.GenericVersionScheme;
import org.eclipse.aether.version.InvalidVersionSpecificationException;
import org.eclipse.aether.version.Version;
import org.eclipse.aether.version.VersionScheme;
import org.junit.Assert;
import org.junit.Test;
/**
* Checks the content of the BOMs in this repository. When some artifacts are not available in Maven
* Central yet, use "-DdisableMavenCentralCheck=true" system property when running this test.
*/
public class BomContentTest {
private static VersionScheme versionScheme = new GenericVersionScheme();
// List of Maven dependency scopes that are visible to library users. For example "provided" scope
// dependencies do not appear in users' class path.
private static final ImmutableList<String> dependencyScopesVisibleToUsers =
ImmutableList.of("compile", "runtime");
@Test
public void testLtsBom() throws Exception {
Path bomPath = Paths.get("..", "cloud-lts-bom", "pom.xml").toAbsolutePath();
checkBom(bomPath);
}
private void checkBom(Path bomPath) throws Exception {
Bom bom = Bom.readBom(bomPath);
// Sometimes the artifacts are not yet available in Maven Central and only available in local
// Maven repository. Use this property in that case.
boolean disableMavenCentralCheck =
"true".equals(System.getProperty("disableMavenCentralCheck"));
List<Artifact> artifacts = bom.getManagedDependencies();
if (!disableMavenCentralCheck) {
for (Artifact artifact : artifacts) {
assertReachable(buildMavenCentralUrl(artifact));
}
}
// Temporarily ignore due to inability to process exclusion statements
assertNoDowngradeRule(bom);
assertUniqueClasses(artifacts);
assertBomIsImported(bom);
}
private static String buildMavenCentralUrl(Artifact artifact) {
return "https://repo1.maven.org/maven2/"
+ artifact.getGroupId().replace('.', '/')
+ "/"
+ artifact.getArtifactId()
+ "/"
+ artifact.getVersion()
+ "/";
}
/**
* Asserts that the BOM only provides JARs which contains unique class names to the classpath.
*/
private static void assertUniqueClasses(List<Artifact> allArtifacts)
throws InvalidVersionSpecificationException, IOException {
StringBuilder errorMessageBuilder = new StringBuilder();
ClassPathBuilder classPathBuilder = new ClassPathBuilder();
ClassPathResult result =
classPathBuilder.resolve(allArtifacts, false, DependencyMediation.MAVEN);
// A Map of every class name to its artifact ID.
HashMap<String, String> fullClasspathMap = new HashMap<>();
for (ClassPathEntry classPathEntry : result.getClassPath()) {
Artifact currentArtifact = classPathEntry.getArtifact();
String artifactId = currentArtifact.getArtifactId();
String groupId = currentArtifact.getGroupId();
if (!groupId.contains("google")
|| groupId.contains("com.google.android")
|| groupId.contains("com.google.cloud.bigtable")
|| artifactId.startsWith("proto-")
|| artifactId.equals("protobuf-javalite")
|| artifactId.startsWith("appengine-")) {
// Skip libraries that produce false positives.
// See: https://github.com/GoogleCloudPlatform/cloud-opensource-java/issues/2226
continue;
}
String artifactCoordinates = Artifacts.toCoordinates(currentArtifact);
for (String className : classPathEntry.getFileNames()) {
if (className.contains("javax.annotation")
|| className.contains("$")
|| className.equals("com.google.cloud.location.LocationsGrpc")
|| className.endsWith("package-info")
|| className.endsWith("module-info")) {
// Ignore annotations, nested classes, and package-info files.
// Ignore module-info files.
// Ignore LocationsGrpc classes which are duplicated in generated grpc libraries.
continue;
}
String previousArtifact = fullClasspathMap.get(className);
if (previousArtifact != null) {
String msg = String.format(
"Duplicate class %s found in classpath. Found in artifacts %s and %s.\n",
className,
previousArtifact,
artifactCoordinates);
errorMessageBuilder.append(msg);
} else {
fullClasspathMap.put(className, artifactCoordinates);
}
}
}
String error = errorMessageBuilder.toString();
Assert.assertTrue(
"Failing test due to duplicate classes found on classpath:\n" + error, error.isEmpty());
}
private static void assertReachable(String url) throws IOException {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestMethod("HEAD");
try {
Assert.assertEquals(
"Could not reach " + url, HttpURLConnection.HTTP_OK, connection.getResponseCode());
} catch (IOException ex) {
Assert.fail("Could not reach " + url + "\n" + ex.getMessage());
}
}
/**
* Asserts that the members of the {@code bom} satisfy the no-downgrade rule. This rule means that
* the members have the highest versions among the dependencies of them. If there's a violation,
* users of the BOM would see our BOM downgrading certain dependencies. Downgrading a dependency
* is bad practice in general because newer versions have more features (classes and methods).
*
* <p>For example, if google-http-client 1.40.1 is in the BOM, then no other libraries in the BOM
* depend on the higher version of the google-http-client.
*
* @param bom the BOM to assert with this no-downgrade rule.
*/
private static void assertNoDowngradeRule(Bom bom) throws InvalidVersionSpecificationException {
List<String> violations = new ArrayList<>();
Map<String, Artifact> bomArtifacts = new HashMap<>();
for (Artifact artifact : bom.getManagedDependencies()) {
bomArtifacts.put(Artifacts.makeKey(artifact), artifact);
}
for (Artifact artifact : bom.getManagedDependencies()) {
violations.addAll(findNoDowngradeViolation(bomArtifacts, artifact));
}
String violationMessage = Joiner.on("\n").join(violations);
Assert.assertTrue(violationMessage, violations.isEmpty());
}
/**
* Returns messages describing the violation of the no-downgrade rule by {@code artifact} against
* the BOM containing {@code bomArtifacts}. An empty list if there is no violations.
*/
private static ImmutableList<String> findNoDowngradeViolation(
Map<String, Artifact> bomArtifacts, Artifact artifact)
throws InvalidVersionSpecificationException {
ImmutableList.Builder<String> violations = ImmutableList.builder();
ClassPathBuilder classPathBuilder = new ClassPathBuilder();
ClassPathResult result =
classPathBuilder.resolve(ImmutableList.of(artifact), false, DependencyMediation.MAVEN);
for (ClassPathEntry entry : result.getClassPath()) {
Artifact transitiveDependency = entry.getArtifact();
String key = Artifacts.makeKey(transitiveDependency);
Artifact bomArtifact = bomArtifacts.get(key);
if (bomArtifact == null) {
// transitiveDependency is not part of the BOM
continue;
}
Version versionInBom = versionScheme.parseVersion(bomArtifact.getVersion());
Version versionInTransitiveDependency =
versionScheme.parseVersion(transitiveDependency.getVersion());
if (versionInTransitiveDependency.compareTo(versionInBom) <= 0) {
// When versionInTransitiveDependency is less than or equal to versionInBom, it satisfies
// the no-downgrade rule.
continue;
}
// Filter by scopes that are invisible to library users
ImmutableList<DependencyPath> dependencyPaths = result.getDependencyPaths(entry);
Verify.verify(
!dependencyPaths.isEmpty(),
"The class path entry should have at least one dependency path from the root");
boolean dependencyVisibleToUsers = false;
for (DependencyPath dependencyPath : dependencyPaths) {
int length = dependencyPath.size();
// As the root element is an empty node, the last element is at "length - 2".
Dependency dependency = dependencyPath.getDependency(length - 2);
if (dependencyScopesVisibleToUsers.contains(dependency.getScope())) {
dependencyVisibleToUsers = true;
break;
}
}
if (!dependencyVisibleToUsers) {
// For provided-scope dependencies, we don't have to worry about them because they don't
// appear in library users' class path. For example, appengine-api-1.0-sdk are used via
// provided scope.
continue;
}
// A violation of the no-downgrade rule is found.
violations.add(
artifact
+ " has a transitive dependency "
+ transitiveDependency
+ ". This is higher version than "
+ bomArtifact
+ " in the BOM. Example dependency path: "
+ dependencyPaths.get(0));
}
return violations.build();
}
private void assertBomIsImported(Bom bom) {
// BOMs must be declared as "import" type. Otherwise, the BOM users would see
// "google-cloud-XXX-bom" as an artifact declared in the BOM, not the content of it.
for (Artifact artifact : bom.getManagedDependencies()) {
String artifactId = artifact.getArtifactId();
Assert.assertFalse(
artifactId + " must be declared with import type", artifactId.endsWith("-bom"));
}
}
}