This repository has been archived by the owner on May 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SDEV-1117: Make sure target resolution always happens from within an …
…MasterToSlaveCallable (#312) * SDEV-1117: Make sure target resolution always happens from within an MasterToSlaveCallable For callflow we will either reuse the scan patterns provied for the traditional scan or alternatively make use of an ovveride. In either case before hadning off to dependencies to do the actual work the patterns need to be resolved to file targets. Because jenkins manages a cluster this needs to be done from the right context. I have refactored out the actuall file resolution logic to make it easier to re-use and created a new class that extends MasterToSlaveCallable so that we can leverage the logic from the right place. The orginal place where this logic was used inside RemoteScanner was already within a MasterToSlaveCallable class, so nothin needed to change there, other than pointing to the logics new location. Some tests were refactored to reflect the new location of the logic * Add license header * make codenarc happy by hook or by crook * Fix a test * Add license header * Ci build fix attempt --------- Co-authored-by: Chris Wininger <[email protected]> Co-authored-by: Eduard Tita <[email protected]>
- Loading branch information
1 parent
99a1b60
commit a609b3a
Showing
8 changed files
with
178 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/main/java/org/sonatype/nexus/ci/iq/RemoteFileResolver.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright (c) 2016-present Sonatype, Inc. All rights reserved. | ||
* | ||
* This program is licensed to you under the Apache License Version 2.0, | ||
* and you may not use this file except in compliance with the Apache License Version 2.0. | ||
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the Apache License Version 2.0 is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. | ||
*/ | ||
package org.sonatype.nexus.ci.iq | ||
|
||
import jenkins.security.MasterToSlaveCallable | ||
|
||
class RemoteFileResolver | ||
extends MasterToSlaveCallable<List<String>, RuntimeException> | ||
{ | ||
private final File workDir; | ||
private final List<String> scanPatterns; | ||
|
||
RemoteFileResolver(final File workDir, final List<String> scanPatterns) { | ||
this.workDir = workDir | ||
this.scanPatterns = scanPatterns | ||
} | ||
|
||
@Override | ||
List<String> call() throws RuntimeException { | ||
return ScanPatternUtil.getScanTargets(workDir, scanPatterns) | ||
.collect { | ||
return it.getAbsolutePath() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/main/java/org/sonatype/nexus/ci/iq/ScanPatternUtil.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright (c) 2016-present Sonatype, Inc. All rights reserved. | ||
* | ||
* This program is licensed to you under the Apache License Version 2.0, | ||
* and you may not use this file except in compliance with the Apache License Version 2.0. | ||
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the Apache License Version 2.0 is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. | ||
*/ | ||
package org.sonatype.nexus.ci.iq | ||
|
||
@SuppressWarnings(['AbcMetric']) | ||
class ScanPatternUtil | ||
{ | ||
static final List<String> DEFAULT_SCAN_PATTERN = | ||
['**/*.jar', '**/*.war', '**/*.ear', '**/*.zip', '**/*.tar.gz'] | ||
|
||
public static final String EXCLUDE_MARKER = '!' | ||
|
||
// Warning: Only use this in the context of a MasterToSlaveCallable | ||
static List<File> getScanTargets(final File workDir, final List<String> scanPatterns) { | ||
def directoryScanner = RemoteScannerFactory.getDirectoryScanner() | ||
def normalizedScanPatterns = scanPatterns ?: DEFAULT_SCAN_PATTERN | ||
def includeScanPatterns = normalizedScanPatterns.findAll{!it.startsWith(EXCLUDE_MARKER)} | ||
def excludeScanPatterns = normalizedScanPatterns.findAll{it.startsWith(EXCLUDE_MARKER)}.collect{it.substring(1)} | ||
directoryScanner.setBasedir(workDir) | ||
directoryScanner.setIncludes(includeScanPatterns.toArray(new String[includeScanPatterns.size()])) | ||
directoryScanner.setExcludes(excludeScanPatterns.toArray(new String[excludeScanPatterns.size()])) | ||
directoryScanner.addDefaultExcludes() | ||
directoryScanner.scan() | ||
return (directoryScanner.getIncludedDirectories() + directoryScanner.getIncludedFiles()) | ||
.collect { f -> new File(workDir, f) } | ||
.sort() | ||
.asImmutable() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/test/java/org/sonatype/nexus/ci/iq/ScanPatternUtilTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright (c) 2016-present Sonatype, Inc. All rights reserved. | ||
* | ||
* This program is licensed to you under the Apache License Version 2.0, | ||
* and you may not use this file except in compliance with the Apache License Version 2.0. | ||
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the Apache License Version 2.0 is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. | ||
*/ | ||
package org.sonatype.nexus.ci.iq | ||
|
||
import org.codehaus.plexus.util.DirectoryScanner | ||
import spock.lang.Specification | ||
import spock.util.mop.ConfineMetaClassChanges | ||
|
||
@ConfineMetaClassChanges([IqClientFactory]) | ||
class ScanPatternUtilTest | ||
extends Specification | ||
{ | ||
DirectoryScanner directoryScanner | ||
|
||
def setup() { | ||
GroovyMock(RemoteScannerFactory, global: true) | ||
directoryScanner = Mock() | ||
RemoteScannerFactory.getDirectoryScanner() >> directoryScanner | ||
} | ||
|
||
def 'Uses default scan patterns when patterns not set'() { | ||
setup: | ||
def workspaceFile = new File('/file/path') | ||
directoryScanner.getIncludedDirectories() >> [] | ||
directoryScanner.getIncludedFiles() >> [] | ||
|
||
when: | ||
ScanPatternUtil.getScanTargets(workspaceFile, []) | ||
|
||
then: | ||
1 * directoryScanner.setIncludes(*_) >> { arguments -> | ||
assert arguments[0] == ScanPatternUtil.DEFAULT_SCAN_PATTERN | ||
} | ||
} | ||
|
||
def 'Uses no excludes by default'() { | ||
setup: | ||
def workspaceFile = new File('/file/path') | ||
directoryScanner.getIncludedDirectories() >> [] | ||
directoryScanner.getIncludedFiles() >> [] | ||
|
||
when: | ||
ScanPatternUtil.getScanTargets(workspaceFile, ['*.jar']) | ||
|
||
then: | ||
1 * directoryScanner.setIncludes(*_) >> { arguments -> | ||
assert arguments[0] == ['*.jar'] | ||
} | ||
1 * directoryScanner.setExcludes(*_) >> { arguments -> | ||
assert arguments[0] == [] | ||
} | ||
} | ||
|
||
def 'Pass excludes to directory scanner'() { | ||
setup: | ||
def workspaceFile = new File('/file/path') | ||
directoryScanner.getIncludedDirectories() >> [] | ||
directoryScanner.getIncludedFiles() >> [] | ||
|
||
when: | ||
ScanPatternUtil.getScanTargets(workspaceFile, ['*.jar','!*.zip','*.war','!*.tar']) | ||
|
||
then: | ||
1 * directoryScanner.setIncludes(*_) >> { arguments -> | ||
assert arguments[0] == ['*.jar','*.war'] | ||
} | ||
1 * directoryScanner.setExcludes(*_) >> { arguments -> | ||
assert arguments[0] == ['*.zip','*.tar'] | ||
} | ||
} | ||
} |