Skip to content

Commit efe3bfa

Browse files
committed
Added unit test coverage for instance profiles.
1 parent 8339194 commit efe3bfa

File tree

1 file changed

+212
-0
lines changed

1 file changed

+212
-0
lines changed
+212
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
package service
2+
3+
import (
4+
"github.com/databrickslabs/databricks-terraform/client/model"
5+
"net/http"
6+
"testing"
7+
)
8+
9+
func TestInstanceProfilesAPI_Create(t *testing.T) {
10+
type args struct {
11+
InstanceProfileArn string `json:"instance_profile_arn,omitempty" url:"instance_profile_arn,omitempty"`
12+
SkipValidation bool `json:"skip_validation,omitempty" url:"skip_validation,omitempty"`
13+
}
14+
15+
tests := []struct {
16+
name string
17+
response string
18+
responseStatus int
19+
args args
20+
wantErr bool
21+
}{
22+
{
23+
name: "Create test",
24+
response: "",
25+
responseStatus: http.StatusOK,
26+
args: args{
27+
InstanceProfileArn: "arn:aws:iam::1231231123123:instance-profile/my-instance-profile",
28+
SkipValidation: true,
29+
},
30+
wantErr: false,
31+
},
32+
{
33+
name: "Create faulure test",
34+
response: "",
35+
responseStatus: http.StatusBadRequest,
36+
args: args{
37+
InstanceProfileArn: "arn:aws:iam::1231231123123:instance-profile/my-instance-profile",
38+
SkipValidation: true,
39+
},
40+
wantErr: true,
41+
},
42+
}
43+
for _, tt := range tests {
44+
t.Run(tt.name, func(t *testing.T) {
45+
var input args
46+
AssertRequestWithMockServer(t, &tt.args, http.MethodPost, "/api/2.0/instance-profiles/add", &input, tt.response, tt.responseStatus, nil, tt.wantErr, func(client DBApiClient) (interface{}, error) {
47+
return nil, client.InstanceProfiles().Create(tt.args.InstanceProfileArn, tt.args.SkipValidation)
48+
})
49+
})
50+
}
51+
}
52+
53+
func TestInstanceProfilesAPI_Delete(t *testing.T) {
54+
type args struct {
55+
InstanceProfileArn string `json:"instance_profile_arn,omitempty" url:"instance_profile_arn,omitempty"`
56+
}
57+
58+
tests := []struct {
59+
name string
60+
response string
61+
responseStatus int
62+
args args
63+
wantErr bool
64+
}{
65+
{
66+
name: "Delete Test",
67+
response: "",
68+
responseStatus: http.StatusOK,
69+
args: args{
70+
InstanceProfileArn: "arn:aws:iam::1231231123123:instance-profile/my-instance-profile",
71+
},
72+
wantErr: false,
73+
},
74+
{
75+
name: "Delete failure Test",
76+
response: "",
77+
responseStatus: http.StatusBadRequest,
78+
args: args{
79+
InstanceProfileArn: "arn:aws:iam::1231231123123:instance-profile/my-instance-profile",
80+
},
81+
wantErr: true,
82+
},
83+
}
84+
for _, tt := range tests {
85+
t.Run(tt.name, func(t *testing.T) {
86+
var input args
87+
AssertRequestWithMockServer(t, &tt.args, http.MethodPost, "/api/2.0/instance-profiles/remove", &input, tt.response, tt.responseStatus, nil, tt.wantErr, func(client DBApiClient) (interface{}, error) {
88+
return nil, client.InstanceProfiles().Delete(tt.args.InstanceProfileArn)
89+
})
90+
})
91+
}
92+
}
93+
94+
func TestInstanceProfilesAPI_List(t *testing.T) {
95+
type args struct {}
96+
tests := []struct {
97+
name string
98+
response string
99+
responseStatus int
100+
args args
101+
wantUri string
102+
want []model.InstanceProfileInfo
103+
wantErr bool
104+
}{
105+
{
106+
name: "List test",
107+
response: `{
108+
"instance_profiles": [{"instance_profile_arn":"arn:aws:iam::123456789:instance-profile/datascience-role1", "is_meta_instance_profile": false},
109+
{"instance_profile_arn":"arn:aws:iam::123456789:instance-profile/datascience-role2", "is_meta_instance_profile": false},
110+
{"instance_profile_arn":"arn:aws:iam::123456789:instance-profile/datascience-role3", "is_meta_instance_profile": false}]
111+
}`,
112+
responseStatus: http.StatusOK,
113+
args: args{},
114+
wantUri: "/api/2.0/instance-profiles/list?",
115+
want: []model.InstanceProfileInfo{
116+
{
117+
InstanceProfileArn: "arn:aws:iam::123456789:instance-profile/datascience-role1",
118+
},
119+
{
120+
InstanceProfileArn: "arn:aws:iam::123456789:instance-profile/datascience-role2",
121+
},
122+
{
123+
InstanceProfileArn: "arn:aws:iam::123456789:instance-profile/datascience-role3",
124+
},
125+
},
126+
wantErr: false,
127+
},
128+
{
129+
name: "List failure test",
130+
response: ``,
131+
responseStatus: http.StatusBadRequest,
132+
args: args{},
133+
wantUri: "/api/2.0/instance-profiles/list?",
134+
want: nil,
135+
wantErr: true,
136+
},
137+
}
138+
for _, tt := range tests {
139+
t.Run(tt.name, func(t *testing.T) {
140+
var input args
141+
AssertRequestWithMockServer(t, tt.args, http.MethodGet, tt.wantUri, &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) {
142+
return client.InstanceProfiles().List()
143+
})
144+
})
145+
}
146+
}
147+
148+
149+
func TestInstanceProfilesAPI_Read(t *testing.T) {
150+
type args struct {
151+
InstanceProfileArn string `json:"instance_profile_arn,omitempty" url:"instance_profile_arn,omitempty"`
152+
}
153+
tests := []struct {
154+
name string
155+
response string
156+
responseStatus int
157+
args args
158+
wantUri string
159+
want string
160+
wantErr bool
161+
}{
162+
{
163+
name: "Read test",
164+
response: `{
165+
"instance_profiles": [{"instance_profile_arn":"arn:aws:iam::123456789:instance-profile/datascience-role1", "is_meta_instance_profile": false},
166+
{"instance_profile_arn":"arn:aws:iam::123456789:instance-profile/datascience-role2", "is_meta_instance_profile": false},
167+
{"instance_profile_arn":"arn:aws:iam::123456789:instance-profile/datascience-role3", "is_meta_instance_profile": false}]
168+
}`,
169+
responseStatus: http.StatusOK,
170+
args: args{
171+
InstanceProfileArn: "arn:aws:iam::123456789:instance-profile/datascience-role1",
172+
},
173+
wantUri: "/api/2.0/instance-profiles/list?",
174+
want: "arn:aws:iam::123456789:instance-profile/datascience-role1",
175+
wantErr: false,
176+
},
177+
{
178+
name: "Read profile not found failure test",
179+
response: `{
180+
"instance_profiles": [{"instance_profile_arn":"arn:aws:iam::123456789:instance-profile/datascience-role1", "is_meta_instance_profile": false},
181+
{"instance_profile_arn":"arn:aws:iam::123456789:instance-profile/datascience-role2", "is_meta_instance_profile": false},
182+
{"instance_profile_arn":"arn:aws:iam::123456789:instance-profile/datascience-role3", "is_meta_instance_profile": false}]
183+
}`,
184+
responseStatus: http.StatusOK,
185+
args: args{
186+
InstanceProfileArn: "arn:aws:iam::123456789:instance-profile/datascience-role4",
187+
},
188+
wantUri: "/api/2.0/instance-profiles/list?",
189+
want: "",
190+
wantErr: true,
191+
},
192+
{
193+
name: "Read list failure test",
194+
response: ``,
195+
responseStatus: http.StatusBadRequest,
196+
args: args{
197+
InstanceProfileArn: "arn:aws:iam::123456789:instance-profile/datascience-role1",
198+
},
199+
wantUri: "/api/2.0/instance-profiles/list?",
200+
want: "",
201+
wantErr: true,
202+
},
203+
}
204+
for _, tt := range tests {
205+
t.Run(tt.name, func(t *testing.T) {
206+
var input args
207+
AssertRequestWithMockServer(t, tt.args, http.MethodGet, tt.wantUri, &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) {
208+
return client.InstanceProfiles().Read(tt.args.InstanceProfileArn)
209+
})
210+
})
211+
}
212+
}

0 commit comments

Comments
 (0)