https://pulumi.com logo
Title
s

steep-translator-9040

05/17/2022, 7:54 AM
Hi there 🙂! I’m trying to use config.RequireObject to load configuration into my struct. When I try to run a simple integration test, it panics, as it always says I haven’t set the configuration variables (even though I see them being set in the test). Am I understanding the functionality wrong? My expectation is that when provided no namespace, it uses the default one (in my example it is
pulumi-project-name
), I then provide RequireObject the key of the object I want to load from configuration (in my example
stack
), and it should deserialise it accordingly.
import (
	"<http://github.com/pulumi/pulumi/pkg/v3/testing/integration|github.com/pulumi/pulumi/pkg/v3/testing/integration>"
	"<http://github.com/pulumi/pulumi/sdk/v3/go/pulumi|github.com/pulumi/pulumi/sdk/v3/go/pulumi>"
	"os"
	"testing"
)

type Configuration struct {
	Project string
	Region
	Ephemeral bool
}

type Region struct {
	LongName  string
	ShortName string
}

//	Pulumi.test.yaml
// 	encryptionsalt: ...........
//	config:
//  	gcp:project: my-gcp-project-name
//  	pulumi-project-name:stack:
//    		project: my-gcp-project-name
//    		region:
//      		long: us-east1-b
//      		short: US
//    		ephemeral: false

// results in error:
// panic: missing required configuration variable 'pulumi-project-name:stack'; run `pulumi config` to set

func main() {
	pulumi.Run(func(ctx *pulumi.Context) error {
		var configuration Configuration
		cfg := config.New(ctx, "")
		cfg.RequireObject("stack", &configuration)
	})
}

func TestExamples(t *testing.T) {
	cwd, _ := os.Getwd()
	integration.ProgramTest(t, &integration.ProgramTestOptions{
		Quick:       true,
		SkipRefresh: true,
		Dir:         cwd,
		Config: map[string]string{
			"gcp:project":                            "my-gcp-project-name",
			"pulumi-project-name:stack.project":      "my-gcp-project-name",
			"pulumi-project-name:stack.region.short": "US",
			"pulumi-project-name:stack.region.long":  "us-east1-b",
			"pulumi-project-name:stack.ephemeral":    "true",
		},
	})
}
1
Ahh figured it out on my own. Since stack is an object, its value should be a json string in the config map:
func TestExamples(t *testing.T) {
	cwd, _ := os.Getwd()
	integration.ProgramTest(t, &integration.ProgramTestOptions{
		Quick:       true,
		SkipRefresh: true,
		Dir:         cwd,
        Config: map[string]string{
			"gcp:project": "my-gcp-project-name",
			"stack": `{
				"project": "my-gcp-project-name",
				"ephemeral": true,
				"region": {
					"short": "US",
					"long": "us-east1-b"
				}
			}`,
		},
	})
}