@@ -17,20 +17,35 @@ type mockDetector struct {
1717 provider string
1818 publicIP string
1919 privateIP string
20+ region string
2021 provErr error
2122 publicErr error
2223 privateErr error
24+ regionErr error
2325 vmEnv string
2426 vmEnvErr error
2527 instanceID string
2628 instanceIDErr error
2729 delay time.Duration
2830}
2931
32+ type legacyDetector struct {
33+ name string
34+ provider string
35+ publicIP string
36+ privateIP string
37+ vmEnv string
38+ instanceID string
39+ }
40+
3041func (m * mockDetector ) Name () string {
3142 return m .name
3243}
3344
45+ func (m * legacyDetector ) Name () string {
46+ return m .name
47+ }
48+
3449func (m * mockDetector ) Provider (ctx context.Context ) (string , error ) {
3550 if m .delay > 0 {
3651 select {
@@ -42,22 +57,46 @@ func (m *mockDetector) Provider(ctx context.Context) (string, error) {
4257 return m .provider , m .provErr
4358}
4459
60+ func (m * legacyDetector ) Provider (ctx context.Context ) (string , error ) {
61+ return m .provider , nil
62+ }
63+
4564func (m * mockDetector ) PublicIPv4 (ctx context.Context ) (string , error ) {
4665 return m .publicIP , m .publicErr
4766}
4867
68+ func (m * legacyDetector ) PublicIPv4 (ctx context.Context ) (string , error ) {
69+ return m .publicIP , nil
70+ }
71+
4972func (m * mockDetector ) PrivateIPv4 (ctx context.Context ) (string , error ) {
5073 return m .privateIP , m .privateErr
5174}
5275
76+ func (m * legacyDetector ) PrivateIPv4 (ctx context.Context ) (string , error ) {
77+ return m .privateIP , nil
78+ }
79+
80+ func (m * mockDetector ) Region (ctx context.Context ) (string , error ) {
81+ return m .region , m .regionErr
82+ }
83+
5384func (m * mockDetector ) VMEnvironment (ctx context.Context ) (string , error ) {
5485 return m .vmEnv , m .vmEnvErr
5586}
5687
88+ func (m * legacyDetector ) VMEnvironment (ctx context.Context ) (string , error ) {
89+ return m .vmEnv , nil
90+ }
91+
5792func (m * mockDetector ) InstanceID (ctx context.Context ) (string , error ) {
5893 return m .instanceID , m .instanceIDErr
5994}
6095
96+ func (m * legacyDetector ) InstanceID (ctx context.Context ) (string , error ) {
97+ return m .instanceID , nil
98+ }
99+
61100// withTemporaryDetectors runs the provided function with a temporary replacement for All
62101// and restores the original value when done
63102func withTemporaryDetectors (tempDetectors []providers.Detector , fn func ()) {
@@ -76,6 +115,56 @@ func TestDetect_Success(t *testing.T) {
76115 provider : "aws" ,
77116 publicIP : "1.2.3.4" ,
78117 privateIP : "10.0.1.100" ,
118+ region : "us-east-1" ,
119+ vmEnv : "AWS" ,
120+ instanceID : "i-abc" ,
121+ },
122+ }
123+
124+ withTemporaryDetectors (testDetectors , func () {
125+ info , err := Detect (context .Background ())
126+ assert .NoError (t , err )
127+ assert .Equal (t , "aws" , info .Provider )
128+ assert .Equal (t , "1.2.3.4" , info .PublicIP )
129+ assert .Equal (t , "10.0.1.100" , info .PrivateIP )
130+ assert .Equal (t , "us-east-1" , info .Region )
131+ assert .Equal (t , "AWS" , info .VMEnvironment )
132+ assert .Equal (t , "i-abc" , info .InstanceID )
133+ })
134+ }
135+
136+ func TestDetect_SuccessWithoutRegionDetector (t * testing.T ) {
137+ testDetectors := []providers.Detector {
138+ & legacyDetector {
139+ name : "legacy" ,
140+ provider : "legacy" ,
141+ publicIP : "1.2.3.4" ,
142+ privateIP : "10.0.1.100" ,
143+ vmEnv : "LEGACY" ,
144+ instanceID : "i-legacy" ,
145+ },
146+ }
147+
148+ withTemporaryDetectors (testDetectors , func () {
149+ info , err := Detect (context .Background ())
150+ assert .NoError (t , err )
151+ assert .Equal (t , "legacy" , info .Provider )
152+ assert .Equal (t , "1.2.3.4" , info .PublicIP )
153+ assert .Equal (t , "10.0.1.100" , info .PrivateIP )
154+ assert .Empty (t , info .Region )
155+ assert .Equal (t , "LEGACY" , info .VMEnvironment )
156+ assert .Equal (t , "i-legacy" , info .InstanceID )
157+ })
158+ }
159+
160+ func TestDetect_RegionError (t * testing.T ) {
161+ testDetectors := []providers.Detector {
162+ & mockDetector {
163+ name : "aws" ,
164+ provider : "aws" ,
165+ publicIP : "1.2.3.4" ,
166+ privateIP : "10.0.1.100" ,
167+ regionErr : errors .New ("region error" ),
79168 vmEnv : "AWS" ,
80169 instanceID : "i-abc" ,
81170 },
@@ -87,6 +176,7 @@ func TestDetect_Success(t *testing.T) {
87176 assert .Equal (t , "aws" , info .Provider )
88177 assert .Equal (t , "1.2.3.4" , info .PublicIP )
89178 assert .Equal (t , "10.0.1.100" , info .PrivateIP )
179+ assert .Empty (t , info .Region )
90180 assert .Equal (t , "AWS" , info .VMEnvironment )
91181 assert .Equal (t , "i-abc" , info .InstanceID )
92182 })
0 commit comments