elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 1 | // - |
| 2 | // ========================LICENSE_START================================= |
| 3 | // O-RAN-SC |
| 4 | // %% |
| 5 | // Copyright (C) 2021: Nordix Foundation |
| 6 | // %% |
| 7 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | // you may not use this file except in compliance with the License. |
| 9 | // You may obtain a copy of the License at |
| 10 | // |
| 11 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | // |
| 13 | // Unless required by applicable law or agreed to in writing, software |
| 14 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | // See the License for the specific language governing permissions and |
| 17 | // limitations under the License. |
| 18 | // ========================LICENSE_END=================================== |
| 19 | // |
| 20 | |
| 21 | package config |
| 22 | |
| 23 | import ( |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 24 | "bytes" |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 25 | "os" |
| 26 | "reflect" |
| 27 | "testing" |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 28 | |
| 29 | log "github.com/sirupsen/logrus" |
| 30 | "github.com/stretchr/testify/require" |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 31 | ) |
| 32 | |
| 33 | func TestNew_envVarsSetConfigContainSetValues(t *testing.T) { |
| 34 | os.Setenv("LOG_LEVEL", "Debug") |
elinuxhenrik | ba96d84 | 2021-09-06 16:05:01 +0200 | [diff] [blame] | 35 | os.Setenv("INFO_PRODUCER_SUPERVISION_CALLBACK_HOST", "supervisionCallbackHost") |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 36 | os.Setenv("INFO_PRODUCER_SUPERVISION_CALLBACK_PORT", "8095") |
elinuxhenrik | ba96d84 | 2021-09-06 16:05:01 +0200 | [diff] [blame] | 37 | os.Setenv("INFO_JOB_CALLBACK_HOST", "jobCallbackHost") |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 38 | os.Setenv("INFO_JOB_CALLBACK_PORT", "8096") |
elinuxhenrik | a77cd65 | 2021-09-06 10:56:21 +0200 | [diff] [blame] | 39 | os.Setenv("INFO_COORD_ADDR", "infoCoordAddr") |
elinuxhenrik | b1fb1d8 | 2021-09-07 02:58:52 +0200 | [diff] [blame^] | 40 | t.Cleanup(func() { |
| 41 | os.Clearenv() |
| 42 | }) |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 43 | wantConfig := Config{ |
elinuxhenrik | ba96d84 | 2021-09-06 16:05:01 +0200 | [diff] [blame] | 44 | LogLevel: "Debug", |
| 45 | InfoProducerSupervisionCallbackHost: "supervisionCallbackHost", |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 46 | InfoProducerSupervisionCallbackPort: 8095, |
elinuxhenrik | ba96d84 | 2021-09-06 16:05:01 +0200 | [diff] [blame] | 47 | InfoJobCallbackHost: "jobCallbackHost", |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 48 | InfoJobCallbackPort: 8096, |
elinuxhenrik | ba96d84 | 2021-09-06 16:05:01 +0200 | [diff] [blame] | 49 | InfoCoordinatorAddress: "infoCoordAddr", |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 50 | } |
| 51 | if got := New(); !reflect.DeepEqual(got, &wantConfig) { |
| 52 | t.Errorf("New() = %v, want %v", got, &wantConfig) |
| 53 | } |
| 54 | } |
| 55 | |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 56 | func TestNew_faultyIntValueSetConfigContainDefaultValueAndWarnInLog(t *testing.T) { |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 57 | assertions := require.New(t) |
| 58 | var buf bytes.Buffer |
| 59 | log.SetOutput(&buf) |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 60 | |
| 61 | os.Setenv("INFO_PRODUCER_SUPERVISION_CALLBACK_PORT", "wrong") |
elinuxhenrik | b1fb1d8 | 2021-09-07 02:58:52 +0200 | [diff] [blame^] | 62 | t.Cleanup(func() { |
| 63 | log.SetOutput(os.Stderr) |
| 64 | os.Clearenv() |
| 65 | }) |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 66 | wantConfig := Config{ |
elinuxhenrik | ba96d84 | 2021-09-06 16:05:01 +0200 | [diff] [blame] | 67 | LogLevel: "Info", |
| 68 | InfoProducerSupervisionCallbackHost: "", |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 69 | InfoProducerSupervisionCallbackPort: 8085, |
elinuxhenrik | ba96d84 | 2021-09-06 16:05:01 +0200 | [diff] [blame] | 70 | InfoJobCallbackHost: "", |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 71 | InfoJobCallbackPort: 8086, |
| 72 | InfoCoordinatorAddress: "http://enrichmentservice:8083", |
| 73 | } |
| 74 | if got := New(); !reflect.DeepEqual(got, &wantConfig) { |
| 75 | t.Errorf("New() = %v, want %v", got, &wantConfig) |
| 76 | } |
| 77 | logString := buf.String() |
| 78 | assertions.Contains(logString, "Invalid int value: wrong for variable: INFO_PRODUCER_SUPERVISION_CALLBACK_PORT. Default value: 8085 will be used") |
| 79 | } |
| 80 | |
| 81 | func TestNew_envVarsNotSetConfigContainDefaultValues(t *testing.T) { |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 82 | wantConfig := Config{ |
| 83 | LogLevel: "Info", |
| 84 | InfoProducerSupervisionCallbackHost: "", |
| 85 | InfoProducerSupervisionCallbackPort: 8085, |
| 86 | InfoJobCallbackHost: "", |
| 87 | InfoJobCallbackPort: 8086, |
elinuxhenrik | ba96d84 | 2021-09-06 16:05:01 +0200 | [diff] [blame] | 88 | InfoCoordinatorAddress: "http://enrichmentservice:8083", |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 89 | } |
| 90 | if got := New(); !reflect.DeepEqual(got, &wantConfig) { |
| 91 | t.Errorf("New() = %v, want %v", got, &wantConfig) |
| 92 | } |
| 93 | } |