keyName string ) BeforeEach(func() { ec2Client = &mocks.EC2Client{} client = awsclient.Client{ EC2: ec2Client, } keyName = fmt.Sprintf("some-key-%x", rand.Int31()>>16) }) Describe("CreateKeyPair", func() { It("should call the SDK CreateKeyPair function and return the resulting private key", func() { ec2Client.CreateKeyPairCall.Returns.Output = &ec2.CreateKeyPairOutput{} ec2Client.CreateKeyPairCall.Returns.Output.KeyMaterial = aws.String("some pem block") key, err := client.CreateKeyPair(keyName) Expect(err).NotTo(HaveOccurred()) Expect(key).To(Equal("some pem block")) Expect(*ec2Client.CreateKeyPairCall.Receives.Input.KeyName).To(Equal(keyName)) }) Context("when the SDK returns an error", func() { It("should return the error", func() { ec2Client.CreateKeyPairCall.Returns.Error = errors.New("some error") _, err := client.CreateKeyPair(keyName) Expect(err).To(MatchError("some error")) }) })