github.com/midokura/kubeedge@v1.2.0-mido.0/tests/e2e/device_crd/device_crd_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_crd
    18  
    19  import (
    20  	"encoding/json"
    21  	"net/http"
    22  	"time"
    23  
    24  	. "github.com/onsi/ginkgo"
    25  	. "github.com/onsi/gomega"
    26  	v1 "k8s.io/api/core/v1"
    27  
    28  	"github.com/kubeedge/kubeedge/cloud/pkg/apis/devices/v1alpha1"
    29  	"github.com/kubeedge/kubeedge/tests/e2e/utils"
    30  )
    31  
    32  const (
    33  	DeviceInstanceHandler = "/apis/devices.kubeedge.io/v1alpha1/namespaces/default/devices"
    34  	DeviceModelHandler    = "/apis/devices.kubeedge.io/v1alpha1/namespaces/default/devicemodels"
    35  	ConfigmapHandler      = "/api/v1/namespaces/default/configmaps"
    36  )
    37  
    38  var CRDTestTimerGroup *utils.TestTimerGroup = utils.NewTestTimerGroup()
    39  
    40  //Run Test cases
    41  var _ = Describe("Device Management test in E2E scenario", func() {
    42  	var testTimer *utils.TestTimer
    43  	var testDescription GinkgoTestDescription
    44  	Context("Test Device Model Creation, Updation and deletion", func() {
    45  		BeforeEach(func() {
    46  			// Delete any pre-exisitng device models
    47  			var deviceModelList v1alpha1.DeviceModelList
    48  			list, err := utils.GetDeviceModel(&deviceModelList, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, nil)
    49  			Expect(err).To(BeNil())
    50  			for _, model := range list {
    51  				IsDeviceModelDeleted, statusCode := utils.HandleDeviceModel(http.MethodDelete, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, "/"+model.Name, "")
    52  				Expect(IsDeviceModelDeleted).Should(BeTrue())
    53  				Expect(statusCode).Should(Equal(http.StatusOK))
    54  			}
    55  			// Get current test description
    56  			testDescription = CurrentGinkgoTestDescription()
    57  			// Start test timer
    58  			testTimer = CRDTestTimerGroup.NewTestTimer(testDescription.TestText)
    59  		})
    60  		AfterEach(func() {
    61  			// End test timer
    62  			testTimer.End()
    63  			// Print result
    64  			testTimer.PrintResult()
    65  			// Delete the device models created
    66  			var deviceModelList v1alpha1.DeviceModelList
    67  			list, err := utils.GetDeviceModel(&deviceModelList, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, nil)
    68  			Expect(err).To(BeNil())
    69  			for _, model := range list {
    70  				IsDeviceModelDeleted, statusCode := utils.HandleDeviceModel(http.MethodDelete, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, "/"+model.Name, "")
    71  				Expect(IsDeviceModelDeleted).Should(BeTrue())
    72  				Expect(statusCode).Should(Equal(http.StatusOK))
    73  			}
    74  			utils.PrintTestcaseNameandStatus()
    75  		})
    76  		It("E2E_CREATE_DEVICE_MODEL_1: Create device model for LED device (No Protocol)", func() {
    77  			var deviceModelList v1alpha1.DeviceModelList
    78  			IsDeviceModelCreated, statusCode := utils.HandleDeviceModel(http.MethodPost, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, "", "led")
    79  			Expect(IsDeviceModelCreated).Should(BeTrue())
    80  			Expect(statusCode).Should(Equal(http.StatusCreated))
    81  			newLedDeviceModel := utils.NewLedDeviceModel()
    82  			_, err := utils.GetDeviceModel(&deviceModelList, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, &newLedDeviceModel)
    83  			Expect(err).To(BeNil())
    84  		})
    85  		It("E2E_CREATE_DEVICE_MODEL_2: Create device model for bluetooth protocol", func() {
    86  			var deviceModelList v1alpha1.DeviceModelList
    87  			IsDeviceModelCreated, statusCode := utils.HandleDeviceModel(http.MethodPost, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, "", "bluetooth")
    88  			Expect(IsDeviceModelCreated).Should(BeTrue())
    89  			Expect(statusCode).Should(Equal(http.StatusCreated))
    90  			newBluetoothDeviceModel := utils.NewBluetoothDeviceModel()
    91  			_, err := utils.GetDeviceModel(&deviceModelList, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, &newBluetoothDeviceModel)
    92  			Expect(err).To(BeNil())
    93  		})
    94  		It("E2E_CREATE_DEVICE_MODEL_3: Create device model for modbus protocol", func() {
    95  			var deviceModelList v1alpha1.DeviceModelList
    96  			IsDeviceModelCreated, statusCode := utils.HandleDeviceModel(http.MethodPost, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, "", "modbus")
    97  			Expect(IsDeviceModelCreated).Should(BeTrue())
    98  			Expect(statusCode).Should(Equal(http.StatusCreated))
    99  			newModbusDeviceMode := utils.NewModbusDeviceModel()
   100  			_, err := utils.GetDeviceModel(&deviceModelList, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, &newModbusDeviceMode)
   101  			Expect(err).To(BeNil())
   102  		})
   103  		It("E2E_CREATE_DEVICE_MODEL_4: Create device model for incorrect device model", func() {
   104  			_, statusCode := utils.HandleDeviceModel(http.MethodPost, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, "", "incorrect-model")
   105  			Expect(statusCode).Should(Equal(http.StatusUnprocessableEntity))
   106  		})
   107  		It("E2E_UPDATE_DEVICE_MODEL_1: Update device model for LED device (No Protocol)", func() {
   108  			var deviceModelList v1alpha1.DeviceModelList
   109  			IsDeviceModelCreated, statusCode := utils.HandleDeviceModel(http.MethodPost, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, "", "led")
   110  			Expect(IsDeviceModelCreated).Should(BeTrue())
   111  			Expect(statusCode).Should(Equal(http.StatusCreated))
   112  			IsDeviceModelUpdated, statusCode := utils.HandleDeviceModel(http.MethodPatch, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, "/"+utils.UpdatedLedDeviceModel().Name, "led")
   113  			Expect(IsDeviceModelUpdated).Should(BeTrue())
   114  			Expect(statusCode).Should(Equal(http.StatusOK))
   115  			updatedLedDeviceModel := utils.UpdatedLedDeviceModel()
   116  			_, err := utils.GetDeviceModel(&deviceModelList, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, &updatedLedDeviceModel)
   117  			Expect(err).To(BeNil())
   118  		})
   119  		It("E2E_UPDATE_DEVICE_MODEL_2: Update device model for bluetooth protocol", func() {
   120  			var deviceModelList v1alpha1.DeviceModelList
   121  			IsDeviceModelCreated, statusCode := utils.HandleDeviceModel(http.MethodPost, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, "", "bluetooth")
   122  			Expect(IsDeviceModelCreated).Should(BeTrue())
   123  			Expect(statusCode).Should(Equal(http.StatusCreated))
   124  			IsDeviceModelUpdated, statusCode := utils.HandleDeviceModel(http.MethodPatch, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, "/"+utils.UpdatedBluetoothDeviceModel().Name, "bluetooth")
   125  			Expect(IsDeviceModelUpdated).Should(BeTrue())
   126  			Expect(statusCode).Should(Equal(http.StatusOK))
   127  			updatedBluetoothDeviceModel := utils.UpdatedBluetoothDeviceModel()
   128  			_, err := utils.GetDeviceModel(&deviceModelList, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, &updatedBluetoothDeviceModel)
   129  			Expect(err).To(BeNil())
   130  		})
   131  		It("E2E_UPDATE_DEVICE_MODEL_3: Update device model for modbus protocol", func() {
   132  			var deviceModelList v1alpha1.DeviceModelList
   133  			IsDeviceModelCreated, statusCode := utils.HandleDeviceModel(http.MethodPost, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, "", "modbus")
   134  			Expect(IsDeviceModelCreated).Should(BeTrue())
   135  			Expect(statusCode).Should(Equal(http.StatusCreated))
   136  			IsDeviceModelUpdated, statusCode := utils.HandleDeviceModel(http.MethodPatch, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, "/"+utils.UpdatedModbusDeviceModel().Name, "modbus")
   137  			Expect(IsDeviceModelUpdated).Should(BeTrue())
   138  			Expect(statusCode).Should(Equal(http.StatusOK))
   139  			updatedModbusDeviceModel := utils.UpdatedModbusDeviceModel()
   140  			_, err := utils.GetDeviceModel(&deviceModelList, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, &updatedModbusDeviceModel)
   141  			Expect(err).To(BeNil())
   142  		})
   143  		It("E2E_UPDATE_DEVICE_MODEL_4: Update device model for incorrect device model", func() {
   144  			IsDeviceModelCreated, statusCode := utils.HandleDeviceModel(http.MethodPost, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, "", "led")
   145  			Expect(IsDeviceModelCreated).Should(BeTrue())
   146  			Expect(statusCode).Should(Equal(http.StatusCreated))
   147  			IsDeviceModelUpdated, statusCode := utils.HandleDeviceModel(http.MethodPatch, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, "/"+utils.UpdatedLedDeviceModel().Name, "incorrect-model")
   148  			Expect(IsDeviceModelUpdated).Should(BeTrue())
   149  			Expect(statusCode).Should(Equal(http.StatusUnprocessableEntity))
   150  		})
   151  		It("E2E_DELETE_DEVICE_MODEL_1: Delete non existent device model(No Protocol)", func() {
   152  			IsDeviceModelDeleted, statusCode := utils.HandleDeviceModel(http.MethodDelete, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, "/"+utils.NewLedDeviceModel().Name, "")
   153  			Expect(IsDeviceModelDeleted).Should(BeTrue())
   154  			Expect(statusCode).Should(Equal(http.StatusNotFound))
   155  		})
   156  	})
   157  	Context("Test Device Instance Creation, Updation and Deletion", func() {
   158  		BeforeEach(func() {
   159  			var deviceModelList v1alpha1.DeviceModelList
   160  			var deviceList v1alpha1.DeviceList
   161  			// Delete the device instances created
   162  			deviceInstanceList, err := utils.GetDevice(&deviceList, ctx.Cfg.K8SMasterForKubeEdge+DeviceInstanceHandler, nil)
   163  			Expect(err).To(BeNil())
   164  			for _, device := range deviceInstanceList {
   165  				IsDeviceDeleted, statusCode := utils.HandleDeviceInstance(http.MethodDelete, ctx.Cfg.K8SMasterForKubeEdge+DeviceInstanceHandler, NodeName, "/"+device.Name, "")
   166  				Expect(IsDeviceDeleted).Should(BeTrue())
   167  				Expect(statusCode).Should(Equal(http.StatusOK))
   168  			}
   169  			// Delete any pre-exisitng device models
   170  			list, err := utils.GetDeviceModel(&deviceModelList, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, nil)
   171  			Expect(err).To(BeNil())
   172  			for _, model := range list {
   173  				IsDeviceModelDeleted, statusCode := utils.HandleDeviceModel(http.MethodDelete, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, "/"+model.Name, "")
   174  				Expect(IsDeviceModelDeleted).Should(BeTrue())
   175  				Expect(statusCode).Should(Equal(http.StatusOK))
   176  			}
   177  			utils.TwinResult = utils.DeviceTwinResult{}
   178  			// Get current test description
   179  			testDescription = CurrentGinkgoTestDescription()
   180  			// Start test timer
   181  			testTimer = CRDTestTimerGroup.NewTestTimer(testDescription.TestText)
   182  		})
   183  		AfterEach(func() {
   184  			// End test timer
   185  			testTimer.End()
   186  			// Print result
   187  			testTimer.PrintResult()
   188  			var deviceModelList v1alpha1.DeviceModelList
   189  			var deviceList v1alpha1.DeviceList
   190  			// Delete the device instances created
   191  			deviceInstanceList, err := utils.GetDevice(&deviceList, ctx.Cfg.K8SMasterForKubeEdge+DeviceInstanceHandler, nil)
   192  			Expect(err).To(BeNil())
   193  			for _, device := range deviceInstanceList {
   194  				IsDeviceDeleted, statusCode := utils.HandleDeviceInstance(http.MethodDelete, ctx.Cfg.K8SMasterForKubeEdge+DeviceInstanceHandler, NodeName, "/"+device.Name, "")
   195  				Expect(IsDeviceDeleted).Should(BeTrue())
   196  				Expect(statusCode).Should(Equal(http.StatusOK))
   197  			}
   198  			// Delete the device models created
   199  			list, err := utils.GetDeviceModel(&deviceModelList, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, nil)
   200  			Expect(err).To(BeNil())
   201  			for _, model := range list {
   202  				IsDeviceModelDeleted, statusCode := utils.HandleDeviceModel(http.MethodDelete, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, "/"+model.Name, "")
   203  				Expect(IsDeviceModelDeleted).Should(BeTrue())
   204  				Expect(statusCode).Should(Equal(http.StatusOK))
   205  			}
   206  			utils.PrintTestcaseNameandStatus()
   207  		})
   208  		It("E2E_CREATE_DEVICE_1: Create device instance for LED device (No Protocol)", func() {
   209  			var deviceList v1alpha1.DeviceList
   210  			IsDeviceModelCreated, statusCode := utils.HandleDeviceModel(http.MethodPost, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, "", "led")
   211  			Expect(IsDeviceModelCreated).Should(BeTrue())
   212  			Expect(statusCode).Should(Equal(http.StatusCreated))
   213  			IsDeviceCreated, statusCode := utils.HandleDeviceInstance(http.MethodPost, ctx.Cfg.K8SMasterForKubeEdge+DeviceInstanceHandler, NodeName, "", "led")
   214  			Expect(IsDeviceCreated).Should(BeTrue())
   215  			Expect(statusCode).Should(Equal(http.StatusCreated))
   216  			newLedDevice := utils.NewLedDeviceInstance(NodeName)
   217  			_, err := utils.GetDevice(&deviceList, ctx.Cfg.K8SMasterForKubeEdge+DeviceInstanceHandler, &newLedDevice)
   218  			Expect(err).To(BeNil())
   219  			time.Sleep(3 * time.Second)
   220  			statusCode, body := utils.GetConfigmap(ctx.Cfg.K8SMasterForKubeEdge + ConfigmapHandler + "/" + "device-profile-config-" + NodeName)
   221  			Expect(statusCode).Should(Equal(http.StatusOK))
   222  			var configMap v1.ConfigMap
   223  			err = json.Unmarshal(body, &configMap)
   224  			Expect(err).To(BeNil())
   225  			isEqual := utils.CompareConfigMaps(configMap, utils.NewConfigMapLED(NodeName))
   226  			Expect(isEqual).Should(Equal(true))
   227  			go utils.TwinSubscribe(utils.NewLedDeviceInstance(NodeName).Name)
   228  			Eventually(func() bool {
   229  				return utils.TwinResult.Twin != nil
   230  			}, "20s", "2s").Should(Equal(true), "Device information not reaching edge!!")
   231  			stringValue := "ON"
   232  			expectedTwin := map[string]*utils.MsgTwin{
   233  				"power-status": {
   234  					Expected: &utils.TwinValue{
   235  						Value: &stringValue,
   236  					},
   237  					Metadata: &utils.TypeMetadata{
   238  						Type: "string",
   239  					},
   240  				},
   241  			}
   242  			isEqual = utils.CompareTwin(utils.TwinResult.Twin, expectedTwin)
   243  			Expect(isEqual).Should(Equal(true))
   244  		})
   245  		It("E2E_CREATE_DEVICE_2: Create device instance for bluetooth protocol", func() {
   246  			var deviceList v1alpha1.DeviceList
   247  			IsDeviceModelCreated, statusCode := utils.HandleDeviceModel(http.MethodPost, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, "", "bluetooth")
   248  			Expect(IsDeviceModelCreated).Should(BeTrue())
   249  			Expect(statusCode).Should(Equal(http.StatusCreated))
   250  			IsDeviceCreated, statusCode := utils.HandleDeviceInstance(http.MethodPost, ctx.Cfg.K8SMasterForKubeEdge+DeviceInstanceHandler, NodeName, "", "bluetooth")
   251  			Expect(IsDeviceCreated).Should(BeTrue())
   252  			Expect(statusCode).Should(Equal(http.StatusCreated))
   253  			newBluetoothDevice := utils.NewBluetoothDeviceInstance(NodeName)
   254  			_, err := utils.GetDevice(&deviceList, ctx.Cfg.K8SMasterForKubeEdge+DeviceInstanceHandler, &newBluetoothDevice)
   255  			Expect(err).To(BeNil())
   256  			time.Sleep(3 * time.Second)
   257  			statusCode, body := utils.GetConfigmap(ctx.Cfg.K8SMasterForKubeEdge + ConfigmapHandler + "/" + "device-profile-config-" + NodeName)
   258  			Expect(statusCode).Should(Equal(http.StatusOK))
   259  			var configMap v1.ConfigMap
   260  			err = json.Unmarshal(body, &configMap)
   261  			Expect(err).To(BeNil())
   262  			isEqual := utils.CompareConfigMaps(configMap, utils.NewConfigMapBluetooth(NodeName))
   263  			Expect(isEqual).Should(Equal(true))
   264  			go utils.TwinSubscribe(utils.NewBluetoothDeviceInstance(NodeName).Name)
   265  			Eventually(func() bool {
   266  				return utils.TwinResult.Twin != nil
   267  			}, "20s", "2s").Should(Equal(true), "Device information not reaching edge!!")
   268  			ioData := "1"
   269  			expectedTwin := map[string]*utils.MsgTwin{
   270  				"io-data": {
   271  					Expected: &utils.TwinValue{
   272  						Value: &ioData,
   273  					},
   274  					Metadata: &utils.TypeMetadata{
   275  						Type: "int",
   276  					},
   277  				},
   278  			}
   279  			isEqual = utils.CompareTwin(utils.TwinResult.Twin, expectedTwin)
   280  			Expect(isEqual).Should(Equal(true))
   281  		})
   282  		It("E2E_CREATE_DEVICE_3: Create device instance for modbus protocol", func() {
   283  			var deviceList v1alpha1.DeviceList
   284  			IsDeviceModelCreated, statusCode := utils.HandleDeviceModel(http.MethodPost, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, "", "modbus")
   285  			Expect(IsDeviceModelCreated).Should(BeTrue())
   286  			Expect(statusCode).Should(Equal(http.StatusCreated))
   287  			IsDeviceCreated, statusCode := utils.HandleDeviceInstance(http.MethodPost, ctx.Cfg.K8SMasterForKubeEdge+DeviceInstanceHandler, NodeName, "", "modbus")
   288  			Expect(IsDeviceCreated).Should(BeTrue())
   289  			Expect(statusCode).Should(Equal(http.StatusCreated))
   290  			newModbusDevice := utils.NewModbusDeviceInstance(NodeName)
   291  			_, err := utils.GetDevice(&deviceList, ctx.Cfg.K8SMasterForKubeEdge+DeviceInstanceHandler, &newModbusDevice)
   292  			Expect(err).To(BeNil())
   293  			time.Sleep(3 * time.Second)
   294  			statusCode, body := utils.GetConfigmap(ctx.Cfg.K8SMasterForKubeEdge + ConfigmapHandler + "/" + "device-profile-config-" + NodeName)
   295  			Expect(statusCode).Should(Equal(http.StatusOK))
   296  			var configMap v1.ConfigMap
   297  			err = json.Unmarshal(body, &configMap)
   298  			Expect(err).To(BeNil())
   299  			isEqual := utils.CompareConfigMaps(configMap, utils.NewConfigMapModbus(NodeName))
   300  			Expect(isEqual).Should(Equal(true))
   301  			go utils.TwinSubscribe(utils.NewModbusDeviceInstance(NodeName).Name)
   302  			Eventually(func() bool {
   303  				return utils.TwinResult.Twin != nil
   304  			}, "20s", "2s").Should(Equal(true), "Device information not reaching edge!!")
   305  			stringValue := "OFF"
   306  			expectedTwin := map[string]*utils.MsgTwin{
   307  				"temperature-enable": {
   308  					Expected: &utils.TwinValue{
   309  						Value: &stringValue,
   310  					},
   311  					Metadata: &utils.TypeMetadata{
   312  						Type: "string",
   313  					},
   314  				},
   315  			}
   316  			isEqual = utils.CompareTwin(utils.TwinResult.Twin, expectedTwin)
   317  			Expect(isEqual).Should(Equal(true))
   318  		})
   319  		It("E2E_CREATE_DEVICE_4: Create device instance for incorrect device instance", func() {
   320  			statusCode := utils.DeleteConfigmap(ctx.Cfg.K8SMasterForKubeEdge + ConfigmapHandler + "/" + "device-profile-config-" + NodeName)
   321  			Expect(statusCode == http.StatusOK || statusCode == http.StatusNotFound).Should(Equal(true))
   322  			IsDeviceModelCreated, statusCode := utils.HandleDeviceModel(http.MethodPost, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, "", "led")
   323  			Expect(IsDeviceModelCreated).Should(BeTrue())
   324  			Expect(statusCode).Should(Equal(http.StatusCreated))
   325  			IsDeviceCreated, statusCode := utils.HandleDeviceInstance(http.MethodPost, ctx.Cfg.K8SMasterForKubeEdge+DeviceInstanceHandler, NodeName, "", "incorrect-instance")
   326  			Expect(IsDeviceCreated).Should(BeTrue())
   327  			Expect(statusCode).Should(Equal(http.StatusUnprocessableEntity))
   328  			statusCode, _ = utils.GetConfigmap(ctx.Cfg.K8SMasterForKubeEdge + ConfigmapHandler + "/" + "device-profile-config-" + NodeName)
   329  			Expect(statusCode).Should(Equal(http.StatusNotFound))
   330  		})
   331  		It("E2E_UPDATE_DEVICE_1: Update device instance for LED device (No Protocol)", func() {
   332  			var deviceList v1alpha1.DeviceList
   333  			IsDeviceModelCreated, statusCode := utils.HandleDeviceModel(http.MethodPost, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, "", "led")
   334  			Expect(IsDeviceModelCreated).Should(BeTrue())
   335  			Expect(statusCode).Should(Equal(http.StatusCreated))
   336  			IsDeviceCreated, statusCode := utils.HandleDeviceInstance(http.MethodPost, ctx.Cfg.K8SMasterForKubeEdge+DeviceInstanceHandler, NodeName, "", "led")
   337  			Expect(IsDeviceCreated).Should(BeTrue())
   338  			Expect(statusCode).Should(Equal(http.StatusCreated))
   339  			IsDeviceUpdated, statusCode := utils.HandleDeviceInstance(http.MethodPatch, ctx.Cfg.K8SMasterForKubeEdge+DeviceInstanceHandler, NodeName, "/"+utils.UpdatedLedDeviceInstance(NodeName).Name, "led")
   340  			Expect(IsDeviceUpdated).Should(BeTrue())
   341  			Expect(statusCode).Should(Equal(http.StatusOK))
   342  			updatedLedDevice := utils.UpdatedLedDeviceInstance(NodeName)
   343  			time.Sleep(2 * time.Second)
   344  			_, err := utils.GetDevice(&deviceList, ctx.Cfg.K8SMasterForKubeEdge+DeviceInstanceHandler, &updatedLedDevice)
   345  			Expect(err).To(BeNil())
   346  			go utils.TwinSubscribe(utils.UpdatedLedDeviceInstance(NodeName).Name)
   347  			Eventually(func() bool {
   348  				return utils.TwinResult.Twin != nil
   349  			}, "20s", "2s").Should(Equal(true), "Device information not reaching edge!!")
   350  			stringValue := "OFF"
   351  			expectedTwin := map[string]*utils.MsgTwin{
   352  				"power-status": {
   353  					Expected: &utils.TwinValue{
   354  						Value: &stringValue,
   355  					},
   356  					Metadata: &utils.TypeMetadata{
   357  						Type: "string",
   358  					},
   359  				},
   360  			}
   361  			isEqual := utils.CompareTwin(utils.TwinResult.Twin, expectedTwin)
   362  			Expect(isEqual).Should(Equal(true))
   363  		})
   364  		It("E2E_UPDATE_DEVICE_2: Update device instance for bluetooth protocol", func() {
   365  			var deviceList v1alpha1.DeviceList
   366  			IsDeviceModelCreated, statusCode := utils.HandleDeviceModel(http.MethodPost, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, "", "bluetooth")
   367  			Expect(IsDeviceModelCreated).Should(BeTrue())
   368  			Expect(statusCode).Should(Equal(http.StatusCreated))
   369  			IsDeviceCreated, statusCode := utils.HandleDeviceInstance(http.MethodPost, ctx.Cfg.K8SMasterForKubeEdge+DeviceInstanceHandler, NodeName, "", "bluetooth")
   370  			Expect(IsDeviceCreated).Should(BeTrue())
   371  			Expect(statusCode).Should(Equal(http.StatusCreated))
   372  			IsDeviceUpdated, statusCode := utils.HandleDeviceInstance(http.MethodPatch, ctx.Cfg.K8SMasterForKubeEdge+DeviceInstanceHandler, NodeName, "/"+utils.UpdatedBluetoothDeviceInstance(NodeName).Name, "bluetooth")
   373  			Expect(IsDeviceUpdated).Should(BeTrue())
   374  			Expect(statusCode).Should(Equal(http.StatusOK))
   375  			updatedBluetoothDevice := utils.UpdatedBluetoothDeviceInstance(NodeName)
   376  			time.Sleep(2 * time.Second)
   377  			_, err := utils.GetDevice(&deviceList, ctx.Cfg.K8SMasterForKubeEdge+DeviceInstanceHandler, &updatedBluetoothDevice)
   378  			Expect(err).To(BeNil())
   379  			go utils.TwinSubscribe(utils.UpdatedBluetoothDeviceInstance(NodeName).Name)
   380  			Eventually(func() bool {
   381  				return utils.TwinResult.Twin != nil
   382  			}, "20s", "2s").Should(Equal(true), "Device information not reaching edge!!")
   383  			ioData := "1"
   384  			expectedTwin := map[string]*utils.MsgTwin{
   385  				"io-data": {
   386  					Expected: &utils.TwinValue{
   387  						Value: &ioData,
   388  					},
   389  					Metadata: &utils.TypeMetadata{
   390  						Type: "int",
   391  					},
   392  				},
   393  			}
   394  			isEqual := utils.CompareTwin(utils.TwinResult.Twin, expectedTwin)
   395  			Expect(isEqual).Should(Equal(true))
   396  		})
   397  		It("E2E_UPDATE_DEVICE_3: Update device instance for modbus protocol", func() {
   398  			var deviceList v1alpha1.DeviceList
   399  			IsDeviceModelCreated, statusCode := utils.HandleDeviceModel(http.MethodPost, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, "", "modbus")
   400  			Expect(IsDeviceModelCreated).Should(BeTrue())
   401  			Expect(statusCode).Should(Equal(http.StatusCreated))
   402  			IsDeviceCreated, statusCode := utils.HandleDeviceInstance(http.MethodPost, ctx.Cfg.K8SMasterForKubeEdge+DeviceInstanceHandler, NodeName, "", "modbus")
   403  			Expect(IsDeviceCreated).Should(BeTrue())
   404  			Expect(statusCode).Should(Equal(http.StatusCreated))
   405  			IsDeviceUpdated, statusCode := utils.HandleDeviceInstance(http.MethodPatch, ctx.Cfg.K8SMasterForKubeEdge+DeviceInstanceHandler, NodeName, "/"+utils.UpdatedModbusDeviceInstance(NodeName).Name, "modbus")
   406  			Expect(IsDeviceUpdated).Should(BeTrue())
   407  			Expect(statusCode).Should(Equal(http.StatusOK))
   408  			updatedModbusDevice := utils.UpdatedModbusDeviceInstance(NodeName)
   409  			time.Sleep(2 * time.Second)
   410  			_, err := utils.GetDevice(&deviceList, ctx.Cfg.K8SMasterForKubeEdge+DeviceInstanceHandler, &updatedModbusDevice)
   411  			Expect(err).To(BeNil())
   412  			go utils.TwinSubscribe(utils.UpdatedModbusDeviceInstance(NodeName).Name)
   413  			Eventually(func() bool {
   414  				return utils.TwinResult.Twin != nil
   415  			}, "20s", "2s").Should(Equal(true), "Device information not reaching edge!!")
   416  			stringValue := "ON"
   417  			expectedTwin := map[string]*utils.MsgTwin{
   418  				"temperature-enable": {
   419  					Expected: &utils.TwinValue{
   420  						Value: &stringValue,
   421  					},
   422  					Metadata: &utils.TypeMetadata{
   423  						Type: "string",
   424  					},
   425  				},
   426  			}
   427  			isEqual := utils.CompareTwin(utils.TwinResult.Twin, expectedTwin)
   428  			Expect(isEqual).Should(Equal(true))
   429  		})
   430  		It("E2E_UPDATE_DEVICE_4: Update device instance for incorrect device instance", func() {
   431  			IsDeviceModelCreated, statusCode := utils.HandleDeviceModel(http.MethodPost, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, "", "led")
   432  			Expect(IsDeviceModelCreated).Should(BeTrue())
   433  			Expect(statusCode).Should(Equal(http.StatusCreated))
   434  			IsDeviceCreated, statusCode := utils.HandleDeviceInstance(http.MethodPost, ctx.Cfg.K8SMasterForKubeEdge+DeviceInstanceHandler, NodeName, "", "led")
   435  			Expect(IsDeviceCreated).Should(BeTrue())
   436  			Expect(statusCode).Should(Equal(http.StatusCreated))
   437  			IsDeviceUpdated, statusCode := utils.HandleDeviceInstance(http.MethodPatch, ctx.Cfg.K8SMasterForKubeEdge+DeviceInstanceHandler, NodeName, "/"+utils.UpdatedLedDeviceInstance(NodeName).Name, "incorrect-instance")
   438  			Expect(IsDeviceUpdated).Should(BeTrue())
   439  			Expect(statusCode).Should(Equal(http.StatusUnprocessableEntity))
   440  		})
   441  		It("E2E_DELETE_DEVICE_1: Delete device instance for an existing device (No Protocol)", func() {
   442  			IsDeviceModelCreated, statusCode := utils.HandleDeviceModel(http.MethodPost, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, "", "led")
   443  			Expect(IsDeviceModelCreated).Should(BeTrue())
   444  			Expect(statusCode).Should(Equal(http.StatusCreated))
   445  			IsDeviceCreated, statusCode := utils.HandleDeviceInstance(http.MethodPost, ctx.Cfg.K8SMasterForKubeEdge+DeviceInstanceHandler, NodeName, "", "led")
   446  			Expect(IsDeviceCreated).Should(BeTrue())
   447  			Expect(statusCode).Should(Equal(http.StatusCreated))
   448  			time.Sleep(1 * time.Second)
   449  			statusCode, _ = utils.GetConfigmap(ctx.Cfg.K8SMasterForKubeEdge + ConfigmapHandler + "/" + "device-profile-config-" + NodeName)
   450  			Expect(statusCode).Should(Equal(http.StatusOK))
   451  			IsDeviceDeleted, statusCode := utils.HandleDeviceInstance(http.MethodDelete, ctx.Cfg.K8SMasterForKubeEdge+DeviceInstanceHandler, NodeName, "/"+utils.NewLedDeviceInstance(NodeName).Name, "")
   452  			Expect(IsDeviceDeleted).Should(BeTrue())
   453  			Expect(statusCode).Should(Equal(http.StatusOK))
   454  			time.Sleep(1 * time.Second)
   455  			statusCode, _ = utils.GetConfigmap(ctx.Cfg.K8SMasterForKubeEdge + ConfigmapHandler + "/" + "device-profile-config-" + NodeName)
   456  			Expect(statusCode).Should(Equal(http.StatusNotFound))
   457  		})
   458  		It("E2E_DELETE_DEVICE_2: Delete device instance for a non-existing device", func() {
   459  			IsDeviceDeleted, statusCode := utils.HandleDeviceInstance(http.MethodDelete, ctx.Cfg.K8SMasterForKubeEdge+DeviceInstanceHandler, NodeName, "/"+utils.NewLedDeviceModel().Name, "")
   460  			Expect(IsDeviceDeleted).Should(BeTrue())
   461  			Expect(statusCode).Should(Equal(http.StatusNotFound))
   462  		})
   463  	})
   464  	Context("Test Change in device twin", func() {
   465  		BeforeEach(func() {
   466  			var deviceModelList v1alpha1.DeviceModelList
   467  			var deviceList v1alpha1.DeviceList
   468  			// Delete the device instances created
   469  			deviceInstanceList, err := utils.GetDevice(&deviceList, ctx.Cfg.K8SMasterForKubeEdge+DeviceInstanceHandler, nil)
   470  			Expect(err).To(BeNil())
   471  			for _, device := range deviceInstanceList {
   472  				IsDeviceDeleted, statusCode := utils.HandleDeviceInstance(http.MethodDelete, ctx.Cfg.K8SMasterForKubeEdge+DeviceInstanceHandler, NodeName, "/"+device.Name, "")
   473  				Expect(IsDeviceDeleted).Should(BeTrue())
   474  				Expect(statusCode).Should(Equal(http.StatusOK))
   475  			}
   476  			// Delete any pre-exisitng device models
   477  			list, err := utils.GetDeviceModel(&deviceModelList, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, nil)
   478  			Expect(err).To(BeNil())
   479  			for _, model := range list {
   480  				IsDeviceModelDeleted, statusCode := utils.HandleDeviceModel(http.MethodDelete, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, "/"+model.Name, "")
   481  				Expect(IsDeviceModelDeleted).Should(BeTrue())
   482  				Expect(statusCode).Should(Equal(http.StatusOK))
   483  			}
   484  			utils.TwinResult = utils.DeviceTwinResult{}
   485  			// Get current test description
   486  			testDescription = CurrentGinkgoTestDescription()
   487  			// Start test timer
   488  			testTimer = CRDTestTimerGroup.NewTestTimer(testDescription.TestText)
   489  		})
   490  		AfterEach(func() {
   491  			// End test timer
   492  			testTimer.End()
   493  			// Print result
   494  			testTimer.PrintResult()
   495  			var deviceModelList v1alpha1.DeviceModelList
   496  			var deviceList v1alpha1.DeviceList
   497  			// Delete the device instances created
   498  			deviceInstanceList, err := utils.GetDevice(&deviceList, ctx.Cfg.K8SMasterForKubeEdge+DeviceInstanceHandler, nil)
   499  			Expect(err).To(BeNil())
   500  			for _, device := range deviceInstanceList {
   501  				IsDeviceDeleted, statusCode := utils.HandleDeviceInstance(http.MethodDelete, ctx.Cfg.K8SMasterForKubeEdge+DeviceInstanceHandler, NodeName, "/"+device.Name, "")
   502  				Expect(IsDeviceDeleted).Should(BeTrue())
   503  				Expect(statusCode).Should(Equal(http.StatusOK))
   504  			}
   505  			// Delete the device models created
   506  			list, err := utils.GetDeviceModel(&deviceModelList, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, nil)
   507  			Expect(err).To(BeNil())
   508  			for _, model := range list {
   509  				IsDeviceModelDeleted, statusCode := utils.HandleDeviceModel(http.MethodDelete, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, "/"+model.Name, "")
   510  				Expect(IsDeviceModelDeleted).Should(BeTrue())
   511  				Expect(statusCode).Should(Equal(http.StatusOK))
   512  			}
   513  			utils.PrintTestcaseNameandStatus()
   514  		})
   515  		It("E2E_TWIN_STATE_1: Change the twin state of an existing device", func() {
   516  			var deviceList v1alpha1.DeviceList
   517  			IsDeviceModelCreated, statusCode := utils.HandleDeviceModel(http.MethodPost, ctx.Cfg.K8SMasterForKubeEdge+DeviceModelHandler, "", "led")
   518  			Expect(IsDeviceModelCreated).Should(BeTrue())
   519  			Expect(statusCode).Should(Equal(http.StatusCreated))
   520  			IsDeviceCreated, statusCode := utils.HandleDeviceInstance(http.MethodPost, ctx.Cfg.K8SMasterForKubeEdge+DeviceInstanceHandler, NodeName, "", "led")
   521  			Expect(IsDeviceCreated).Should(BeTrue())
   522  			Expect(statusCode).Should(Equal(http.StatusCreated))
   523  			newLedDevice := utils.NewLedDeviceInstance(NodeName)
   524  			time.Sleep(3 * time.Second)
   525  			var deviceTwinUpdateMessage utils.DeviceTwinUpdate
   526  			reportedValue := "OFF"
   527  			deviceTwinUpdateMessage.Twin = map[string]*utils.MsgTwin{
   528  				"power-status": {Actual: &utils.TwinValue{Value: &reportedValue}, Metadata: &utils.TypeMetadata{Type: "string"}},
   529  			}
   530  			err := utils.ChangeTwinValue(deviceTwinUpdateMessage, utils.NewLedDeviceInstance(NodeName).Name)
   531  			Expect(err).To(BeNil())
   532  			time.Sleep(3 * time.Second)
   533  			newLedDevice = utils.NewLedDeviceInstance(NodeName)
   534  			list, err := utils.GetDevice(&deviceList, ctx.Cfg.K8SMasterForKubeEdge+DeviceInstanceHandler, &newLedDevice)
   535  			Expect(err).To(BeNil())
   536  			Expect(list[0].Status.Twins[0].PropertyName).To(Equal("power-status"))
   537  			Expect(list[0].Status.Twins[0].Reported.Value).To(Equal("OFF"))
   538  		})
   539  	})
   540  })