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