Merge "Test env updates"
[nonrtric.git] / test / usecases / oruclosedlooprecovery / goversion / internal / config / config_test.go
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 (
24         "bytes"
25         "os"
26         "reflect"
27         "testing"
28
29         log "github.com/sirupsen/logrus"
30         "github.com/stretchr/testify/require"
31 )
32
33 func TestNew_envVarsSetConfigContainSetValues(t *testing.T) {
34         os.Setenv("LOG_LEVEL", "Debug")
35         os.Setenv("CONSUMER_HOST", "consumerHost")
36         os.Setenv("CONSUMER_PORT", "8095")
37         os.Setenv("INFO_COORD_ADDR", "infoCoordAddr")
38         os.Setenv("SDNR_HOST", "sdnrHost")
39         os.Setenv("SDNR_PORT", "3908")
40         os.Setenv("SDNR_USER", "admin")
41         os.Setenv("SDNR_PASSWORD", "pwd")
42         os.Setenv("ORU_TO_ODU_MAP_FILE", "file")
43         t.Cleanup(func() {
44                 os.Clearenv()
45         })
46         wantConfig := Config{
47                 LogLevel:               log.DebugLevel,
48                 ConsumerHost:           "consumerHost",
49                 ConsumerPort:           8095,
50                 InfoCoordinatorAddress: "infoCoordAddr",
51                 SDNRHost:               "sdnrHost",
52                 SDNRPort:               3908,
53                 SDNRUser:               "admin",
54                 SDNPassword:            "pwd",
55                 ORUToODUMapFile:        "file",
56         }
57         if got := New(); !reflect.DeepEqual(got, &wantConfig) {
58                 t.Errorf("New() = %v, want %v", got, &wantConfig)
59         }
60 }
61
62 func TestNew_faultyIntValueSetConfigContainDefaultValueAndWarnInLog(t *testing.T) {
63         assertions := require.New(t)
64         var buf bytes.Buffer
65         log.SetOutput(&buf)
66
67         os.Setenv("CONSUMER_PORT", "wrong")
68         t.Cleanup(func() {
69                 log.SetOutput(os.Stderr)
70                 os.Clearenv()
71         })
72         wantConfig := Config{
73                 LogLevel:               log.InfoLevel,
74                 ConsumerHost:           "",
75                 ConsumerPort:           0,
76                 InfoCoordinatorAddress: "http://enrichmentservice:8083",
77                 SDNRHost:               "http://localhost",
78                 SDNRPort:               3904,
79                 SDNRUser:               "admin",
80                 SDNPassword:            "Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U",
81                 ORUToODUMapFile:        "o-ru-to-o-du-map.csv",
82         }
83         if got := New(); !reflect.DeepEqual(got, &wantConfig) {
84                 t.Errorf("New() = %v, want %v", got, &wantConfig)
85         }
86         logString := buf.String()
87         assertions.Contains(logString, "Invalid int value: wrong for variable: CONSUMER_PORT. Default value: 0 will be used")
88 }
89
90 func TestNew_envFaultyLogLevelConfigContainDefaultValues(t *testing.T) {
91         assertions := require.New(t)
92         var buf bytes.Buffer
93         log.SetOutput(&buf)
94
95         os.Setenv("LOG_LEVEL", "wrong")
96         t.Cleanup(func() {
97                 log.SetOutput(os.Stderr)
98                 os.Clearenv()
99         })
100         wantConfig := Config{
101                 LogLevel:               log.InfoLevel,
102                 ConsumerHost:           "",
103                 ConsumerPort:           0,
104                 InfoCoordinatorAddress: "http://enrichmentservice:8083",
105                 SDNRHost:               "http://localhost",
106                 SDNRPort:               3904,
107                 SDNRUser:               "admin",
108                 SDNPassword:            "Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U",
109                 ORUToODUMapFile:        "o-ru-to-o-du-map.csv",
110         }
111         if got := New(); !reflect.DeepEqual(got, &wantConfig) {
112                 t.Errorf("New() = %v, want %v", got, &wantConfig)
113         }
114         if got := New(); !reflect.DeepEqual(got, &wantConfig) {
115                 t.Errorf("New() = %v, want %v", got, &wantConfig)
116         }
117         logString := buf.String()
118         assertions.Contains(logString, "Invalid log level: wrong. Log level will be Info!")
119 }