Skip to content

Commit 8303efa

Browse files
committed
Updated tests
Updated test and removed some unnecessary things
1 parent ad7adaa commit 8303efa

File tree

2 files changed

+71
-24
lines changed

2 files changed

+71
-24
lines changed

apps/managedidentity/managedidentity.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func WithHTTPClient(httpClient ops.HTTPClient) ClientOption {
157157
//
158158
// Options: [WithHTTPClient]
159159
func New(id ID, options ...ClientOption) (Client, error) {
160-
source, err := GetSource(id)
160+
source, err := GetSource()
161161
if err != nil {
162162
return Client{}, err
163163
}
@@ -210,7 +210,7 @@ func New(id ID, options ...ClientOption) (Client, error) {
210210
}
211211

212212
// GetSource detects and returns the managed identity source available on the environment.
213-
func GetSource(id ID) (Source, error) {
213+
func GetSource() (Source, error) {
214214
identityEndpoint := os.Getenv(identityEndpointEnvVar)
215215
identityHeader := os.Getenv(identityHeaderEnvVar)
216216
identityServerThumbprint := os.Getenv(identityServerThumbprintEnvVar)

apps/managedidentity/managedidentity_test.go

Lines changed: 69 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -143,38 +143,77 @@ func setCustomAzureArcFilePath(t *testing.T, path string) {
143143
}
144144

145145
func TestGetSource(t *testing.T) {
146-
// todo update as required
147146
testCases := []struct {
148147
name string
149148
source Source
150-
endpoint string
151149
expectedSource Source
152-
miType ID
153150
}{
154-
{name: "testAzureArcSystemAssigned", source: AzureArc, endpoint: imdsDefaultEndpoint, expectedSource: AzureArc, miType: SystemAssigned()},
155-
{name: "testAzureArcUserClientAssigned", source: AzureArc, endpoint: imdsDefaultEndpoint, expectedSource: AzureArc, miType: UserAssignedClientID("clientId")},
156-
{name: "testAzureArcUserResourceAssigned", source: AzureArc, endpoint: imdsDefaultEndpoint, expectedSource: AzureArc, miType: UserAssignedResourceID("resourceId")},
157-
{name: "testAzureArcUserObjectAssigned", source: AzureArc, endpoint: imdsDefaultEndpoint, expectedSource: AzureArc, miType: UserAssignedObjectID("objectId")},
158-
{name: "testDefaultToImds", source: DefaultToIMDS, endpoint: imdsDefaultEndpoint, expectedSource: DefaultToIMDS, miType: SystemAssigned()},
159-
{name: "testDefaultToImdsClientAssigned", source: DefaultToIMDS, endpoint: imdsDefaultEndpoint, expectedSource: DefaultToIMDS, miType: UserAssignedClientID("clientId")},
160-
{name: "testDefaultToImdsResourceAssigned", source: DefaultToIMDS, endpoint: imdsDefaultEndpoint, expectedSource: DefaultToIMDS, miType: UserAssignedResourceID("resourceId")},
161-
{name: "testDefaultToImdsObjectAssigned", source: DefaultToIMDS, endpoint: imdsDefaultEndpoint, expectedSource: DefaultToIMDS, miType: UserAssignedObjectID("objectId")},
162-
{name: "testDefaultToImdsEmptyEndpoint", source: DefaultToIMDS, endpoint: "", expectedSource: DefaultToIMDS, miType: SystemAssigned()},
163-
{name: "testDefaultToImdsLinux", source: DefaultToIMDS, endpoint: imdsDefaultEndpoint, expectedSource: DefaultToIMDS, miType: SystemAssigned()},
164-
{name: "testDefaultToImdsEmptyEndpointLinux", source: DefaultToIMDS, endpoint: "", expectedSource: DefaultToIMDS, miType: SystemAssigned()},
151+
{
152+
name: "testAzureArcSystemAssigned",
153+
source: AzureArc,
154+
expectedSource: AzureArc,
155+
},
156+
{
157+
name: "testAzureArcUserClientAssigned",
158+
source: AzureArc,
159+
expectedSource: AzureArc,
160+
},
161+
{
162+
name: "testAzureArcUserResourceAssigned",
163+
source: AzureArc,
164+
expectedSource: AzureArc,
165+
},
166+
{
167+
name: "testAzureArcUserObjectAssigned",
168+
source: AzureArc,
169+
expectedSource: AzureArc,
170+
},
171+
{
172+
name: "testDefaultToImds",
173+
source: DefaultToIMDS,
174+
expectedSource: DefaultToIMDS,
175+
},
176+
{
177+
name: "testDefaultToImdsClientAssigned",
178+
source: DefaultToIMDS,
179+
expectedSource: DefaultToIMDS,
180+
},
181+
{
182+
name: "testDefaultToImdsResourceAssigned",
183+
source: DefaultToIMDS,
184+
expectedSource: DefaultToIMDS,
185+
},
186+
{
187+
name: "testDefaultToImdsObjectAssigned",
188+
source: DefaultToIMDS,
189+
expectedSource: DefaultToIMDS,
190+
},
191+
{
192+
name: "testDefaultToImdsEmptyEndpoint",
193+
source: DefaultToIMDS,
194+
expectedSource: DefaultToIMDS,
195+
},
196+
{
197+
name: "testDefaultToImdsLinux",
198+
source: DefaultToIMDS,
199+
expectedSource: DefaultToIMDS,
200+
},
201+
{
202+
name: "testDefaultToImdsEmptyEndpointLinux",
203+
source: DefaultToIMDS,
204+
expectedSource: DefaultToIMDS,
205+
},
165206
}
166-
167207
for _, testCase := range testCases {
168208
t.Run(string(testCase.source), func(t *testing.T) {
169209
unsetEnvVars(t)
170210
setEnvVars(t, testCase.source)
171211
setCustomAzureArcFilePath(t, fakeAzureArcFilePath)
172212

173-
actualSource, err := GetSource(testCase.miType)
213+
actualSource, err := GetSource()
174214
if err != nil {
175215
t.Fatalf("error while getting source: %s", err.Error())
176216
}
177-
178217
if actualSource != testCase.expectedSource {
179218
t.Errorf(errorExpectedButGot, testCase.expectedSource, actualSource)
180219
}
@@ -197,7 +236,7 @@ func TestAzureArcReturnsWhenHimdsFound(t *testing.T) {
197236
}
198237
})
199238

200-
actualSource, err := GetSource(SystemAssigned())
239+
actualSource, err := GetSource()
201240
if err != nil {
202241
t.Fatalf("error while getting source: %s", err.Error())
203242
}
@@ -313,7 +352,6 @@ func TestAzureArcAcquireTokenReturnsToken(t *testing.T) {
313352
expectedError string
314353
platform string
315354
createMockFile bool
316-
context context.Context
317355
shouldFail bool
318356
}
319357
testCases := []struct {
@@ -343,8 +381,7 @@ func TestAzureArcAcquireTokenReturnsToken(t *testing.T) {
343381
statusCode: http.StatusUnauthorized,
344382
headers: map[string]string{wwwAuthenticateHeaderName: "Basic realm=/path/to/secret.key"},
345383
expectedError: "platform not supported, expected linux or windows",
346-
platform: "platformNotSupported",
347-
context: context.Background(),
384+
platform: "freebsd",
348385
shouldFail: true,
349386
}},
350387
{resource: resource, miType: SystemAssigned(), apiVersion: azureArcAPIVersion, request: ArcRequest{
@@ -388,6 +425,16 @@ func TestAzureArcAcquireTokenReturnsToken(t *testing.T) {
388425
createMockFile: true,
389426
shouldFail: true,
390427
}},
428+
// {resource: resourceDefaultSuffix, miType: UserAssignedClientID("FakeClientID"), apiVersion: azureArcAPIVersion,
429+
// request: ArcRequest{
430+
// name: "success",
431+
// statusCode: http.StatusUnauthorized,
432+
// headers: map[string]string{wwwAuthenticateHeaderName: basicRealm + filepath.Join(testCaseFilePath, secretKey)},
433+
// expectedError: "",
434+
// platform: runtime.GOOS,
435+
// createMockFile: true,
436+
// shouldFail: false,
437+
// }},
391438
{resource: resourceDefaultSuffix, miType: SystemAssigned(), apiVersion: azureArcAPIVersion,
392439
request: ArcRequest{
393440
name: "success",
@@ -412,7 +459,7 @@ func TestAzureArcAcquireTokenReturnsToken(t *testing.T) {
412459
mockClient := mock.Client{}
413460

414461
mockFilePath := filepath.Join(testCaseFilePath, secretKey)
415-
if testCase.request.platform != "platformNotSupported" {
462+
if testCase.request.platform != "freebsd" {
416463
setCustomAzureArcPlatformPath(t, testCaseFilePath)
417464
}
418465
if testCase.request.name == "Invalid secret file size" {

0 commit comments

Comments
 (0)