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