Skip to content

Commit

Permalink
Add initial tests for registry
Browse files Browse the repository at this point in the history
  • Loading branch information
eveld committed May 1, 2024
1 parent e2872cb commit f926a12
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
4 changes: 0 additions & 4 deletions pkg/config/zz_hclparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion pkg/jumppad/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
57 changes: 57 additions & 0 deletions pkg/jumppad/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)

Expand Down Expand Up @@ -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": [
Expand Down

0 comments on commit f926a12

Please sign in to comment.