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

gifted-city-99717

10/21/2020, 8:20 PM
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

lemon-agent-27707

10/21/2020, 8:36 PM
Can you share the code/repro?
g

gifted-city-99717

10/22/2020, 2:40 PM
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

lemon-agent-27707

10/22/2020, 2:57 PM
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

gifted-city-99717

10/23/2020, 3:27 PM
Yup. I have mocks in place already. The
t.Run
’s live inside a
err := pulumi.RunErr
that’s passing in mocks
4 Views