@@ -5,6 +5,7 @@ package managedidentity
55import (
66 "context"
77 "encoding/json"
8+ "fmt"
89 "net/http"
910 "net/url"
1011 "os"
@@ -181,6 +182,41 @@ func TestGetSource(t *testing.T) {
181182 }
182183}
183184
185+ func TestAzureArcReturnsWhenHimdsFound (t * testing.T ) {
186+ if runtime .GOOS == "darwin" {
187+ t .Skip ("Skipping test on macOS as HIMDS is not supported" )
188+ }
189+
190+ unsetEnvVars (t )
191+ // Get system dependent mock file path
192+ var mockFilePath string
193+ if runtime .GOOS == "windows" {
194+ mockFilePath = filepath .Join (os .TempDir (), "himds.exe" )
195+ } else {
196+ mockFilePath = filepath .Join ("/tmp" , "himds" )
197+ }
198+ setCustomAzureArcFilePath (t , mockFilePath )
199+
200+ // Create the mock himds file
201+ createMockFile (t , mockFilePath , 1024 )
202+
203+ // Ensure file is deleted after test
204+ t .Cleanup (func () {
205+ if err := os .Remove (mockFilePath ); err != nil {
206+ t .Fatalf ("failed to delete mock file: %s" , err )
207+ }
208+ })
209+
210+ actualSource , err := GetSource (SystemAssigned ())
211+ if err != nil {
212+ t .Fatalf ("error while getting source: %s" , err .Error ())
213+ }
214+
215+ if actualSource != AzureArc {
216+ t .Errorf (errorExpectedButGot , AzureArc , actualSource )
217+ }
218+ }
219+
184220func TestIMDSAcquireTokenReturnsTokenSuccess (t * testing.T ) {
185221 testCases := []struct {
186222 resource string
@@ -619,29 +655,13 @@ func TestCreatingIMDSClient(t *testing.T) {
619655}
620656
621657func TestAzureArcUserAssignedFailure (t * testing.T ) {
622- tests := []struct {
623- name string
624- id ID
625- }{
626- {
627- name : "Client ID" ,
628- id : UserAssignedClientID ("test-client-id" ),
629- },
630- {
631- name : "Resource ID" ,
632- id : UserAssignedResourceID ("test-resource-id" ),
633- },
634- {
635- name : "Object ID" ,
636- id : UserAssignedObjectID ("test-object-id" ),
637- },
638- }
639-
640- for _ , tt := range tests {
641- t .Run (tt .name , func (t * testing.T ) {
658+ for _ , id := range []ID {UserAssignedClientID ("clientID" ),
659+ UserAssignedResourceID ("resourceID" ),
660+ UserAssignedObjectID ("objectID" )} {
661+ t .Run (fmt .Sprintf ("%T" , id ), func (t * testing.T ) {
642662 unsetEnvVars (t )
643663 setEnvVars (t , AzureArc )
644- _ , err := New (tt . id )
664+ _ , err := New (id )
645665 if err == nil {
646666 t .Fatal ("client New() should return a error but did not." )
647667 }
@@ -703,53 +723,3 @@ func TestValidateAzureArcEnvironment(t *testing.T) {
703723 })
704724 }
705725}
706-
707- func TestAzureArcReturnsWhenHimdsFound (t * testing.T ) {
708- if runtime .GOOS == "darwin" {
709- t .Skip ("Skipping test on macOS as HIMDS is not supported" )
710- }
711-
712- testCases := []struct {
713- name string
714- source Source
715- endpoint string
716- expectedSource Source
717- miType ID
718- }{
719- {name : "testAzureArcSystemAssigned" , source : AzureArc , endpoint : "imdsDefaultEndpoint" , expectedSource : AzureArc , miType : SystemAssigned ()},
720- }
721-
722- for _ , testCase := range testCases {
723- t .Run (string (testCase .source ), func (t * testing.T ) {
724- unsetEnvVars (t )
725-
726- // Get system dependent mock file path
727- var mockFilePath string
728- if runtime .GOOS == "windows" {
729- mockFilePath = filepath .Join (os .TempDir (), "himds.exe" )
730- } else {
731- mockFilePath = filepath .Join ("/tmp" , "himds" )
732- }
733- setCustomAzureArcFilePath (t , mockFilePath )
734-
735- // Create the mock himds file
736- createMockFile (t , mockFilePath , 1024 )
737-
738- // Ensure file is deleted after test
739- t .Cleanup (func () {
740- if err := os .Remove (mockFilePath ); err != nil {
741- t .Fatalf ("failed to delete mock file: %v" , err )
742- }
743- })
744-
745- actualSource , err := GetSource (testCase .miType )
746- if err != nil {
747- t .Fatalf ("error while getting source: %s" , err .Error ())
748- }
749-
750- if actualSource != testCase .expectedSource {
751- t .Errorf (errorExpectedButGot , testCase .expectedSource , actualSource )
752- }
753- })
754- }
755- }
0 commit comments