22a56aa7452b2dc34725ae1ee85b8161e3337e7e
[nonrtric/rapp/ransliceassurance.git] / smoversion / 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 TestNewEnvVarsSetConfigContainSetValues(t *testing.T) {
33         assertions := require.New(t)
34         os.Setenv("MR_HOST", "consumerHost")
35         os.Setenv("MR_PORT", "8095")
36         os.Setenv("SDNR_ADDR", "http://localhost:3904")
37         os.Setenv("SDNR_USER", "admin")
38         os.Setenv("SDNR_PASSWORD", "Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U")
39         os.Setenv("Polltime", "30")
40         os.Setenv("LOG_LEVEL", "Debug")
41         os.Setenv("NODE_ID", "o-du-1122")
42         t.Cleanup(func() {
43                 os.Clearenv()
44         })
45         wantConfig := Config{
46                 MRHost:      "consumerHost",
47                 MRPort:      "8095",
48                 SDNRAddress: "http://localhost:3904",
49                 SDNRUser:    "admin",
50                 SDNPassword: "Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U",
51                 Polltime:    30,
52                 LogLevel:    log.DebugLevel,
53                 NodeId:      "o-du-1122",
54         }
55
56         got := New()
57         assertions.Equal(&wantConfig, got)
58 }
59
60 func TestNewFaultyIntValueSetConfigContainDefaultValueAndWarnInLog(t *testing.T) {
61         assertions := require.New(t)
62         var buf bytes.Buffer
63         log.SetOutput(&buf)
64
65         os.Setenv("Polltime", "wrong")
66         t.Cleanup(func() {
67                 log.SetOutput(os.Stderr)
68                 os.Clearenv()
69         })
70         wantConfig := Config{
71                 MRHost:      "",
72                 MRPort:      "",
73                 SDNRAddress: "http://localhost:3904",
74                 SDNRUser:    "admin",
75                 SDNPassword: "Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U",
76                 Polltime:    30,
77                 LogLevel:    log.InfoLevel,
78                 NodeId:      "",
79         }
80
81         got := New()
82         assertions.Equal(&wantConfig, got)
83
84         logString := buf.String()
85         assertions.Contains(logString, "Invalid int value: wrong for variable: Polltime. Default value: 30 will be used")
86 }
87
88 func TestNewEnvFaultyLogLevelConfigContainDefaultValues(t *testing.T) {
89         assertions := require.New(t)
90         var buf bytes.Buffer
91         log.SetOutput(&buf)
92
93         os.Setenv("LOG_LEVEL", "wrong")
94         t.Cleanup(func() {
95                 log.SetOutput(os.Stderr)
96                 os.Clearenv()
97         })
98         wantConfig := Config{
99                 MRHost:      "",
100                 MRPort:      "",
101                 SDNRAddress: "http://localhost:3904",
102                 SDNRUser:    "admin",
103                 SDNPassword: "Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U",
104                 Polltime:    30,
105                 LogLevel:    log.InfoLevel,
106                 NodeId:      "",
107         }
108         got := New()
109         assertions.Equal(&wantConfig, got)
110         logString := buf.String()
111         assertions.Contains(logString, "Invalid log level: wrong. Log level will be Info!")
112 }