Skip to content

Commit 3aa8888

Browse files
authored
bug: add ability to mock instance principal config (#460)
In order for this to pass locally this needs to be mocked, otherwise it attempts to make http calls to pull instance principal info, which doesn't exist locally.
1 parent 45e5c83 commit 3aa8888

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

cloud/config/config.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ import (
2727
"gopkg.in/yaml.v2"
2828
)
2929

30+
// set this way to allow for mocks to be used for testing
31+
var instancePrincipalProviderFunc = auth.InstancePrincipalConfigurationProvider
32+
3033
const (
3134
UseInstancePrincipal = "useInstancePrincipal"
3235
Tenancy = "tenancy"
@@ -150,7 +153,7 @@ func NewConfigurationProvider(cfg *AuthConfig) (common.ConfigurationProvider, er
150153
return nil, errors.New("auth config must not be nil")
151154
}
152155
if cfg.UseInstancePrincipals {
153-
return auth.InstancePrincipalConfigurationProvider()
156+
return instancePrincipalProviderFunc()
154157
} else {
155158
return NewConfigurationProviderWithUserPrincipal(cfg)
156159
}

cloud/config/config_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,51 @@ limitations under the License.
1717
package config
1818

1919
import (
20+
"crypto/rsa"
2021
"os"
2122
"path/filepath"
2223
"reflect"
2324
"testing"
2425

2526
"github.com/oracle/oci-go-sdk/v65/common"
27+
"github.com/oracle/oci-go-sdk/v65/common/auth"
2628
)
2729

30+
// mockConfigurationProvider implements common.ConfigurationProvider for testing
31+
type mockInstancePrincipalConfigurationProvider struct{}
32+
33+
func (m *mockInstancePrincipalConfigurationProvider) TenancyOCID() (string, error) {
34+
return "mock-tenancy", nil
35+
}
36+
37+
func (m *mockInstancePrincipalConfigurationProvider) UserOCID() (string, error) {
38+
return "mock-user", nil
39+
}
40+
41+
func (m *mockInstancePrincipalConfigurationProvider) KeyID() (string, error) {
42+
return "mock-key-id", nil
43+
}
44+
45+
func (m *mockInstancePrincipalConfigurationProvider) KeyFingerprint() (string, error) {
46+
return "mock-fingerprint", nil
47+
}
48+
49+
func (m *mockInstancePrincipalConfigurationProvider) Key() (string, error) {
50+
return "mock-key", nil
51+
}
52+
53+
func (m *mockInstancePrincipalConfigurationProvider) PrivateRSAKey() (*rsa.PrivateKey, error) {
54+
return nil, nil // Mock implementation
55+
}
56+
57+
func (m *mockInstancePrincipalConfigurationProvider) Region() (string, error) {
58+
return "us-phoenix-1", nil
59+
}
60+
61+
func (m *mockInstancePrincipalConfigurationProvider) AuthType() (common.AuthConfig, error) {
62+
return common.AuthConfig{AuthType: "instance_principal"}, nil
63+
}
64+
2865
func TestFromDir(t *testing.T) {
2966
type args struct {
3067
path string
@@ -263,6 +300,8 @@ func TestNewConfigurationProvider(t *testing.T) {
263300
name string
264301
args args
265302
wantErr bool
303+
setup func()
304+
cleanup func()
266305
}{
267306
{
268307
name: "nil config",
@@ -275,10 +314,24 @@ func TestNewConfigurationProvider(t *testing.T) {
275314
UseInstancePrincipals: true,
276315
}},
277316
wantErr: false,
317+
setup: func() {
318+
instancePrincipalProviderFunc = func() (common.ConfigurationProvider, error) {
319+
return &mockInstancePrincipalConfigurationProvider{}, nil
320+
}
321+
},
322+
cleanup: func() {
323+
instancePrincipalProviderFunc = auth.InstancePrincipalConfigurationProvider
324+
},
278325
},
279326
}
280327
for _, tt := range tests {
281328
t.Run(tt.name, func(t *testing.T) {
329+
if tt.setup != nil {
330+
tt.setup()
331+
}
332+
if tt.cleanup != nil {
333+
defer tt.cleanup()
334+
}
282335
got, err := NewConfigurationProvider(tt.args.cfg)
283336
if (err != nil) != tt.wantErr {
284337
t.Errorf("NewConfigurationProvider() error = %v, wantErr %v", err, tt.wantErr)

0 commit comments

Comments
 (0)