diff options
| author | KubeEdge Bot <48982446+kubeedge-bot@users.noreply.github.com> | 2020-05-28 21:34:52 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-05-28 21:34:52 +0800 |
| commit | de214535a4673a9e82cfd11e0a2b139050cc5276 (patch) | |
| tree | 3c053e1858a0b4b9c0ef4476c398e2c820c09742 | |
| parent | Merge pull request #1730 from daixiang0/unused (diff) | |
| parent | feedback (diff) | |
| download | kubeedge-de214535a4673a9e82cfd11e0a2b139050cc5276.tar.gz | |
Merge pull request #1720 from daixiang0/enable-goconst
Lint: enable goconst linter
| -rw-r--r-- | .golangci.yml | 1 | ||||
| -rw-r--r-- | edge/pkg/devicetwin/dtmanager/twin.go | 14 | ||||
| -rw-r--r-- | edge/pkg/devicetwin/dtmanager/twin_test.go | 8 | ||||
| -rw-r--r-- | edge/pkg/edged/edged_pods.go | 10 | ||||
| -rw-r--r-- | edge/pkg/eventbus/mqtt/client.go | 10 | ||||
| -rw-r--r-- | edge/test/test.go | 24 | ||||
| -rw-r--r-- | tests/e2e/deployment/device_crd_test.go | 10 | ||||
| -rw-r--r-- | tests/e2e/mapper/bluetooth/bluetooth_test.go | 15 | ||||
| -rw-r--r-- | tests/e2e/utils/common.go | 49 |
9 files changed, 77 insertions, 64 deletions
diff --git a/.golangci.yml b/.golangci.yml index 81b2a6185..88791b05b 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -45,6 +45,7 @@ linters-settings: linters: disable-all: true enable: + - goconst - gofmt - golint - gosimple diff --git a/edge/pkg/devicetwin/dtmanager/twin.go b/edge/pkg/devicetwin/dtmanager/twin.go index b3c9d536c..1e852169a 100644 --- a/edge/pkg/devicetwin/dtmanager/twin.go +++ b/edge/pkg/devicetwin/dtmanager/twin.go @@ -31,6 +31,8 @@ const ( DealActual = 1 //DealExpected deal exepected DealExpected = 0 + + stringType = "string" ) var ( @@ -505,7 +507,7 @@ func isTwinValueDiff(twin *dttype.MsgTwin, msgTwin *dttype.MsgTwin, dealType int if msgTwinValue != nil { hasMsgTwin = true } - valueType := "string" + valueType := stringType if strings.Compare(twin.Metadata.Type, "deleted") == 0 { if msgTwin.Metadata != nil { valueType = msgTwin.Metadata.Type @@ -701,8 +703,8 @@ func dealTwinCompare(returnResult *dttype.DealTwinResult, deviceID string, key s } } else { if strings.Compare(twin.Metadata.Type, "deleted") == 0 { - twin.Metadata = &dttype.TypeMetadata{Type: "string"} - cols["attr_type"] = "string" + twin.Metadata = &dttype.TypeMetadata{Type: stringType} + cols["attr_type"] = stringType syncResult[key].Metadata = twin.Metadata isChange = true } @@ -784,7 +786,7 @@ func dealTwinAdd(returnResult *dttype.DealTwinResult, deviceID string, key strin return nil } // value type default string - valueType := "string" + valueType := stringType if msgTwin.Metadata != nil { valueType = msgTwin.Metadata.Type } @@ -824,7 +826,7 @@ func dealTwinAdd(returnResult *dttype.DealTwinResult, deviceID string, key strin } return nil } - valueType := "string" + valueType := stringType if msgTwin.Metadata != nil { valueType = msgTwin.Metadata.Type } @@ -868,7 +870,7 @@ func dealTwinAdd(returnResult *dttype.DealTwinResult, deviceID string, key strin msgTwin.Metadata.Type = deviceTwin.AttrType isChange = true } else { - deviceTwin.AttrType = "string" + deviceTwin.AttrType = stringType isChange = true } diff --git a/edge/pkg/devicetwin/dtmanager/twin_test.go b/edge/pkg/devicetwin/dtmanager/twin_test.go index fae293cd9..120f6f643 100644 --- a/edge/pkg/devicetwin/dtmanager/twin_test.go +++ b/edge/pkg/devicetwin/dtmanager/twin_test.go @@ -47,6 +47,8 @@ var ( typeDeleted = "deleted" typeInt = "int" typeString = "string" + + valueType = "value" ) // sendMsg sends message to receiverChannel and heartbeatChannel @@ -74,7 +76,7 @@ func receiveMsg(commChannel chan interface{}, message *dttype.DTMessage) { // twinValueFunc returns a new TwinValue func twinValueFunc() *dttype.TwinValue { var twinValue dttype.TwinValue - value := "value" + value := valueType valueMetaData := &dttype.ValueMetadata{Timestamp: time.Now().UnixNano() / 1e6} twinValue.Value = &value twinValue.Metadata = valueMetaData @@ -522,7 +524,7 @@ func TestDealDeviceTwinResult(t *testing.T) { str := typeString optionTrue := true - value := "value" + value := valueType msgTwinValue := make(map[string]*dttype.MsgTwin) msgTwinValue[deviceB] = &dttype.MsgTwin{ Expected: &dttype.TwinValue{Value: &value}, @@ -1326,7 +1328,7 @@ func TestDealTwinAdd(t *testing.T) { // TestDealMsgTwin is function to test DealMsgTwin func TestDealMsgTwin(t *testing.T) { - value := "value" + value := valueType str := typeString optionTrue := true optionFalse := false diff --git a/edge/pkg/edged/edged_pods.go b/edge/pkg/edged/edged_pods.go index 073f0d1d9..09c984600 100644 --- a/edge/pkg/edged/edged_pods.go +++ b/edge/pkg/edged/edged_pods.go @@ -74,6 +74,8 @@ import ( const ( etcHostsPath = "/etc/hosts" systemdSuffix = ".slice" + + windows = "windows" ) // GetActivePods returns non-terminal pods @@ -287,7 +289,7 @@ func makeMounts(pod *v1.Pod, podDir string, container *v1.Container, hostName, h // - OS is not Windows // Kubernetes will not mount /etc/hosts if: // - when the Pod sandbox is being created, its IP is still unknown. Hence, PodIP will not have been set. - mountEtcHostsFile := len(podIP) > 0 && runtime.GOOS != "windows" + mountEtcHostsFile := len(podIP) > 0 && runtime.GOOS != windows klog.Infof("container: %v/%v/%v podIP: %q creating hosts mount: %v", pod.Namespace, pod.Name, container.Name, podIP, mountEtcHostsFile) mounts := []kubecontainer.Mount{} for _, mount := range container.VolumeMounts { @@ -351,7 +353,7 @@ func makeMounts(pod *v1.Pod, podDir string, container *v1.Container, hostName, h // Docker Volume Mounts fail on Windows if it is not of the form C:/ containerPath := mount.MountPath - if runtime.GOOS == "windows" { + if runtime.GOOS == windows { if (strings.HasPrefix(hostPath, "/") || strings.HasPrefix(hostPath, "\\")) && !strings.Contains(hostPath, ":") { hostPath = "c:" + hostPath } @@ -393,7 +395,7 @@ func makeMounts(pod *v1.Pod, podDir string, container *v1.Container, hostName, h } func makeAbsolutePath(goos, path string) string { - if goos != "windows" { + if goos != windows { return "/" + path } // These are all for windows @@ -601,7 +603,7 @@ func (e *edged) GenerateRunContainerOptions(pod *v1.Pod, container *v1.Container // Disabling adding TerminationMessagePath on Windows as these files would be mounted as docker volume and // Docker for Windows has a bug where only directories can be mounted - if len(container.TerminationMessagePath) != 0 && runtime.GOOS != "windows" { + if len(container.TerminationMessagePath) != 0 && runtime.GOOS != windows { p := e.getPodContainerDir(pod.UID, container.Name) if err := os.MkdirAll(p, 0750); err != nil { klog.Errorf("Error on creating %q: %v", p, err) diff --git a/edge/pkg/eventbus/mqtt/client.go b/edge/pkg/eventbus/mqtt/client.go index c16c49f85..8b780ecf2 100644 --- a/edge/pkg/eventbus/mqtt/client.go +++ b/edge/pkg/eventbus/mqtt/client.go @@ -16,6 +16,8 @@ import ( "github.com/kubeedge/kubeedge/edge/pkg/eventbus/common/util" ) +const UploadTopic = "SYS/dis/upload_records" + var ( // MQTTHub client MQTTHub *Client @@ -47,7 +49,7 @@ var ( "$hw/events/device/+/state/update", "$hw/events/device/+/twin/+", "$hw/events/node/+/membership/get", - "SYS/dis/upload_records", + UploadTopic, } ) @@ -92,15 +94,15 @@ func OnSubMessageReceived(client MQTT.Client, message MQTT.Message) { klog.Infof("OnSubMessageReceived receive msg from topic: %s", message.Topic()) // for "$hw/events/device/+/twin/+", "$hw/events/node/+/membership/get", send to twin // for other, send to hub - // for "SYS/dis/upload_records", no need to base64 topic + // for topic, no need to base64 topic var target string resource := base64.URLEncoding.EncodeToString([]byte(message.Topic())) if strings.HasPrefix(message.Topic(), "$hw/events/device") || strings.HasPrefix(message.Topic(), "$hw/events/node") { target = modules.TwinGroup } else { target = modules.HubGroup - if message.Topic() == "SYS/dis/upload_records" { - resource = "SYS/dis/upload_records" + if message.Topic() == UploadTopic { + resource = UploadTopic } } // routing key will be $hw.<project_id>.events.user.bus.response.cluster.<cluster_id>.node.<node_id>.<base64_topic> diff --git a/edge/test/test.go b/edge/test/test.go index 9ade18702..b506d5ab7 100644 --- a/edge/test/test.go +++ b/edge/test/test.go @@ -108,11 +108,11 @@ func (tm *testManager) podHandler(w http.ResponseWriter, req *http.Request) { } switch req.Method { - case "POST": + case http.MethodPost: operation = model.InsertOperation - case "DELETE": + case http.MethodDelete: operation = model.DeleteOperation - case "PUT": + case http.MethodPut: operation = model.UpdateOperation } @@ -144,11 +144,11 @@ func (tm *testManager) deviceHandler(w http.ResponseWriter, req *http.Request) { w.Write([]byte("unmarshal request body error")) } switch req.Method { - case "POST": + case http.MethodPost: operation = model.InsertOperation - case "DELETE": + case http.MethodDelete: operation = model.DeleteOperation - case "PUT": + case http.MethodPut: operation = model.UpdateOperation } msgReq := message.BuildMsg("edgehub", "", "edgemgr", "membership", operation, Content) @@ -173,11 +173,11 @@ func (tm *testManager) secretHandler(w http.ResponseWriter, req *http.Request) { } switch req.Method { - case "POST": + case http.MethodPost: operation = model.InsertOperation - case "DELETE": + case http.MethodDelete: operation = model.DeleteOperation - case "PUT": + case http.MethodPut: operation = model.UpdateOperation } @@ -203,11 +203,11 @@ func (tm *testManager) configmapHandler(w http.ResponseWriter, req *http.Request } switch req.Method { - case "POST": + case http.MethodPost: operation = model.InsertOperation - case "DELETE": + case http.MethodDelete: operation = model.DeleteOperation - case "PUT": + case http.MethodPut: operation = model.UpdateOperation } diff --git a/tests/e2e/deployment/device_crd_test.go b/tests/e2e/deployment/device_crd_test.go index 0e1e750e4..664f5a5e5 100644 --- a/tests/e2e/deployment/device_crd_test.go +++ b/tests/e2e/deployment/device_crd_test.go @@ -33,6 +33,8 @@ const ( DeviceInstanceHandler = "/apis/devices.kubeedge.io/v1alpha1/namespaces/default/devices" DeviceModelHandler = "/apis/devices.kubeedge.io/v1alpha1/namespaces/default/devicemodels" ConfigmapHandler = "/api/v1/namespaces/default/configmaps" + + off = "OFF" ) var CRDTestTimerGroup *utils.TestTimerGroup = utils.NewTestTimerGroup() @@ -302,7 +304,7 @@ var _ = Describe("Device Management test in E2E scenario", func() { Eventually(func() bool { return utils.TwinResult.Twin != nil }, "20s", "2s").Should(Equal(true), "Device information not reaching edge!!") - stringValue := "OFF" + stringValue := off expectedTwin := map[string]*utils.MsgTwin{ "temperature-enable": { Expected: &utils.TwinValue{ @@ -355,7 +357,7 @@ var _ = Describe("Device Management test in E2E scenario", func() { Eventually(func() bool { return utils.TwinResult.Twin != nil }, "20s", "2s").Should(Equal(true), "Device information not reaching edge!!") - stringValue := "OFF" + stringValue := off expectedTwin := map[string]*utils.MsgTwin{ "power-status": { Expected: &utils.TwinValue{ @@ -547,7 +549,7 @@ var _ = Describe("Device Management test in E2E scenario", func() { newLedDevice := utils.NewLedDeviceInstance(nodeName) time.Sleep(3 * time.Second) var deviceTwinUpdateMessage utils.DeviceTwinUpdate - reportedValue := "OFF" + reportedValue := off deviceTwinUpdateMessage.Twin = map[string]*utils.MsgTwin{ "power-status": {Actual: &utils.TwinValue{Value: &reportedValue}, Metadata: &utils.TypeMetadata{Type: "string"}}, } @@ -558,7 +560,7 @@ var _ = Describe("Device Management test in E2E scenario", func() { list, err := utils.GetDevice(&deviceList, ctx.Cfg.K8SMasterForKubeEdge+DeviceInstanceHandler, &newLedDevice) Expect(err).To(BeNil()) Expect(list[0].Status.Twins[0].PropertyName).To(Equal("power-status")) - Expect(list[0].Status.Twins[0].Reported.Value).To(Equal("OFF")) + Expect(list[0].Status.Twins[0].Reported.Value).To(Equal(off)) }) }) }) diff --git a/tests/e2e/mapper/bluetooth/bluetooth_test.go b/tests/e2e/mapper/bluetooth/bluetooth_test.go index e9241f9d8..cf4e8be0f 100644 --- a/tests/e2e/mapper/bluetooth/bluetooth_test.go +++ b/tests/e2e/mapper/bluetooth/bluetooth_test.go @@ -37,6 +37,8 @@ import ( "github.com/kubeedge/kubeedge/tests/e2e/utils" ) +const topic = "$ke/device/bluetooth-mapper/mock-temp-sensor-instance/scheduler/result" + var TokenClient Token var ClientOpts *MQTT.ClientOptions var Client MQTT.Client @@ -55,7 +57,6 @@ var readWrittenData bool // DataConversion checks whether data is properly as expected by the data converter. func DataConversion(client MQTT.Client, message MQTT.Message) { - topic := "$ke/device/bluetooth-mapper/mock-temp-sensor-instance/scheduler/result" expectedTemp := "32.375000" dataConverted = false if message.Topic() == topic { @@ -73,7 +74,6 @@ func DataConversion(client MQTT.Client, message MQTT.Message) { // WriteDataReceived checks whether data is properly written to connected device. func WriteDataReceived(client MQTT.Client, message MQTT.Message) { - topic := "$ke/device/bluetooth-mapper/mock-temp-sensor-instance/scheduler/result" readWrittenData = false if message.Topic() == topic { devicePayload := message.Payload() @@ -91,7 +91,6 @@ func WriteDataReceived(client MQTT.Client, message MQTT.Message) { // ScheculeExecute counts the number of times schedule is executed by the Scheduler. func ScheduleExecute(client MQTT.Client, message MQTT.Message) { - topic := "$ke/device/bluetooth-mapper/mock-temp-sensor-instance/scheduler/result" expectedTemp := "36" if message.Topic() == topic { devicePayload := message.Payload() @@ -119,10 +118,9 @@ var _ = Describe("Application deployment test in E2E scenario", func() { utils.Infof("Connection successful") } Expect(TokenClient.Error()).NotTo(HaveOccurred()) - scheduletopic := "$ke/device/bluetooth-mapper/mock-temp-sensor-instance/scheduler/result" - Token := Client.Subscribe(scheduletopic, 0, WriteDataReceived) + Token := Client.Subscribe(topic, 0, WriteDataReceived) if Token.Wait() && TokenClient.Error() != nil { - utils.Fatalf("Subscribe to Topic Failed %s, %s", TokenClient.Error(), scheduletopic) + utils.Fatalf("Subscribe to Topic Failed %s, %s", TokenClient.Error(), topic) } Expect(TokenClient.Error()).NotTo(HaveOccurred()) }) @@ -164,10 +162,9 @@ var _ = Describe("Application deployment test in E2E scenario", func() { utils.Infof("Subscribe Connection Successful") } Expect(TokenClient.Error()).NotTo(HaveOccurred()) - scheduletopic := "$ke/device/bluetooth-mapper/mock-temp-sensor-instance/scheduler/result" - Token := Client.Subscribe(scheduletopic, 0, DataConversion) + Token := Client.Subscribe(topic, 0, DataConversion) if Token.Wait() && TokenClient.Error() != nil { - utils.Fatalf("Subscribe to Topic Failed %s, %s", TokenClient.Error(), scheduletopic) + utils.Fatalf("Subscribe to Topic Failed %s, %s", TokenClient.Error(), topic) } Expect(TokenClient.Error()).NotTo(HaveOccurred()) var expectedSchedule []scheduler.Schedule diff --git a/tests/e2e/utils/common.go b/tests/e2e/utils/common.go index a1308bbb6..897f28888 100644 --- a/tests/e2e/utils/common.go +++ b/tests/e2e/utils/common.go @@ -48,6 +48,11 @@ const ( TwinETUpdateSuffix = "/twin/update" TwinETGetSuffix = "/twin/get" TwinETGetResultSuffix = "/twin/get/result" + + BlueTooth = "bluetooth" + ModBus = "modbus" + Led = "led" + IncorrectInstance = "incorrect-instance" ) var ( @@ -325,14 +330,14 @@ func HandlePod(operation string, apiserver string, UID string, pod *v1.Pod) bool Transport: tr, } switch operation { - case "POST": + case http.MethodPost: body := pod respBytes, err := json.Marshal(body) if err != nil { Fatalf("Marshalling body failed: %v", err) } req, err = http.NewRequest(http.MethodPost, apiserver, bytes.NewBuffer(respBytes)) - case "DELETE": + case http.MethodDelete: req, err = http.NewRequest(http.MethodDelete, apiserver+UID, body) } if err != nil { @@ -367,7 +372,7 @@ func HandleDeployment(IsCloudCore, IsEdgeCore bool, operation, apiserver, UID, I } switch operation { - case "POST": + case http.MethodPost: depObj := newDeployment(IsCloudCore, IsEdgeCore, UID, ImageURL, nodeselector, configmapname, replica) if err != nil { Fatalf("GenerateDeploymentBody marshalling failed: %v", err) @@ -377,7 +382,7 @@ func HandleDeployment(IsCloudCore, IsEdgeCore bool, operation, apiserver, UID, I Fatalf("Marshalling body failed: %v", err) } req, err = http.NewRequest(http.MethodPost, apiserver, bytes.NewBuffer(respBytes)) - case "DELETE": + case http.MethodDelete: req, err = http.NewRequest(http.MethodDelete, apiserver+UID, body) } if err != nil { @@ -543,7 +548,7 @@ func HandleDeviceModel(operation string, apiserver string, UID string, protocolT } switch operation { - case "POST": + case http.MethodPost: body := newDeviceModelObject(protocolType, false) respBytes, err := json.Marshal(body) if err != nil { @@ -559,7 +564,7 @@ func HandleDeviceModel(operation string, apiserver string, UID string, protocolT } req, err = http.NewRequest(http.MethodPatch, apiserver+UID, bytes.NewBuffer(respBytes)) req.Header.Set("Content-Type", "application/merge-patch+json") - case "DELETE": + case http.MethodDelete: req, err = http.NewRequest(http.MethodDelete, apiserver+UID, body) req.Header.Set("Content-Type", "application/json") } @@ -592,7 +597,7 @@ func HandleDeviceInstance(operation string, apiserver string, nodeSelector strin Transport: tr, } switch operation { - case "POST": + case http.MethodPost: body := newDeviceInstanceObject(nodeSelector, protocolType, false) respBytes, err := json.Marshal(body) if err != nil { @@ -608,7 +613,7 @@ func HandleDeviceInstance(operation string, apiserver string, nodeSelector strin } req, err = http.NewRequest(http.MethodPatch, apiserver+UID, bytes.NewBuffer(respBytes)) req.Header.Set("Content-Type", "application/merge-patch+json") - case "DELETE": + case http.MethodDelete: req, err = http.NewRequest(http.MethodDelete, apiserver+UID, body) req.Header.Set("Content-Type", "application/json") } @@ -633,24 +638,24 @@ func newDeviceInstanceObject(nodeSelector string, protocolType string, updated b var deviceInstance v1alpha1.Device if !updated { switch protocolType { - case "bluetooth": + case BlueTooth: deviceInstance = NewBluetoothDeviceInstance(nodeSelector) - case "modbus": + case ModBus: deviceInstance = NewModbusDeviceInstance(nodeSelector) - case "led": + case Led: deviceInstance = NewLedDeviceInstance(nodeSelector) - case "incorrect-instance": + case IncorrectInstance: deviceInstance = IncorrectDeviceInstance() } } else { switch protocolType { - case "bluetooth": + case BlueTooth: deviceInstance = UpdatedBluetoothDeviceInstance(nodeSelector) - case "modbus": + case ModBus: deviceInstance = UpdatedModbusDeviceInstance(nodeSelector) - case "led": + case Led: deviceInstance = UpdatedLedDeviceInstance(nodeSelector) - case "incorrect-instance": + case IncorrectInstance: deviceInstance = IncorrectDeviceInstance() } } @@ -662,22 +667,22 @@ func newDeviceModelObject(protocolType string, updated bool) *v1alpha1.DeviceMod var deviceModel v1alpha1.DeviceModel if !updated { switch protocolType { - case "bluetooth": + case BlueTooth: deviceModel = NewBluetoothDeviceModel() - case "modbus": + case ModBus: deviceModel = NewModbusDeviceModel() - case "led": + case Led: deviceModel = NewLedDeviceModel() case "incorrect-model": deviceModel = IncorrectDeviceModel() } } else { switch protocolType { - case "bluetooth": + case BlueTooth: deviceModel = UpdatedBluetoothDeviceModel() - case "modbus": + case ModBus: deviceModel = UpdatedModbusDeviceModel() - case "led": + case Led: deviceModel = UpdatedLedDeviceModel() case "incorrect-model": deviceModel = IncorrectDeviceModel() |
