diff --git a/Jenkinsfile b/Jenkinsfile index 51cd03fb94ef0eaced06253d5557f7303ea2041e..48724ef5ef4e0202ba153b49f1f6f1c4c619bedf 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -81,6 +81,14 @@ tools { go 'go-1.22.0' } } } } + stage('Test Info-Manager-Proxy') { + steps { + script { + FAILED_STAGE=env.STAGE_NAME + sh "go test ./internal/*" + } + } + } stage('Build and publish Docker image') { steps { diff --git a/README.md b/README.md index 1108891a7d44035b107d69d7add7b42526146899..dcb09576ce5260d50cdc92e36d9774a3e5890306 100644 --- a/README.md +++ b/README.md @@ -29,13 +29,20 @@ buf generate ### Anwendung starten ``` -go run cmd/info-manager-proxy/main.go +ACTIVE_PROFILE=local go run cmd/info-manager-proxy/main.go ``` ## Config +config.yml is the default config for deployment [Config-Datei](./config/config.yml) +config-local.yml is the config file for local development +setting ACTIVE_PROFILE=local will load the config-local.yml + +[Config-Datei](./config/config-local.yml) + + ``` server: port: Port des HTTP Servers (int) diff --git a/internal/server/testdata/test_config.yml b/config/config-local.yml similarity index 75% rename from internal/server/testdata/test_config.yml rename to config/config-local.yml index ab046e21279584ca07ff098edcf604f39c0b51a4..8605b248d11050d5a8ad3c71b3e41e20eb8993d6 100644 --- a/internal/server/testdata/test_config.yml +++ b/config/config-local.yml @@ -3,4 +3,4 @@ server: grpc: mock: true logging: - level: "DEBUG" \ No newline at end of file + level: "INFO" \ No newline at end of file diff --git a/config/config.yml b/config/config.yml index 8605b248d11050d5a8ad3c71b3e41e20eb8993d6..ff630ca060fced93c31b571a7082932488450d81 100755 --- a/config/config.yml +++ b/config/config.yml @@ -1,6 +1,6 @@ server: port: 8082 grpc: - mock: true + mock: false logging: level: "INFO" \ No newline at end of file diff --git a/internal/config/config.go b/internal/config/config.go index b246c3d63d3ad026e142c890f9532722f122358e..dec3a3a4ee7d79ce14549e26777a40131dc1fa97 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -32,35 +32,45 @@ import ( "os" "regexp" "testing" + "fmt" ) const ( - defaultFilePath = "config/config.yml" - testFilePath = "testdata/test_config.yml" grpcUrlRegex = `^[A-Za-z\d-]+(\.[A-Za-z\d-]+){0,512}:\d{1,5}$` + defaultConfig = "config.yml" + testFilePath = "testdata/" + activeProfileParam = "ACTIVE_PROFILE" ) type Config struct { Server struct { - Port int `yaml:"port" envconfig:"SERVER_PORT"` + Port int `yaml:"port"` } `yaml:"server"` Grpc struct { - Mock bool `yaml:"mock" envconfig:"GRPC_MOCK"` + Mock bool `yaml:"mock"` Url string `yaml:"url" envconfig:"GRPC_URL"` } `yaml:"grpc"` Logging struct { - Level string `yaml:"level" envconfig:"LOGGING_LEVEL"` + Level string `yaml:"level"` } `yaml:"logging"` } func LoadConfig(configFilePath ...string) Config { var config Config - fp := defaultFilePath + configFile := defaultConfig + + env := os.Getenv(activeProfileParam) + if env != "" { + configFile = fmt.Sprintf("config-%s.yml", env) + } + + fp := fmt.Sprintf("config/%s", configFile) + if len(configFilePath) > 0 { fp = configFilePath[0] } else if testing.Testing() { - fp = testFilePath + fp = testFilePath + configFile } file, err := os.ReadFile(fp) @@ -72,12 +82,10 @@ func LoadConfig(configFilePath ...string) Config { if err != nil { log.Fatalf("FATAL: error unmarshalling YAML file: %v", err) } - err = envconfig.Process("", &config) if err != nil { log.Fatalf("FATAL: error reading env: %v", err) } - if len(config.Grpc.Url) > 0 && !ValidateGrpcUrl(config.Grpc.Url) { log.Fatalf("FATAL: gRPC URL is not in host:port format") } diff --git a/internal/config/config_test.go b/internal/config/config_test.go index b423ad6b36d8343e001df094c668fac3bd42b16b..9312466379075c42dbe9b378d56edb0281eb0f1f 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -33,56 +33,44 @@ import ( ) func TestLoadConfig(t *testing.T) { - t.Run("should load config from file", func(t *testing.T) { + t.Run("should load default config", func(t *testing.T) { config := LoadConfig() expectedConfig := Config{} - expectedConfig.Server.Port = 8082 - expectedConfig.Grpc.Url = "localhost:50051" expectedConfig.Grpc.Mock = false - expectedConfig.Logging.Level = "DEBUG" + expectedConfig.Server.Port = 8082 + expectedConfig.Logging.Level = "INFO" assert.Equal(t, expectedConfig, config) }) - t.Run("should load config from env", func(t *testing.T) { - envVars := map[string]string{ - "SERVER_PORT": "9090", - "GRPC_URL": "localhost:99999", - "LOGGING_LEVEL": "ERROR", - } + t.Run("should load local config", func(t *testing.T) { + assert.NoError(t, os.Setenv(activeProfileParam, "local"), "Setenv "+activeProfileParam+" should not return an error") - for key, value := range envVars { - assert.NoError(t, os.Setenv(key, value), "Setenv "+key+" should not return an error") - } - - config := LoadConfig("testdata/empty_test_config.yml") + config := LoadConfig() - for key := range envVars { - assert.NoError(t, os.Unsetenv(key), "Unsetenv "+key+" should not return an error") - } + assert.NoError(t, os.Unsetenv(activeProfileParam), "Unsetenv "+activeProfileParam+" should not return an error") expectedConfig := Config{} - expectedConfig.Server.Port = 9090 - expectedConfig.Grpc.Url = "localhost:99999" - expectedConfig.Grpc.Mock = false - expectedConfig.Logging.Level = "ERROR" + + expectedConfig.Server.Port = 8082 + expectedConfig.Grpc.Url = "localhost:50051" + expectedConfig.Logging.Level = "DEBUG" assert.Equal(t, expectedConfig, config) }) - t.Run("should overwrite config with env", func(t *testing.T) { - assert.NoError(t, os.Setenv("SERVER_PORT", "9090"), "Setenv SERVER_PORT should not return an error") + assert.NoError(t, os.Setenv("GRPC_URL", "zufi:9090"), "Setenv GRPC_URL should not return an error") config := LoadConfig() - assert.NoError(t, os.Unsetenv("SERVER_PORT"), "Unsetenv SERVER_PORT should not return an error") + assert.NoError(t, os.Unsetenv("GRPC_URL"), "Unsetenv GRPC_URL should not return an error") expectedConfig := Config{} - expectedConfig.Server.Port = 9090 - expectedConfig.Grpc.Url = "localhost:50051" expectedConfig.Grpc.Mock = false - expectedConfig.Logging.Level = "DEBUG" + expectedConfig.Grpc.Url = "zufi:9090" + expectedConfig.Server.Port = 8082 + expectedConfig.Logging.Level = "INFO" assert.Equal(t, expectedConfig, config) }) diff --git a/internal/config/testdata/test_config.yml b/internal/config/testdata/config-local.yml similarity index 100% rename from internal/config/testdata/test_config.yml rename to internal/config/testdata/config-local.yml diff --git a/internal/config/testdata/config.yml b/internal/config/testdata/config.yml new file mode 100644 index 0000000000000000000000000000000000000000..ff630ca060fced93c31b571a7082932488450d81 --- /dev/null +++ b/internal/config/testdata/config.yml @@ -0,0 +1,6 @@ +server: + port: 8082 +grpc: + mock: false +logging: + level: "INFO" \ No newline at end of file diff --git a/internal/config/testdata/empty_test_config.yml b/internal/config/testdata/empty_test_config.yml deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/internal/logging/testdata/test_config.yml b/internal/logging/testdata/config.yml similarity index 100% rename from internal/logging/testdata/test_config.yml rename to internal/logging/testdata/config.yml diff --git a/internal/mock/testdata/test_config.yml b/internal/mock/testdata/config.yml similarity index 100% rename from internal/mock/testdata/test_config.yml rename to internal/mock/testdata/config.yml diff --git a/internal/server/testdata/config.yml b/internal/server/testdata/config.yml new file mode 100644 index 0000000000000000000000000000000000000000..3138586b8c8b7858dda264c704619e6e712c3d23 --- /dev/null +++ b/internal/server/testdata/config.yml @@ -0,0 +1,6 @@ +server: + port: 8082 +grpc: + url: "localhost:50051" +logging: + level: "DEBUG" \ No newline at end of file diff --git a/src/main/helm/templates/deployment.yaml b/src/main/helm/templates/deployment.yaml index 7961ec2999b9420e6c72c8061c6b79ee5d1d6818..5ceae09fcef1eea07bc435c5a4ca0827ae74c2bd 100644 --- a/src/main/helm/templates/deployment.yaml +++ b/src/main/helm/templates/deployment.yaml @@ -58,10 +58,10 @@ spec: app.kubernetes.io/name: {{ .Release.Name }} containers: - env: - - name: SERVER_PORT - value: "8082" - - name: GRPC_MOCK - value: "{{ (.Values.grpc).mock | default false }}" + {{- if (.Values.env).overrideGoProfiles }} + - name: ACTIVE_PROFILE + value: {{ (.Values.env).overrideGoProfiles }} + {{- end }} - name: GRPC_URL value: "{{ required ".Values.grpc.url must be set" (.Values.grpc).url }}" {{- with include "app.getCustomList" . }} diff --git a/src/test/helm-linter-values.yaml b/src/test/helm-linter-values.yaml index 0887765787ed4f7b3ef457d5402567e076f7b55b..7f0f69f8fbf420fb26a30080c8ac29c654bd7107 100644 --- a/src/test/helm-linter-values.yaml +++ b/src/test/helm-linter-values.yaml @@ -2,8 +2,6 @@ networkPolicy: dnsServerNamespace: test-dns-namespace imagePullSecret: ozgcloud-image-pull-secret -server: - port: 8082 grpc: url: "info.de:9090" diff --git a/src/test/helm/deployment_defaults_labels_test.yaml b/src/test/helm/deployment_defaults_labels_test.yaml index e015d275a560860e11af8f0933918dc478215b6a..fb7659c18ccc0c761e67f1f4b507c3d280da93ab 100644 --- a/src/test/helm/deployment_defaults_labels_test.yaml +++ b/src/test/helm/deployment_defaults_labels_test.yaml @@ -30,8 +30,6 @@ templates: - templates/deployment.yaml set: imagePullSecret: image-pull-secret - server: - port: 8080 grpc: url: "zufi.de:9090" tests: diff --git a/src/test/helm/deployment_env_test.yaml b/src/test/helm/deployment_env_test.yaml index 291459906207c5e4cceb50f29962d43e5e58ed6c..66d8c82a1a7722c0d6215d3e1f86d37679ec4050 100644 --- a/src/test/helm/deployment_env_test.yaml +++ b/src/test/helm/deployment_env_test.yaml @@ -77,18 +77,31 @@ tests: content: name: GRPC_URL value: "zufi.de:9090" - - contains: - path: spec.template.spec.containers[0].env - content: - name: GRPC_MOCK - value: "false" - - contains: - path: spec.template.spec.containers[0].env - content: - name: SERVER_PORT - value: "8082" - it: should fail template if grpc.url not set set: asserts: - failedTemplate: - errormessage: ".Values.grpc.url must be set" \ No newline at end of file + errormessage: ".Values.grpc.url must be set" + + - it: should by default not have ACTIVE_PROFILE env + set: + grpc: + url: "zufi.de:9090" + asserts: + - notContains: + path: spec.template.spec.containers[0].env + content: + name: ACTIVE_PROFILE + any: true + + - it: should have env ACTIVE_PROFILE with correct value when set + set: + grpc: + url: "zufi.de:9090" + env.overrideGoProfiles: oc,test,ea + asserts: + - contains: + path: spec.template.spec.containers[0].env + content: + name: ACTIVE_PROFILE + value: oc,test,ea \ No newline at end of file diff --git a/src/test/helm/deployment_imagepull_secret_test.yaml b/src/test/helm/deployment_imagepull_secret_test.yaml index 81ceb259ad5793c38e3ba3bc692840b49ddf86bc..b31f487c1d8fb3da22f06b231620e9a6f0d7b40c 100644 --- a/src/test/helm/deployment_imagepull_secret_test.yaml +++ b/src/test/helm/deployment_imagepull_secret_test.yaml @@ -29,8 +29,6 @@ release: templates: - templates/deployment.yaml set: - server: - port: 8080 grpc: url: "zufi.de:9090" tests: diff --git a/src/test/helm/deployment_resources_test.yaml b/src/test/helm/deployment_resources_test.yaml index 965ad12a2d74d63fabe20106d8687af9e73f3a69..16a0dc2c8ce3e6da888ce9e67bd511d26606deb9 100644 --- a/src/test/helm/deployment_resources_test.yaml +++ b/src/test/helm/deployment_resources_test.yaml @@ -29,8 +29,6 @@ templates: - templates/deployment.yaml set: imagePullSecret: image-pull-secret - server: - port: 8080 grpc: url: "zufi.de:9090" diff --git a/src/test/helm/deployment_test.yaml b/src/test/helm/deployment_test.yaml index 108210c281cab1df1efa94cd4e4edfdfd8f8ea34..5031d853b3cd1e609045164ed9c7c73052890d8b 100644 --- a/src/test/helm/deployment_test.yaml +++ b/src/test/helm/deployment_test.yaml @@ -29,8 +29,6 @@ templates: - templates/deployment.yaml set: imagePullSecret: image-pull-secret - server: - port: 8080 grpc: url: "zufi.de:9090"