blob: 90d3c0368283f2e7d1479687db6b5e40363a7ae7 [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) {
elinuxhenrik65a53d22021-09-29 15:41:26 +020034 assertions := require.New(t)
elinuxhenrikcce95ff2021-09-05 17:27:02 +020035 os.Setenv("LOG_LEVEL", "Debug")
elinuxhenrik382870d2021-09-23 11:09:09 +020036 os.Setenv("INFO_PRODUCER_HOST", "producerHost")
37 os.Setenv("INFO_PRODUCER_PORT", "8095")
elinuxhenrika77cd652021-09-06 10:56:21 +020038 os.Setenv("INFO_COORD_ADDR", "infoCoordAddr")
elinuxhenrikc4960f12021-10-28 16:27:57 +020039 os.Setenv("DMAAP_MR_ADDR", "mrHost:3908")
40 os.Setenv("PRODUCER_CERT_PATH", "cert")
41 os.Setenv("PRODUCER_KEY_PATH", "key")
elinuxhenrikb1fb1d82021-09-07 02:58:52 +020042 t.Cleanup(func() {
43 os.Clearenv()
44 })
elinuxhenrikcce95ff2021-09-05 17:27:02 +020045 wantConfig := Config{
elinuxhenrik65a53d22021-09-29 15:41:26 +020046 LogLevel: log.DebugLevel,
elinuxhenrik382870d2021-09-23 11:09:09 +020047 InfoProducerHost: "producerHost",
48 InfoProducerPort: 8095,
49 InfoCoordinatorAddress: "infoCoordAddr",
elinuxhenrikc4960f12021-10-28 16:27:57 +020050 DMaaPMRAddress: "mrHost:3908",
51 ProducerCertPath: "cert",
52 ProducerKeyPath: "key",
elinuxhenrikcce95ff2021-09-05 17:27:02 +020053 }
elinuxhenrik65a53d22021-09-29 15:41:26 +020054 got := New()
55
56 assertions.Equal(&wantConfig, got)
elinuxhenrikcce95ff2021-09-05 17:27:02 +020057}
58
elinuxhenrik63a42ca2021-09-06 22:16:24 +020059func TestNew_faultyIntValueSetConfigContainDefaultValueAndWarnInLog(t *testing.T) {
elinuxhenrik63a42ca2021-09-06 22:16:24 +020060 assertions := require.New(t)
61 var buf bytes.Buffer
62 log.SetOutput(&buf)
elinuxhenrik63a42ca2021-09-06 22:16:24 +020063
elinuxhenrik382870d2021-09-23 11:09:09 +020064 os.Setenv("INFO_PRODUCER_PORT", "wrong")
elinuxhenrikb1fb1d82021-09-07 02:58:52 +020065 t.Cleanup(func() {
66 log.SetOutput(os.Stderr)
67 os.Clearenv()
68 })
elinuxhenrikcce95ff2021-09-05 17:27:02 +020069 wantConfig := Config{
elinuxhenrik65a53d22021-09-29 15:41:26 +020070 LogLevel: log.InfoLevel,
elinuxhenrik382870d2021-09-23 11:09:09 +020071 InfoProducerHost: "",
72 InfoProducerPort: 8085,
elinuxhenrikc4960f12021-10-28 16:27:57 +020073 InfoCoordinatorAddress: "https://enrichmentservice:8434",
74 DMaaPMRAddress: "https://message-router.onap:3905",
elinuxhenrikbfce1942021-11-08 15:58:20 +010075 ProducerCertPath: "security/producer.crt",
76 ProducerKeyPath: "security/producer.key",
elinuxhenrik63a42ca2021-09-06 22:16:24 +020077 }
78 if got := New(); !reflect.DeepEqual(got, &wantConfig) {
79 t.Errorf("New() = %v, want %v", got, &wantConfig)
80 }
81 logString := buf.String()
elinuxhenrik382870d2021-09-23 11:09:09 +020082 assertions.Contains(logString, "Invalid int value: wrong for variable: INFO_PRODUCER_PORT. Default value: 8085 will be used")
elinuxhenrik63a42ca2021-09-06 22:16:24 +020083}
84
elinuxhenrik65a53d22021-09-29 15:41:26 +020085func TestNew_envFaultyLogLevelConfigContainDefaultValues(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
elinuxhenrik63a42ca2021-09-06 22:16:24 +020096 wantConfig := Config{
elinuxhenrik65a53d22021-09-29 15:41:26 +020097 LogLevel: log.InfoLevel,
elinuxhenrik382870d2021-09-23 11:09:09 +020098 InfoProducerHost: "",
99 InfoProducerPort: 8085,
elinuxhenrikc4960f12021-10-28 16:27:57 +0200100 InfoCoordinatorAddress: "https://enrichmentservice:8434",
101 DMaaPMRAddress: "https://message-router.onap:3905",
elinuxhenrikbfce1942021-11-08 15:58:20 +0100102 ProducerCertPath: "security/producer.crt",
103 ProducerKeyPath: "security/producer.key",
elinuxhenrikcce95ff2021-09-05 17:27:02 +0200104 }
elinuxhenrik65a53d22021-09-29 15:41:26 +0200105
106 got := New()
107
108 assertions.Equal(&wantConfig, got)
109 logString := buf.String()
110 assertions.Contains(logString, "Invalid log level: wrong. Log level will be Info!")
elinuxhenrikcce95ff2021-09-05 17:27:02 +0200111}