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) { |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame^] | 34 | assertions := require.New(t) |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 35 | os.Setenv("LOG_LEVEL", "Debug") |
elinuxhenrik | 382870d | 2021-09-23 11:09:09 +0200 | [diff] [blame] | 36 | os.Setenv("INFO_PRODUCER_HOST", "producerHost") |
| 37 | os.Setenv("INFO_PRODUCER_PORT", "8095") |
elinuxhenrik | a77cd65 | 2021-09-06 10:56:21 +0200 | [diff] [blame] | 38 | os.Setenv("INFO_COORD_ADDR", "infoCoordAddr") |
elinuxhenrik | 2803856 | 2021-09-21 15:43:11 +0200 | [diff] [blame] | 39 | os.Setenv("MR_HOST", "mrHost") |
| 40 | os.Setenv("MR_PORT", "3908") |
elinuxhenrik | b1fb1d8 | 2021-09-07 02:58:52 +0200 | [diff] [blame] | 41 | t.Cleanup(func() { |
| 42 | os.Clearenv() |
| 43 | }) |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 44 | wantConfig := Config{ |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame^] | 45 | LogLevel: log.DebugLevel, |
elinuxhenrik | 382870d | 2021-09-23 11:09:09 +0200 | [diff] [blame] | 46 | InfoProducerHost: "producerHost", |
| 47 | InfoProducerPort: 8095, |
| 48 | InfoCoordinatorAddress: "infoCoordAddr", |
| 49 | MRHost: "mrHost", |
| 50 | MRPort: 3908, |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 51 | } |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame^] | 52 | got := New() |
| 53 | |
| 54 | assertions.Equal(&wantConfig, got) |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 55 | } |
| 56 | |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 57 | func TestNew_faultyIntValueSetConfigContainDefaultValueAndWarnInLog(t *testing.T) { |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 58 | assertions := require.New(t) |
| 59 | var buf bytes.Buffer |
| 60 | log.SetOutput(&buf) |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 61 | |
elinuxhenrik | 382870d | 2021-09-23 11:09:09 +0200 | [diff] [blame] | 62 | os.Setenv("INFO_PRODUCER_PORT", "wrong") |
elinuxhenrik | b1fb1d8 | 2021-09-07 02:58:52 +0200 | [diff] [blame] | 63 | t.Cleanup(func() { |
| 64 | log.SetOutput(os.Stderr) |
| 65 | os.Clearenv() |
| 66 | }) |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 67 | wantConfig := Config{ |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame^] | 68 | LogLevel: log.InfoLevel, |
elinuxhenrik | 382870d | 2021-09-23 11:09:09 +0200 | [diff] [blame] | 69 | InfoProducerHost: "", |
| 70 | InfoProducerPort: 8085, |
| 71 | InfoCoordinatorAddress: "http://enrichmentservice:8083", |
| 72 | MRHost: "http://message-router.onap", |
| 73 | MRPort: 3904, |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 74 | } |
| 75 | if got := New(); !reflect.DeepEqual(got, &wantConfig) { |
| 76 | t.Errorf("New() = %v, want %v", got, &wantConfig) |
| 77 | } |
| 78 | logString := buf.String() |
elinuxhenrik | 382870d | 2021-09-23 11:09:09 +0200 | [diff] [blame] | 79 | assertions.Contains(logString, "Invalid int value: wrong for variable: INFO_PRODUCER_PORT. Default value: 8085 will be used") |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 80 | } |
| 81 | |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame^] | 82 | func TestNew_envFaultyLogLevelConfigContainDefaultValues(t *testing.T) { |
| 83 | assertions := require.New(t) |
| 84 | var buf bytes.Buffer |
| 85 | log.SetOutput(&buf) |
| 86 | |
| 87 | os.Setenv("LOG_LEVEL", "wrong") |
| 88 | t.Cleanup(func() { |
| 89 | log.SetOutput(os.Stderr) |
| 90 | os.Clearenv() |
| 91 | }) |
| 92 | |
elinuxhenrik | 63a42ca | 2021-09-06 22:16:24 +0200 | [diff] [blame] | 93 | wantConfig := Config{ |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame^] | 94 | LogLevel: log.InfoLevel, |
elinuxhenrik | 382870d | 2021-09-23 11:09:09 +0200 | [diff] [blame] | 95 | InfoProducerHost: "", |
| 96 | InfoProducerPort: 8085, |
| 97 | InfoCoordinatorAddress: "http://enrichmentservice:8083", |
| 98 | MRHost: "http://message-router.onap", |
| 99 | MRPort: 3904, |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 100 | } |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame^] | 101 | |
| 102 | got := New() |
| 103 | |
| 104 | assertions.Equal(&wantConfig, got) |
| 105 | logString := buf.String() |
| 106 | assertions.Contains(logString, "Invalid log level: wrong. Log level will be Info!") |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 107 | } |