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 | |
| 36 | VOLTHA_LOGLEVEL = log.FatalLevel |
| 37 | ) |
| 38 | |
| 39 | func localInit() { |
| 40 | |
| 41 | // Logger must be configured or bad things happen |
| 42 | _, err := log.SetDefaultLogger(log.JSON, VOLTHA_LOGLEVEL, log.Fields{"instanceId": 1}) |
| 43 | if err != nil { |
| 44 | panic(err) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func checkConfigFlags(t *testing.T, cf *ROCoreFlags) { |
| 49 | |
| 50 | if cf.ROCoreEndpoint != default_ROCoreEndpoint { |
| 51 | t.Errorf("ROCoreEndpoint want %s, got %s", default_ROCoreEndpoint, cf.ROCoreEndpoint) |
| 52 | } |
| 53 | if cf.GrpcHost != default_GrpcHost { |
| 54 | t.Errorf("GrpcHost want %s, got %s", default_GrpcHost, cf.GrpcHost) |
| 55 | } |
| 56 | if cf.GrpcPort != default_GrpcPort { |
| 57 | t.Errorf("GrpcPort want %d, got %d", default_GrpcPort, cf.GrpcPort) |
| 58 | } |
| 59 | if cf.KVStoreType != default_KVStoreType { |
| 60 | t.Errorf("KVStoreType want %s, got %s", default_KVStoreType, cf.KVStoreType) |
| 61 | } |
| 62 | if cf.KVStoreTimeout != default_KVStoreTimeout { |
| 63 | t.Errorf("KVStoreTimeout want %d, got %d", default_KVStoreTimeout, cf.KVStoreTimeout) |
| 64 | } |
| 65 | if cf.KVStoreHost != default_KVStoreHost { |
| 66 | t.Errorf("KVStoreHost want %s, got %s", default_KVStoreHost, cf.KVStoreHost) |
| 67 | } |
| 68 | if cf.KVStorePort != default_KVStorePort { |
| 69 | t.Errorf("KVStorePort want %d, got %d", default_KVStorePort, cf.KVStorePort) |
| 70 | } |
| 71 | if cf.KVTxnKeyDelTime != default_KVTxnKeyDelTime { |
| 72 | t.Errorf("KVTxnKeyDelTime want %d, got %d", default_KVTxnKeyDelTime, cf.KVTxnKeyDelTime) |
| 73 | } |
| 74 | if cf.CoreTopic != default_CoreTopic { |
| 75 | t.Errorf("CoreTopic want %s, got %s", default_CoreTopic, cf.CoreTopic) |
| 76 | } |
| 77 | if cf.LogLevel != default_LogLevel { |
| 78 | t.Errorf("LogLevel want %d, got %d", default_LogLevel, cf.LogLevel) |
| 79 | } |
| 80 | if cf.Banner != default_Banner { |
| 81 | t.Errorf("Banner want %v, got %v", default_Banner, cf.Banner) |
| 82 | } |
| 83 | if cf.DisplayVersionOnly != default_DisplayVersionOnly { |
| 84 | t.Errorf("DisplayVersionOnly want %v, got %v", default_DisplayVersionOnly, cf.DisplayVersionOnly) |
| 85 | } |
| 86 | if cf.ROCoreKey != default_ROCoreKey { |
| 87 | t.Errorf("ROCoreKey want %s, got %s", default_ROCoreKey, cf.ROCoreKey) |
| 88 | } |
| 89 | if cf.ROCoreCert != default_ROCoreCert { |
| 90 | t.Errorf("ROCoreCert want %s, got %s", default_ROCoreCert, cf.ROCoreCert) |
| 91 | } |
| 92 | if cf.ROCoreCA != default_ROCoreCA { |
| 93 | t.Errorf("ROCoreCA want %s, got %s", default_ROCoreCA, cf.ROCoreCA) |
| 94 | } |
| 95 | if cf.AffinityRouterTopic != default_Affinity_Router_Topic { |
| 96 | t.Errorf("AffinityRouterTopic want %s, got %s", default_Affinity_Router_Topic, cf.AffinityRouterTopic) |
| 97 | } |
| 98 | if cf.ProbeHost != default_ProbeHost { |
| 99 | t.Errorf("ProbeHost want %s, got %s", default_ProbeHost, cf.ProbeHost) |
| 100 | } |
| 101 | if cf.ProbePort != default_ProbePort { |
| 102 | t.Errorf("ProbePort want %d, got %d", default_ProbePort, cf.ProbePort) |
| 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 | } |