Skip to content

Commit 13e2819

Browse files
bridge: updates for tests (#464)
* upgrade golang to 1.22 --------- Co-authored-by: Jozef Kralik <jozef.kralik@plgd.dev>
1 parent 6021006 commit 13e2819

File tree

25 files changed

+873
-699
lines changed

25 files changed

+873
-699
lines changed

.github/workflows/build-with-cfg.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
- name: Set up Go
2424
uses: actions/setup-go@v5
2525
with:
26-
go-version: ${{ inputs.go-version || '^1.20' }}
26+
go-version: ${{ inputs.go-version || '^1.22' }}
2727
check-latest: true
2828

2929
- run: |

.github/workflows/builds.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ jobs:
1717
matrix:
1818
include:
1919
# test build of oldest supported go version
20-
- name: go1.20
21-
go-version: "~1.20"
20+
- name: go1.22
21+
go-version: "~1.22"
2222
uses: ./.github/workflows/build-with-cfg.yml
2323
with:
2424
go-version: ${{ matrix.go-version }}

.github/workflows/golangci-lint.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ jobs:
1616
- name: Checkout
1717
uses: actions/checkout@v4
1818

19-
- name: Set up Go 1.20+
19+
- name: Set up Go 1.22+
2020
uses: actions/setup-go@v5
2121
with:
22-
go-version: "^1.20" # The Go version to download (if necessary) and use.
22+
go-version: "^1.22" # The Go version to download (if necessary) and use.
2323
check-latest: true
2424
cache: false
2525

.github/workflows/test-with-cfg.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ jobs:
3737
- name: Shallow checkout
3838
uses: actions/checkout@v4
3939

40-
- name: Set up Go 1.20+
40+
- name: Set up Go 1.22+
4141
uses: actions/setup-go@v5
4242
with:
43-
go-version: "^1.20"
43+
go-version: "^1.22"
4444
check-latest: true
4545

4646
- run: go version

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,4 @@ issues:
163163
# fix: true
164164

165165
run:
166-
go: "1.20"
166+
go: "1.22"

Makefile

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,34 @@ test: env build-testcontainer
123123
-v $(TMP_PATH):/tmp \
124124
$(SERVICE_NAME):$(VERSION_TAG) -test.parallel 1 -test.v -test.coverprofile=/tmp/coverage.txt
125125

126-
test-bridge:
126+
test-bridge/clean:
127+
pkill -KILL bridge-device || :
128+
rm -rf $(TMP_PATH)/bridge || :
129+
130+
define SET-BRIDGE-DEVICE-CONFIG
131+
yq -i '.apis.coap.externalAddresses=["127.0.0.1:15683","[::1]:15683"]' $(1)
132+
yq -i '.cloud.enabled=true' $(1)
133+
yq -i '.cloud.cloudID="$(CLOUD_SID)"' $(1)
134+
yq -i '.cloud.tls.caPoolPath="$(2)/data/certs/root_ca.crt"' $(1)
135+
yq -i '.cloud.tls.keyPath="$(2)/data/certs/external/coap-gateway.key"' $(1)
136+
yq -i '.cloud.tls.certPath="$(2)/data/certs/external/coap-gateway.crt"' $(1)
137+
yq -i '.numGeneratedBridgedDevices=3' $(1)
138+
yq -i '.numResourcesPerDevice=0' $(1)
139+
yq -i '.thingDescription.enabled=true' $(1)
140+
yq -i '.thingDescription.file="$(2)/bridge/bridge-device.jsonld"' $(1)
141+
endef
142+
143+
# config-docker.yaml -> copy of configuration with paths valid inside docker container
144+
# config-test.yaml -> copy of configuration with paths valid on host machine
145+
test-bridge/env: test-bridge/clean
146+
mkdir -p $(TMP_PATH)/bridge
147+
cp ./cmd/bridge-device/bridge-device.jsonld $(TMP_PATH)/bridge
148+
cp ./cmd/bridge-device/config.yaml $(TMP_PATH)/bridge/config-docker.yaml
149+
$(call SET-BRIDGE-DEVICE-CONFIG,$(TMP_PATH)/bridge/config-docker.yaml,)
150+
cp $(TMP_PATH)/bridge/config-docker.yaml $(TMP_PATH)/bridge/config-test.yaml
151+
$(call SET-BRIDGE-DEVICE-CONFIG,$(TMP_PATH)/bridge/config-test.yaml,$(TMP_PATH))
152+
153+
test-bridge: test-bridge/env
127154
sudo rm -rf $(TMP_PATH)/data || :
128155
mkdir -p $(TMP_PATH)/data
129156
# pull image
@@ -141,15 +168,10 @@ test-bridge:
141168
$(HUB_TEST_DEVICE_IMAGE)
142169

143170
# start device
144-
rm -rf $(TMP_PATH)/bridge || :
145-
mkdir -p $(TMP_PATH)/bridge
146-
go build -C ./test/bridge-device -cover -o ./bridge-device
171+
go build -C ./cmd/bridge-device -cover -o ./bridge-device
147172
pkill -KILL bridge-device || :
148-
CLOUD_SID=$(CLOUD_SID) CA_POOL=$(TMP_PATH)/data/certs/root_ca.crt \
149-
CERT_FILE=$(TMP_PATH)/data/certs/external/coap-gateway.crt \
150-
KEY_FILE=$(TMP_PATH)/data/certs/external/coap-gateway.key \
151173
GOCOVERDIR=$(TMP_PATH)/bridge \
152-
./test/bridge-device/bridge-device &
174+
./cmd/bridge-device/bridge-device -config $(TMP_PATH)/bridge/config-test.yaml &
153175

154176
# run tests
155177
docker run \
@@ -161,9 +183,11 @@ test-bridge:
161183
--env COAP_GATEWAY_CLOUD_ID="$(CLOUD_SID)" \
162184
--env TEST_DEVICE_NAME="bridged-device-0" \
163185
--env TEST_DEVICE_TYPE="bridged" \
186+
--env TEST_BRIDGE_DEVICE_CONFIG="/bridge/config-docker.yaml" \
164187
--env GRPC_GATEWAY_TEST_DISABLED=1 \
165188
--env IOTIVITY_LITE_TEST_RUN="(TestOffboard|TestOffboardWithoutSignIn|TestOffboardWithRepeat|TestRepublishAfterRefresh)$$" \
166189
-v $(TMP_PATH):/tmp \
190+
-v $(TMP_PATH)/bridge:/bridge \
167191
-v $(TMP_PATH)/data:/data \
168192
$(HUB_TEST_DEVICE_IMAGE)
169193

@@ -175,11 +199,10 @@ test-bridge:
175199
done
176200
go tool covdata textfmt -i=$(TMP_PATH)/bridge -o $(TMP_PATH)/bridge.coverage.txt
177201

178-
clean:
202+
clean: test-bridge/clean
179203
docker rm -f devsim-net-host || :
180204
docker rm -f hub-device-tests-environment || :
181205
docker rm -f hub-device-tests || :
182-
pkill -KILL bridge-device || :
183206
sudo rm -rf .tmp/*
184207

185208
.PHONY: build-testcontainer build certificates clean env test unit-test

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The **client** enables interaction with devices in a local network:
1616

1717
## Requirements
1818

19-
- Go 1.20 or higher
19+
- Go 1.22 or higher
2020

2121
## Installation OCF Client
2222

Lines changed: 0 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,19 @@
11
package thingDescription
22

33
import (
4-
"net/url"
54
"reflect"
65
"sync/atomic"
76

8-
"github.com/fredbi/uri"
97
"github.com/google/uuid"
108
"github.com/plgd-dev/device/v2/bridge/net"
119
"github.com/plgd-dev/device/v2/bridge/resources"
1210
"github.com/plgd-dev/device/v2/pkg/eventloop"
1311
"github.com/plgd-dev/device/v2/schema"
14-
"github.com/plgd-dev/go-coap/v3/message"
1512
"github.com/plgd-dev/go-coap/v3/message/pool"
1613
"github.com/plgd-dev/go-coap/v3/pkg/sync"
1714
"github.com/web-of-things-open-source/thingdescription-go/thingDescription"
1815
)
1916

20-
var (
21-
SecurityNoSec = "nosec_sc"
22-
SecurityDefinitions = map[string]thingDescription.SecurityScheme{
23-
SecurityNoSec: {
24-
Scheme: "nosec",
25-
},
26-
}
27-
HTTPSWWWW3Org2022WotTdV11 = thingDescription.HTTPSWWWW3Org2022WotTdV11
28-
Context = thingDescription.ThingContext{
29-
Enum: &HTTPSWWWW3Org2022WotTdV11,
30-
}
31-
)
32-
3317
// Resource to avoid import cycle also it is same as in Device package to avoid wrapping it
3418
type Resource = interface {
3519
Close()
@@ -117,115 +101,3 @@ func (t *Manager) NotifySubscriptions(td thingDescription.ThingDescription) {
117101
default:
118102
}
119103
}
120-
121-
func supportedOperationToTDOperation(ops resources.SupportedOperation) []string {
122-
tdOps := make([]string, 0, 3)
123-
if ops.HasOperation(resources.SupportedOperationRead) {
124-
tdOps = append(tdOps, string(thingDescription.Readproperty))
125-
}
126-
if ops.HasOperation(resources.SupportedOperationWrite) {
127-
tdOps = append(tdOps, string(thingDescription.Writeproperty))
128-
}
129-
if ops.HasOperation(resources.SupportedOperationObserve) {
130-
tdOps = append(tdOps, string(thingDescription.Observeproperty), string(thingDescription.Unobserveproperty))
131-
}
132-
if len(tdOps) == 0 {
133-
return nil
134-
}
135-
return tdOps
136-
}
137-
138-
func boolToPtr(v bool) *bool {
139-
if !v {
140-
return nil
141-
}
142-
return &v
143-
}
144-
145-
func stringToPtr(v string) *string {
146-
if v == "" {
147-
return nil
148-
}
149-
return &v
150-
}
151-
152-
func createForms(deviceID uuid.UUID, href string, supportedOperations resources.SupportedOperation, setForm bool) []thingDescription.FormElementProperty {
153-
if !setForm {
154-
return nil
155-
}
156-
ops := supportedOperationToTDOperation(supportedOperations)
157-
if len(ops) > 0 {
158-
hrefStr := href
159-
if deviceID != uuid.Nil {
160-
hrefStr += "?di=" + deviceID.String()
161-
}
162-
href, err := url.Parse(hrefStr)
163-
if err == nil {
164-
return []thingDescription.FormElementProperty{
165-
{
166-
ContentType: stringToPtr(message.AppCBOR.String()),
167-
Op: &thingDescription.FormElementPropertyOp{
168-
StringArray: ops,
169-
},
170-
Href: *href,
171-
},
172-
}
173-
}
174-
}
175-
return nil
176-
}
177-
178-
func PatchPropertyElement(prop thingDescription.PropertyElement, deviceID uuid.UUID, resource Resource, setForm bool) thingDescription.PropertyElement {
179-
ops := resource.SupportsOperations()
180-
observable := ops.HasOperation(resources.SupportedOperationObserve)
181-
isReadOnly := ops.HasOperation(resources.SupportedOperationRead) && !ops.HasOperation(resources.SupportedOperationWrite)
182-
isWriteOnly := ops.HasOperation(resources.SupportedOperationWrite) && !ops.HasOperation(resources.SupportedOperationRead)
183-
resourceTypes := resource.GetResourceTypes()
184-
185-
prop.Type = &thingDescription.TypeDeclaration{
186-
StringArray: resourceTypes,
187-
}
188-
prop.Observable = boolToPtr(observable)
189-
prop.ReadOnly = boolToPtr(isReadOnly)
190-
prop.WriteOnly = boolToPtr(isWriteOnly)
191-
prop.Observable = boolToPtr(observable)
192-
prop.Forms = createForms(deviceID, resource.GetHref(), ops, setForm)
193-
return prop
194-
}
195-
196-
func PatchThingDescription(td thingDescription.ThingDescription, device Device, endpoint string, getPropertyElement func(resourceHref string, resource Resource) (thingDescription.PropertyElement, bool)) thingDescription.ThingDescription {
197-
if td.Context == nil {
198-
td.Context = &Context
199-
}
200-
id, err := uri.Parse("urn:uuid:" + device.GetID().String())
201-
if err == nil {
202-
td.ID = id
203-
}
204-
td.Title = device.GetName()
205-
if endpoint != "" {
206-
// base
207-
u, err := url.Parse(endpoint)
208-
if err == nil {
209-
td.Base = *u
210-
}
211-
// security
212-
td.Security = &thingDescription.TypeDeclaration{
213-
String: &SecurityNoSec,
214-
}
215-
// securityDefinitions
216-
td.SecurityDefinitions = SecurityDefinitions
217-
}
218-
219-
device.Range(func(resourceHref string, resource Resource) bool {
220-
pe, ok := getPropertyElement(resourceHref, resource)
221-
if !ok {
222-
return true
223-
}
224-
if td.Properties == nil {
225-
td.Properties = make(map[string]thingDescription.PropertyElement)
226-
}
227-
td.Properties[resourceHref] = pe
228-
return true
229-
})
230-
return td
231-
}

0 commit comments

Comments
 (0)