Daniele Rossi | b48f4e1 | 2019-11-14 16:36:43 +0100 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package config |
| 17 | |
| 18 | import ( |
| 19 | "github.com/opencord/voltha-lib-go/v2/pkg/log" |
| 20 | "github.com/stretchr/testify/assert" |
| 21 | "os" |
| 22 | "testing" |
| 23 | ) |
| 24 | |
| 25 | const ( |
| 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 | |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 36 | VOLTHALogLevel = log.FatalLevel |
Daniele Rossi | b48f4e1 | 2019-11-14 16:36:43 +0100 | [diff] [blame] | 37 | ) |
| 38 | |
| 39 | func localInit() { |
| 40 | |
| 41 | // Logger must be configured or bad things happen |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 42 | _, err := log.SetDefaultLogger(log.JSON, VOLTHALogLevel, log.Fields{"instanceId": 1}) |
Daniele Rossi | b48f4e1 | 2019-11-14 16:36:43 +0100 | [diff] [blame] | 43 | if err != nil { |
| 44 | panic(err) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func checkConfigFlags(t *testing.T, cf *ROCoreFlags) { |
| 49 | |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 50 | if cf.ROCoreEndpoint != defaultROCoreEndpoint { |
| 51 | t.Errorf("ROCoreEndpoint want %s, got %s", defaultROCoreEndpoint, cf.ROCoreEndpoint) |
Daniele Rossi | b48f4e1 | 2019-11-14 16:36:43 +0100 | [diff] [blame] | 52 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 53 | if cf.GrpcHost != defaultGrpcHost { |
| 54 | t.Errorf("GrpcHost want %s, got %s", defaultGrpcHost, cf.GrpcHost) |
Daniele Rossi | b48f4e1 | 2019-11-14 16:36:43 +0100 | [diff] [blame] | 55 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 56 | if cf.GrpcPort != defaultGrpcPort { |
| 57 | t.Errorf("GrpcPort want %d, got %d", defaultGrpcPort, cf.GrpcPort) |
Daniele Rossi | b48f4e1 | 2019-11-14 16:36:43 +0100 | [diff] [blame] | 58 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 59 | if cf.KVStoreType != defaultKVStoreType { |
| 60 | t.Errorf("KVStoreType want %s, got %s", defaultKVStoreType, cf.KVStoreType) |
Daniele Rossi | b48f4e1 | 2019-11-14 16:36:43 +0100 | [diff] [blame] | 61 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 62 | if cf.KVStoreTimeout != defaultKVStoreTimeout { |
| 63 | t.Errorf("KVStoreTimeout want %d, got %d", defaultKVStoreTimeout, cf.KVStoreTimeout) |
Daniele Rossi | b48f4e1 | 2019-11-14 16:36:43 +0100 | [diff] [blame] | 64 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 65 | if cf.KVStoreHost != defaultKVStoreHost { |
| 66 | t.Errorf("KVStoreHost want %s, got %s", defaultKVStoreHost, cf.KVStoreHost) |
Daniele Rossi | b48f4e1 | 2019-11-14 16:36:43 +0100 | [diff] [blame] | 67 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 68 | if cf.KVStorePort != defaultKVStorePort { |
| 69 | t.Errorf("KVStorePort want %d, got %d", defaultKVStorePort, cf.KVStorePort) |
Daniele Rossi | b48f4e1 | 2019-11-14 16:36:43 +0100 | [diff] [blame] | 70 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 71 | if cf.KVTxnKeyDelTime != defaultKVTxnKeyDelTime { |
| 72 | t.Errorf("KVTxnKeyDelTime want %d, got %d", defaultKVTxnKeyDelTime, cf.KVTxnKeyDelTime) |
Daniele Rossi | b48f4e1 | 2019-11-14 16:36:43 +0100 | [diff] [blame] | 73 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 74 | if cf.CoreTopic != defaultCoreTopic { |
| 75 | t.Errorf("CoreTopic want %s, got %s", defaultCoreTopic, cf.CoreTopic) |
Daniele Rossi | b48f4e1 | 2019-11-14 16:36:43 +0100 | [diff] [blame] | 76 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 77 | if cf.LogLevel != defaultLogLevel { |
| 78 | t.Errorf("LogLevel want %d, got %d", defaultLogLevel, cf.LogLevel) |
Daniele Rossi | b48f4e1 | 2019-11-14 16:36:43 +0100 | [diff] [blame] | 79 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 80 | if cf.Banner != defaultBanner { |
| 81 | t.Errorf("Banner want %v, got %v", defaultBanner, cf.Banner) |
Daniele Rossi | b48f4e1 | 2019-11-14 16:36:43 +0100 | [diff] [blame] | 82 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 83 | if cf.DisplayVersionOnly != defaultDisplayVersionOnly { |
| 84 | t.Errorf("DisplayVersionOnly want %v, got %v", defaultDisplayVersionOnly, cf.DisplayVersionOnly) |
Daniele Rossi | b48f4e1 | 2019-11-14 16:36:43 +0100 | [diff] [blame] | 85 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 86 | if cf.ROCoreKey != defaultROCoreKey { |
| 87 | t.Errorf("ROCoreKey want %s, got %s", defaultROCoreKey, cf.ROCoreKey) |
Daniele Rossi | b48f4e1 | 2019-11-14 16:36:43 +0100 | [diff] [blame] | 88 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 89 | if cf.ROCoreCert != defaultROCoreCert { |
| 90 | t.Errorf("ROCoreCert want %s, got %s", defaultROCoreCert, cf.ROCoreCert) |
Daniele Rossi | b48f4e1 | 2019-11-14 16:36:43 +0100 | [diff] [blame] | 91 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 92 | if cf.ROCoreCA != defaultROCoreCA { |
| 93 | t.Errorf("ROCoreCA want %s, got %s", defaultROCoreCA, cf.ROCoreCA) |
Daniele Rossi | b48f4e1 | 2019-11-14 16:36:43 +0100 | [diff] [blame] | 94 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 95 | if cf.AffinityRouterTopic != defaultAffinityRouterTopic { |
| 96 | t.Errorf("AffinityRouterTopic want %s, got %s", defaultAffinityRouterTopic, cf.AffinityRouterTopic) |
Daniele Rossi | b48f4e1 | 2019-11-14 16:36:43 +0100 | [diff] [blame] | 97 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 98 | if cf.ProbeHost != defaultProbeHost { |
| 99 | t.Errorf("ProbeHost want %s, got %s", defaultProbeHost, cf.ProbeHost) |
Daniele Rossi | b48f4e1 | 2019-11-14 16:36:43 +0100 | [diff] [blame] | 100 | } |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 101 | if cf.ProbePort != defaultProbePort { |
| 102 | t.Errorf("ProbePort want %d, got %d", defaultProbePort, cf.ProbePort) |
Daniele Rossi | b48f4e1 | 2019-11-14 16:36:43 +0100 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | func 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 | } |