github.com/jingruilea/kubeedge@v1.2.0-beta.0.0.20200410162146-4bb8902b3879/edge/pkg/eventbus/common/util/common_test.go (about) 1 /* 2 Copyright 2019 The KubeEdge Authors. 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 17 package util 18 19 import ( 20 "crypto/tls" 21 "fmt" 22 "net/url" 23 "reflect" 24 "testing" 25 "time" 26 27 MQTT "github.com/eclipse/paho.mqtt.golang" 28 "github.com/stretchr/testify/assert" 29 "k8s.io/klog" 30 ) 31 32 var clientOptions = MQTT.NewClientOptions() 33 34 //TestCheckKeyExist checks the functionality of CheckKeyExist function 35 func TestCheckKeyExist(t *testing.T) { 36 37 tests := []struct { 38 name string 39 keys []string 40 disinfo map[string]interface{} 41 expectedError string 42 }{ 43 { 44 name: "TestCheckKeyExist: Key exists in passed map", 45 keys: []string{"key1"}, 46 disinfo: map[string]interface{}{"key1": "value1"}, 47 expectedError: "", 48 }, 49 { 50 name: "TestCheckKeyExist: Key does not exists in passed map", 51 keys: []string{"key1"}, 52 disinfo: map[string]interface{}{"key2": "value2"}, 53 expectedError: "key not found", 54 }, 55 } 56 for _, tt := range tests { 57 t.Run(tt.name, func(t *testing.T) { 58 err := CheckKeyExist(tt.keys, tt.disinfo) 59 assert.Containsf(t, fmt.Sprintf("%e", err), tt.expectedError, "error message %s", "formatted") 60 }) 61 } 62 } 63 64 //TestCheckClientToken checks client token received 65 func TestCheckClientToken(t *testing.T) { 66 tests := []struct { 67 name string 68 token MQTT.Token 69 expectedError string 70 }{ 71 { 72 name: "TestCheckClientToken: Client Token with no error", 73 token: MQTT.NewClient(clientOptions).Connect(), 74 expectedError: "", 75 }, 76 { 77 name: "TestCheckClientToken: Client token created with error", 78 token: MQTT.NewClient(HubClientInit("tcp://127.0.0:8000", "12345", "", "")).Connect(), 79 expectedError: "Network Error", 80 }, 81 } 82 for _, tt := range tests { 83 t.Run(tt.name, func(t *testing.T) { 84 rs, err := CheckClientToken(tt.token) 85 fmt.Printf("rs = %v", rs) 86 assert.Containsf(t, fmt.Sprintf("%e", err), tt.expectedError, "error message %s", "formatted") 87 }) 88 } 89 } 90 91 //TestPathExist checks the functionality of PathExist function 92 func TestPathExist(t *testing.T) { 93 tests := []struct { 94 name string 95 path string 96 want bool 97 }{ 98 { 99 name: "TestPathExist: Path Exist", 100 path: "/", 101 want: true, 102 }, 103 { 104 name: "TestPathExist: Path does not Exist", 105 path: "Wrong_Path", 106 want: false, 107 }, 108 } 109 for _, tt := range tests { 110 t.Run(tt.name, func(t *testing.T) { 111 if got := PathExist(tt.path); !reflect.DeepEqual(got, tt.want) { 112 t.Errorf("common.TestPathExist() got = %v, want = %v", got, tt.want) 113 } 114 }) 115 } 116 } 117 118 //TestHubClientInit checks the HubClientInit method that it returns the same clientOptions object or not 119 func TestHubClientInit(t *testing.T) { 120 tests := []struct { 121 name string 122 server string 123 clientID string 124 username string 125 password string 126 want *MQTT.ClientOptions 127 expectedError error 128 }{ 129 { 130 name: "TestHubclientInit: given username and password", 131 server: "tcp://127.0.0.1:1880", 132 clientID: "12345", 133 username: "test_user", 134 password: "123456789", 135 want: clientOptions, 136 expectedError: nil, 137 }, 138 { 139 name: "TestHubclientInit: given username and no password", 140 server: "tcp://127.0.0.1:1882", 141 clientID: "12345", 142 username: "test_user", 143 password: "", 144 want: clientOptions, 145 expectedError: nil, 146 }, 147 { 148 name: "TestHubclientInit: no username and password", 149 server: "tcp://127.0.0.1:1883", 150 clientID: "12345", 151 username: "", 152 password: "", 153 want: clientOptions, 154 expectedError: nil, 155 }, 156 } 157 for _, tt := range tests { 158 t.Run(tt.name, func(t *testing.T) { 159 brokerURI, _ := url.Parse(tt.server) 160 tt.want.Servers = append([]*url.URL{}, brokerURI) 161 tt.want.ClientID = tt.clientID 162 tt.want.Username = tt.username 163 tt.want.Password = tt.password 164 tt.want.TLSConfig = &tls.Config{InsecureSkipVerify: true, ClientAuth: tls.NoClientCert} 165 got := HubClientInit(tt.server, tt.clientID, tt.username, tt.password) 166 assert.Equal(t, tt.want.Servers, got.Servers) 167 assert.Equal(t, tt.want.ClientID, got.ClientID) 168 assert.Equal(t, tt.want.CleanSession, got.CleanSession) 169 assert.Equal(t, tt.want.Username, got.Username) 170 assert.Equal(t, tt.want.Password, got.Password) 171 assert.Equal(t, tt.want.TLSConfig, got.TLSConfig) 172 }) 173 } 174 } 175 176 //TestLoopConnect checks LoopConnect to connect to MQTT broker 177 func TestLoopConnect(t *testing.T) { 178 tests := []struct { 179 name string 180 client MQTT.Client 181 clientID string 182 clientOptions *MQTT.ClientOptions 183 connect bool 184 }{ 185 { 186 name: "TestLoopConnect: success in connection", 187 clientID: "12345", 188 clientOptions: MQTT.NewClientOptions(), 189 connect: true, 190 }, 191 { 192 name: "TestLoopConnect: Connection error", 193 clientID: "12345", 194 clientOptions: HubClientInit("tcp://127.0.0.1:1882", "12345", "test_user", "123456789"), 195 connect: false, 196 }, 197 } 198 for _, tt := range tests { 199 t.Run(tt.name, func(t *testing.T) { 200 tt.client = MQTT.NewClient(tt.clientOptions) 201 go LoopConnect(tt.clientID, tt.client) 202 time.Sleep(5 * time.Millisecond) 203 if !tt.client.IsConnected() { 204 if len(tt.clientOptions.Servers) != 0 { 205 if tt.connect { 206 t.Errorf("common.TestLoopConnect() Options.Servers = %v, want connect = %v", tt.clientOptions.Servers, tt.connect) 207 } 208 } 209 klog.Info("No servers defined to connect to") 210 } 211 }) 212 } 213 }