Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scorecard: fix enabling of optional tests with CLI flag #584

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading