Skip to content

Commit

Permalink
Merge pull request helm-unittest#317 from ivankatliarchuk/issue-316
Browse files Browse the repository at this point in the history
bug(issue-316): containsDocument fix negative context support
  • Loading branch information
quintush authored Apr 29, 2024
2 parents e830e69 + 5dbecb7 commit 89af23e
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 24 deletions.
2 changes: 1 addition & 1 deletion pkg/unittest/.snapshots/TestV3RunnerOkWithFailedTests
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
Template: basic/templates/rbac.yaml
DocumentIndex: 1
Expected to contain document:
Kind = ClusterRole, apiVersion = rbac.authorization.k8s.io/v1, Name = , Namespace =
Kind = ClusterRole, apiVersion = rbac.authorization.k8s.io/v1
FAIL test autoscaling ../../test/data/v3/basic/tests_failed/nofile_test.yaml
- should use GLOBAL scaling config when release autoscaling AND Global autoscaling are enabled

Expand Down
21 changes: 19 additions & 2 deletions pkg/unittest/validators/contains_document_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package validators

import (
"fmt"
"strings"

log "github.com/sirupsen/logrus"

Expand All @@ -27,11 +28,25 @@ func (v ContainsDocumentValidator) failInfo(actual interface{}, index int, not b
return splitInfof(
setFailFormat(not, false, false, false, " to contain document"),
index,
fmt.Sprintf("Kind = %s, apiVersion = %s, Name = %s, Namespace = %s",
v.Kind, v.APIVersion, v.Name, v.Namespace),
v.joinOutput(),
)
}

// joinOutput constructs a string representation of the ContainsDocumentValidator
// object with the provided fields: Kind, apiVersion, Name, and Namespace.
func (v ContainsDocumentValidator) joinOutput() string {
parts := []string{
fmt.Sprintf("Kind = %s, apiVersion = %s", v.Kind, v.APIVersion),
}
if v.Name != "" {
parts = append(parts, fmt.Sprintf("Name = %s", v.Name))
}
if v.Namespace != "" {
parts = append(parts, fmt.Sprintf("Namespace = %s", v.Namespace))
}
return strings.Join(parts, ", ")
}

func (v ContainsDocumentValidator) validateManifest(manifest common.K8sManifest) bool {
if kind, ok := manifest["kind"].(string); ok && kind != v.Kind {
// if no match, move onto next document
Expand Down Expand Up @@ -105,6 +120,8 @@ func (v ContainsDocumentValidator) Validate(context *ValidateContext) (bool, []s
if len(manifests) == 0 && !context.Negative {
errorMessage := v.failInfo(v.Kind, 0, context.Negative)
validateErrors = append(validateErrors, errorMessage...)
} else if len(manifests) == 0 && context.Negative {
validateSuccess = true
}

return validateSuccess, validateErrors
Expand Down
68 changes: 48 additions & 20 deletions pkg/unittest/validators/contains_document_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var docToTestContainsDocument3 = `
apiVersion: v1
kind: Service
metadata:
name: bar
name: bar
`

var docToTestContainsDocument4 = `
Expand All @@ -38,7 +38,7 @@ metadata:
namespace: foo
`

func TestContainsDocumentValidatorWhenEmptyNOk(t *testing.T) {
func TestContainsDocumentValidatorWhenEmptyOk(t *testing.T) {
validator := ContainsDocumentValidator{
Kind: "Service",
APIVersion: "v1",
Expand All @@ -59,22 +59,50 @@ func TestContainsDocumentValidatorWhenEmptyNOk(t *testing.T) {
}, diff)
}

func TestContainsDocumentValidatorNegativeWhenEmptyOk(t *testing.T) {
validator := ContainsDocumentValidator{
Kind: "Service",
APIVersion: "v1",
Name: "bar",
Namespace: "foo",
Any: true,
func TestContainsDocumentValidatorNegativeOk(t *testing.T) {
tests := []struct {
name string
validator ContainsDocumentValidator
docs []common.K8sManifest
expected []string
}{
{
name: "should not fail with empty manifest and negative context",
validator: ContainsDocumentValidator{
Kind: "Deployment",
APIVersion: "v1",
Name: "bar",
Namespace: "foo",
Any: true,
},
docs: []common.K8sManifest{},
expected: []string{},
},
{
name: "should not fail with multiple manifest and negative context",
validator: ContainsDocumentValidator{
Kind: "Deployment",
APIVersion: "v1",
Name: "bar",
Namespace: "foo",
Any: true,
},
docs: []common.K8sManifest{makeManifest(docToTestContainsDocument1),
makeManifest(docToTestContainsDocument2)},
expected: []string{},
},
}
pass, diff := validator.Validate(&ValidateContext{
Index: -1,
Docs: []common.K8sManifest{},
Negative: true,
})

assert.False(t, pass)
assert.Equal(t, []string{}, diff)
for _, test := range tests {
pass, diff := test.validator.Validate(&ValidateContext{
Index: -1,
Docs: test.docs,
Negative: true,
})

assert.True(t, pass)
assert.Equal(t, test.expected, diff)
}
}

func TestContainsDocumentValidatorWhenNotAllDocumentsAreOk(t *testing.T) {
Expand Down Expand Up @@ -250,10 +278,10 @@ func TestContainsDocumentValidatorNoNameNamespaceWhenNegativeNOk(t *testing.T) {
assert.Equal(t, []string{
"DocumentIndex:\t0",
"Expected NOT to contain document:",
"\tKind = Service, apiVersion = v1, Name = , Namespace =",
"\tKind = Service, apiVersion = v1",
"DocumentIndex:\t1",
"Expected NOT to contain document:",
"\tKind = Service, apiVersion = v1, Name = , Namespace =",
"\tKind = Service, apiVersion = v1",
}, diff)
}

Expand Down Expand Up @@ -330,7 +358,7 @@ func TestContainsDocumentValidatorFail(t *testing.T) {
expected: []string{
"DocumentIndex:\t0",
"Expected to contain document:",
"\tKind = Service, apiVersion = apps/v1, Name = foo, Namespace =",
"\tKind = Service, apiVersion = apps/v1, Name = foo",
},
},
{
Expand All @@ -347,7 +375,7 @@ func TestContainsDocumentValidatorFail(t *testing.T) {
expected: []string{
"DocumentIndex:\t0",
"Expected to contain document:",
"\tKind = Service, apiVersion = apps/v1, Name = , Namespace = bar",
"\tKind = Service, apiVersion = apps/v1, Namespace = bar",
},
},
}
Expand Down
8 changes: 7 additions & 1 deletion test/data/v3/basic/tests/ingress_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ tests:
servicePort: 12345
- notExists:
path: spec.tls
- containsDocument:
kind: Ingress
apiVersion: extensions/v1beta1

- it: should set annotations if given
set:
Expand Down Expand Up @@ -92,7 +95,10 @@ tests:

- it: should render nothing if not enabled
asserts:
- containsDocument:
kind: Ingress
apiVersion: extensions/v1beta1
not: true
- hasDocuments:
count: 0
- matchSnapshot: {}

0 comments on commit 89af23e

Please sign in to comment.