@@ -61,31 +61,57 @@ public class LibraryCachingConfigurationTest {
61
61
private static int NO_REFRESH_TIME_MINUTES = 0 ;
62
62
63
63
private static String NULL_EXCLUDED_VERSION = null ;
64
+ private static String NULL_INCLUDED_VERSION = null ;
65
+
64
66
private static String ONE_EXCLUDED_VERSION = "branch-1" ;
65
67
68
+ private static String ONE_INCLUDED_VERSION = "branch-1i" ;
69
+
70
+
66
71
private static String MULTIPLE_EXCLUDED_VERSIONS_1 = "main" ;
72
+
73
+ private static String MULTIPLE_INCLUDED_VERSIONS_1 = "master" ;
74
+
67
75
private static String MULTIPLE_EXCLUDED_VERSIONS_2 = "branch-2" ;
76
+
77
+ private static String MULTIPLE_INCLUDED_VERSIONS_2 = "branch-2i" ;
78
+
68
79
private static String MULTIPLE_EXCLUDED_VERSIONS_3 = "branch-3" ;
80
+ private static String MULTIPLE_INCLUDED_VERSIONS_3 = "branch-3i" ;
81
+
69
82
70
83
private static String SUBSTRING_EXCLUDED_VERSIONS_1 = "feature/test-substring-exclude" ;
84
+
85
+ private static String SUBSTRING_INCLUDED_VERSIONS_1 = "feature_include/test-substring" ;
86
+
71
87
private static String SUBSTRING_EXCLUDED_VERSIONS_2 = "test-other-substring-exclude" ;
88
+ private static String SUBSTRING_INCLUDED_VERSIONS_2 = "test-other-substring-include" ;
89
+
72
90
73
91
private static String MULTIPLE_EXCLUDED_VERSIONS =
74
92
MULTIPLE_EXCLUDED_VERSIONS_1 + " " +
75
93
MULTIPLE_EXCLUDED_VERSIONS_2 + " " +
76
94
MULTIPLE_EXCLUDED_VERSIONS_3 ;
77
95
96
+ private static String MULTIPLE_INCLUDED_VERSIONS =
97
+ MULTIPLE_INCLUDED_VERSIONS_1 + " " +
98
+ MULTIPLE_INCLUDED_VERSIONS_2 + " " +
99
+ MULTIPLE_INCLUDED_VERSIONS_3 ;
100
+
78
101
private static String SUBSTRING_EXCLUDED_VERSIONS =
79
102
"feature/ other-substring" ;
80
103
104
+ private static String SUBSTRING_INCLUDED_VERSIONS =
105
+ "feature_include/ other-substring" ;
106
+
81
107
private static String NEVER_EXCLUDED_VERSION = "never-excluded-version" ;
82
108
83
109
@ Before
84
110
public void createCachingConfiguration () {
85
- nullVersionConfig = new LibraryCachingConfiguration (REFRESH_TIME_MINUTES , NULL_EXCLUDED_VERSION );
86
- oneVersionConfig = new LibraryCachingConfiguration (NO_REFRESH_TIME_MINUTES , ONE_EXCLUDED_VERSION );
87
- multiVersionConfig = new LibraryCachingConfiguration (REFRESH_TIME_MINUTES , MULTIPLE_EXCLUDED_VERSIONS );
88
- substringVersionConfig = new LibraryCachingConfiguration (REFRESH_TIME_MINUTES , SUBSTRING_EXCLUDED_VERSIONS );
111
+ nullVersionConfig = new LibraryCachingConfiguration (REFRESH_TIME_MINUTES , NULL_EXCLUDED_VERSION , NULL_INCLUDED_VERSION );
112
+ oneVersionConfig = new LibraryCachingConfiguration (NO_REFRESH_TIME_MINUTES , ONE_EXCLUDED_VERSION , ONE_INCLUDED_VERSION );
113
+ multiVersionConfig = new LibraryCachingConfiguration (REFRESH_TIME_MINUTES , MULTIPLE_EXCLUDED_VERSIONS , MULTIPLE_INCLUDED_VERSIONS );
114
+ substringVersionConfig = new LibraryCachingConfiguration (REFRESH_TIME_MINUTES , SUBSTRING_EXCLUDED_VERSIONS , SUBSTRING_INCLUDED_VERSIONS );
89
115
}
90
116
91
117
@ Issue ("JENKINS-66045" ) // NPE getting excluded versions
@@ -125,6 +151,15 @@ public void getExcludedVersionsStr() {
125
151
assertThat (substringVersionConfig .getExcludedVersionsStr (), is (SUBSTRING_EXCLUDED_VERSIONS ));
126
152
}
127
153
154
+ @ Test
155
+ @ WithoutJenkins
156
+ public void getIncludedVersionsStr () {
157
+ assertThat (nullVersionConfig .getIncludedVersionsStr (), is (NULL_INCLUDED_VERSION ));
158
+ assertThat (oneVersionConfig .getIncludedVersionsStr (), is (ONE_INCLUDED_VERSION ));
159
+ assertThat (multiVersionConfig .getIncludedVersionsStr (), is (MULTIPLE_INCLUDED_VERSIONS ));
160
+ assertThat (substringVersionConfig .getIncludedVersionsStr (), is (SUBSTRING_INCLUDED_VERSIONS ));
161
+ }
162
+
128
163
@ Test
129
164
@ WithoutJenkins
130
165
public void isExcluded () {
@@ -155,6 +190,24 @@ public void isExcluded() {
155
190
assertFalse (substringVersionConfig .isExcluded (null ));
156
191
}
157
192
193
+ @ Issue ("JENKINS-69135" ) //"Versions to include" feature for caching
194
+ @ Test
195
+ @ WithoutJenkins
196
+ public void isIncluded () {
197
+ assertFalse (nullVersionConfig .isIncluded (NULL_INCLUDED_VERSION ));
198
+ assertFalse (nullVersionConfig .isIncluded ("" ));
199
+
200
+ assertTrue (oneVersionConfig .isIncluded (ONE_INCLUDED_VERSION ));
201
+
202
+ assertTrue (multiVersionConfig .isIncluded (MULTIPLE_INCLUDED_VERSIONS_1 ));
203
+ assertTrue (multiVersionConfig .isIncluded (MULTIPLE_INCLUDED_VERSIONS_2 ));
204
+ assertTrue (multiVersionConfig .isIncluded (MULTIPLE_INCLUDED_VERSIONS_3 ));
205
+
206
+ assertTrue (substringVersionConfig .isIncluded (SUBSTRING_INCLUDED_VERSIONS_1 ));
207
+ assertTrue (substringVersionConfig .isIncluded (SUBSTRING_INCLUDED_VERSIONS_2 ));
208
+
209
+ }
210
+
158
211
@ Test
159
212
public void clearCache () throws Exception {
160
213
sampleRepo .init ();
@@ -182,4 +235,72 @@ public void clearCache() throws Exception {
182
235
assertThat (new File (cache .withSuffix ("-name.txt" ).getRemote ()), not (anExistingFile ()));
183
236
}
184
237
238
+ //Test similar substrings in "Versions to include" & "Versions to exclude"
239
+ //Exclusion takes precedence
240
+ @ Issue ("JENKINS-69135" ) //"Versions to include" feature for caching
241
+ @ Test
242
+ public void clearCacheConflict () throws Exception {
243
+ sampleRepo .init ();
244
+ sampleRepo .write ("vars/foo.groovy" , "def call() { echo 'foo' }" );
245
+ sampleRepo .git ("add" , "vars" );
246
+ sampleRepo .git ("commit" , "--message=init" );
247
+ LibraryConfiguration config = new LibraryConfiguration ("library" ,
248
+ new SCMSourceRetriever (new GitSCMSource (null , sampleRepo .toString (), "" , "*" , "" , true )));
249
+ config .setDefaultVersion ("master" );
250
+ config .setImplicit (true );
251
+ // Same version specified in both include and exclude version
252
+ //Exclude takes precedence
253
+ config .setCachingConfiguration (new LibraryCachingConfiguration (30 , "master" , "master" ));
254
+ GlobalLibraries .get ().getLibraries ().add (config );
255
+ // Run build and check that cache gets created.
256
+ WorkflowJob p = r .createProject (WorkflowJob .class );
257
+ p .setDefinition (new CpsFlowDefinition ("foo()" , true ));
258
+ WorkflowRun b = r .buildAndAssertSuccess (p );
259
+ LibrariesAction action = b .getAction (LibrariesAction .class );
260
+ LibraryRecord record = action .getLibraries ().get (0 );
261
+ FilePath cache = LibraryCachingConfiguration .getGlobalLibrariesCacheDir ().child (record .getDirectoryName ());
262
+ // Cache should not get created since the version is included in "Versions to exclude"
263
+ assertThat (new File (cache .getRemote ()), not (anExistingDirectory ()));
264
+ assertThat (new File (cache .withSuffix ("-name.txt" ).getRemote ()), not (anExistingFile ()));
265
+ }
266
+
267
+ @ Issue ("JENKINS-69135" ) //"Versions to include" feature for caching
268
+ @ Test
269
+ public void clearCacheIncludedVersion () throws Exception {
270
+ sampleRepo .init ();
271
+ sampleRepo .write ("vars/foo.groovy" , "def call() { echo 'foo' }" );
272
+ sampleRepo .git ("add" , "vars" );
273
+ sampleRepo .git ("commit" , "--message=init" );
274
+ sampleRepo .git ("branch" , "test/include" );
275
+ LibraryConfiguration config = new LibraryConfiguration ("library" ,
276
+ new SCMSourceRetriever (new GitSCMSource (null , sampleRepo .toString (), "" , "*" , "" , true )));
277
+ config .setDefaultVersion ("master" );
278
+ config .setAllowVersionOverride (true );
279
+ config .setImplicit (false );
280
+ config .setCachingConfiguration (new LibraryCachingConfiguration (30 , "" , "test/include" ));
281
+ GlobalLibraries .get ().getLibraries ().add (config );
282
+ // Run build and check that cache gets created.
283
+ WorkflowJob p = r .createProject (WorkflowJob .class );
284
+ p .setDefinition (new CpsFlowDefinition ("library identifier: 'library', changelog:false\n \n foo()" , true ));
285
+ WorkflowRun b = r .buildAndAssertSuccess (p );
286
+ WorkflowJob p2 = r .createProject (WorkflowJob .class );
287
+ p2 .setDefinition (new CpsFlowDefinition ("library identifier: 'library@test/include', changelog:false\n \n foo()" , true ));
288
+ WorkflowRun b2 = r .buildAndAssertSuccess (p2 );
289
+ LibrariesAction action = b .getAction (LibrariesAction .class );
290
+ LibraryRecord record = action .getLibraries ().get (0 );
291
+ LibrariesAction action2 = b2 .getAction (LibrariesAction .class );
292
+ LibraryRecord record2 = action2 .getLibraries ().get (0 );
293
+ FilePath cache = LibraryCachingConfiguration .getGlobalLibrariesCacheDir ().child (record .getDirectoryName ());
294
+ FilePath cache2 = LibraryCachingConfiguration .getGlobalLibrariesCacheDir ().child (record2 .getDirectoryName ());
295
+ assertThat (new File (cache .getRemote ()), not (anExistingDirectory ()));
296
+ assertThat (new File (cache .withSuffix ("-name.txt" ).getRemote ()), not (anExistingFile ()));
297
+ assertThat (new File (cache2 .getRemote ()), anExistingDirectory ());
298
+ assertThat (new File (cache2 .withSuffix ("-name.txt" ).getRemote ()), anExistingFile ());
299
+ // Clears cache for the entire library, until the "Delete specific cache version" feature in merged
300
+ // Clear the cache. TODO: Would be more realistic to set up security and use WebClient.
301
+ ExtensionList .lookupSingleton (LibraryCachingConfiguration .DescriptorImpl .class ).doClearCache ("library" , false );
302
+ assertThat (new File (cache2 .getRemote ()), not (anExistingDirectory ()));
303
+ assertThat (new File (cache2 .withSuffix ("-name.txt" ).getRemote ()), not (anExistingFile ()));
304
+ }
305
+
185
306
}
0 commit comments