Skip to content

Commit

Permalink
fixup! Refactor and add unit tests (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Jan 22, 2024
1 parent 2a55444 commit a83e282
Show file tree
Hide file tree
Showing 17 changed files with 143 additions and 29 deletions.
100 changes: 100 additions & 0 deletions bridge/onboardDevice_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/****************************************************************************
*
* Copyright (c) 2024 plgd.dev s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License"),
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*
****************************************************************************/

package bridge_test

import (
"context"
"testing"
"time"

"github.com/google/uuid"
bridgeTest "github.com/plgd-dev/device/v2/bridge/test"
"github.com/plgd-dev/device/v2/test"
testClient "github.com/plgd-dev/device/v2/test/client"
"github.com/stretchr/testify/require"
)

func TestOnboardDevice(t *testing.T) {
s := bridgeTest.NewBridgeService(t)
deviceID := uuid.New().String()
d := bridgeTest.NewBridgedDevice(t, s, true, deviceID)
defer func() {
s.DeleteAndCloseDevice(d.GetID())
}()
cleanup := bridgeTest.RunBridgeService(s)
defer cleanup()

type args struct {
deviceID string
authorizationProvider string
authorizationCode string
cloudURL string
cloudID string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "notFound",
args: args{
deviceID: "notFound",
authorizationProvider: "authorizationProvider",
authorizationCode: "authorizationCode",
cloudURL: "coaps+tcp://test:5684",
cloudID: "cloudID",
},
wantErr: true,
},
{
name: "valid",
args: args{
deviceID: deviceID,
authorizationProvider: "authorizationProvider",
authorizationCode: "authorizationCode",
cloudURL: "coaps+tcp://test:5684",
cloudID: "cloudID",
},
},
}

c, err := testClient.NewTestSecureClient()
require.NoError(t, err)
defer func() {
errC := c.Close(context.Background())
require.NoError(t, errC)
}()
ctx, cancel := context.WithTimeout(context.Background(), test.TestTimeout)
defer cancel()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ttCtx, ttCancel := context.WithTimeout(ctx, time.Second)
defer ttCancel()
err = c.OnboardDevice(ttCtx, tt.args.deviceID, tt.args.authorizationProvider, tt.args.cloudURL, tt.args.authorizationCode, tt.args.cloudID, withDeviceID(d.GetID().String()))
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
err = c.OffboardDevice(ttCtx, tt.args.deviceID, withDeviceID(d.GetID().String()))
require.NoError(t, err)
})
}
}
4 changes: 1 addition & 3 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ import (
"github.com/stretchr/testify/require"
)

const TestTimeout = time.Second * 8

var (
ETagSupported = false
ETagBatchSupported = false
Expand Down Expand Up @@ -91,7 +89,7 @@ func init() {
panicIfErr(errC)
}()

ctx, cancel := context.WithTimeout(context.Background(), TestTimeout)
ctx, cancel := context.WithTimeout(context.Background(), test.TestTimeout)
defer cancel()

deviceID, err = c.OwnDevice(ctx, deviceID)
Expand Down
2 changes: 1 addition & 1 deletion client/createResource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestClientCreateResource(t *testing.T) {
errC := c.Close(context.Background())
require.NoError(t, errC)
}()
ctx, cancel := context.WithTimeout(context.Background(), TestTimeout)
ctx, cancel := context.WithTimeout(context.Background(), test.TestTimeout)
defer cancel()
deviceID, err = c.OwnDevice(ctx, deviceID, client.WithOTMs([]client.OTMType{client.OTMType_JustWorks}))
require.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions client/deleteResource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestClientDeleteResource(t *testing.T) {
errC := c.Close(context.Background())
require.NoError(t, errC)
}()
ctx, cancel := context.WithTimeout(context.Background(), TestTimeout)
ctx, cancel := context.WithTimeout(context.Background(), test.TestTimeout)
defer cancel()
deviceID, err = c.OwnDevice(ctx, deviceID, client.WithOTMs([]client.OTMType{client.OTMType_JustWorks}))
require.NoError(t, err)
Expand Down Expand Up @@ -124,7 +124,7 @@ func TestClientBatchDeleteResources(t *testing.T) {
errC := c.Close(context.Background())
require.NoError(t, errC)
}()
ctx, cancel := context.WithTimeout(context.Background(), TestTimeout*50)
ctx, cancel := context.WithTimeout(context.Background(), test.TestTimeout)
defer cancel()
deviceID, err = c.OwnDevice(ctx, deviceID, client.WithOTMs([]client.OTMType{client.OTMType_JustWorks}))
require.NoError(t, err)
Expand Down
6 changes: 3 additions & 3 deletions client/getDevice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func TestClientGetDevice(t *testing.T) {
},
}

