Skip to content

Commit

Permalink
fix(issue-286): limit documentselector to only execute against specif…
Browse files Browse the repository at this point in the history
…ic templates

Signed-off-by: ivan katliarchuk <[email protected]>
  • Loading branch information
ivankatliarchuk committed Mar 9, 2024
1 parent 09ad954 commit f810465
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,9 @@ tests:
limits:
memory: 128Mi
template: deployment.yaml
documentSelector:
documentSelector:
path: metadata.name
value: my-service-name
value: my-service-name
asserts:
- equal:
path: metadata.name
Expand Down
30 changes: 29 additions & 1 deletion pkg/unittest/test_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strings"
"time"

log "github.com/sirupsen/logrus"

"github.com/helm-unittest/helm-unittest/internal/common"
"github.com/helm-unittest/helm-unittest/pkg/unittest/results"
"github.com/helm-unittest/helm-unittest/pkg/unittest/snapshot"
Expand Down Expand Up @@ -476,8 +478,10 @@ func (t *TestJob) determineRenderSuccess() {
}

func (t *TestJob) determineDocumentIndex(manifestOfFiles map[string][]common.K8sManifest) error {
filteredManifests := t.manifestsUnderTest(manifestOfFiles)
log.WithField("assertion", "determine-document-index").Debugln("manifests", filteredManifests)
if t.DocumentSelector != nil {
idx, err := t.DocumentSelector.FindDocumentsIndex(manifestOfFiles)
idx, err := t.DocumentSelector.FindDocumentsIndex(filteredManifests)
if err != nil {
return err
} else {
Expand All @@ -491,6 +495,30 @@ func (t *TestJob) determineDocumentIndex(manifestOfFiles map[string][]common.K8s
return nil
}

// evaluate documents produced by the file in test manifest
func (t *TestJob) manifestsUnderTest(manifests map[string][]common.K8sManifest) map[string][]common.K8sManifest {
log.WithField("assertion", "manifests-under-test").Debugln("total manifests", len(manifests))
result := make(map[string][]common.K8sManifest)
if t.Template != "" {
for key, value := range manifests {
if strings.Contains(key, t.Template) {
result[key] = value
}
}
}
if t.Templates != nil && len(t.Templates) > 0 {
for _, template := range t.Templates {
for key, value := range manifests {
if strings.Contains(key, template) {
result[key] = value
}
}
}
}
log.WithField("assertion", "manifests-under-test").Debugln("manifests to test against", len(result))
return result
}

// add prefix to Assertion.Template
func (t *TestJob) polishAssertionsTemplate(targetChartName string, outputOfFiles map[string]string) {
if t.chartRoute == "" {
Expand Down

0 comments on commit f810465

Please sign in to comment.