github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/provider/ec2/iam_test.go (about)

     1  // Copyright 2021 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package ec2
     5  
     6  import (
     7  	"context"
     8  
     9  	"github.com/aws/aws-sdk-go-v2/aws"
    10  	"github.com/aws/aws-sdk-go-v2/service/iam"
    11  	"github.com/juju/errors"
    12  	jc "github.com/juju/testing/checkers"
    13  	gc "gopkg.in/check.v1"
    14  
    15  	"github.com/juju/juju/provider/ec2/internal/testing"
    16  )
    17  
    18  type IAMSuite struct {
    19  	server *testing.IAMServer
    20  }
    21  
    22  var _ = gc.Suite(&IAMSuite{})
    23  
    24  func (i *IAMSuite) SetUpTest(c *gc.C) {
    25  	server, err := testing.NewIAMServer()
    26  	c.Assert(err, jc.ErrorIsNil)
    27  	i.server = server
    28  }
    29  
    30  func (i *IAMSuite) TestEnsureControllerInstanceProfileFromScratch(c *gc.C) {
    31  	ip, _, err := ensureControllerInstanceProfile(context.Background(), i.server, "test", "AABBCC")
    32  	c.Assert(err, jc.ErrorIsNil)
    33  	c.Assert(*ip.InstanceProfileName, gc.Equals, "juju-controller-test")
    34  	c.Assert(*ip.Path, gc.Equals, "/juju/controller/AABBCC/")
    35  
    36  	roleOutput, err := i.server.GetRole(context.Background(), &iam.GetRoleInput{
    37  		RoleName: aws.String("juju-controller-test"),
    38  	})
    39  
    40  	c.Assert(err, jc.ErrorIsNil)
    41  	c.Assert(*roleOutput.Role.RoleName, gc.Equals, "juju-controller-test")
    42  }
    43  
    44  func (i *IAMSuite) TestEnsureControllerInstanceProfileAlreadyExists(c *gc.C) {
    45  	_, err := i.server.CreateInstanceProfile(context.Background(), &iam.CreateInstanceProfileInput{
    46  		InstanceProfileName: aws.String("juju-controller-test"),
    47  	})
    48  	c.Assert(err, jc.ErrorIsNil)
    49  
    50  	instanceProfile, _, err := ensureControllerInstanceProfile(context.Background(), i.server, "test", "ABCD")
    51  	c.Assert(err, jc.ErrorIsNil)
    52  	c.Assert(*instanceProfile.InstanceProfileName, gc.Equals, "juju-controller-test")
    53  }
    54  
    55  func (i *IAMSuite) TestFindInstanceProfileExists(c *gc.C) {
    56  	_, err := i.server.CreateInstanceProfile(context.Background(), &iam.CreateInstanceProfileInput{
    57  		InstanceProfileName: aws.String("juju-controller-test"),
    58  	})
    59  	c.Assert(err, jc.ErrorIsNil)
    60  
    61  	instanceProfile, err := findInstanceProfileFromName(context.Background(), i.server, "juju-controller-test")
    62  	c.Assert(err, jc.ErrorIsNil)
    63  	c.Assert(*instanceProfile.InstanceProfileName, gc.Equals, "juju-controller-test")
    64  }
    65  
    66  func (i *IAMSuite) TestFindInstanceProfileWithNotFoundError(c *gc.C) {
    67  	instanceProfile, err := findInstanceProfileFromName(context.Background(), i.server, "test")
    68  	c.Assert(instanceProfile, gc.IsNil)
    69  	c.Assert(errors.IsNotFound(err), jc.IsTrue)
    70  }
    71  
    72  func (i *IAMSuite) TestFindRoleExists(c *gc.C) {
    73  	_, err := i.server.CreateRole(context.Background(), &iam.CreateRoleInput{
    74  		RoleName:    aws.String("test-role"),
    75  		Description: aws.String("test-description"),
    76  	})
    77  	c.Assert(err, jc.ErrorIsNil)
    78  
    79  	role, err := findRoleFromName(context.Background(), i.server, "test-role")
    80  	c.Assert(err, jc.ErrorIsNil)
    81  	c.Assert(*role.RoleName, gc.Equals, "test-role")
    82  }