blob: e66a8182c4476ef18b46c6ecc53a34433e7d342f [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"
elinuxhenrik6f5d3d12021-12-23 16:36:31 +010025 "encoding/json"
elinuxhenrikcce95ff2021-09-05 17:27:02 +020026 "os"
elinuxhenrikf4969902021-11-24 15:53:24 +010027 "path/filepath"
elinuxhenrikcce95ff2021-09-05 17:27:02 +020028 "testing"
elinuxhenrik63a42ca2021-09-06 22:16:24 +020029
30 log "github.com/sirupsen/logrus"
31 "github.com/stretchr/testify/require"
elinuxhenrikcce95ff2021-09-05 17:27:02 +020032)
33
34func TestNew_envVarsSetConfigContainSetValues(t *testing.T) {
elinuxhenrik65a53d22021-09-29 15:41:26 +020035 assertions := require.New(t)
elinuxhenrikcce95ff2021-09-05 17:27:02 +020036 os.Setenv("LOG_LEVEL", "Debug")
elinuxhenrik382870d2021-09-23 11:09:09 +020037 os.Setenv("INFO_PRODUCER_HOST", "producerHost")
38 os.Setenv("INFO_PRODUCER_PORT", "8095")
elinuxhenrika77cd652021-09-06 10:56:21 +020039 os.Setenv("INFO_COORD_ADDR", "infoCoordAddr")
elinuxhenrikc4960f12021-10-28 16:27:57 +020040 os.Setenv("DMAAP_MR_ADDR", "mrHost:3908")
elinuxhenrik6f5d3d12021-12-23 16:36:31 +010041 os.Setenv("KAFKA_BOOTSTRAP_SERVERS", "localhost:9093")
elinuxhenrikc4960f12021-10-28 16:27:57 +020042 os.Setenv("PRODUCER_CERT_PATH", "cert")
43 os.Setenv("PRODUCER_KEY_PATH", "key")
elinuxhenrikb1fb1d82021-09-07 02:58:52 +020044 t.Cleanup(func() {
45 os.Clearenv()
46 })
elinuxhenrikcce95ff2021-09-05 17:27:02 +020047 wantConfig := Config{
elinuxhenrik65a53d22021-09-29 15:41:26 +020048 LogLevel: log.DebugLevel,
elinuxhenrik382870d2021-09-23 11:09:09 +020049 InfoProducerHost: "producerHost",
50 InfoProducerPort: 8095,
51 InfoCoordinatorAddress: "infoCoordAddr",
elinuxhenrikc4960f12021-10-28 16:27:57 +020052 DMaaPMRAddress: "mrHost:3908",
elinuxhenrik6f5d3d12021-12-23 16:36:31 +010053 KafkaBootstrapServers: "localhost:9093",
elinuxhenrikc4960f12021-10-28 16:27:57 +020054 ProducerCertPath: "cert",
55 ProducerKeyPath: "key",
elinuxhenrikcce95ff2021-09-05 17:27:02 +020056 }
elinuxhenrik65a53d22021-09-29 15:41:26 +020057 got := New()
58
59 assertions.Equal(&wantConfig, got)
elinuxhenrikcce95ff2021-09-05 17:27:02 +020060}
61
elinuxhenrik63a42ca2021-09-06 22:16:24 +020062func TestNew_faultyIntValueSetConfigContainDefaultValueAndWarnInLog(t *testing.T) {
elinuxhenrik63a42ca2021-09-06 22:16:24 +020063 assertions := require.New(t)
64 var buf bytes.Buffer
65 log.SetOutput(&buf)
elinuxhenrik63a42ca2021-09-06 22:16:24 +020066
elinuxhenrik382870d2021-09-23 11:09:09 +020067 os.Setenv("INFO_PRODUCER_PORT", "wrong")
elinuxhenrikb1fb1d82021-09-07 02:58:52 +020068 t.Cleanup(func() {
69 log.SetOutput(os.Stderr)
70 os.Clearenv()
71 })
elinuxhenrikcce95ff2021-09-05 17:27:02 +020072 wantConfig := Config{
elinuxhenrik65a53d22021-09-29 15:41:26 +020073 LogLevel: log.InfoLevel,
elinuxhenrik382870d2021-09-23 11:09:09 +020074 InfoProducerHost: "",
75 InfoProducerPort: 8085,
PatrikBuhr118d82d2021-11-26 14:47:25 +010076 InfoCoordinatorAddress: "https://informationservice:8434",
elinuxhenrikc4960f12021-10-28 16:27:57 +020077 DMaaPMRAddress: "https://message-router.onap:3905",
elinuxhenrik6f5d3d12021-12-23 16:36:31 +010078 KafkaBootstrapServers: "localhost:9092",
elinuxhenrikbfce1942021-11-08 15:58:20 +010079 ProducerCertPath: "security/producer.crt",
80 ProducerKeyPath: "security/producer.key",
elinuxhenrik63a42ca2021-09-06 22:16:24 +020081 }
elinuxhenrikf4969902021-11-24 15:53:24 +010082 got := New()
83 assertions.Equal(&wantConfig, got)
elinuxhenrik63a42ca2021-09-06 22:16:24 +020084 logString := buf.String()
elinuxhenrik382870d2021-09-23 11:09:09 +020085 assertions.Contains(logString, "Invalid int value: wrong for variable: INFO_PRODUCER_PORT. Default value: 8085 will be used")
elinuxhenrik63a42ca2021-09-06 22:16:24 +020086}
87
elinuxhenrik65a53d22021-09-29 15:41:26 +020088func TestNew_envFaultyLogLevelConfigContainDefaultValues(t *testing.T) {
89 assertions := require.New(t)
90 var buf bytes.Buffer
91 log.SetOutput(&buf)
92
93 os.Setenv("LOG_LEVEL", "wrong")
94 t.Cleanup(func() {
95 log.SetOutput(os.Stderr)
96 os.Clearenv()
97 })
98
elinuxhenrik63a42ca2021-09-06 22:16:24 +020099 wantConfig := Config{
elinuxhenrik65a53d22021-09-29 15:41:26 +0200100 LogLevel: log.InfoLevel,
elinuxhenrik382870d2021-09-23 11:09:09 +0200101 InfoProducerHost: "",
102 InfoProducerPort: 8085,
PatrikBuhr118d82d2021-11-26 14:47:25 +0100103 InfoCoordinatorAddress: "https://informationservice:8434",
elinuxhenrikc4960f12021-10-28 16:27:57 +0200104 DMaaPMRAddress: "https://message-router.onap:3905",
elinuxhenrik6f5d3d12021-12-23 16:36:31 +0100105 KafkaBootstrapServers: "localhost:9092",
elinuxhenrikbfce1942021-11-08 15:58:20 +0100106 ProducerCertPath: "security/producer.crt",
107 ProducerKeyPath: "security/producer.key",
elinuxhenrikcce95ff2021-09-05 17:27:02 +0200108 }
elinuxhenrik65a53d22021-09-29 15:41:26 +0200109
110 got := New()
111
112 assertions.Equal(&wantConfig, got)
113 logString := buf.String()
114 assertions.Contains(logString, "Invalid log level: wrong. Log level will be Info!")
elinuxhenrikcce95ff2021-09-05 17:27:02 +0200115}
elinuxhenrikf4969902021-11-24 15:53:24 +0100116
elinuxhenrik6f5d3d12021-12-23 16:36:31 +0100117const typeDefinition = `{"types": [{"id": "type1", "dmaapTopicUrl": "events/unauthenticated.SEC_FAULT_OUTPUT/dmaapmediatorproducer/type1"}, {"id": "type2", "kafkaInputTopic": "TestTopic"}]}`
118const typeSchemaFileContent = `{
119 "$schema": "http://json-schema.org/draft-04/schema#",
120 "type": "object",
121 "properties": {
122 "filter": {
123 "type": "string"
124 }
125 },
126 "additionalProperties": false
127 }`
elinuxhenrikf4969902021-11-24 15:53:24 +0100128
129func TestGetTypesFromConfiguration_fileOkShouldReturnSliceOfTypeDefinitions(t *testing.T) {
130 assertions := require.New(t)
131 typesDir, err := os.MkdirTemp("", "configs")
132 if err != nil {
133 t.Errorf("Unable to create temporary directory for types due to: %v", err)
134 }
135 fname := filepath.Join(typesDir, "type_config.json")
136 t.Cleanup(func() {
137 os.RemoveAll(typesDir)
138 })
139 if err = os.WriteFile(fname, []byte(typeDefinition), 0666); err != nil {
140 t.Errorf("Unable to create temporary config file for types due to: %v", err)
141 }
elinuxhenrik6f5d3d12021-12-23 16:36:31 +0100142 fname = filepath.Join(typesDir, "typeSchemaDmaap.json")
143 if err = os.WriteFile(fname, []byte(typeSchemaFileContent), 0666); err != nil {
144 t.Errorf("Unable to create temporary schema file for DMaaP type due to: %v", err)
elinuxhenrikf4969902021-11-24 15:53:24 +0100145 }
elinuxhenrik6f5d3d12021-12-23 16:36:31 +0100146 fname = filepath.Join(typesDir, "typeSchemaKafka.json")
147 if err = os.WriteFile(fname, []byte(typeSchemaFileContent), 0666); err != nil {
148 t.Errorf("Unable to create temporary schema file for Kafka type due to: %v", err)
149 }
150 var typeSchemaObj interface{}
151 json.Unmarshal([]byte(typeSchemaFileContent), &typeSchemaObj)
152
153 types, err := GetJobTypesFromConfiguration(typesDir)
154
155 wantedDMaaPType := TypeDefinition{
156 Identity: "type1",
157 DMaaPTopicURL: "events/unauthenticated.SEC_FAULT_OUTPUT/dmaapmediatorproducer/type1",
158 TypeSchema: typeSchemaObj,
159 }
160 wantedKafkaType := TypeDefinition{
161 Identity: "type2",
162 KafkaInputTopic: "TestTopic",
163 TypeSchema: typeSchemaObj,
164 }
165 wantedTypes := []TypeDefinition{wantedDMaaPType, wantedKafkaType}
elinuxhenrikf4969902021-11-24 15:53:24 +0100166 assertions.EqualValues(wantedTypes, types)
167 assertions.Nil(err)
168}