Skip to content

Commit

Permalink
scorecard: fix enabling of optional tests with CLI flag
Browse files Browse the repository at this point in the history
  • Loading branch information
zegl committed Feb 5, 2024
1 parent a457bb9 commit fe958e6
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 0 deletions.
57 changes: 57 additions & 0 deletions score/optional_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package score

import (
"testing"

"github.com/zegl/kube-score/config"
ks "github.com/zegl/kube-score/domain"
"github.com/zegl/kube-score/scorecard"
)

func TestOptionalSkippedByDefault(t *testing.T) {
t.Parallel()
enabledOptionalTests := make(map[string]struct{})
wasSkipped(t, config.Configuration{
AllFiles: []ks.NamedReader{testFile("pod-container-memory-requests.yaml")},
EnabledOptionalTests: enabledOptionalTests,
}, "Container Memory Requests Equal Limits")
}

func TestOptionalIgnoredAndEnabled(t *testing.T) {
t.Parallel()

enabledOptionalTests := make(map[string]struct{})
enabledOptionalTests["container-resource-requests-equal-limits"] = struct{}{}

ignoredTests := make(map[string]struct{})
ignoredTests["container-resource-requests-equal-limits"] = struct{}{}

wasSkipped(t, config.Configuration{
AllFiles: []ks.NamedReader{testFile("pod-container-memory-requests.yaml")},
EnabledOptionalTests: enabledOptionalTests,
IgnoredTests: ignoredTests,
}, "Container Memory Requests Equal Limits")
}

func TestOptionalRunCliFlagEnabledDefault(t *testing.T) {
t.Parallel()

enabledOptionalTests := make(map[string]struct{})
enabledOptionalTests["container-resource-requests-equal-limits"] = struct{}{}

testExpectedScoreWithConfig(t, config.Configuration{
AllFiles: []ks.NamedReader{testFile("pod-container-memory-requests.yaml")},
EnabledOptionalTests: enabledOptionalTests,
}, "Container Memory Requests Equal Limits", scorecard.GradeCritical)
}

func TestOptionalRunAnnotationEnabled(t *testing.T) {
t.Parallel()

enabledOptionalTests := make(map[string]struct{})

testExpectedScoreWithConfig(t, config.Configuration{
AllFiles: []ks.NamedReader{testFile("pod-container-memory-requests-annotation-optional.yaml")},
EnabledOptionalTests: enabledOptionalTests,
}, "Container Memory Requests Equal Limits", scorecard.GradeCritical)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
apiVersion: v1
kind: Pod
metadata:
name: pod-test-1
namespace: testspace
labels:
app: foo-all-ok
annotations:
kube-score/enable: container-resource-requests-equal-limits
spec:
containers:
- name: foobar
image: foo/bar:123
imagePullPolicy: Always
resources:
requests:
cpu: 1
memory: 1Gi
ephemeral-storage: 500Mi
limits:
cpu: 1
memory: 2Gi
ephemeral-storage: 500Mi
readinessProbe:
httpGet:
path: /ready
port: 8080
livenessProbe:
httpGet:
path: /live
port: 8080
securityContext:
privileged: False
runAsUser: 30000
runAsGroup: 30000
readOnlyRootFilesystem: True
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: foo-all-ok-netpol
namespace: testspace
spec:
podSelector:
matchLabels:
app: foo-all-ok
policyTypes:
- Egress
- Ingress
47 changes: 47 additions & 0 deletions score/testdata/pod-container-memory-requests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
apiVersion: v1
kind: Pod
metadata:
name: pod-test-1
namespace: testspace
labels:
app: foo-all-ok
spec:
containers:
- name: foobar
image: foo/bar:123
imagePullPolicy: Always
resources:
requests:
cpu: 1
memory: 1Gi
ephemeral-storage: 500Mi
limits:
cpu: 1
memory: 2Gi
ephemeral-storage: 500Mi
readinessProbe:
httpGet:
path: /ready
port: 8080
livenessProbe:
httpGet:
path: /live
port: 8080
securityContext:
privileged: False
runAsUser: 30000
runAsGroup: 30000
readOnlyRootFilesystem: True
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: foo-all-ok-netpol
namespace: testspace
spec:
podSelector:
matchLabels:
app: foo-all-ok
policyTypes:
- Egress
- Ingress
5 changes: 5 additions & 0 deletions scorecard/enabled.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ func (so *ScoredObject) isEnabled(check ks.Check, annotations, childAnnotations
return true
}

// Enabled optional test from command line arguments
if _, ok := so.enabledOptionalTests[check.ID]; ok {
return true
}

// Optional checks are disabled unless explicitly allowed above
if check.Optional {
return false
Expand Down

0 comments on commit fe958e6

Please sign in to comment.