-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support module CRUD operations in device client
This change adds all the CRUD module operations to the HTTP device client. Note that the `Module` model definitions are the same as for the `iotservice` client and they have been reused instead of redefining them. Ideally, this model definitions should be in the common package but that would break backward compatibility.
- Loading branch information
Showing
7 changed files
with
495 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package iotdevice | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"strconv" | ||
"testing" | ||
"time" | ||
|
||
"github.com/amenzhinsky/iothub/iotdevice/transport/http" | ||
"github.com/amenzhinsky/iothub/iotservice" | ||
) | ||
|
||
var testRunID = strconv.Itoa(int(time.Now().Unix())) | ||
|
||
func newServiceClient(t *testing.T) *iotservice.Client { | ||
t.Helper() | ||
cs := os.Getenv("TEST_IOTHUB_SERVICE_CONNECTION_STRING") | ||
if cs == "" { | ||
t.Fatal("$TEST_IOTHUB_SERVICE_CONNECTION_STRING is empty") | ||
} | ||
c, err := iotservice.NewFromConnectionString(cs) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
return c | ||
} | ||
|
||
func newDevice(t *testing.T, c *iotservice.Client) *iotservice.Device { | ||
t.Helper() | ||
|
||
device := &iotservice.Device{ | ||
DeviceID: "test-device-" + testRunID, | ||
} | ||
device, err := c.CreateDevice(context.Background(), device) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
t.Cleanup(func() { | ||
device.ETag = "" | ||
if err := c.DeleteDevice(context.Background(), device); err != nil { | ||
t.Fatal(err) | ||
} | ||
}) | ||
return device | ||
} | ||
|
||
func newDeviceClient(t *testing.T) *Client { | ||
t.Helper() | ||
sc := newServiceClient(t) | ||
device := 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 | ||
} | ||
|
||
func newModule(t *testing.T, c *Client) *iotservice.Module { | ||
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) | ||
} | ||
} |
Oops, something went wrong.