-
Notifications
You must be signed in to change notification settings - Fork 246
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
issue#308 Allow document selector match many documents and skip empty templates #335
base: main
Are you sure you want to change the base?
issue#308 Allow document selector match many documents and skip empty templates #335
Conversation
allows document selector to ignore templates which have no matching documents
1cba194
to
885a076
Compare
Index int | ||
Negative bool | ||
Docs []common.K8sManifest | ||
SelectedDocs *[]common.K8sManifest |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The core of the solution is that instead of relying on the "Index" of selected document we simply pass the array of "selected" documents to the assertion context.
I believe it's a bit simpler design to operate on the documents array directly instead of dealing with Indices array which then would have to be validated against indices going outside of boundary.
The context also includes unfiltered list of all documents (Docs
) which makes assertions such as hasDocuments
to stil work fine ignoring the by ignoring the Selection.
return map[string][]common.K8sManifest{}, errors.New("multiple indexes found") | ||
} | ||
|
||
if filteredManifestsCount > 0 || !ds.SkipEmptyTemplates { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea is that the Selector should decide which documents in which templates should be asserted.
If the "skipEmptyTemplates" is true - it wont return templates which don't have any matching documents in the resulting map.
invalidDocumentIndex := []string{"Error:", indexError.Error()} | ||
failInfo = append(failInfo, invalidDocumentIndex...) | ||
} else { | ||
for idx, template := range selectedTemplates { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should iterate only over selectedTemplates - That what's makes skipEmptyTemplates flag work.
@mszygenda Well done! I finally got around to trying it out, and it works as expected. Regarding functionality, it looks to me that it is a nice extension to the current feature-set, without breaking or forcing it on anyone. |
I would love to see this added, as a way to enforce a general hygeine tests across all resources of a specific type. Tremendous work @mszygenda |
One nice addition, though of course it could easily be another PR, is to allow the |
This PR builds on top of:
#328
Which partially addressed the issue:
#308
This PR addresses use-case in it's entirety.
Use-case:
I want to assert whether or not all manifests of type "Deployment":
I want this assertion to work for current and future deployments I might have in my chart.
I have to use document selector because I keep different types of k8s manifests in a single YAML files (eg.
foo-application.yaml
contains definition ofDeployment
as well as theService
)It is resolved with introduction of two new properties on
documentSelector
level:matchMany
andskipEmptyTemplates
Here's the updated documentation of the module
documentSelector
path to assert.true
to allow matching multiple documents. Defaults tofalse
which means selector has to match single document across all templates.true
to skip asserting templates which didn't render any matching documents. Defaults tofalse
which means selector have to find at least one document in every template.EXAMPLE:
Happy to hear any feedback you might have. The code might need some styling fixes etc. but functionally it seems to be doing what it's supposed to do.