From f926a122adf22e7cded520cabd76f7d379aac30e Mon Sep 17 00:00:00 2001 From: Erik Veld Date: Wed, 1 May 2024 09:37:57 +0200 Subject: [PATCH] Add initial tests for registry --- pkg/config/zz_hclparser.go | 4 --- pkg/jumppad/config.go | 7 ++++- pkg/jumppad/engine_test.go | 57 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 5 deletions(-) diff --git a/pkg/config/zz_hclparser.go b/pkg/config/zz_hclparser.go index 1c9ddb4b..10d1d767 100644 --- a/pkg/config/zz_hclparser.go +++ b/pkg/config/zz_hclparser.go @@ -34,10 +34,6 @@ func RegisterResource(name string, r types.Resource, p sdk.Provider) { func NewParser(callback hclconfig.WalkCallback, variables map[string]string, variablesFiles []string, defaultRegistry string, registryCredentials map[string]string) *hclconfig.Parser { cfg := hclconfig.DefaultOptions() - if defaultRegistry == "" { - defaultRegistry = "https://registry.jumppad.dev" - } - cfg.DefaultRegistry = defaultRegistry cfg.RegistryCredentials = registryCredentials cfg.Callback = callback diff --git a/pkg/jumppad/config.go b/pkg/jumppad/config.go index c5d88d5c..2431a625 100644 --- a/pkg/jumppad/config.go +++ b/pkg/jumppad/config.go @@ -3,7 +3,12 @@ package jumppad import "github.com/spf13/viper" func GetDefaultRegistry() string { - return viper.GetString("default_registry") + registry := viper.GetString("default_registry") + if registry == "" { + return "https://registry.jumppad.dev" + } + + return registry } func GetRegistryCredentials() map[string]string { diff --git a/pkg/jumppad/engine_test.go b/pkg/jumppad/engine_test.go index 80f07862..f7ce62bc 100644 --- a/pkg/jumppad/engine_test.go +++ b/pkg/jumppad/engine_test.go @@ -20,6 +20,7 @@ import ( "github.com/jumppad-labs/jumppad/pkg/utils" "github.com/jumppad-labs/jumppad/testutils" + "github.com/spf13/viper" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" ) @@ -39,6 +40,29 @@ func setupTestsBase(t *testing.T, returnVals map[string]error, state string) (*E log.SetOutput(l.StandardWriter()) + defaultRegistry := GetDefaultRegistry() + credentials := GetRegistryCredentials() + + pm := mocks.NewProviders(returnVals) + pm.On("GetProvider", mock.Anything) + + e := &EngineImpl{ + log: l, + providers: pm, + defaultRegistry: defaultRegistry, + registryCredentials: credentials, + } + + testutils.SetupState(t, state) + + return e, pm +} + +func setupTestsBaseWithConfig(t *testing.T, returnVals map[string]error, state string) (*EngineImpl, *mocks.Providers) { + l := logger.NewTestLogger(t) + + log.SetOutput(l.StandardWriter()) + pm := mocks.NewProviders(returnVals) pm.On("GetProvider", mock.Anything) @@ -514,6 +538,39 @@ func testAssertMethodCalled(t *testing.T, p *mocks.Providers, method string, n i require.Equal(t, n, callCount, fmt.Sprintf("expected %d calls, actual calls %d: %s", n, callCount, callString)) } +// Test that the default registry is set when set in the config +func TestDefaultRegistryWhenProvided(t *testing.T) { + // e, _ := setupTests(t, nil) +} + +// Test that the default registry is set when not set in the config +func TestDefaultRegistryFallbackWhenNotProvided(t *testing.T) { + viper.Set("default_registry", "") + + e, _ := setupTests(t, nil) + + require.Equal(t, "https://registry.jumppad.dev", e.defaultRegistry) +} + +// Test that the credentials for the registry are set when provided +func TestRegistryCredentialsWhenProvided(t *testing.T) { + viper.Set("default_registry", "https://my.registry.com") + viper.Set("credentials", []map[string]interface{}{ + { + "my.registry.com": []map[string]interface{}{ + { + "token": "mytoken", + }, + }, + }, + }) + + e, _ := setupTests(t, nil) + + require.Equal(t, "https://my.registry.com", e.defaultRegistry) + require.Equal(t, "mytoken", e.registryCredentials["my.registry.com"]) +} + var failedState = ` { "resources": [