From cb08fe63724e2c89431c3505d13be353217314c0 Mon Sep 17 00:00:00 2001
From: OZGCloud <ozgcloud@mgm-tp.com>
Date: Tue, 26 Nov 2024 17:41:54 +0100
Subject: [PATCH] OZG-6747 refactor envs

---
 Jenkinsfile                                   |  8 ++++
 README.md                                     |  9 +++-
 .../config-local.yml                          |  2 +-
 config/config.yml                             |  2 +-
 internal/config/config.go                     | 26 +++++++----
 internal/config/config_test.go                | 44 +++++++------------
 .../{test_config.yml => config-local.yml}     |  0
 internal/config/testdata/config.yml           |  6 +++
 .../config/testdata/empty_test_config.yml     |  0
 .../testdata/{test_config.yml => config.yml}  |  0
 .../testdata/{test_config.yml => config.yml}  |  0
 internal/server/testdata/config.yml           |  6 +++
 src/main/helm/templates/deployment.yaml       |  8 ++--
 src/test/helm-linter-values.yaml              |  2 -
 .../helm/deployment_defaults_labels_test.yaml |  2 -
 src/test/helm/deployment_env_test.yaml        | 35 ++++++++++-----
 .../deployment_imagepull_secret_test.yaml     |  2 -
 src/test/helm/deployment_resources_test.yaml  |  2 -
 src/test/helm/deployment_test.yaml            |  2 -
 19 files changed, 91 insertions(+), 65 deletions(-)
 rename internal/server/testdata/test_config.yml => config/config-local.yml (75%)
 rename internal/config/testdata/{test_config.yml => config-local.yml} (100%)
 create mode 100644 internal/config/testdata/config.yml
 delete mode 100644 internal/config/testdata/empty_test_config.yml
 rename internal/logging/testdata/{test_config.yml => config.yml} (100%)
 rename internal/mock/testdata/{test_config.yml => config.yml} (100%)
 create mode 100644 internal/server/testdata/config.yml

diff --git a/Jenkinsfile b/Jenkinsfile
index 51cd03f..48724ef 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 1108891..dcb0957 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 ab046e2..8605b24 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 8605b24..ff630ca 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 b246c3d..dec3a3a 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 b423ad6..9312466 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 0000000..ff630ca
--- /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 e69de29..0000000
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 0000000..3138586
--- /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 7961ec2..5ceae09 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 0887765..7f0f69f 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 e015d27..fb7659c 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 2914599..66d8c82 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 81ceb25..b31f487 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 965ad12..16a0dc2 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 108210c..5031d85 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"
 
-- 
GitLab