https://pulumi.com logo
#golang
Title
# golang
f

fierce-ability-58936

07/12/2023, 10:24 AM
Hi all, I can't find any example of how to mock component resources for unit testing. Specifically, in my program I use https://www.pulumi.com/registry/packages/awsx/api-docs/ecs/fargatetaskdefinition/ to use with ecs.Service as
Copy code
_, err = ecs.NewService(ctx, localName(ctx, "scheduler"), &ecs.ServiceArgs{
		Cluster:        cluster.Arn,
		TaskDefinition: schedulerTaskDefinition.TaskDefinition.Arn(),
		DesiredCount:   <http://pulumi.Int|pulumi.Int>(1),
	}, pulumi.Parent(cluster))
	if err != nil {
		return nil, err
	}
Which works fine with
pulumi up
but with tests if panics with
Copy code
goroutine 70 [running]:
<http://github.com/pulumi/pulumi-aws/sdk/v5/go/aws/ecs.TaskDefinitionOutput.Arn.func1(0x1?)|github.com/pulumi/pulumi-aws/sdk/v5/go/aws/ecs.TaskDefinitionOutput.Arn.func1(0x1?)>
	/home/ekini/go/pkg/mod/github.com/pulumi/pulumi-aws/sdk/v5@v5.33.0/go/aws/ecs/taskDefinition.go:732
reflect.Value.call({0x179b000?, 0x1e69a00?, 0x13?}, {0x1deb5e3, 0x4}, {0xc000707e90, 0x1, 0x2?})
	/usr/lib/go/src/reflect/value.go:586 +0xb0b
reflect.Value.Call({0x179b000?, 0x1e69a00?, 0xc0002c6701?}, {0xc000278690?, 0xc0002786c0?, 0xbf0b1c?})
	/usr/lib/go/src/reflect/value.go:370 +0xbc
<http://github.com/pulumi/pulumi/sdk/v3/go/pulumi.(*applier).Call(0xc000268c80|github.com/pulumi/pulumi/sdk/v3/go/pulumi.(*applier).Call(0xc000268c80>, {0x1fed698?, 0xc000078170?}, {0x1c09c40?, 0x0?, 0x19?})
	/home/ekini/go/pkg/mod/github.com/pulumi/pulumi/sdk/v3@v3.60.1/go/pulumi/types.go:494 +0x215
<http://github.com/pulumi/pulumi/sdk/v3/go/pulumi.(*OutputState).applyTWithApplier.func1()|github.com/pulumi/pulumi/sdk/v3/go/pulumi.(*OutputState).applyTWithApplier.func1()>
	/home/ekini/go/pkg/mod/github.com/pulumi/pulumi/sdk/v3@v3.60.1/go/pulumi/types.go:606 +0x198
created by <http://github.com/pulumi/pulumi/sdk/v3/go/pulumi.(*OutputState).applyTWithApplier|github.com/pulumi/pulumi/sdk/v3/go/pulumi.(*OutputState).applyTWithApplier>
	/home/ekini/go/pkg/mod/github.com/pulumi/pulumi/sdk/v3@v3.60.1/go/pulumi/types.go:593 +0x325
FAIL	<http://github.com/xxx/infra/xxx|github.com/xxx/infra/xxx>	0.019s
FAIL
[[Command exited with 1]]
e

echoing-dinner-19531

07/13/2023, 8:44 AM
https://stackoverflow.com/questions/76009185/how-to-mock-pulumi-resources-in-go-unit-tests might help You should just be able to override NewResource in the mocks to return data expected for your program.
f

fierce-ability-58936

07/13/2023, 9:11 AM
I've tried! But in all those examples those parameters are simple types, but here it's ecs.TaskDefinition I've tried this in many variations:
Copy code
func (mocks) NewResource(args pulumi.MockResourceArgs) (string, resource.PropertyMap, error) {
> outputs := args.Inputs.Mappable()
> if args.TypeToken == "awsx:ecs:FargateTaskDefinition" {
> > outputs["taskDefinition"] = (&ecs.TaskDefinition{Arn: pulumi.String("fake").ToStringOutput()}).ToTaskDefinitionOutput()

> }
> outputs["arn"] = "arn:aws:ecs:ap-southeast-6:123451234512:fake-arn/fakeresource"
> return args.Name + "_id", resource.NewPropertyMapFromMap(outputs), nil
}
and other things constructing map of parameters, thinking it'll deserialise it into a proper object, which didn't work either