This repository has been archived by the owner on May 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
13 changed files
with
242 additions
and
167 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package config_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestConfig(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Config Suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,113 @@ | ||
/* | ||
Package config implements a way to to | ||
handle configuration management using | ||
viper. | ||
*/ | ||
package config | ||
|
||
// Configuration for build tool detector | ||
import ( | ||
"strings" | ||
|
||
"github.com/spf13/viper" | ||
) | ||
|
||
const ( | ||
authURI = "auth.uri" | ||
serverHost = "server.host" | ||
serverPort = "server.port" | ||
metricsPort = "server.port" | ||
githubClientID = "github.client.id" | ||
githubClientSecret = "github.client.secret" | ||
sentryDSN = "sentry.dsn" | ||
) | ||
|
||
const ( | ||
defaultAuth = "https://auth.prod-preview.openshift.io" | ||
defaultHost = "localhost" | ||
defaultPort = "8099" | ||
) | ||
|
||
const ( | ||
prefix = "BUILD_TOOL_DETECTOR" | ||
authKeysPath = "/api/token/keys" | ||
) | ||
|
||
// Configuration for build tool detector. | ||
type Configuration struct { | ||
Auth AuthConfiguration | ||
Github GithubConfiguration | ||
Sentry SentryConfiguration | ||
Server ServerConfiguration | ||
Metrics ServerConfiguration | ||
viper *viper.Viper | ||
} | ||
|
||
// New returns a configuration with defaults set. | ||
func New() *Configuration { | ||
|
||
// Create new viper | ||
configuration := Configuration{ | ||
viper: viper.New(), | ||
} | ||
|
||
// Setup configuration. | ||
configuration.viper.SetEnvPrefix(prefix) | ||
configuration.viper.AutomaticEnv() | ||
configuration.viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) | ||
configuration.viper.SetTypeByDefaultValue(true) | ||
configuration.setConfigDefaults() | ||
|
||
return &configuration | ||
} | ||
|
||
// GetAuthServiceURL returns the server's port. | ||
func (c *Configuration) GetAuthServiceURL() string { | ||
return c.viper.GetString(authURI) | ||
} | ||
|
||
// GetHost returns the server's host. | ||
func (c *Configuration) GetHost() string { | ||
return c.viper.GetString(serverHost) | ||
} | ||
|
||
// GetPort returns the server's port. | ||
func (c *Configuration) GetPort() string { | ||
return c.viper.GetString(serverPort) | ||
} | ||
|
||
// GetMetricsPort returns the server's port. | ||
func (c *Configuration) GetMetricsPort() string { | ||
return c.viper.GetString(metricsPort) | ||
} | ||
|
||
// GetGithubClientID returs the github client id. | ||
func (c *Configuration) GetGithubClientID() string { | ||
return c.viper.GetString(githubClientID) | ||
} | ||
|
||
// GetGithubClientSecret returs the github client id. | ||
func (c *Configuration) GetGithubClientSecret() string { | ||
return c.viper.GetString(githubClientSecret) | ||
} | ||
|
||
// GetSentryDSN returs the github client id. | ||
func (c *Configuration) GetSentryDSN() string { | ||
return c.viper.GetString(sentryDSN) | ||
} | ||
|
||
// GetAuthKeysPath provides a URL path to be called for retrieving the keys. | ||
func (c *Configuration) GetAuthKeysPath() string { | ||
// Fixed with https://github.com/fabric8-services/fabric8-common/pull/25. | ||
return authKeysPath | ||
} | ||
|
||
// GetDevModePrivateKey not used right now. | ||
func (c *Configuration) GetDevModePrivateKey() []byte { | ||
// No need for now | ||
return nil | ||
} | ||
|
||
// setConfigDefaults sets defaults for configuration. | ||
func (c *Configuration) setConfigDefaults() { | ||
c.viper.SetDefault(authURI, defaultAuth) | ||
c.viper.SetDefault(serverHost, defaultHost) | ||
c.viper.SetDefault(serverPort, defaultPort) | ||
c.viper.SetDefault(metricsPort, defaultPort) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package config_test | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/fabric8-services/build-tool-detector/config" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"gopkg.in/h2non/gock.v1" | ||
) | ||
|
||
var _ = Describe("Configuration", func() { | ||
Context("Configuration Defaults", func() { | ||
var configuration *config.Configuration | ||
|
||
BeforeEach(func() { | ||
configuration = config.New() | ||
}) | ||
AfterEach(func() { | ||
gock.Off() | ||
}) | ||
|
||
It("Configuration defaults - test defaults are set", func() { | ||
Expect(configuration.GetHost()).Should(Equal("localhost"), "the host should default to localhost") | ||
Expect(configuration.GetPort()).Should(Equal("8099"), "the port should default to 8099") | ||
Expect(configuration.GetMetricsPort()).Should(Equal("8099"), "the metrics port should default to 8099") | ||
Expect(configuration.GetAuthServiceURL()).Should(Equal("https://auth.prod-preview.openshift.io"), "the auth url should default to https://auth.prod-preview.openshift.io") | ||
Expect(configuration.GetGithubClientID()).Should(Equal(""), "the github client id should default to empty") | ||
Expect(configuration.GetGithubClientSecret()).Should(Equal(""), "the github client secret should default to empty") | ||
Expect(configuration.GetSentryDSN()).Should(Equal(""), "the sentry dsn should default to empty") | ||
Expect(configuration.GetAuthKeysPath()).Should(Equal("/api/token/keys"), "the sentry dsn should return /api/token/keys") | ||
}) | ||
}) | ||
|
||
Context("Configuration Overriden Defaults", func() { | ||
var configuration *config.Configuration | ||
|
||
BeforeEach(func() { | ||
os.Setenv("BUILD_TOOL_DETECTOR_METRICS_PORT", "1234") | ||
os.Setenv("BUILD_TOOL_DETECTOR_SERVER_PORT", "1234") | ||
os.Setenv("BUILD_TOOL_DETECTOR_SERVER_HOST", "test") | ||
os.Setenv("BUILD_TOOL_DETECTOR_GITHUB_CLIENT_ID", "test") | ||
os.Setenv("BUILD_TOOL_DETECTOR_GITHUB_CLIENT_SECRET", "test") | ||
os.Setenv("BUILD_TOOL_DETECTOR_AUTH_URI", "test") | ||
os.Setenv("BUILD_TOOL_DETECTOR_SENTRY_DSN", "test") | ||
configuration = config.New() | ||
}) | ||
AfterEach(func() { | ||
gock.Off() | ||
os.Unsetenv("BUILD_TOOL_DETECTOR_METRICS_PORT") | ||
os.Unsetenv("BUILD_TOOL_DETECTOR_SERVER_PORT") | ||
os.Unsetenv("BUILD_TOOL_DETECTOR_SERVER_HOST") | ||
os.Unsetenv("BUILD_TOOL_DETECTOR_GITHUB_CLIENT_ID") | ||
os.Unsetenv("BUILD_TOOL_DETECTOR_GITHUB_CLIENT_SECRET") | ||
os.Unsetenv("BUILD_TOOL_DETECTOR_AUTH_URI") | ||
os.Unsetenv("BUILD_TOOL_DETECTOR_SENTRY_DSN") | ||
}) | ||
It("Configuration defaults - test defaults are set", func() { | ||
Expect(configuration.GetHost()).Should(Equal("test"), "the host should default to test") | ||
Expect(configuration.GetPort()).Should(Equal("1234"), "the port should default to 1234") | ||
Expect(configuration.GetMetricsPort()).Should(Equal("1234"), "the metrics port should default to 1234") | ||
Expect(configuration.GetAuthServiceURL()).Should(Equal("test"), "the auth url should default to test") | ||
Expect(configuration.GetGithubClientID()).Should(Equal("test"), "the github client id should default to test") | ||
Expect(configuration.GetGithubClientSecret()).Should(Equal("test"), "the github client secret should default to test") | ||
Expect(configuration.GetSentryDSN()).Should(Equal("test"), "the sentry dsn should default to test") | ||
}) | ||
}) | ||
}) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.