question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

(assert): there should be something like ```haveManifest(...)``` and ```haveHelmChart``` or a matcher like ```k8sobjectlike``` that allow unit tests for kubernetes objects

See original GitHub issue

Description

It would be nice if we could write unit tests for eks components like helmcharts and manifests. These resources contain JSON objects (the kubernetes manifests or helmchart values) that are stringified via:

/**
   * Convert an object, potentially containing tokens, to a JSON string
   */
public toJsonString(obj: any, space?: number): string {
  return CloudFormationLang.toJSON(obj, space).toString();
}

It would be nice if this would be parsed and then could be used with matchers like objectLike or arrayWith

Use Case

I want to create unit tests for constructs that contain kubernetes resources. At the moment it’s not possible or I didn’t found the docs how to do this.

I only find examples that do something like:

expect(stack).toHaveResource(KubernetesManifest.RESOURCE_TYPE, {
  Manifest: JSON.stringify([
    {
      apiVersion: 'v1beta',
      kind: 'Foo',
      metadata: {
        labels: {
          'aws.cdk.eks/prune-c89a5983505f58231ac2a9a86fd82735ccf2308eac': '',
        },
      },
    },
  ]),
  PruneLabel: 'aws.cdk.eks/prune-c89a5983505f58231ac2a9a86fd82735ccf2308eac',
});

but I would like to have something like:

expect(stack).toHaveManifest('SomeIdentifier?', {
  Manifest: arrayWith(
    {
      apiVersion: 'v1beta',
      kind: 'Foo',
      ...
      anything: objectLike({
        some: 'value'
      }
    },
  ]),
...
});

Proposed Solution

I have no Idea at the moment… somehow we have to JSON parse the stringified k8s objects

Other information

no

Acknowledge

  • I may be able to implement this feature request
  • This feature might incur a breaking change

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
markussiebertcommented, Dec 5, 2021

Looks perfect! Will give it a try tomorrow!

0reactions
markussiebertcommented, Dec 6, 2021

only for others who stumble over this: works like a charm!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unit testing Kubernetes operators using mocks - ITNEXT
In this post, I'll walk you through the idea of unit testing Kubernetes operators using mocks, why you'd need them, and how to...
Read more >
Mocking the Kubernetes client in Go for Unit Testing - Medium
When we run this main.go file against a running cluster, we end up creating a pod called test-pod in the default namespace. Now,...
Read more >
Testing - The Cluster API Book
In Cluster API, integration tests are based on envtest and one or more controllers configured to run against the test cluster. With this...
Read more >
Unit Testing Kubernetes Resources with Go - Thomas Stringer
In the previous blog post I talked about how to connect to a Kubernetes cluster from Go. But we're missing something here with...
Read more >
Running integration tests in Kubernetes - Opensource.com
This can only be tested by running all these parts next to each other and not enabling "test mode" within our application. Unit...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found