Skip to content

Commit

Permalink
Merge pull request #4 from kapetacom/mock
Browse files Browse the repository at this point in the history
Adding helper method and mock struct
  • Loading branch information
sorenmat authored Feb 15, 2024
2 parents 6527627 + 9a49235 commit 174bf10
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
95 changes: 95 additions & 0 deletions mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package config

import "github.com/kapetacom/sdk-go-config/providers"

// ConfigProviderMock is a mock implementation of providers.ConfigProvider.
// It can be used to mock the ConfigProvider interface.
// Example:
//
// configProvider := &ConfigProviderMock{
// GetResourceInfoFunc: func(resourceType, resourcePort, resourceName string) (*providers.ResourceInfo, error) {
// return &providers.ResourceInfo{
// Host: "localhost",
// Port: "8080",
// }, nil
// },
// }
// configProvider.GetResourceInfo("service", "http", "service1")
// // Output: &providers.ResourceInfo{Host: "localhost", Port: "8080"}
type ConfigProviderMock struct {
GetResourceInfoFunc func(resourceType, resourcePort, resourceName string) (*providers.ResourceInfo, error)
GetFunc func(path string) interface{}
GetBlockDefinitionFunc func() interface{}
GetBlockReferenceFunc func() string
GetInstanceForConsumerFunc func(resourceName string) (*providers.BlockInstanceDetails, error)
GetInstanceHostFunc func(instanceID string) (string, error)
GetInstanceIdFunc func() string
GetInstanceOperatorFunc func(instanceId string) (*providers.InstanceOperator, error)
GetInstancesForProviderFunc func(resourceName string) ([]*providers.BlockInstanceDetails, error)
GetOrDefaultFunc func(path string, defaultValue interface{}) interface{}
GetProviderIdFunc func() string
GetServerHostFunc func() (string, error)
GetServerPortFunc func(portType string) (string, error)
GetServiceAddressFunc func(serviceName string, portType string) (string, error)
GetSystemIdFunc func() string
}

func (c *ConfigProviderMock) Get(path string) interface{} {
return c.GetFunc(path)
}

func (c *ConfigProviderMock) GetBlockDefinition() interface{} {
return c.GetBlockDefinitionFunc()
}

func (c *ConfigProviderMock) GetBlockReference() string {
return c.GetBlockReferenceFunc()
}

func (c *ConfigProviderMock) GetInstanceForConsumer(resourceName string) (*providers.BlockInstanceDetails, error) {
return c.GetInstanceForConsumerFunc(resourceName)
}

func (c *ConfigProviderMock) GetInstanceHost(instanceID string) (string, error) {
return c.GetInstanceHostFunc(instanceID)
}

func (c *ConfigProviderMock) GetInstanceId() string {
return c.GetInstanceIdFunc()
}

func (c *ConfigProviderMock) GetInstanceOperator(instanceId string) (*providers.InstanceOperator, error) {
return c.GetInstanceOperatorFunc(instanceId)
}

func (c *ConfigProviderMock) GetInstancesForProvider(resourceName string) ([]*providers.BlockInstanceDetails, error) {
return c.GetInstancesForProviderFunc(resourceName)
}

func (c *ConfigProviderMock) GetOrDefault(path string, defaultValue interface{}) interface{} {
return c.GetOrDefaultFunc(path, defaultValue)
}

func (c *ConfigProviderMock) GetProviderId() string {
return c.GetProviderIdFunc()
}

func (c *ConfigProviderMock) GetResourceInfo(resourceType string, portType string, resourceName string) (*providers.ResourceInfo, error) {
return c.GetResourceInfoFunc(resourceType, portType, resourceName)
}

func (c *ConfigProviderMock) GetServerHost() (string, error) {
return c.GetServerHostFunc()
}

func (c *ConfigProviderMock) GetServerPort(portType string) (string, error) {
return c.GetServerPortFunc(portType)
}

func (c *ConfigProviderMock) GetServiceAddress(serviceName string, portType string) (string, error) {
return c.GetServiceAddressFunc(serviceName, portType)
}

func (c *ConfigProviderMock) GetSystemId() string {
return c.GetSystemIdFunc()
}
8 changes: 8 additions & 0 deletions provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ const (
defaultInstanceID = ""
)

// GetProvider returns the configured provider or panics if it's not initialized
func GetProvider() providers.ConfigProvider {
if CONFIG.provider == nil {
panic("Configuration not yet initialized, call Init('path to kapeta.yml') first")
}
return CONFIG.provider
}

func getEnvOrDefault(envVarName, defaultValue string) string {
if value, exists := os.LookupEnv(envVarName); exists {
return value
Expand Down
22 changes: 22 additions & 0 deletions provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"os"
"strings"
"testing"

"github.com/kapetacom/sdk-go-config/providers"
)

func hostAndFromURL(url string) (string, string) {
Expand Down Expand Up @@ -125,3 +127,23 @@ func TestGetOrDefault(t *testing.T) {
})
}
}

func TestGetProvider(t *testing.T) {
t.Run("provider is nil", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
CONFIG.provider = nil
_ = GetProvider()
})

t.Run("provider is not nil", func(t *testing.T) {
CONFIG.provider = &providers.KubernetesConfigProvider{}
got := GetProvider()
if got == nil {
t.Errorf("GetProvider() = nil, want not nil")
}
})
}

0 comments on commit 174bf10

Please sign in to comment.