I’m writing unit tests in go. In a function I’m us...
# golang
g
I’m writing unit tests in go. In a function I’m using an ApplyString to extract the string out of the StringOutput, When testing I’m seeing and I’m not sure what it means, or how to fix it
Copy code
panic: applier must have 1 input parameter assignable from interface {} [recovered]
	panic: applier must have 1 input parameter assignable from interface {}
l
Can you share the code/repro?
g
yup. i think this should capture the intent:
Copy code
// create a new network
func (resource *Network) create(ctx *pulumi.Context, name string, args *NetworkArgs) error {

	var err error

	resource.Vpc, err = args.VpcCreator(ctx, name+"-vpc", &ec2.VpcArgs{
		CidrBlock:                   pulumi.String("10.99.0.0/16"),
		EnableDnsHostnames:          pulumi.Bool(true),
		EnableClassiclinkDnsSupport: pulumi.Bool(true),
	}, pulumi.Parent(resource))
	if err != nil {
		return errors.Wrap(err, "Cannot create VPC")
	}

	_ = resource.Vpc.DefaultSecurityGroupId.ApplyString(func(s string) string {
		resource.SecurityGroupIds = append(resource.SecurityGroupIds, s)
		return s
	})

  // ...
}

// test code

t.Run("create adds default security group to network", func(t *testing.T) {

  mock := mock{}

  n := Network{}
  fakeVpc := &ec2.Vpc{}

  expects := []string{fakeVpc.DefaultSecurityGroupId}

  args := &NetworkArgs{
    NumAvailabilityZones:   2,
    VpcCreator:             mock.FakeVpcCreator(fakeVpc, nil),
  }

  err := n.create(ctx, name, args)
  assert.Equal(t, expects, n.SecurityGroupIds)

  ...
})
l
Have you taken a look at the example? You'll need to implement
NewResource
and other mocks https://github.com/pulumi/examples/blob/master/testing-unit-go/main_test.go#L16-L22
g
Yup. I have mocks in place already. The
t.Run
’s live inside a
err := pulumi.RunErr
that’s passing in mocks