Skip to content

Commit e496dce

Browse files
committed
ROX-37162: add Groovy e2e for Job workload type exclusion
1 parent d69a8cc commit e496dce

1 file changed

Lines changed: 112 additions & 0 deletions

File tree

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import static Services.checkForNoActiveViolations
2+
import static Services.waitForViolation
3+
4+
import io.stackrox.proto.storage.PolicyOuterClass
5+
import io.stackrox.proto.storage.PolicyOuterClass.Exclusion
6+
import io.stackrox.proto.storage.PolicyOuterClass.LifecycleStage
7+
import io.stackrox.proto.storage.PolicyOuterClass.Policy
8+
9+
import objects.Deployment
10+
import objects.Job
11+
import services.FeatureFlagService
12+
import services.PolicyService
13+
14+
import org.junit.Assume
15+
import spock.lang.Tag
16+
17+
@Tag("PZ")
18+
class PolicyWorkloadTypeExclusionTest extends BaseSpecification {
19+
static final private Integer WAIT_FOR_VIOLATION_TIMEOUT = isRaceBuild() ? 450 : 90
20+
21+
static final private Deployment DEPLOYMENT = new Deployment()
22+
.setName("wte-deploy-${RUN_ID}")
23+
.setImagePrefetcherAffinity()
24+
.setImage(TEST_IMAGE)
25+
.addLabel("app", "wte")
26+
27+
// Keep the Job running. The only other Job helper usage creates a short-lived Job that
28+
// completes and is then deleted from StackRox (DeploymentTest).
29+
static final private Job JOB = new Job()
30+
.setName("wte-job-${RUN_ID}")
31+
.setImagePrefetcherAffinity()
32+
.setImage(TEST_IMAGE)
33+
.addLabel("app", "wte")
34+
.setCommand(["/bin/sh", "-c", "sleep 600"])
35+
36+
def setupSpec() {
37+
Assume.assumeTrue(
38+
"ROX_POLICY_WORKLOAD_TYPE_EXCLUSION is disabled",
39+
FeatureFlagService.isFeatureFlagEnabled("ROX_POLICY_WORKLOAD_TYPE_EXCLUSION"))
40+
41+
orchestrator.createDeployment(DEPLOYMENT)
42+
assert Services.waitForDeployment(DEPLOYMENT)
43+
44+
def createdJob = orchestrator.createJob(JOB)
45+
assert createdJob != null
46+
assert Services.waitForDeploymentByID(createdJob.getMetadata().getUid(), JOB.name)
47+
}
48+
49+
def cleanupSpec() {
50+
orchestrator.deleteDeployment(DEPLOYMENT)
51+
orchestrator.deleteJob(JOB)
52+
}
53+
54+
@Tag("BAT")
55+
def "exclude-by-type JOB suppresses Job violations and still fires on Deployments"() {
56+
given:
57+
def baselineID
58+
def policyID
59+
def baselineName = "wte-baseline-${RUN_ID}"
60+
def policyName = "wte-job-type-${RUN_ID}"
61+
62+
when:
63+
"the Job is evaluated like any other workload when no type exclusion is set"
64+
baselineID = PolicyService.createNewPolicy(imageTagPolicy(baselineName).build())
65+
assert baselineID != null
66+
67+
then:
68+
waitForViolation(DEPLOYMENT.name, baselineName, WAIT_FOR_VIOLATION_TIMEOUT)
69+
waitForViolation(JOB.name, baselineName, WAIT_FOR_VIOLATION_TIMEOUT)
70+
71+
when:
72+
"a matching policy excludes all Jobs"
73+
policyID = PolicyService.createNewPolicy(imageTagPolicy(policyName)
74+
.addExclusions(Exclusion.newBuilder()
75+
.setExcludeByType(Exclusion.ExcludeByType.newBuilder()
76+
.addTypes(Exclusion.WorkloadType.JOB)
77+
.build())
78+
.build())
79+
.build())
80+
assert policyID != null
81+
82+
then:
83+
"the Deployment is still in violation and the Job is not"
84+
waitForViolation(DEPLOYMENT.name, policyName, WAIT_FOR_VIOLATION_TIMEOUT)
85+
checkForNoActiveViolations(JOB.name, policyName, WAIT_FOR_VIOLATION_TIMEOUT)
86+
87+
cleanup:
88+
baselineID == null ?: PolicyService.deletePolicy(baselineID)
89+
policyID == null ?: PolicyService.deletePolicy(policyID)
90+
}
91+
92+
private static Policy.Builder imageTagPolicy(String name) {
93+
return Policy.newBuilder()
94+
.setName(name)
95+
.setDescription("Workload type exclusion e2e")
96+
.setRationale("Workload type exclusion e2e")
97+
.addLifecycleStages(LifecycleStage.DEPLOY)
98+
.addCategories("DevOps Best Practices")
99+
.setDisabled(false)
100+
.setSeverityValue(2)
101+
.addPolicySections(
102+
PolicyOuterClass.PolicySection.newBuilder().addPolicyGroups(
103+
PolicyOuterClass.PolicyGroup.newBuilder()
104+
.setFieldName("Image Tag")
105+
.addValues(PolicyOuterClass.PolicyValue.newBuilder()
106+
.setValue("nginx-2.0.3")
107+
.build())
108+
.build()
109+
).build()
110+
)
111+
}
112+
}

0 commit comments

Comments
 (0)