forked from amenzhinsky/iothub
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient_test.go
94 lines (80 loc) · 2.1 KB
/
client_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package iotdevice
import (
"context"
"testing"
"gitlab.com/michaeljohn/iothub/iotdevice/iotdevicetest"
"gitlab.com/michaeljohn/iothub/iotdevice/transport/http"
"gitlab.com/michaeljohn/iothub/iotservice"
)
func newDeviceClient(t *testing.T) *Client {
t.Helper()
sc := iotdevicetest.NewServiceClient(t)
device := iotdevicetest.NewDevice(t, sc)
dcs, err := sc.DeviceConnectionString(device, false)
if err != nil {
t.Fatal(err)
}
dc, err := NewFromConnectionString(http.New(), dcs)
if err != nil {
t.Fatal(err)
}
if err := dc.Connect(context.Background()); err != nil {
t.Fatal(err)
}
return dc
}
// newModule creates a module using the device client
func newModule(t *testing.T, c *Client) *iotservice.Module {
testRunID := iotdevicetest.GenerateRandomID()
module := &iotservice.Module{
DeviceID: c.DeviceID(),
ModuleID: "test-module-" + testRunID,
ManagedBy: "admin",
}
module, err := c.CreateModule(context.Background(), module)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
module.ETag = ""
if err := c.DeleteModule(context.Background(), module); err != nil {
t.Fatal(err)
}
})
return module
}
func TestListModules(t *testing.T) {
c := newDeviceClient(t)
module := newModule(t, c)
modules, err := c.ListModules(context.Background())
if err != nil {
t.Fatal(err)
}
if len(modules) != 1 {
t.Errorf("module count = %d, want 1", len(modules))
}
if modules[0].ModuleID != module.ModuleID {
t.Errorf("moduleID = %s, want %s", modules[0].ModuleID, module.ModuleID)
}
}
func TestGetModule(t *testing.T) {
c := newDeviceClient(t)
module := newModule(t, c)
if _, err := c.GetModule(
context.Background(), module.ModuleID,
); err != nil {
t.Fatal(err)
}
}
func TestUpdateModule(t *testing.T) {
c := newDeviceClient(t)
module := newModule(t, c)
module.Authentication.Type = iotservice.AuthSAS
updatedModule, err := c.UpdateModule(context.Background(), module)
if err != nil {
t.Fatal(err)
}
if updatedModule.Authentication.Type != iotservice.AuthSAS {
t.Errorf("authentication type = `%s`, want `%s`", updatedModule.Authentication.Type, iotservice.AuthSAS)
}
}