blob: 23227399863648fa5ab955026d58a9a6976ab51e [file] [log] [blame]
elinuxhenrikcce95ff2021-09-05 17:27:02 +02001// -
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
21package config
22
23import (
elinuxhenrik63a42ca2021-09-06 22:16:24 +020024 "bytes"
elinuxhenrikcce95ff2021-09-05 17:27:02 +020025 "os"
26 "reflect"
27 "testing"
elinuxhenrik63a42ca2021-09-06 22:16:24 +020028
29 log "github.com/sirupsen/logrus"
30 "github.com/stretchr/testify/require"
elinuxhenrikcce95ff2021-09-05 17:27:02 +020031)
32
33func TestNew_envVarsSetConfigContainSetValues(t *testing.T) {
34 os.Setenv("LOG_LEVEL", "Debug")
elinuxhenrikba96d842021-09-06 16:05:01 +020035 os.Setenv("INFO_PRODUCER_SUPERVISION_CALLBACK_HOST", "supervisionCallbackHost")
elinuxhenrik63a42ca2021-09-06 22:16:24 +020036 os.Setenv("INFO_PRODUCER_SUPERVISION_CALLBACK_PORT", "8095")
elinuxhenrikba96d842021-09-06 16:05:01 +020037 os.Setenv("INFO_JOB_CALLBACK_HOST", "jobCallbackHost")
elinuxhenrik63a42ca2021-09-06 22:16:24 +020038 os.Setenv("INFO_JOB_CALLBACK_PORT", "8096")
elinuxhenrika77cd652021-09-06 10:56:21 +020039 os.Setenv("INFO_COORD_ADDR", "infoCoordAddr")
elinuxhenrikcce95ff2021-09-05 17:27:02 +020040 defer os.Clearenv()
41 wantConfig := Config{
elinuxhenrikba96d842021-09-06 16:05:01 +020042 LogLevel: "Debug",
43 InfoProducerSupervisionCallbackHost: "supervisionCallbackHost",
elinuxhenrik63a42ca2021-09-06 22:16:24 +020044 InfoProducerSupervisionCallbackPort: 8095,
elinuxhenrikba96d842021-09-06 16:05:01 +020045 InfoJobCallbackHost: "jobCallbackHost",
elinuxhenrik63a42ca2021-09-06 22:16:24 +020046 InfoJobCallbackPort: 8096,
elinuxhenrikba96d842021-09-06 16:05:01 +020047 InfoCoordinatorAddress: "infoCoordAddr",
elinuxhenrikcce95ff2021-09-05 17:27:02 +020048 }
49 if got := New(); !reflect.DeepEqual(got, &wantConfig) {
50 t.Errorf("New() = %v, want %v", got, &wantConfig)
51 }
52}
53
elinuxhenrik63a42ca2021-09-06 22:16:24 +020054func TestNew_faultyIntValueSetConfigContainDefaultValueAndWarnInLog(t *testing.T) {
55 os.Clearenv()
56 assertions := require.New(t)
57 var buf bytes.Buffer
58 log.SetOutput(&buf)
59 defer func() {
60 log.SetOutput(os.Stderr)
61 }()
62
63 os.Setenv("INFO_PRODUCER_SUPERVISION_CALLBACK_PORT", "wrong")
64 defer os.Clearenv()
elinuxhenrikcce95ff2021-09-05 17:27:02 +020065 wantConfig := Config{
elinuxhenrikba96d842021-09-06 16:05:01 +020066 LogLevel: "Info",
67 InfoProducerSupervisionCallbackHost: "",
elinuxhenrik63a42ca2021-09-06 22:16:24 +020068 InfoProducerSupervisionCallbackPort: 8085,
elinuxhenrikba96d842021-09-06 16:05:01 +020069 InfoJobCallbackHost: "",
elinuxhenrik63a42ca2021-09-06 22:16:24 +020070 InfoJobCallbackPort: 8086,
71 InfoCoordinatorAddress: "http://enrichmentservice:8083",
72 }
73 if got := New(); !reflect.DeepEqual(got, &wantConfig) {
74 t.Errorf("New() = %v, want %v", got, &wantConfig)
75 }
76 logString := buf.String()
77 assertions.Contains(logString, "Invalid int value: wrong for variable: INFO_PRODUCER_SUPERVISION_CALLBACK_PORT. Default value: 8085 will be used")
78}
79
80func TestNew_envVarsNotSetConfigContainDefaultValues(t *testing.T) {
81 os.Clearenv()
82 wantConfig := Config{
83 LogLevel: "Info",
84 InfoProducerSupervisionCallbackHost: "",
85 InfoProducerSupervisionCallbackPort: 8085,
86 InfoJobCallbackHost: "",
87 InfoJobCallbackPort: 8086,
elinuxhenrikba96d842021-09-06 16:05:01 +020088 InfoCoordinatorAddress: "http://enrichmentservice:8083",
elinuxhenrikcce95ff2021-09-05 17:27:02 +020089 }
90 if got := New(); !reflect.DeepEqual(got, &wantConfig) {
91 t.Errorf("New() = %v, want %v", got, &wantConfig)
92 }
93}