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

issue#308 Allow document selector match many documents and skip empty templates #335

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

mszygenda
Copy link

@mszygenda mszygenda commented May 8, 2024

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":

  • eg. define default resources section.
  • eg. follow naming convention
  • eg. set right namespace

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 of Deployment as well as the Service)

It is resolved with introduction of two new properties on documentSelector level: matchMany and skipEmptyTemplates

Here's the updated documentation of the module

  • documentSelector: DocumentSelector, optional. The path of the key to be match and the match value to assert. Using this information, helm-unittest will automatically discover the documents for asserting. Generally you can ignore this field if the template file render only one document.
    • path: string. The documentSelector path to assert.
    • value: any. The expected value.
    • matchMany: bool, optional. Set to true to allow matching multiple documents. Defaults to false which means selector has to match single document across all templates.
    • skipEmptyTemplates: bool, optional. Set to true to skip asserting templates which didn't render any matching documents. Defaults to false which means selector have to find at least one document in every template.

EXAMPLE:

suite: Document Selector is matching many documents
templates:
  - "*"
tests:
  - it: deployment names should end with -deployment suffix
    documentSelector:
      path: kind
      value: Deployment
      matchMany: true
      skipEmptyTemplates: true
    asserts:
      - matchRegex:
          path: metadata.name
          pattern: -deployment$

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.

@mszygenda mszygenda force-pushed the feature/AllowDocumentSelectorSkipEmptyTemplates branch from 1cba194 to 885a076 Compare May 14, 2024 18:36
@mszygenda mszygenda marked this pull request as ready for review May 14, 2024 18:40
@mszygenda mszygenda changed the title Feature/allow document selector skip empty templates Feature/allow document selector match many documents and skip empty templates May 14, 2024
@mszygenda mszygenda changed the title Feature/allow document selector match many documents and skip empty templates issue#308 Allow document selector match many documents and skip empty templates May 15, 2024
Index int
Negative bool
Docs []common.K8sManifest
SelectedDocs *[]common.K8sManifest
Copy link
Author

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 {
Copy link
Author

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 {
Copy link
Author

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.

@MikaelElkiaer
Copy link

@mszygenda Well done!

I finally got around to trying it out, and it works as expected.
Good idea to add the skipEmptyTemplates.

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 have not spent much time looking into the code, so I dare not comment on code quality.

@quintush quintush added this to the v0.6.0 milestone Jun 5, 2024
@mcg1969
Copy link

mcg1969 commented Jun 9, 2024

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

@mcg1969
Copy link

mcg1969 commented Jun 9, 2024

One nice addition, though of course it could easily be another PR, is to allow the value: field to be a string or a list of strings. This could enable, for instance, a set of conventions applied to both Deployment and StatefulSet objects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants