blob: 104302726406fc9a970c176fffc18e013ad6aa9c [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")
elinuxhenrik28038562021-09-21 15:43:11 +020040 os.Setenv("MR_HOST", "mrHost")
41 os.Setenv("MR_PORT", "3908")
elinuxhenrikb1fb1d82021-09-07 02:58:52 +020042 t.Cleanup(func() {
43 os.Clearenv()
44 })
elinuxhenrikcce95ff2021-09-05 17:27:02 +020045 wantConfig := Config{
elinuxhenrikba96d842021-09-06 16:05:01 +020046 LogLevel: "Debug",
47 InfoProducerSupervisionCallbackHost: "supervisionCallbackHost",
elinuxhenrik63a42ca2021-09-06 22:16:24 +020048 InfoProducerSupervisionCallbackPort: 8095,
elinuxhenrikba96d842021-09-06 16:05:01 +020049 InfoJobCallbackHost: "jobCallbackHost",
elinuxhenrik63a42ca2021-09-06 22:16:24 +020050 InfoJobCallbackPort: 8096,
elinuxhenrikba96d842021-09-06 16:05:01 +020051 InfoCoordinatorAddress: "infoCoordAddr",
elinuxhenrik28038562021-09-21 15:43:11 +020052 MRHost: "mrHost",
53 MRPort: 3908,
elinuxhenrikcce95ff2021-09-05 17:27:02 +020054 }
55 if got := New(); !reflect.DeepEqual(got, &wantConfig) {
56 t.Errorf("New() = %v, want %v", got, &wantConfig)
57 }
58}
59
elinuxhenrik63a42ca2021-09-06 22:16:24 +020060func TestNew_faultyIntValueSetConfigContainDefaultValueAndWarnInLog(t *testing.T) {
elinuxhenrik63a42ca2021-09-06 22:16:24 +020061 assertions := require.New(t)
62 var buf bytes.Buffer
63 log.SetOutput(&buf)
elinuxhenrik63a42ca2021-09-06 22:16:24 +020064
65 os.Setenv("INFO_PRODUCER_SUPERVISION_CALLBACK_PORT", "wrong")
elinuxhenrikb1fb1d82021-09-07 02:58:52 +020066 t.Cleanup(func() {
67 log.SetOutput(os.Stderr)
68 os.Clearenv()
69 })
elinuxhenrikcce95ff2021-09-05 17:27:02 +020070 wantConfig := Config{
elinuxhenrikba96d842021-09-06 16:05:01 +020071 LogLevel: "Info",
72 InfoProducerSupervisionCallbackHost: "",
elinuxhenrik63a42ca2021-09-06 22:16:24 +020073 InfoProducerSupervisionCallbackPort: 8085,
elinuxhenrikba96d842021-09-06 16:05:01 +020074 InfoJobCallbackHost: "",
elinuxhenrik63a42ca2021-09-06 22:16:24 +020075 InfoJobCallbackPort: 8086,
76 InfoCoordinatorAddress: "http://enrichmentservice:8083",
elinuxhenrik28038562021-09-21 15:43:11 +020077 MRHost: "http://message-router.onap",
78 MRPort: 3904,
elinuxhenrik63a42ca2021-09-06 22:16:24 +020079 }
80 if got := New(); !reflect.DeepEqual(got, &wantConfig) {
81 t.Errorf("New() = %v, want %v", got, &wantConfig)
82 }
83 logString := buf.String()
84 assertions.Contains(logString, "Invalid int value: wrong for variable: INFO_PRODUCER_SUPERVISION_CALLBACK_PORT. Default value: 8085 will be used")
85}
86
87func TestNew_envVarsNotSetConfigContainDefaultValues(t *testing.T) {
elinuxhenrik63a42ca2021-09-06 22:16:24 +020088 wantConfig := Config{
89 LogLevel: "Info",
90 InfoProducerSupervisionCallbackHost: "",
91 InfoProducerSupervisionCallbackPort: 8085,
92 InfoJobCallbackHost: "",
93 InfoJobCallbackPort: 8086,
elinuxhenrikba96d842021-09-06 16:05:01 +020094 InfoCoordinatorAddress: "http://enrichmentservice:8083",
elinuxhenrik28038562021-09-21 15:43:11 +020095 MRHost: "http://message-router.onap",
96 MRPort: 3904,
elinuxhenrikcce95ff2021-09-05 17:27:02 +020097 }
98 if got := New(); !reflect.DeepEqual(got, &wantConfig) {
99 t.Errorf("New() = %v, want %v", got, &wantConfig)
100 }
101}