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

Document mocking capabilities for v4 Helm Chart #3183

Open
blampe opened this issue Aug 21, 2024 · 1 comment
Open

Document mocking capabilities for v4 Helm Chart #3183

blampe opened this issue Aug 21, 2024 · 1 comment
Assignees
Labels
area/helm awaiting-feedback Blocked on input from the author customer/lighthouse Lighthouse customer bugs and enhancements kind/enhancement Improvements or new features
Milestone

Comments

@blampe
Copy link
Contributor

blampe commented Aug 21, 2024

Users were previously able to inject resources into a v3 Helm Chart via the Call method, but the v4 Chart no longer invokes this client-side.

Let's put together an example of how to mock the v4 Chart resource.

@blampe blampe added area/helm customer/lighthouse Lighthouse customer bugs and enhancements kind/enhancement Improvements or new features labels Aug 21, 2024
@blampe blampe added the awaiting-feedback Blocked on input from the author label Oct 1, 2024
@blampe
Copy link
Contributor Author

blampe commented Oct 1, 2024

If this works for the customer we can update docs to reflect it.

cat main.go
package main

import (
	helmv4 "github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes/helm/v4"
	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

func createChart(ctx *pulumi.Context) (*helmv4.Chart, error) {
	return helmv4.NewChart(ctx, "nginx", &helmv4.ChartArgs{
		Chart:   pulumi.String("oci://registry-1.docker.io/bitnamicharts/nginx"),
		Version: pulumi.String("16.0.7"),
	})
}

func main() {
	pulumi.Run(func(ctx *pulumi.Context) error {
		chart, err := createChart(ctx)
		if err != nil {
			return err
		}
		ctx.Export("resources", chart.Resources)
		return nil
	})
}

❯ cat main_test.go
package main

import (
	"fmt"
	"testing"

	"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
	"github.com/stretchr/testify/assert"
)

type HelmMocks []map[string]interface{}

func (m HelmMocks) NewResource(args pulumi.MockResourceArgs) (string, resource.PropertyMap, error) {
	// Copy our inputs.
	outputs := args.Inputs

	// Convert our map into a PropertyValue Pulumi understands.
	values := []resource.PropertyValue{}
	for _, v := range m {
		values = append(values, resource.NewObjectProperty(resource.NewPropertyMapFromMap(v)))
	}

	// Add our resources to the chart's output.
	outputs["resources"] = resource.NewArrayProperty(values)

	return args.Name + "_id", outputs, nil
}

func (HelmMocks) Call(args pulumi.MockCallArgs) (resource.PropertyMap, error) {
	return nil, fmt.Errorf("not implemented")
}

func TestHelm(t *testing.T) {

	mockResources := []map[string]any{
		{"foo": "bar"},
	}

	pulumi.Run(
		func(ctx *pulumi.Context) error {
			chart, err := createChart(ctx)
			if err != nil {
				return err
			}

			pulumi.All(chart.Resources).ApplyT(func(all []any) error {
				resources := all[0].([]any)

				assert.Len(t, resources, 1)
				return nil
			})

			return nil

		},
		pulumi.WithMocks("project", "stack", HelmMocks(mockResources)),
	)
}

@mjeffryes mjeffryes added this to the 0.111 milestone Oct 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/helm awaiting-feedback Blocked on input from the author customer/lighthouse Lighthouse customer bugs and enhancements kind/enhancement Improvements or new features
Projects
None yet
Development

No branches or pull requests

2 participants