Skip to content

Commit 94ef45f

Browse files
#136 refactor tests
1 parent 37ce2f9 commit 94ef45f

File tree

1 file changed

+37
-128
lines changed

1 file changed

+37
-128
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.cloudogu.ces.cesbuildlib
22

3-
3+
import junit.framework.AssertionFailedError
44
import org.mockito.invocation.InvocationOnMock
55
import org.mockito.stubbing.Answer
66

@@ -20,17 +20,15 @@ class TrivyTest extends GroovyTestCase {
2020
Path installDir = Paths.get("target/trivyInstalls")
2121
Path workDir = Paths.get("")
2222
TrivyExecutor trivyExec = new TrivyExecutor(installDir)
23+
String trivyImage = "aquasec/trivy:" + Trivy.DEFAULT_TRIVY_VERSION
2324

24-
void testScanImage_successfulTrivyExecution() {
25-
// with hopes that this image will never have CVEs
26-
String imageName = "hello-world"
27-
String severityLevel = TrivySeverityLevel.CRITICAL
25+
26+
ScriptMock doTestScan(String imageName, String severityLevel, String strategy, int expectedStatusCode) {
2827
File trivyReportFile = new File("trivy/trivyReport.json")
2928
Path trivyDir = Paths.get(trivyReportFile.getParent())
3029
String trivyArguments = "image --exit-code 10 --exit-on-eol 10 --format ${TrivyScanFormat.JSON} -o ${trivyReportFile} --severity ${severityLevel} ${additionalFlags} ${imageName}"
3130
String expectedTrivyCommand = "trivy $trivyArguments"
3231

33-
String trivyImage = "aquasec/trivy:" + Trivy.DEFAULT_TRIVY_VERSION
3432
def scriptMock = new ScriptMock()
3533
scriptMock.env.WORKSPACE = "/test"
3634
Docker dockerMock = mock(Docker.class)
@@ -42,7 +40,6 @@ class TrivyTest extends GroovyTestCase {
4240
@Override
4341
Integer answer(InvocationOnMock invocation) throws Throwable {
4442
// mock "sh trivy" so that it returns the expected status code and check trivy arguments
45-
Integer expectedStatusCode = 0
4643
Closure closure = invocation.getArgument(1)
4744
scriptMock.expectedShRetValueForScript.put(expectedTrivyCommand, expectedStatusCode)
4845
Integer statusCode = closure.call() as Integer
@@ -63,7 +60,18 @@ class TrivyTest extends GroovyTestCase {
6360
}
6461
})
6562
Trivy trivy = new Trivy(scriptMock, Trivy.DEFAULT_TRIVY_VERSION, dockerMock)
66-
trivy.scanImage(imageName, severityLevel, TrivyScanStrategy.UNSTABLE)
63+
64+
trivy.scanImage(imageName, severityLevel, strategy)
65+
66+
return scriptMock
67+
}
68+
69+
void testScanImage_successfulTrivyExecution() {
70+
// with hopes that this image will never have CVEs
71+
String imageName = "hello-world"
72+
String severityLevel = TrivySeverityLevel.CRITICAL
73+
74+
def scriptMock = doTestScan(imageName, severityLevel, TrivyScanStrategy.UNSTABLE, 0)
6775

6876
assertEquals(false, scriptMock.getUnstable())
6977
}
@@ -72,45 +80,8 @@ class TrivyTest extends GroovyTestCase {
7280
// with hopes that this image will always have CVEs
7381
String imageName = "alpine:3.18.7"
7482
String severityLevel = TrivySeverityLevel.ALL
75-
File trivyReportFile = new File("trivy/trivyReport.json")
76-
Path trivyDir = Paths.get(trivyReportFile.getParent())
77-
String trivyArguments = "image --exit-code 10 --exit-on-eol 10 --format ${TrivyScanFormat.JSON} -o ${trivyReportFile} --severity ${severityLevel} ${additionalFlags} ${imageName}"
78-
String expectedTrivyCommand = "trivy $trivyArguments"
7983

80-
String trivyImage = "aquasec/trivy:" + Trivy.DEFAULT_TRIVY_VERSION
81-
def scriptMock = new ScriptMock()
82-
scriptMock.env.WORKSPACE = "/test"
83-
Docker dockerMock = mock(Docker.class)
84-
Docker.Image imageMock = mock(Docker.Image.class)
85-
when(dockerMock.image(trivyImage)).thenReturn(imageMock)
86-
when(imageMock.mountJenkinsUser()).thenReturn(imageMock)
87-
when(imageMock.mountDockerSocket()).thenReturn(imageMock)
88-
when(imageMock.inside(matches("-v /test/.trivy/.cache:/root/.cache/"), any())).thenAnswer(new Answer<Integer>() {
89-
@Override
90-
Integer answer(InvocationOnMock invocation) throws Throwable {
91-
// mock "sh trivy" so that it returns the expected status code and check trivy arguments
92-
Integer expectedStatusCode = 10
93-
Closure closure = invocation.getArgument(1)
94-
scriptMock.expectedShRetValueForScript.put(expectedTrivyCommand, expectedStatusCode)
95-
Integer statusCode = closure.call() as Integer
96-
assertEquals(expectedStatusCode, statusCode)
97-
assertEquals(expectedTrivyCommand, scriptMock.getActualShMapArgs().getLast())
98-
99-
// emulate trivy call with local trivy installation and check that it has the same behavior
100-
Files.createDirectories(trivyDir)
101-
Process process = trivyExec.exec(Trivy.DEFAULT_TRIVY_VERSION, trivyArguments, workDir)
102-
if(process.waitFor(2, TimeUnit.MINUTES)) {
103-
assertEquals(expectedStatusCode, process.exitValue())
104-
} else {
105-
process.destroyForcibly()
106-
fail("terminate trivy due to timeout")
107-
}
108-
109-
return expectedStatusCode
110-
}
111-
})
112-
Trivy trivy = new Trivy(scriptMock, Trivy.DEFAULT_TRIVY_VERSION, dockerMock)
113-
trivy.scanImage(imageName, severityLevel, TrivyScanStrategy.UNSTABLE)
84+
def scriptMock = doTestScan(imageName, severityLevel, TrivyScanStrategy.UNSTABLE, 10)
11485

11586
assertEquals(true, scriptMock.getUnstable())
11687
}
@@ -119,97 +90,35 @@ class TrivyTest extends GroovyTestCase {
11990
// with hopes that this image will always have CVEs
12091
String imageName = "alpine:3.18.7"
12192
String severityLevel = TrivySeverityLevel.ALL
122-
File trivyReportFile = new File("trivy/trivyReport.json")
123-
Path trivyDir = Paths.get(trivyReportFile.getParent())
124-
String trivyArguments = "image --exit-code 10 --exit-on-eol 10 --format ${TrivyScanFormat.JSON} -o ${trivyReportFile} --severity ${severityLevel} ${additionalFlags} ${imageName}"
125-
String expectedTrivyCommand = "trivy $trivyArguments"
126-
127-
String trivyImage = "aquasec/trivy:" + Trivy.DEFAULT_TRIVY_VERSION
128-
def scriptMock = new ScriptMock()
129-
scriptMock.env.WORKSPACE = "/test"
130-
Docker dockerMock = mock(Docker.class)
131-
Docker.Image imageMock = mock(Docker.Image.class)
132-
when(dockerMock.image(trivyImage)).thenReturn(imageMock)
133-
when(imageMock.mountJenkinsUser()).thenReturn(imageMock)
134-
when(imageMock.mountDockerSocket()).thenReturn(imageMock)
135-
when(imageMock.inside(matches("-v /test/.trivy/.cache:/root/.cache/"), any())).thenAnswer(new Answer<Integer>() {
136-
@Override
137-
Integer answer(InvocationOnMock invocation) throws Throwable {
138-
// mock "sh trivy" so that it returns the expected status code and check trivy arguments
139-
Integer expectedStatusCode = 10
140-
Closure closure = invocation.getArgument(1)
141-
scriptMock.expectedShRetValueForScript.put(expectedTrivyCommand, expectedStatusCode)
142-
Integer statusCode = closure.call() as Integer
143-
assertEquals(expectedStatusCode, statusCode)
144-
assertEquals(expectedTrivyCommand, scriptMock.getActualShMapArgs().getLast())
14593

146-
// emulate trivy call with local trivy installation and check that it has the same behavior
147-
Files.createDirectories(trivyDir)
148-
Process process = trivyExec.exec(Trivy.DEFAULT_TRIVY_VERSION, trivyArguments, workDir)
149-
if(process.waitFor(2, TimeUnit.MINUTES)) {
150-
assertEquals(expectedStatusCode, process.exitValue())
151-
} else {
152-
process.destroyForcibly()
153-
fail("terminate trivy due to timeout")
154-
}
155-
156-
return expectedStatusCode
157-
}
158-
})
159-
Trivy trivy = new Trivy(scriptMock, Trivy.DEFAULT_TRIVY_VERSION, dockerMock)
160-
def errorMsg = shouldFail {
161-
trivy.scanImage(imageName, severityLevel, TrivyScanStrategy.FAIL)
94+
def gotException = false
95+
try {
96+
doTestScan(imageName, severityLevel, TrivyScanStrategy.FAIL, 10)doTestScan(imageName, severityLevel, TrivyScanStrategy.FAIL, 10)
97+
} catch (AssertionFailedError e) {
98+
// exception could also be a junit assertion exception. This means a previous assertion failed
99+
throw e
100+
} catch (Exception e) {
101+
assertTrue("exception is: ${e.getMessage()}", e.getMessage().contains("Trivy has found vulnerabilities in image"))
102+
gotException = true
162103
}
163-
assertTrue("exception is: $errorMsg", errorMsg.contains("Trivy has found vulnerabilities in image"))
164-
assertEquals(false, scriptMock.getUnstable())
104+
assertTrue(gotException)
165105
}
166106

167107
void testScanImage_unsuccessfulTrivyExecution() {
168108
// with hopes that this image will always have CVEs
169109
String imageName = "inval!d:::///1.1...1.1."
170110
String severityLevel = TrivySeverityLevel.ALL
171-
File trivyReportFile = new File("trivy/trivyReport.json")
172-
Path trivyDir = Paths.get(trivyReportFile.getParent())
173-
String trivyArguments = "image --exit-code 10 --exit-on-eol 10 --format ${TrivyScanFormat.JSON} -o ${trivyReportFile} --severity ${severityLevel} ${additionalFlags} ${imageName}"
174-
String expectedTrivyCommand = "trivy $trivyArguments"
175111

176-
String trivyImage = "aquasec/trivy:" + Trivy.DEFAULT_TRIVY_VERSION
177-
def scriptMock = new ScriptMock()
178-
scriptMock.env.WORKSPACE = "/test"
179-
Docker dockerMock = mock(Docker.class)
180-
Docker.Image imageMock = mock(Docker.Image.class)
181-
when(dockerMock.image(trivyImage)).thenReturn(imageMock)
182-
when(imageMock.mountJenkinsUser()).thenReturn(imageMock)
183-
when(imageMock.mountDockerSocket()).thenReturn(imageMock)
184-
when(imageMock.inside(matches("-v /test/.trivy/.cache:/root/.cache/"), any())).thenAnswer(new Answer<Integer>() {
185-
@Override
186-
Integer answer(InvocationOnMock invocation) throws Throwable {
187-
// mock "sh trivy" so that it returns the expected status code and check trivy arguments
188-
Integer expectedStatusCode = 1
189-
Closure closure = invocation.getArgument(1)
190-
scriptMock.expectedShRetValueForScript.put(expectedTrivyCommand, expectedStatusCode)
191-
Integer statusCode = closure.call() as Integer
192-
assertEquals(expectedTrivyCommand, scriptMock.getActualShMapArgs().getLast())
193-
assertEquals(expectedStatusCode, statusCode)
194-
195-
// emulate trivy call with local trivy installation and check that it has the same behavior
196-
Files.createDirectories(trivyDir)
197-
Process process = trivyExec.exec(Trivy.DEFAULT_TRIVY_VERSION, trivyArguments, workDir)
198-
if(process.waitFor(2, TimeUnit.MINUTES)) {
199-
assertEquals(expectedStatusCode, process.exitValue())
200-
} else {
201-
process.destroyForcibly()
202-
fail("terminate trivy due to timeout")
203-
}
204-
205-
return expectedStatusCode
206-
}
207-
})
208-
Trivy trivy = new Trivy(scriptMock, Trivy.DEFAULT_TRIVY_VERSION, dockerMock)
209-
def errorMsg = shouldFail {
210-
trivy.scanImage("inval!d:::///1.1...1.1.", severityLevel, TrivyScanStrategy.UNSTABLE)
112+
def gotException = false
113+
try {
114+
doTestScan(imageName, severityLevel, TrivyScanStrategy.FAIL, 1)
115+
} catch (AssertionFailedError e) {
116+
// exception could also be a junit assertion exception. This means a previous assertion failed
117+
throw e
118+
} catch (Exception e) {
119+
assertTrue("exception is: ${e.getMessage()}", e.getMessage().contains("Error during trivy scan; exit code: 1"))
120+
gotException = true
211121
}
212-
assertTrue("exception is: $errorMsg", errorMsg.contains("Error during trivy scan; exit code: 1"))
122+
assertTrue(gotException)
213123
}
214-
215124
}

0 commit comments

Comments
 (0)