blob: 2ebcfbbf4b4d6bbbdc11ef82e03439738770a94e [file] [log] [blame]
Daniele Rossib48f4e12019-11-14 16:36:43 +01001/*
2 * Copyright 2019-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package config
17
18import (
19 "github.com/opencord/voltha-lib-go/v2/pkg/log"
20 "github.com/stretchr/testify/assert"
21 "os"
22 "testing"
23)
24
25const (
26 /*
27 * This sets the LogLevel of the Voltha logger. It's pinned to FatalLevel here, as we
28 * generally don't want to see logger output, even when running go test in verbose
29 * mode. Even "Error" level messages are expected to be output by some unit tests.
30 *
31 * If you are developing a unit test, and experiencing problems or wish additional
32 * debugging from Voltha, then changing this constant to log.DebugLevel may be
33 * useful.
34 */
35
npujar03b018e2019-11-13 15:29:36 +053036 VOLTHALogLevel = log.FatalLevel
Daniele Rossib48f4e12019-11-14 16:36:43 +010037)
38
39func localInit() {
40
41 // Logger must be configured or bad things happen
npujar03b018e2019-11-13 15:29:36 +053042 _, err := log.SetDefaultLogger(log.JSON, VOLTHALogLevel, log.Fields{"instanceId": 1})
Daniele Rossib48f4e12019-11-14 16:36:43 +010043 if err != nil {
44 panic(err)
45 }
46}
47
48func checkConfigFlags(t *testing.T, cf *ROCoreFlags) {
49
npujar03b018e2019-11-13 15:29:36 +053050 if cf.ROCoreEndpoint != defaultROCoreEndpoint {
51 t.Errorf("ROCoreEndpoint want %s, got %s", defaultROCoreEndpoint, cf.ROCoreEndpoint)
Daniele Rossib48f4e12019-11-14 16:36:43 +010052 }
npujar03b018e2019-11-13 15:29:36 +053053 if cf.GrpcHost != defaultGrpcHost {
54 t.Errorf("GrpcHost want %s, got %s", defaultGrpcHost, cf.GrpcHost)
Daniele Rossib48f4e12019-11-14 16:36:43 +010055 }
npujar03b018e2019-11-13 15:29:36 +053056 if cf.GrpcPort != defaultGrpcPort {
57 t.Errorf("GrpcPort want %d, got %d", defaultGrpcPort, cf.GrpcPort)
Daniele Rossib48f4e12019-11-14 16:36:43 +010058 }
npujar03b018e2019-11-13 15:29:36 +053059 if cf.KVStoreType != defaultKVStoreType {
60 t.Errorf("KVStoreType want %s, got %s", defaultKVStoreType, cf.KVStoreType)
Daniele Rossib48f4e12019-11-14 16:36:43 +010061 }
npujar03b018e2019-11-13 15:29:36 +053062 if cf.KVStoreTimeout != defaultKVStoreTimeout {
63 t.Errorf("KVStoreTimeout want %d, got %d", defaultKVStoreTimeout, cf.KVStoreTimeout)
Daniele Rossib48f4e12019-11-14 16:36:43 +010064 }
npujar03b018e2019-11-13 15:29:36 +053065 if cf.KVStoreHost != defaultKVStoreHost {
66 t.Errorf("KVStoreHost want %s, got %s", defaultKVStoreHost, cf.KVStoreHost)
Daniele Rossib48f4e12019-11-14 16:36:43 +010067 }
npujar03b018e2019-11-13 15:29:36 +053068 if cf.KVStorePort != defaultKVStorePort {
69 t.Errorf("KVStorePort want %d, got %d", defaultKVStorePort, cf.KVStorePort)
Daniele Rossib48f4e12019-11-14 16:36:43 +010070 }
npujar03b018e2019-11-13 15:29:36 +053071 if cf.KVTxnKeyDelTime != defaultKVTxnKeyDelTime {
72 t.Errorf("KVTxnKeyDelTime want %d, got %d", defaultKVTxnKeyDelTime, cf.KVTxnKeyDelTime)
Daniele Rossib48f4e12019-11-14 16:36:43 +010073 }
npujar03b018e2019-11-13 15:29:36 +053074 if cf.CoreTopic != defaultCoreTopic {
75 t.Errorf("CoreTopic want %s, got %s", defaultCoreTopic, cf.CoreTopic)
Daniele Rossib48f4e12019-11-14 16:36:43 +010076 }
npujar03b018e2019-11-13 15:29:36 +053077 if cf.LogLevel != defaultLogLevel {
78 t.Errorf("LogLevel want %d, got %d", defaultLogLevel, cf.LogLevel)
Daniele Rossib48f4e12019-11-14 16:36:43 +010079 }
npujar03b018e2019-11-13 15:29:36 +053080 if cf.Banner != defaultBanner {
81 t.Errorf("Banner want %v, got %v", defaultBanner, cf.Banner)
Daniele Rossib48f4e12019-11-14 16:36:43 +010082 }
npujar03b018e2019-11-13 15:29:36 +053083 if cf.DisplayVersionOnly != defaultDisplayVersionOnly {
84 t.Errorf("DisplayVersionOnly want %v, got %v", defaultDisplayVersionOnly, cf.DisplayVersionOnly)
Daniele Rossib48f4e12019-11-14 16:36:43 +010085 }
npujar03b018e2019-11-13 15:29:36 +053086 if cf.ROCoreKey != defaultROCoreKey {
87 t.Errorf("ROCoreKey want %s, got %s", defaultROCoreKey, cf.ROCoreKey)
Daniele Rossib48f4e12019-11-14 16:36:43 +010088 }
npujar03b018e2019-11-13 15:29:36 +053089 if cf.ROCoreCert != defaultROCoreCert {
90 t.Errorf("ROCoreCert want %s, got %s", defaultROCoreCert, cf.ROCoreCert)
Daniele Rossib48f4e12019-11-14 16:36:43 +010091 }
npujar03b018e2019-11-13 15:29:36 +053092 if cf.ROCoreCA != defaultROCoreCA {
93 t.Errorf("ROCoreCA want %s, got %s", defaultROCoreCA, cf.ROCoreCA)
Daniele Rossib48f4e12019-11-14 16:36:43 +010094 }
npujar03b018e2019-11-13 15:29:36 +053095 if cf.AffinityRouterTopic != defaultAffinityRouterTopic {
96 t.Errorf("AffinityRouterTopic want %s, got %s", defaultAffinityRouterTopic, cf.AffinityRouterTopic)
Daniele Rossib48f4e12019-11-14 16:36:43 +010097 }
npujar03b018e2019-11-13 15:29:36 +053098 if cf.ProbeHost != defaultProbeHost {
99 t.Errorf("ProbeHost want %s, got %s", defaultProbeHost, cf.ProbeHost)
Daniele Rossib48f4e12019-11-14 16:36:43 +0100100 }
npujar03b018e2019-11-13 15:29:36 +0530101 if cf.ProbePort != defaultProbePort {
102 t.Errorf("ProbePort want %d, got %d", defaultProbePort, cf.ProbePort)
Daniele Rossib48f4e12019-11-14 16:36:43 +0100103 }
104}
105
106func TestNewROCoreFlags(t *testing.T) {
107 localInit()
108
109 var testStr string
110
111 configFlags := NewROCoreFlags()
112 assert.NotNil(t, configFlags)
113
114 configFlags.ParseCommandArguments()
115 checkConfigFlags(t, configFlags)
116
117 seErr := os.Setenv("HOSTNAME", "PC-4")
118 if seErr == nil {
119 testStr = getContainerInfo()
120 assert.NotNil(t, testStr)
121 t.Logf("hostname: %s \n", testStr)
122 if testStr != "PC-4" {
123 t.Errorf("getContainerInfo failed. want: %s, got: %s", "PC-3", testStr)
124 }
125 } else {
126 testStr = getContainerInfo()
127 assert.NotNil(t, testStr)
128 }
129}