Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

INT - add unit tests #51

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions pkg/discovery/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,27 @@ import (
"net/url"
"strings"

"github.com/Axway/agent-sdk/pkg/cmd/properties"
corecfg "github.com/Axway/agent-sdk/pkg/config"
"github.com/Axway/agent-sdk/pkg/util/log"
)

type props interface {
AddStringProperty(name string, defaultVal string, description string)
AddStringSliceProperty(name string, defaultVal []string, description string)
AddIntProperty(name string, defaultVal int, description string)
AddBoolProperty(name string, defaultVal bool, description string)
StringPropertyValue(name string) string
StringSlicePropertyValue(name string) []string
IntPropertyValue(name string) int
BoolPropertyValue(name string) bool
}

const (
cfgKongAdminUrl = "kong.admin.url"
cfgKongAdminAPIKey = "kong.admin.auth.apiKey.value"
cfgKongAdminAPIKeyHeader = "kong.admin.auth.apiKey.header"
cfgKongAdminUsername = "kong.admin.auth.basicauth.username"
cfgKongAdminPassword = "kong.admin.auth.basicauth.password"
cfgKongAdminBasicUsername = "kong.admin.auth.basicauth.username"
cfgKongAdminBasicPassword = "kong.admin.auth.basicauth.password"
cfgKongProxyHost = "kong.proxy.host"
cfgKongProxyPortHttp = "kong.proxy.ports.http"
cfgKongProxyPortHttpDisabled = "kong.proxy.ports.http.disabled"
Expand All @@ -25,14 +35,15 @@ const (
cfgKongSpecURLPaths = "kong.spec.urlPaths"
cfgKongSpecLocalPath = "kong.spec.localPath"
cfgKongSpecFilter = "kong.spec.filter"
cfgKongSpecDevPortal = "kong.spec.devPortalEnabled"
)

func AddKongProperties(rootProps properties.Properties) {
func AddKongProperties(rootProps props) {
rootProps.AddStringProperty(cfgKongAdminUrl, "", "The Admin API url")
rootProps.AddStringProperty(cfgKongAdminAPIKey, "", "API Key value to authenticate with Kong Gateway")
rootProps.AddStringProperty(cfgKongAdminAPIKeyHeader, "", "API Key header to authenticate with Kong Gateway")
rootProps.AddStringProperty(cfgKongAdminUsername, "", "Username for basic auth to authenticate with Kong Admin API")
rootProps.AddStringProperty(cfgKongAdminPassword, "", "Password for basic auth to authenticate with Kong Admin API")
rootProps.AddStringProperty(cfgKongAdminBasicUsername, "", "Username for basic auth to authenticate with Kong Admin API")
rootProps.AddStringProperty(cfgKongAdminBasicPassword, "", "Password for basic auth to authenticate with Kong Admin API")
rootProps.AddStringProperty(cfgKongProxyHost, "", "The Kong proxy endpoint")
rootProps.AddIntProperty(cfgKongProxyPortHttp, 80, "The Kong proxy http port")
rootProps.AddBoolProperty(cfgKongProxyPortHttpDisabled, false, "Set to true to disable adding an http endpoint to discovered routes")
Expand All @@ -42,6 +53,7 @@ func AddKongProperties(rootProps properties.Properties) {
rootProps.AddStringSliceProperty(cfgKongSpecURLPaths, []string{}, "URL paths that the agent will look in for spec files")
rootProps.AddStringProperty(cfgKongSpecLocalPath, "", "Local paths where the agent will look for spec files")
rootProps.AddStringProperty(cfgKongSpecFilter, "", "SDK Filter format. Empty means filters are ignored.")
rootProps.AddBoolProperty(cfgKongSpecDevPortal, false, "Set to true to enable gathering specs from teh Kong's dev portal.")
}

// AgentConfig - represents the config for agent
Expand Down Expand Up @@ -181,7 +193,7 @@ func invalidCredentialConfig(c *KongGatewayConfig) bool {
return false
}

func ParseProperties(rootProps properties.Properties) *KongGatewayConfig {
func ParseProperties(rootProps props) *KongGatewayConfig {
// Parse the config from bound properties and setup gateway config
httpPortConf := KongPortSettingsConfig{
Disable: rootProps.BoolPropertyValue(cfgKongProxyPortHttpDisabled),
Expand All @@ -208,8 +220,8 @@ func ParseProperties(rootProps properties.Properties) *KongGatewayConfig {
Header: rootProps.StringPropertyValue(cfgKongAdminAPIKeyHeader),
},
BasicAuth: KongAdminBasicAuthConfig{
Username: rootProps.StringPropertyValue(cfgKongAdminUsername),
Password: rootProps.StringPropertyValue(cfgKongAdminPassword),
Username: rootProps.StringPropertyValue(cfgKongAdminBasicUsername),
Password: rootProps.StringPropertyValue(cfgKongAdminBasicPassword),
},
},
},
Expand All @@ -222,9 +234,10 @@ func ParseProperties(rootProps properties.Properties) *KongGatewayConfig {
BasePath: rootProps.StringPropertyValue(cfgKongProxyBasePath),
},
Spec: KongSpecConfig{
URLPaths: rootProps.StringSlicePropertyValue(cfgKongSpecURLPaths),
LocalPath: rootProps.StringPropertyValue(cfgKongSpecLocalPath),
Filter: rootProps.StringPropertyValue(cfgKongSpecFilter),
DevPortalEnabled: rootProps.BoolPropertyValue(cfgKongSpecDevPortal),
URLPaths: rootProps.StringSlicePropertyValue(cfgKongSpecURLPaths),
LocalPath: rootProps.StringPropertyValue(cfgKongSpecLocalPath),
Filter: rootProps.StringPropertyValue(cfgKongSpecFilter),
},
}
}
134 changes: 134 additions & 0 deletions pkg/discovery/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,137 @@ func TestKongGatewayCfg(t *testing.T) {
assert.Equal(t, nil, err)

}

type propData struct {
pType string
desc string
val interface{}
}

type fakeProps struct {
props map[string]propData
}

func (f *fakeProps) AddStringProperty(name string, defaultVal string, description string) {
f.props[name] = propData{"string", description, defaultVal}
}

func (f *fakeProps) AddStringSliceProperty(name string, defaultVal []string, description string) {
f.props[name] = propData{"string", description, defaultVal}
}

func (f *fakeProps) AddIntProperty(name string, defaultVal int, description string) {
f.props[name] = propData{"int", description, defaultVal}
}

func (f *fakeProps) AddBoolProperty(name string, defaultVal bool, description string) {
f.props[name] = propData{"bool", description, defaultVal}
}

func (f *fakeProps) StringPropertyValue(name string) string {
if prop, ok := f.props[name]; ok {
return prop.val.(string)
}
return ""
}

func (f *fakeProps) StringSlicePropertyValue(name string) []string {
if prop, ok := f.props[name]; ok {
return prop.val.([]string)
}
return []string{}
}

func (f *fakeProps) IntPropertyValue(name string) int {
if prop, ok := f.props[name]; ok {
return prop.val.(int)
}
return 0
}

func (f *fakeProps) BoolPropertyValue(name string) bool {
if prop, ok := f.props[name]; ok {
return prop.val.(bool)
}
return false
}

func TestKongProperties(t *testing.T) {
newProps := &fakeProps{props: map[string]propData{}}

// validate add props
AddKongProperties(newProps)
assert.Contains(t, newProps.props, cfgKongAdminUrl)
assert.Contains(t, newProps.props, cfgKongAdminAPIKey)
assert.Contains(t, newProps.props, cfgKongAdminAPIKeyHeader)
assert.Contains(t, newProps.props, cfgKongAdminBasicUsername)
assert.Contains(t, newProps.props, cfgKongAdminBasicPassword)
assert.Contains(t, newProps.props, cfgKongProxyHost)
assert.Contains(t, newProps.props, cfgKongProxyPortHttp)
assert.Contains(t, newProps.props, cfgKongProxyPortHttpDisabled)
assert.Contains(t, newProps.props, cfgKongProxyPortHttps)
assert.Contains(t, newProps.props, cfgKongProxyPortHttpsDisabled)
assert.Contains(t, newProps.props, cfgKongProxyBasePath)
assert.Contains(t, newProps.props, cfgKongSpecURLPaths)
assert.Contains(t, newProps.props, cfgKongSpecLocalPath)
assert.Contains(t, newProps.props, cfgKongSpecFilter)
assert.Contains(t, newProps.props, cfgKongSpecDevPortal)

// validate defaults
cfg := ParseProperties(newProps)
assert.Equal(t, "", cfg.Admin.Url)
assert.Equal(t, "", cfg.Admin.Auth.APIKey.Value)
assert.Equal(t, "", cfg.Admin.Auth.APIKey.Header)
assert.Equal(t, "", cfg.Admin.Auth.BasicAuth.Username)
assert.Equal(t, "", cfg.Admin.Auth.BasicAuth.Password)
assert.Equal(t, "", cfg.Proxy.Host)
assert.Equal(t, 80, cfg.Proxy.Ports.HTTP.Value)
assert.Equal(t, 443, cfg.Proxy.Ports.HTTPS.Value)
assert.Equal(t, false, cfg.Proxy.Ports.HTTP.Disable)
assert.Equal(t, false, cfg.Proxy.Ports.HTTPS.Disable)
assert.Equal(t, "", cfg.Proxy.BasePath)
assert.Equal(t, []string{}, cfg.Spec.URLPaths)
assert.Equal(t, "", cfg.Spec.LocalPath)
assert.Equal(t, "", cfg.Spec.Filter)
assert.Equal(t, false, cfg.Spec.DevPortalEnabled)

// validate changed values
newProps.props[cfgKongAdminUrl] = propData{"string", "", "http://host:port/path"}
newProps.props[cfgKongAdminAPIKey] = propData{"string", "", "apikey"}
newProps.props[cfgKongAdminAPIKeyHeader] = propData{"string", "", "header"}
newProps.props[cfgKongAdminBasicUsername] = propData{"string", "", "username"}
newProps.props[cfgKongAdminBasicPassword] = propData{"string", "", "password"}
newProps.props[cfgKongProxyHost] = propData{"string", "", "proxyhost"}
newProps.props[cfgKongProxyPortHttp] = propData{"int", "", 8080}
newProps.props[cfgKongProxyPortHttps] = propData{"int", "", 8443}
newProps.props[cfgKongProxyHost] = propData{"string", "", "proxyhost"}
newProps.props[cfgKongSpecURLPaths] = propData{"string", "", []string{"path1", "path2"}}
newProps.props[cfgKongSpecLocalPath] = propData{"string", "", "/path/to/specs"}
newProps.props[cfgKongSpecFilter] = propData{"string", "", "tag_filter"}
newProps.props[cfgKongSpecDevPortal] = propData{"bool", "", true}
cfg = ParseProperties(newProps)
assert.Equal(t, "http://host:port/path", cfg.Admin.Url)
assert.Equal(t, "apikey", cfg.Admin.Auth.APIKey.Value)
assert.Equal(t, "header", cfg.Admin.Auth.APIKey.Header)
assert.Equal(t, "username", cfg.Admin.Auth.BasicAuth.Username)
assert.Equal(t, "password", cfg.Admin.Auth.BasicAuth.Password)
assert.Equal(t, "proxyhost", cfg.Proxy.Host)
assert.Equal(t, 8080, cfg.Proxy.Ports.HTTP.Value)
assert.Equal(t, 8443, cfg.Proxy.Ports.HTTPS.Value)
assert.Equal(t, false, cfg.Proxy.Ports.HTTP.Disable)
assert.Equal(t, false, cfg.Proxy.Ports.HTTPS.Disable)
assert.Equal(t, "", cfg.Proxy.BasePath)
assert.Equal(t, []string{"path1", "path2"}, cfg.Spec.URLPaths)
assert.Equal(t, "/path/to/specs", cfg.Spec.LocalPath)
assert.Equal(t, "tag_filter", cfg.Spec.Filter)
assert.Equal(t, true, cfg.Spec.DevPortalEnabled)

// validate no port configured when port type disabled
newProps.props[cfgKongProxyPortHttpDisabled] = propData{"bool", "", true}
newProps.props[cfgKongProxyPortHttpsDisabled] = propData{"bool", "", true}
cfg = ParseProperties(newProps)
assert.Equal(t, 0, cfg.Proxy.Ports.HTTP.Value)
assert.Equal(t, 0, cfg.Proxy.Ports.HTTPS.Value)
assert.Equal(t, true, cfg.Proxy.Ports.HTTP.Disable)
assert.Equal(t, true, cfg.Proxy.Ports.HTTPS.Disable)
}