ctx, cancel := context.WithTimeout(context.Background(), TestTimeout)
ctx, cancel := context.WithTimeout(context.Background(), test.TestTimeout)
defer cancel()

c, err := testClient.NewTestSecureClient()
Expand Down Expand Up @@ -189,7 +189,7 @@ func TestClientGetDeviceByIP(t *testing.T) {
},
}

ctx, cancel := context.WithTimeout(context.Background(), TestTimeout)
ctx, cancel := context.WithTimeout(context.Background(), test.TestTimeout)
defer cancel()

c, err := testClient.NewTestSecureClient()
Expand Down Expand Up @@ -297,7 +297,7 @@ func TestClientGetDeviceByIPOwnedByOther(t *testing.T) {
deviceID := test.MustFindDeviceByName(test.DevsimName)
ip4 := test.MustFindDeviceIP(test.DevsimName, test.IP4)

ctx, cancel := context.WithTimeout(context.Background(), TestTimeout)
ctx, cancel := context.WithTimeout(context.Background(), test.TestTimeout)
defer cancel()

c, err := testClient.NewTestSecureClient()
Expand Down
10 changes: 5 additions & 5 deletions client/getResource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestClientGetResource(t *testing.T) {
errC := c.Close(context.Background())
require.NoError(t, errC)
}()
ctx, cancel := context.WithTimeout(context.Background(), TestTimeout)
ctx, cancel := context.WithTimeout(context.Background(), test.TestTimeout)
defer cancel()
deviceID, err = c.OwnDevice(ctx, deviceID)
require.NoError(t, err)
Expand Down Expand Up @@ -138,7 +138,7 @@ func TestClientGetDiscoveryResourceWithResourceTypeFilter(t *testing.T) {
errC := c.Close(context.Background())
require.NoError(t, errC)
}()
ctx, cancel := context.WithTimeout(context.Background(), TestTimeout*8)
ctx, cancel := context.WithTimeout(context.Background(), test.TestTimeout*8)
defer cancel()
deviceID, err = c.OwnDevice(ctx, deviceID)
require.NoError(t, err)
Expand Down Expand Up @@ -176,7 +176,7 @@ func TestClientGetDiscoveryResourceWithBatchInterface(t *testing.T) {
errC := c.Close(context.Background())
require.NoError(t, errC)
}()
ctx, cancel := context.WithTimeout(context.Background(), TestTimeout)
ctx, cancel := context.WithTimeout(context.Background(), test.TestTimeout)
defer cancel()
deviceID, err = c.OwnDevice(ctx, deviceID)
require.NoError(t, err)
Expand Down Expand Up @@ -254,7 +254,7 @@ func TestClientGetDiscoveryResourceWithBatchInterfaceCreateAndDeleteResource(t *
errC := c.Close(context.Background())
require.NoError(t, errC)
}()
ctx, cancel := context.WithTimeout(context.Background(), TestTimeout)
ctx, cancel := context.WithTimeout(context.Background(), test.TestTimeout)
defer cancel()
deviceID, err = c.OwnDevice(ctx, deviceID)
require.NoError(t, err)
Expand Down Expand Up @@ -305,7 +305,7 @@ func TestClientGetConResourceByETag(t *testing.T) {
errC := c.Close(context.Background())
require.NoError(t, errC)
}()
ctx, cancel := context.WithTimeout(context.Background(), TestTimeout)
ctx, cancel := context.WithTimeout(context.Background(), test.TestTimeout)
defer cancel()
deviceID, err = c.OwnDevice(ctx, deviceID)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion client/maitenance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestClientFactoryReset(t *testing.T) {
errC := c.Close(context.Background())
require.NoError(t, errC)
}()
ctx, cancel := context.WithTimeout(context.Background(), TestTimeout)
ctx, cancel := context.WithTimeout(context.Background(), test.TestTimeout)
defer cancel()
_, err = c.OwnDevice(ctx, deviceID)
require.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions client/observeDevices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestObserveDevicesAddedByIP(t *testing.T) {
require.NoError(t, errC)
}()

ctx, cancel := context.WithTimeout(context.Background(), TestTimeout*2)
ctx, cancel := context.WithTimeout(context.Background(), test.TestTimeout*2)
defer cancel()

h := makeTestDevicesObservationHandler()
Expand Down Expand Up @@ -110,7 +110,7 @@ func TestObserveDevices(t *testing.T) {
require.NoError(t, errC)
}()

ctx, cancel := context.WithTimeout(context.Background(), TestTimeout*2)
ctx, cancel := context.WithTimeout(context.Background(), test.TestTimeout*2)
defer cancel()

h := makeTestDevicesObservationHandler()
Expand Down
2 changes: 1 addition & 1 deletion client/observeResource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func testDevice(t *testing.T, name string, runTest func(ctx context.Context, t *
require.NoError(t, errC)
}()

ctx, cancel := context.WithTimeout(context.Background(), TestTimeout)
ctx, cancel := context.WithTimeout(context.Background(), test.TestTimeout)
defer cancel()

deviceID, err = c.OwnDevice(ctx, deviceID)
Expand Down
2 changes: 1 addition & 1 deletion client/offboardDevice.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ func (c *Client) OffboardDevice(ctx context.Context, deviceID string, opts ...Co
return err
}

return setCloudResource(ctx, links, d, "", "", "", "")
return setCloudResource(ctx, links, d, "", "", "", "", cfg.opts...)
}
9 changes: 5 additions & 4 deletions client/onboardDevice.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ import (
"fmt"

"github.com/plgd-dev/device/v2/client/core"
"github.com/plgd-dev/device/v2/pkg/net/coap"
"github.com/plgd-dev/device/v2/schema"
"github.com/plgd-dev/device/v2/schema/acl"
"github.com/plgd-dev/device/v2/schema/cloud"
"github.com/plgd-dev/device/v2/schema/maintenance"
"github.com/plgd-dev/device/v2/schema/softwareupdate"
)

func setCloudResource(ctx context.Context, links schema.ResourceLinks, d *core.Device, authorizationProvider, authorizationCode, cloudURL, cloudID string) error {
func setCloudResource(ctx context.Context, links schema.ResourceLinks, d *core.Device, authorizationProvider, authorizationCode, cloudURL, cloudID string, options ...coap.OptionFunc) error {
ob := cloud.ConfigurationUpdateRequest{
AuthorizationProvider: authorizationProvider,
AuthorizationCode: authorizationCode,
Expand All @@ -37,7 +38,7 @@ func setCloudResource(ctx context.Context, links schema.ResourceLinks, d *core.D
}

for _, l := range links.GetResourceLinks(cloud.ResourceType) {
return d.UpdateResource(ctx, l, ob, nil)
return d.UpdateResource(ctx, l, ob, nil, options...)
}

return fmt.Errorf("cloud resource not found")
Expand Down Expand Up @@ -88,7 +89,7 @@ func (c *Client) OnboardDevice(
opts ...CommonCommandOption,
) error {
cfg := applyCommonOptions(opts...)
d, links, err := c.GetDevice(ctx, deviceID, WithDiscoveryConfiguration(cfg.discoveryConfiguration))
d, links, err := c.GetDevice(ctx, deviceID, cfg)
if err != nil {
return err
}
Expand Down Expand Up @@ -117,5 +118,5 @@ func (c *Client) OnboardDevice(
CloudID: cloudID,
})
}
return setCloudResource(ctx, links, d, authorizationProvider, authCode, cloudURL, cloudID)
return setCloudResource(ctx, links, d, authorizationProvider, authCode, cloudURL, cloudID, cfg.opts...)
}
2 changes: 1 addition & 1 deletion client/onboardDevice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestClientOnboardDevice(t *testing.T) {
errC := c.Close(context.Background())
require.NoError(t, errC)
}()
ctx, cancel := context.WithTimeout(context.Background(), TestTimeout)
ctx, cancel := context.WithTimeout(context.Background(), test.TestTimeout)
defer cancel()
deviceID, err = c.OwnDevice(ctx, deviceID)
require.NoError(t, err)
Expand Down
13 changes: 13 additions & 0 deletions client/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@ func (r ResourceQueryOption) applyOnUpdate(opts updateOptions) updateOptions {
return opts
}

func (r ResourceQueryOption) applyOnCommonCommand(opts commonCommandOptions) commonCommandOptions {
if r.resourceQuery != "" {
opts.opts = append(opts.opts, coap.WithQuery(r.resourceQuery))
}
return opts
}

type DiscoveryConfigurationOption struct {
cfg core.DiscoveryConfiguration
}
Expand Down Expand Up @@ -457,6 +464,7 @@ func (r otmOption) applyOnOwn(opts ownOptions) ownOptions {
}

type commonCommandOptions struct {
opts []coap.OptionFunc
discoveryConfiguration core.DiscoveryConfiguration
}

Expand All @@ -474,3 +482,8 @@ func applyCommonOptions(opts ...CommonCommandOption) commonCommandOptions {
}
return cfg
}

func (r commonCommandOptions) applyOnGetDevice(opts getDeviceOptions) getDeviceOptions {
opts.discoveryConfiguration = r.discoveryConfiguration
return opts
}
4 changes: 2 additions & 2 deletions client/updateResource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func TestClientUpdateResource(t *testing.T) {
wantErr: true,
},
}
ctx, cancel := context.WithTimeout(context.Background(), TestTimeout)
ctx, cancel := context.WithTimeout(context.Background(), test.TestTimeout)
defer cancel()

