Skip to content
This repository has been archived by the owner on Nov 7, 2023. It is now read-only.

Commit

Permalink
add unit tests for edgex client (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
wawlian authored Aug 31, 2022
1 parent 2fa8ab1 commit 95b5ab9
Show file tree
Hide file tree
Showing 11 changed files with 507 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
run: find ./* -name "*" | xargs misspell -error
- name: Lint markdown files
run: find ./ -name "*.md" | grep -v enhancements | grep -v .github | xargs mdl -r ~MD010,~MD013,~MD014,~MD022,~MD024,~MD029,~MD031,~MD032,~MD033,~MD034,~MD036
build-binaries:
build-and-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
test: generate fmt vet manifests
mkdir -p ${ENVTEST_ASSETS_DIR}
test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/v0.8.3/hack/setup-envtest.sh
/bin/bash -c "source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./pkg/controllers/... -coverprofile cover.out"
/bin/bash -c "source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./pkg/controllers/... ./pkg/clients/edgex-foundry/... -coverprofile cover.out"


# Build binaries
# ARGS:
Expand Down
4 changes: 2 additions & 2 deletions apis/device.openyurt.io/v1alpha1/groupversion_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ limitations under the License.
*/

// Package v1alpha1 contains API Schema definitions for the device v1alpha1 API group
//+kubebuilder:object:generate=true
//+groupName=device.openyurt.io
// +kubebuilder:object:generate=true
// +groupName=device.openyurt.io
package v1alpha1

import (
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.15
require (
github.com/edgexfoundry/go-mod-core-contracts/v2 v2.1.0
github.com/go-resty/resty/v2 v2.4.0
github.com/jarcoal/httpmock v1.2.0
github.com/onsi/ginkgo v1.16.4
github.com/onsi/gomega v1.14.0
github.com/spf13/cobra v1.2.1
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,8 @@ github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/influxdata/influxdb1-client v0.0.0-20200827194710-b269163b24ab/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
github.com/jarcoal/httpmock v1.2.0 h1:gSvTxxFR/MEMfsGrvRbdfpRUMBStovlSRLw0Ep1bwwc=
github.com/jarcoal/httpmock v1.2.0/go.mod h1:oCoTsnAz4+UoOUIf5lJOWV2QQIW5UoeUI6aM2YnWAZk=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
Expand Down Expand Up @@ -467,6 +469,8 @@ github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 h1:I0XW9+e1XWDxdcEniV4rQAIOPUGDq67JSCiRCgGCZLI=
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
github.com/maxatome/go-testdeep v1.11.0 h1:Tgh5efyCYyJFGUYiT0qxBSIDeXw0F5zSoatlou685kk=
github.com/maxatome/go-testdeep v1.11.0/go.mod h1:011SgQ6efzZYAen6fDn4BqQ+lUR72ysdyKe7Dyogw70=
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/miekg/dns v1.1.26/go.mod h1:bPDLeHnStXmXAq1m/Ch/hvfNHr14JKNPMBo3VZKjuso=
github.com/minio/highwayhash v1.0.1/go.mod h1:BQskDq+xkJ12lmlUUi7U0M5Swg3EWR+dLTk+kldvVxY=
Expand Down
48 changes: 21 additions & 27 deletions pkg/clients/edgex-foundry/device_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,13 @@ type EdgexDeviceClient struct {
}

func NewEdgexDeviceClient(coreMetaAddr, coreCommandAddr string) *EdgexDeviceClient {
cookieJar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
instance := resty.NewWithClient(&http.Client{
Jar: cookieJar,
Timeout: 10 * time.Second,
})
return &EdgexDeviceClient{
Client: resty.New(),
Client: instance,
CoreMetaAddr: coreMetaAddr,
CoreCommandAddr: coreCommandAddr,
}
Expand Down Expand Up @@ -105,29 +110,23 @@ func (efc *EdgexDeviceClient) Delete(ctx context.Context, name string, options c
// TODO support to update other fields
func (efc *EdgexDeviceClient) Update(ctx context.Context, device *devicev1alpha1.Device, options clients.UpdateOptions) (*devicev1alpha1.Device, error) {
actualDeviceName := getEdgeXName(device)
putURL := fmt.Sprintf("http://%s%s/name/%s", efc.CoreMetaAddr, DevicePath, actualDeviceName)
patchURL := fmt.Sprintf("http://%s%s", efc.CoreMetaAddr, DevicePath)
if device == nil {
return nil, nil
}
updateData := map[string]string{}
if device.Spec.AdminState != "" {
updateData["adminState"] = string(device.Spec.AdminState)
}
if device.Spec.OperatingState != "" {
updateData["operatingState"] = string(device.Spec.OperatingState)
}
if len(updateData) == 0 {
return nil, nil
devs := []*devicev1alpha1.Device{device}
req := makeEdgeXDeviceUpdateRequest(devs)
reqBody, err := json.Marshal(req)
if err != nil {
return nil, err
}

data, _ := json.Marshal(updateData)
rep, err := resty.New().R().
rep, err := efc.R().
SetHeader("Content-Type", "application/json").
SetBody(data).
Put(putURL)
SetBody(reqBody).
Patch(patchURL)
if err != nil {
return nil, err
} else if rep.StatusCode() != http.StatusOK {
} else if rep.StatusCode() != http.StatusMultiStatus {
return nil, fmt.Errorf("failed to update device: %s, get response: %s", actualDeviceName, string(rep.Body()))
}
return device, nil
Expand Down Expand Up @@ -200,7 +199,7 @@ func (efc *EdgexDeviceClient) GetPropertyState(ctx context.Context, propertyName
Name: propertyName,
GetURL: propertyGetURL,
}
if resp, err := getPropertyState(propertyGetURL); err != nil {
if resp, err := efc.getPropertyState(propertyGetURL); err != nil {
return nil, err
} else {
var eResp edgex_resp.EventResponse
Expand All @@ -213,13 +212,8 @@ func (efc *EdgexDeviceClient) GetPropertyState(ctx context.Context, propertyName
}

// getPropertyState returns different error messages according to the status code
func getPropertyState(getURL string) (*resty.Response, error) {
cookieJar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
// TODO: no need to instantiate a client for every request
resp, err := resty.NewWithClient(&http.Client{
Jar: cookieJar,
Timeout: 10 * time.Second,
}).R().Get(getURL)
func (efc *EdgexDeviceClient) getPropertyState(getURL string) (*resty.Response, error) {
resp, err := efc.R().Get(getURL)
if err != nil {
return resp, err
}
Expand Down Expand Up @@ -256,7 +250,7 @@ func (efc *EdgexDeviceClient) UpdatePropertyState(ctx context.Context, propertyN
bodyMap[parameterName] = dps.DesiredValue
body, _ := json.Marshal(bodyMap)
klog.V(5).Infof("setting the property to desired value", "propertyName", parameterName, "desiredValue", string(body))
rep, err := resty.New().R().
rep, err := efc.R().
SetHeader("Content-Type", "application/json").
SetBody(body).
Put(dps.PutURL)
Expand Down Expand Up @@ -310,7 +304,7 @@ func (efc *EdgexDeviceClient) ListPropertiesState(ctx context.Context, device *d
aps = devicev1alpha1.ActualPropertyState{Name: c.Name, GetURL: getURL}
}
apsm[c.Name] = aps
resp, err := getPropertyState(getURL)
resp, err := efc.getPropertyState(getURL)
if err != nil {
klog.V(5).ErrorS(err, "getPropertyState failed", "propertyName", c.Name, "deviceName", actualDeviceName)
} else {
Expand Down
Loading

0 comments on commit 95b5ab9

Please # to comment.