forked from ekristen/aws-nuke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiam-instance-profile_mock_test.go
116 lines (93 loc) · 3.02 KB
/
iam-instance-profile_mock_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package resources
import (
"context"
"testing"
"time"
"github.com/golang/mock/gomock"
"github.com/gotidy/ptr"
"github.com/stretchr/testify/assert"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/ekristen/aws-nuke/v3/mocks/mock_iamiface"
)
func Test_Mock_IAMInstanceProfile_List(t *testing.T) {
a := assert.New(t)
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockIAM := mock_iamiface.NewMockIAMAPI(ctrl)
instanceProfile := &iam.InstanceProfile{
Arn: ptr.String("arn:aws:iam::123456789012:instance-profile/profile:foobar"),
InstanceProfileName: ptr.String("profile:foobar"),
CreateDate: ptr.Time(time.Now()),
Roles: []*iam.Role{
{
Arn: ptr.String("arn:aws:iam::123456789012:role/role:foobar"),
RoleName: ptr.String("role:foobar"),
},
},
}
instanceProfile2 := &iam.InstanceProfile{
Arn: ptr.String("arn:aws:iam::123456789012:instance-profile/profile:foobar2"),
InstanceProfileName: ptr.String("profile:foobar2"),
CreateDate: ptr.Time(time.Now()),
Roles: []*iam.Role{
{
Arn: ptr.String("arn:aws:iam::123456789012:role/role:foobar2"),
RoleName: ptr.String("role:foobar2"),
},
},
}
mockIAM.EXPECT().ListInstanceProfiles(gomock.Any()).Return(&iam.ListInstanceProfilesOutput{
InstanceProfiles: []*iam.InstanceProfile{
instanceProfile,
instanceProfile2,
},
}, nil)
mockIAM.EXPECT().GetInstanceProfile(&iam.GetInstanceProfileInput{
InstanceProfileName: ptr.String("profile:foobar"),
}).Return(&iam.GetInstanceProfileOutput{
InstanceProfile: instanceProfile,
}, nil)
mockIAM.EXPECT().GetInstanceProfile(&iam.GetInstanceProfileInput{
InstanceProfileName: ptr.String("profile:foobar2"),
}).Return(nil, awserr.New("400", "InstanceProfileNotFound", nil))
lister := IAMInstanceProfileLister{
mockSvc: mockIAM,
}
resources, err := lister.List(context.TODO(), testListerOpts)
a.Nil(err)
a.Len(resources, 1)
}
func Test_Mock_IAMInstanceProfile_Remove(t *testing.T) {
a := assert.New(t)
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockIAM := mock_iamiface.NewMockIAMAPI(ctrl)
iamInstanceProfile := IAMInstanceProfile{
svc: mockIAM,
Name: ptr.String("ip:foobar"),
Path: ptr.String("/"),
}
mockIAM.EXPECT().DeleteInstanceProfile(gomock.Eq(&iam.DeleteInstanceProfileInput{
InstanceProfileName: iamInstanceProfile.Name,
})).Return(&iam.DeleteInstanceProfileOutput{}, nil)
err := iamInstanceProfile.Remove(context.TODO())
a.Nil(err)
}
func Test_Mock_IAMInstanceProfile_Properties(t *testing.T) {
a := assert.New(t)
iamInstanceProfile := IAMInstanceProfile{
Name: ptr.String("ip:foobar"),
Path: ptr.String("/"),
Tags: []*iam.Tag{
{
Key: ptr.String("foo"),
Value: ptr.String("bar"),
},
},
}
a.Equal("ip:foobar", iamInstanceProfile.Properties().Get("Name"))
a.Equal("/", iamInstanceProfile.Properties().Get("Path"))
a.Equal("bar", iamInstanceProfile.Properties().Get("tag:foo"))
a.Equal("ip:foobar", iamInstanceProfile.String())
}