Add NodeId and JobId as configuration in ICS Version
[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                                         NodeId:                 "O-DU-1122",
56                                         JobId:                  "14e7bb84-a44d-44c1-90b7-6995a92ad83d",
57                                 },
58                         },
59                 },
60                 {
61                         name: "Test faulty int value is set for consumer port variable",
62                         args: args{
63                                 conf: Configuration{
64                                         ConsumerHost:           "consumerHost",
65                                         SDNRAddress:            "sdnrAddr",
66                                         SDNRUser:               "admin",
67                                         SDNPassword:            "pass",
68                                         InfoCoordinatorAddress: "infoCoordAddr",
69                                         LogLevel:               log.InfoLevel,
70                                         NodeId:                 "O-DU-1122",
71                                         JobId:                  "14e7bb84-a44d-44c1-90b7-6995a92ad83d",
72                                 },
73                                 envVar: map[string]string{"CONSUMER_PORT": "wrong"},
74                         },
75                 },
76                 {
77                         name: "Test log level is wrongly set",
78                         args: args{
79                                 conf: Configuration{
80                                         ConsumerHost:           "consumerHost",
81                                         ConsumerPort:           8095,
82                                         SDNRAddress:            "sdnrAddr",
83                                         SDNRUser:               "admin",
84                                         SDNPassword:            "pass",
85                                         InfoCoordinatorAddress: "infoCoordAddr",
86                                         LogLevel:               log.InfoLevel,
87                                         NodeId:                 "O-DU-1122",
88                                         JobId:                  "14e7bb84-a44d-44c1-90b7-6995a92ad83d",
89                                 },
90                                 envVar: map[string]string{"LOG_LEVEL": "wrong"},
91                         },
92                 },
93         }
94
95         for i, tt := range tests {
96                 t.Run(tt.name, func(t *testing.T) {
97                         os.Setenv("CONSUMER_HOST", "consumerHost")
98                         os.Setenv("CONSUMER_PORT", "8095")
99                         os.Setenv("SDNR_ADDR", "sdnrAddr")
100                         os.Setenv("SDNR_USER", "admin")
101                         os.Setenv("SDNR_PASSWORD", "pass")
102                         os.Setenv("INFO_COORD_ADDR", "infoCoordAddr")
103                         os.Setenv("NODE_ID", "O-DU-1122")
104                         os.Setenv("JOB_ID", "14e7bb84-a44d-44c1-90b7-6995a92ad83d")
105
106                         for key, element := range tt.args.envVar {
107                                 os.Setenv(key, element)
108                         }
109
110                         var buf bytes.Buffer
111                         log.SetOutput(&buf)
112                         t.Cleanup(func() {
113                                 log.SetOutput(os.Stderr)
114                                 os.Clearenv()
115                         })
116
117                         got := New()
118                         assertions.Equal(&tt.args.conf, got)
119
120                         logString := buf.String()
121                         if i == 1 {
122                                 assertions.Contains(logString, "Invalid int value: wrong for variable: CONSUMER_PORT. Default value: 0 will be used")
123                         }
124                         if i == 2 {
125                                 assertions.Contains(logString, "Invalid log level: wrong. Log level will be Info!")
126                         }
127                 })
128         }
129 }