github.com/jingruilea/kubeedge@v1.2.0-beta.0.0.20200410162146-4bb8902b3879/edge/test/integration/device/device_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 device_test 18 19 import ( 20 "encoding/json" 21 "net/http" 22 "strings" 23 24 MQTT "github.com/eclipse/paho.mqtt.golang" 25 _ "github.com/mattn/go-sqlite3" 26 . "github.com/onsi/ginkgo" 27 . "github.com/onsi/gomega" 28 29 "github.com/kubeedge/kubeedge/edge/pkg/devicetwin/dtcommon" 30 "github.com/kubeedge/kubeedge/edge/pkg/devicetwin/dttype" 31 "github.com/kubeedge/kubeedge/edge/test/integration/utils/common" 32 . "github.com/kubeedge/kubeedge/edge/test/integration/utils/helpers" 33 ) 34 35 //Devicestate from subscribed MQTT topic 36 var DeviceState string 37 38 type DeviceUpdates struct { 39 EventID string `json:"event_id"` 40 Timestamp int `json:"timestamp"` 41 DeviceField `json:"device"` 42 } 43 44 type DeviceField struct { 45 Name string `json:"name"` 46 State string `json:"state"` 47 LastOnline string `json:"last_online"` 48 } 49 50 type MembershipUpdate struct { 51 BaseMessage 52 AddDevices []Device `json:"added_devices"` 53 RemoveDevices []Device `json:"removed_devices"` 54 } 55 56 type BaseMessage struct { 57 EventID string `json:"event_id"` 58 Timestamp int64 `json:"timestamp"` 59 } 60 61 //var MemDeviceUpdate MembershipUpdate 62 63 var MemDeviceUpdate *MembershipUpdate 64 65 var TokenClient Token 66 var ClientOpts *MQTT.ClientOptions 67 var Client MQTT.Client 68 69 func SubMessageReceived(client MQTT.Client, message MQTT.Message) { 70 var deviceState DeviceUpdates 71 topic := dtcommon.DeviceETPrefix + DeviceIDN + dtcommon.DeviceETStateUpdateSuffix + "/result" 72 if message.Topic() == topic { 73 devicePayload := (message.Payload()) 74 err := json.Unmarshal(devicePayload, &deviceState) 75 if err != nil { 76 common.Fatalf("Unmarshall failed %s", err) 77 } 78 } 79 DeviceState = deviceState.State 80 } 81 func DeviceSubscribed(client MQTT.Client, message MQTT.Message) { 82 topic := dtcommon.MemETPrefix + ctx.Cfg.NodeID + dtcommon.MemETUpdateSuffix 83 if message.Topic() == topic { 84 devicePayload := (message.Payload()) 85 err := json.Unmarshal(devicePayload, MemDeviceUpdate) 86 if err != nil { 87 common.Fatalf("Unmarshall failed %s", err) 88 } 89 } 90 } 91 92 // Deviceid from the DB and assigning to it 93 var DeviceIDN string 94 var DeviceN dttype.Device 95 var DeviceIDWithAttr string 96 var DeviceATT dttype.Device 97 var DeviceIDWithTwin string 98 var DeviceTW dttype.Device 99 100 //Run Test cases 101 var _ = Describe("Event Bus Testing", func() { 102 Context("Publish on eventbus topics throgh MQTT internal broker", func() { 103 BeforeEach(func() { 104 ClientOpts = HubClientInit(ctx.Cfg.MqttEndpoint, ClientID, "", "") 105 Client = MQTT.NewClient(ClientOpts) 106 if TokenClient = Client.Connect(); TokenClient.Wait() && TokenClient.Error() != nil { 107 common.Fatalf("client.Connect() Error is %s", TokenClient.Error()) 108 } 109 }) 110 AfterEach(func() { 111 Client.Disconnect(1) 112 common.PrintTestcaseNameandStatus() 113 }) 114 It("TC_TEST_EBUS_1: Sending data to Cloud", func() { 115 var data = "messagetoUpload_record_to_cloud" 116 body, err := json.Marshal(data) 117 if err != nil { 118 common.Fatalf("Marshal failed %v", err) 119 } 120 if TokenClient = Client.Publish(UploadRecordToCloud, 0, false, body); TokenClient.Wait() && TokenClient.Error() != nil { 121 common.Fatalf("client.Publish() Error is %s", TokenClient.Error()) 122 } else { 123 common.Infof("client.Publish Success !!") 124 } 125 Expect(TokenClient.Error()).NotTo(HaveOccurred()) 126 127 }) 128 129 It("TC_TEST_EBUS_2: Sending data to device module", func() { 130 var data = "messagetoDevice_status_update" 131 body, err := json.Marshal(data) 132 if err != nil { 133 common.Fatalf("Marshal failed %v", err) 134 } 135 if TokenClient = Client.Publish(DevicestatusUpdate, 0, false, body); TokenClient.Wait() && TokenClient.Error() != nil { 136 common.Fatalf("client.Publish() Error is %s", TokenClient.Error()) 137 } else { 138 common.Infof("client.Publish Success !!") 139 } 140 Expect(TokenClient.Error()).NotTo(HaveOccurred()) 141 //Client.Disconnect(1) 142 }) 143 144 It("TC_TEST_EBUS_3: Sending data to device twin module", func() { 145 var data = "messagetoDevice_Twin_update" 146 body, err := json.Marshal(data) 147 if err != nil { 148 common.Fatalf("Marshal failed %v", err) 149 } 150 if TokenClient = Client.Publish(DeviceTwinUpdate, 0, false, body); TokenClient.Wait() && TokenClient.Error() != nil { 151 common.Fatalf("client.Publish() Error is %s", TokenClient.Error()) 152 } else { 153 common.Infof("client.Publish Success !!") 154 } 155 Expect(TokenClient.Error()).NotTo(HaveOccurred()) 156 }) 157 158 It("TC_TEST_EBUS_4: Sending data to membership module", func() { 159 var data = "messagetoDevice_Membership_update" 160 body, err := json.Marshal(data) 161 if err != nil { 162 common.Fatalf("Marshal failed %v", err) 163 } 164 if TokenClient = Client.Publish(DeviceMembershipUpdate, 0, false, body); TokenClient.Wait() && TokenClient.Error() != nil { 165 common.Fatalf("client.Publish() Error is %s", TokenClient.Error()) 166 } else { 167 common.Infof("client.Publish Success !!") 168 } 169 Expect(TokenClient.Error()).NotTo(HaveOccurred()) 170 }) 171 172 It("TC_TEST_EBUS_5: Sending data to device module", func() { 173 var data = "messagetoDevice_upload" 174 body, err := json.Marshal(data) 175 if err != nil { 176 common.Fatalf("Marshal failed %v", err) 177 } 178 if TokenClient = Client.Publish(DeviceUpload, 0, false, body); TokenClient.Wait() && TokenClient.Error() != nil { 179 common.Fatalf("client.Publish() Error is %s", TokenClient.Error()) 180 } else { 181 common.Infof("client.Publish Success !!") 182 } 183 Expect(TokenClient.Error()).NotTo(HaveOccurred()) 184 }) 185 }) 186 187 Context("Publish on eventbus topics throgh MQTT internal broker", func() { 188 BeforeEach(func() { 189 common.Infof("Adding Mock device to edgenode !!") 190 191 DeviceIDN = GenerateDeviceID("kubeedge-device-") 192 DeviceN = CreateDevice(DeviceIDN, "edgedevice", "unknown") 193 194 ClientOpts = HubClientInit(ctx.Cfg.MqttEndpoint, ClientID, "", "") 195 Client = MQTT.NewClient(ClientOpts) 196 if TokenClient = Client.Connect(); TokenClient.Wait() && TokenClient.Error() != nil { 197 common.Fatalf("client.Connect() Error is %s", TokenClient.Error()) 198 } 199 Expect(TokenClient.Error()).NotTo(HaveOccurred()) 200 devicetopic := dtcommon.MemETPrefix + ctx.Cfg.NodeID + dtcommon.MemETUpdateSuffix 201 topic := dtcommon.DeviceETPrefix + DeviceIDN + dtcommon.DeviceETStateUpdateSuffix + "/result" 202 Token := Client.Subscribe(devicetopic, 0, DeviceSubscribed) 203 if Token.Wait() && TokenClient.Error() != nil { 204 common.Fatalf("Subscribe to Topic Failed %s, %s", TokenClient.Error(), topic) 205 } 206 207 Token = Client.Subscribe(topic, 0, SubMessageReceived) 208 if Token.Wait() && TokenClient.Error() != nil { 209 common.Fatalf("Subscribe to Topic Failed %s, %s", TokenClient.Error(), devicetopic) 210 } 211 IsDeviceAdded := HandleAddAndDeleteDevice(http.MethodPut, ctx.Cfg.TestManager+Devicehandler, DeviceN) 212 Expect(IsDeviceAdded).Should(BeTrue()) 213 }) 214 AfterEach(func() { 215 Client.Disconnect(1) 216 common.PrintTestcaseNameandStatus() 217 }) 218 219 It("TC_TEST_EBUS_6: change the device status to online from eventbus", func() { 220 var message DeviceUpdate 221 message.State = "online" 222 topic := dtcommon.DeviceETPrefix + DeviceIDN + dtcommon.DeviceETStateUpdateSuffix + "/result" 223 body, err := json.Marshal(message) 224 if err != nil { 225 common.Fatalf("Marshal failed %v", err) 226 } 227 Eventually(func() string { 228 var ID string 229 for _, deviceEvent := range MemDeviceUpdate.AddDevices { 230 if deviceEvent.ID == DeviceIDN { 231 ID = deviceEvent.ID 232 if TokenClient = Client.Publish(dtcommon.DeviceETPrefix+DeviceIDN+dtcommon.DeviceETStateUpdateSuffix, 0, false, body); TokenClient.Wait() && TokenClient.Error() != nil { 233 common.Fatalf("client.Publish() Error is %s", TokenClient.Error()) 234 } else { 235 common.Infof("client.Publish Success !!") 236 } 237 } 238 } 239 return ID 240 }, "10s", "2s").Should(Equal(DeviceIDN), "Device state is not online within specified time") 241 Expect(TokenClient.Error()).NotTo(HaveOccurred()) 242 Eventually(func() string { 243 common.Infof("subscribed to the topic %v", topic) 244 return DeviceState 245 }, "10s", "2s").Should(Equal("online"), "Device state is not online within specified time") 246 }) 247 248 It("TC_TEST_EBUS_7: change the device status to unknown from eventbus", func() { 249 var message DeviceUpdate 250 message.State = "unknown" 251 topic := dtcommon.DeviceETPrefix + DeviceIDN + dtcommon.DeviceETStateUpdateSuffix + "/result" 252 body, err := json.Marshal(message) 253 if err != nil { 254 common.Fatalf("Marshal failed %v", err) 255 } 256 Eventually(func() string { 257 var deviceEvent Device 258 for _, deviceEvent = range MemDeviceUpdate.AddDevices { 259 if strings.Compare(deviceEvent.ID, DeviceIDN) == 0 { 260 if TokenClient = Client.Publish(dtcommon.DeviceETPrefix+DeviceIDN+dtcommon.DeviceETStateUpdateSuffix, 0, false, body); TokenClient.Wait() && TokenClient.Error() != nil { 261 common.Fatalf("client.Publish() Error is %s", TokenClient.Error()) 262 } else { 263 common.Infof("client.Publish Success !!") 264 } 265 } 266 } 267 return deviceEvent.ID 268 }, "10s", "2s").Should(Equal(DeviceIDN), "Device state is not online within specified time") 269 Expect(TokenClient.Error()).NotTo(HaveOccurred()) 270 Eventually(func() string { 271 common.Infof("subscribed to the topic %v", topic) 272 return DeviceState 273 }, "10s", "2s").Should(Equal("unknown"), "Device state is not unknown within specified time") 274 }) 275 276 It("TC_TEST_EBUS_8: change the device status to offline from eventbus", func() { 277 var message DeviceUpdate 278 message.State = "offline" 279 topic := dtcommon.DeviceETPrefix + DeviceIDN + dtcommon.DeviceETStateUpdateSuffix + "/result" 280 body, err := json.Marshal(message) 281 if err != nil { 282 common.Fatalf("Marshal failed %v", err) 283 } 284 Eventually(func() string { 285 var deviceEvent Device 286 for _, deviceEvent = range MemDeviceUpdate.AddDevices { 287 if strings.Compare(deviceEvent.ID, DeviceIDN) == 0 { 288 if TokenClient = Client.Publish(dtcommon.DeviceETPrefix+DeviceIDN+dtcommon.DeviceETStateUpdateSuffix, 0, false, body); TokenClient.Wait() && TokenClient.Error() != nil { 289 common.Fatalf("client.Publish() Error is %s", TokenClient.Error()) 290 } else { 291 common.Infof("client.Publish Success !!") 292 } 293 } 294 } 295 return deviceEvent.ID 296 }, "10s", "2s").Should(Equal(DeviceIDN), "Device state is not online within specified time") 297 Expect(TokenClient.Error()).NotTo(HaveOccurred()) 298 Eventually(func() string { 299 common.Infof("subscribed to the topic %v", topic) 300 return DeviceState 301 }, "10s", "2s").Should(Equal("offline"), "Device state is not offline within specified time") 302 }) 303 304 }) 305 Context(" Add a device with Twin attributes", func() { 306 BeforeEach(func() { 307 308 }) 309 AfterEach(func() { 310 common.PrintTestcaseNameandStatus() 311 }) 312 It("TC_TEST_EBUS_9: Add a sample device with device attributes to kubeedge node", func() { 313 //Generating Device ID 314 DeviceIDWithAttr = GenerateDeviceID("kubeedge-device-WithDeviceAttributes") 315 //Generate a Device 316 DeviceATT = CreateDevice(DeviceIDWithAttr, "DeviceATT", "unknown") 317 //Add Attribute to device 318 AddDeviceAttribute(DeviceATT, "Temperature", "25.25", "float") 319 320 IsDeviceAdded := HandleAddAndDeleteDevice(http.MethodPut, ctx.Cfg.TestManager+Devicehandler, DeviceATT) 321 Expect(IsDeviceAdded).Should(BeTrue()) 322 323 Eventually(func() string { 324 attributeDB := GetDeviceAttributesFromDB(DeviceIDWithAttr, "Temperature") 325 common.Infof("DeviceID= %s, Value= %s", attributeDB.DeviceID, attributeDB.Value) 326 return attributeDB.Value 327 }, "60s", "2s").Should(Equal("25.25"), "Device is not added within specified time") 328 329 }) 330 331 It("TC_TEST_EBUS_10: Add a sample device with Twin attributes to kubeedge node", func() { 332 //Generating Device ID 333 DeviceIDWithTwin = GenerateDeviceID("kubeedge-device-WithTwinAttributes") 334 //Generate a Device 335 DeviceTW = CreateDevice(DeviceIDWithTwin, "DeviceTW", "unknown") 336 //Add twin attribute 337 AddTwinAttribute(DeviceTW, "Temperature", "25.25", "float") 338 339 IsDeviceAdded := HandleAddAndDeleteDevice(http.MethodPut, ctx.Cfg.TestManager+Devicehandler, DeviceTW) 340 Expect(IsDeviceAdded).Should(BeTrue()) 341 342 Eventually(func() string { 343 attributeDB := GetTwinAttributesFromDB(DeviceIDWithTwin, "Temperature") 344 common.Infof("DeviceID= %s, Value= %s", attributeDB.DeviceID, attributeDB.Expected) 345 return attributeDB.Expected 346 }, "60s", "2s").Should(Equal("25.25"), "Device is not added within specified time") 347 348 }) 349 350 It("TC_TEST_EBUS_11: Update existing device with new attributes", func() { 351 352 //Generate a Device 353 device := CreateDevice(DeviceIDWithAttr, "DeviceATT", "unknown") 354 //Add Attribute to device 355 AddDeviceAttribute(device, "Temperature", "50.50", "float") 356 357 IsDeviceAdded := HandleAddAndDeleteDevice(http.MethodPut, ctx.Cfg.TestManager+Devicehandler, device) 358 Expect(IsDeviceAdded).Should(BeTrue()) 359 360 Eventually(func() string { 361 attributeDB := GetDeviceAttributesFromDB(DeviceIDWithAttr, "Temperature") 362 common.Infof("DeviceID= %s, Value= %s", attributeDB.DeviceID, attributeDB.Value) 363 return attributeDB.Value 364 }, "60s", "2s").Should(Equal("50.50"), "Device Attributes are not updated within specified time") 365 366 }) 367 368 It("TC_TEST_EBUS_12: Update existing device with new Twin attributes", func() { 369 370 //Generate a Device 371 device := CreateDevice(DeviceIDWithTwin, "DeviceTW", "unknown") 372 //Add twin attribute 373 AddTwinAttribute(device, "Temperature", "50.50", "float") 374 375 IsDeviceAdded := HandleAddAndDeleteDevice(http.MethodPut, ctx.Cfg.TestManager+Devicehandler, device) 376 Expect(IsDeviceAdded).Should(BeTrue()) 377 378 Eventually(func() string { 379 attributeDB := GetTwinAttributesFromDB(DeviceIDWithTwin, "Temperature") 380 common.Infof("DeviceID= %s, Value= %s", attributeDB.DeviceID, attributeDB.Expected) 381 return attributeDB.Expected 382 }, "60s", "2s").Should(Equal("50.50"), "Device Twin Attributes are not updated within specified time") 383 384 }) 385 386 It("TC_TEST_EBUS_13: Add a new Device attribute to existing device", func() { 387 //Adding a new attribute to a device 388 AddDeviceAttribute(DeviceATT, "Humidity", "30", "Int") 389 390 IsDeviceAdded := HandleAddAndDeleteDevice(http.MethodPut, ctx.Cfg.TestManager+Devicehandler, DeviceATT) 391 Expect(IsDeviceAdded).Should(BeTrue()) 392 393 Eventually(func() string { 394 attributeDB := GetDeviceAttributesFromDB(DeviceIDWithAttr, "Humidity") 395 common.Infof("DeviceID= %s, Value= %s", attributeDB.DeviceID, attributeDB.Value) 396 return attributeDB.Value 397 }, "60s", "2s").Should(Equal("30"), "Device Attributes are not Added within specified time") 398 399 }) 400 401 It("TC_TEST_EBUS_14: Add a new Twin attribute to existing device", func() { 402 //Preparing temporary Twin Attributes 403 AddTwinAttribute(DeviceTW, "Humidity", "100.100", "float") 404 405 IsDeviceAdded := HandleAddAndDeleteDevice(http.MethodPut, ctx.Cfg.TestManager+Devicehandler, DeviceTW) 406 Expect(IsDeviceAdded).Should(BeTrue()) 407 408 Eventually(func() string { 409 attributeDB := GetTwinAttributesFromDB(DeviceIDWithTwin, "Humidity") 410 common.Infof("DeviceID= %s, Value= %s", attributeDB.DeviceID, attributeDB.Expected) 411 return attributeDB.Expected 412 }, "60s", "2s").Should(Equal("100.100"), "Device Twin Attributes are not Added within specified time") 413 414 }) 415 }) 416 })