New to pulumi and trying my hand at unit testing i...
# getting-started
f
New to pulumi and trying my hand at unit testing in golang. My code uses config values that are set, but the test always panics saying the config value is not set. How can I tell the test that about the config values set for my stack?
b
Hey Jim, are you able to share your code?
f
Copy code
package main

import (
	"fmt"

	"<http://github.com/pulumi/pulumi-azure-native/sdk/go/azure/compute|github.com/pulumi/pulumi-azure-native/sdk/go/azure/compute>"
	"<http://github.com/pulumi/pulumi/sdk/v3/go/pulumi|github.com/pulumi/pulumi/sdk/v3/go/pulumi>"
	"<http://github.com/pulumi/pulumi/sdk/v3/go/pulumi/config|github.com/pulumi/pulumi/sdk/v3/go/pulumi/config>"
)

func createImageGallery(ctx *pulumi.Context) (*compute.Gallery, error) {
	stack := ctx.Stack()
	conf := config.New(ctx, "")
	name := conf.Require("application-name")

	gallery, err := compute.NewGallery(ctx, "gallery", &compute.GalleryArgs{
		Description:       pulumi.String("This is the gallery description."),
		GalleryName:       pulumi.String(fmt.Sprintf("%s-image-gallery-%s", name, stack)),
		Location:          pulumi.String("West US"),
		ResourceGroupName: pulumi.String("myResourceGroup"),
	})
	if err != nil {
		return nil, err
	}
	return gallery, nil
}
Copy code
package main

import (
	"fmt"
	"sync"
	"testing"

	"<http://github.com/pulumi/pulumi/sdk/v3/go/common/resource|github.com/pulumi/pulumi/sdk/v3/go/common/resource>"
	"<http://github.com/pulumi/pulumi/sdk/v3/go/pulumi|github.com/pulumi/pulumi/sdk/v3/go/pulumi>"
	"<http://github.com/stretchr/testify/assert|github.com/stretchr/testify/assert>"
)

type mocks int

func (mocks) NewResource(args pulumi.MockResourceArgs) (string, resource.PropertyMap, error) {
	outputs := args.Inputs.Mappable()
	fmt.Println(outputs)
	fmt.Println(args.TypeToken)
	switch args.TypeToken {
	case "azure-native:compute:Gallery":
		outputs["name"] = outputs["galleryName"]
	}

	return args.Name, resource.NewPropertyMapFromMap(outputs), nil
}

func (mocks) Call(args pulumi.MockCallArgs) (resource.PropertyMap, error) {
	return args.Args, nil
}

func TestCreateImageGallery(t *testing.T) {
	err := pulumi.RunErr(func(ctx *pulumi.Context) error {
		gallery, err := createImageGallery(ctx)
		assert.NoError(t, err)

		var wg sync.WaitGroup
		wg.Add(1)

		pulumi.All(gallery.Name).ApplyT(func(all []interface{}) error {
			name := all[0].(string)
			assert.Equal(t, "kong-image-gallery-nonprod", name)

			wg.Done()
			return nil
		})

		wg.Wait()
		return nil
	}, pulumi.WithMocks("core", "azure-core-nonprod", mocks(0)))
	assert.NoError(t, err)
}
Copy code
config:
  azure-native:location: EastUS2
  core:application-name: kong
code, test and config
Copy code
panic: fatal: A failure has occurred: missing required configuration variable 'core:application-name'; run `pulumi config` to set [recovered]
        panic: fatal: A failure has occurred: missing required configuration variable 'core:application-name'; run `pulumi config` to set
and error
And the directory structure if that matter
Copy code
.
├── Pulumi.azure-core-nonprod.yaml
├── Pulumi.yaml
├── go.mod
├── go.sum
├── image_gallery.go
├── image_gallery_test.go
└── main.go

0 directories, 7 files
b
hey @freezing-greece-4855 looks like this issue: https://github.com/pulumi/pulumi/issues/4472 Props to @red-match-15116 for reminding me about this Essentially, you'll need to set the config via environment variables unfortunately 😞
f
ok
oof
b
actually the last commentor has a mechanism to do this in Go, take a look
1