c, err := testClient.NewTestSecureClient()
Expand Down Expand Up @@ -228,7 +228,7 @@ func TestClientUpdateResourceInRFOTM(t *testing.T) {
wantErr: true,
},
}
ctx, cancel := context.WithTimeout(context.Background(), TestTimeout)
ctx, cancel := context.WithTimeout(context.Background(), test.TestTimeout)
defer cancel()

c, err := testClient.NewTestSecureClient()
Expand Down
4 changes: 2 additions & 2 deletions cmd/ocfbridge/config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apis:
coap:
id: 8f596b43-29c0-4147-8b40-e99268ab30f7
name: "my-bridge-example"
name: "bridge-example"
externalAddress: "127.0.0.1:15683"
maxMessageSize: 262144
numGeneratedBridgedDevices: 3
numGeneratedBridgedDevices: 1
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
go.uber.org/atomic v1.11.0
golang.org/x/sync v0.6.0
google.golang.org/grpc v1.60.1
gopkg.in/yaml.v3 v3.0.1
)

require (
Expand All @@ -37,7 +38,6 @@ require (
golang.org/x/sys v0.16.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240108191215-35c7eff3a6b1 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

// note: github.com/pion/dtls/v2/pkg/net package is not yet available in release branches
Expand Down
2 changes: 2 additions & 0 deletions test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import (
"github.com/stretchr/testify/require"
)

const TestTimeout = time.Second * 8

func MustGetHostname() string {
n, err := os.Hostname()
if err != nil {
Expand Down

0 comments on commit a83e282

Please sign in to comment.