Consumer O-DU slice assurance rApp
[nonrtric/rapp/ransliceassurance.git] / icsversion / internal / config / config_test.go
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2022: 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 TestConfigurationValuesSetProperly(t *testing.T) {
33
34         assertions := require.New(t)
35
36         type args struct {
37                 conf   Configuration
38                 envVar map[string]string
39         }
40         tests := []struct {
41                 name string
42                 args args
43         }{
44                 {
45                         name: "Test env variable contain correct set values",
46                         args: args{
47                                 conf: Configuration{
48                                         ConsumerHost:           "consumerHost",
49                                         ConsumerPort:           8095,
50                                         SDNRAddress:            "sdnrAddr",
51                                         SDNRUser:               "admin",
52                                         SDNPassword:            "pass",
53                                         InfoCoordinatorAddress: "infoCoordAddr",
54                                         LogLevel:               log.InfoLevel,
55                                 },
56                         },
57                 },
58                 {
59                         name: "Test faulty int value is set for consumer port variable",
60                         args: args{
61                                 conf: Configuration{
62                                         ConsumerHost:           "consumerHost",
63                                         SDNRAddress:            "sdnrAddr",
64                                         SDNRUser:               "admin",
65                                         SDNPassword:            "pass",
66                                         InfoCoordinatorAddress: "infoCoordAddr",
67                                         LogLevel:               log.InfoLevel,
68                                 },
69                                 envVar: map[string]string{"CONSUMER_PORT": "wrong"},
70                         },
71                 },
72                 {
73                         name: "Test log level is wrongly set",
74                         args: args{
75                                 conf: Configuration{
76                                         ConsumerHost:           "consumerHost",
77                                         ConsumerPort:           8095,
78                                         SDNRAddress:            "sdnrAddr",
79                                         SDNRUser:               "admin",
80                                         SDNPassword:            "pass",
81                                         InfoCoordinatorAddress: "infoCoordAddr",
82                                         LogLevel:               log.InfoLevel,
83                                 },
84                                 envVar: map[string]string{"LOG_LEVEL": "wrong"},
85                         },
86                 },
87         }
88
89         for i, tt := range tests {
90                 t.Run(tt.name, func(t *testing.T) {
91                         os.Setenv("CONSUMER_HOST", "consumerHost")
92                         os.Setenv("CONSUMER_PORT", "8095")
93                         os.Setenv("SDNR_ADDR", "sdnrAddr")
94                         os.Setenv("SDNR_USER", "admin")
95                         os.Setenv("SDNR_PASSWORD", "pass")
96                         os.Setenv("INFO_COORD_ADDR", "infoCoordAddr")
97
98                         for key, element := range tt.args.envVar {
99                                 os.Setenv(key, element)
100                         }
101
102                         var buf bytes.Buffer
103                         log.SetOutput(&buf)
104                         t.Cleanup(func() {
105                                 log.SetOutput(os.Stderr)
106                                 os.Clearenv()
107                         })
108
109                         got := New()
110                         assertions.Equal(&tt.args.conf, got)
111
112                         logString := buf.String()
113                         if i == 1 {
114                                 assertions.Contains(logString, "Invalid int value: wrong for variable: CONSUMER_PORT. Default value: 0 will be used")
115                         }
116                         if i == 2 {
117                                 assertions.Contains(logString, "Invalid log level: wrong. Log level will be Info!")
118                         }
119                 })
120         }
121 }