-
Notifications
You must be signed in to change notification settings - Fork 472
Expand file tree
/
Copy pathDevice.cs
More file actions
245 lines (219 loc) · 9.4 KB
/
Device.cs
File metadata and controls
245 lines (219 loc) · 9.4 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Copyright (c) Microsoft. All rights reserved.
namespace Microsoft.Azure.Devices.Edge.Test
{
using System;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Devices.Edge.Test.Common;
using Microsoft.Azure.Devices.Edge.Test.Common.Config;
using Microsoft.Azure.Devices.Edge.Test.Helpers;
using Microsoft.Azure.Devices.Edge.Util;
using NUnit.Framework;
using Serilog;
[EndToEnd]
class Device : SasManualProvisioningFixture
{
[Test]
public async Task QuickstartCerts()
{
CancellationToken token = this.TestToken;
await this.runtime.DeployConfigurationAsync(this.cli, token, this.device.NestedEdge.IsNestedEdge);
string leafDeviceId = DeviceId.Current.Generate();
using var leaf = await LeafDevice.CreateAsync(
leafDeviceId,
Protocol.Amqp,
AuthenticationType.Sas,
Option.Some(this.runtime.DeviceId),
false,
this.ca,
this.daemon.GetCertificatesPath(),
this.IotHub,
this.device.NestedEdge.DeviceHostname,
token,
Option.None<string>(),
this.device.NestedEdge.IsNestedEdge);
await TryFinally.DoAsync(
async () =>
{
DateTime seekTime = DateTime.Now;
await leaf.SendEventAsync(token);
await leaf.WaitForEventsReceivedAsync(seekTime, token);
await leaf.InvokeDirectMethodAsync(token);
},
async () =>
{
await leaf.CloseAsync();
await leaf.DeleteIdentityAsync(token);
});
}
[Test]
[Category("NestedEdgeOnly")]
[Category("FlakyOnNested")]
public async Task QuickstartChangeSasKey()
{
CancellationToken token = this.TestToken;
await this.runtime.DeployConfigurationAsync(this.cli, token, this.device.NestedEdge.IsNestedEdge);
string leafDeviceId = DeviceId.Current.Generate();
// Create leaf and send message
using var leaf = await LeafDevice.CreateAsync(
leafDeviceId,
Protocol.Amqp,
AuthenticationType.Sas,
Option.Some(this.runtime.DeviceId),
false,
this.ca,
this.daemon.GetCertificatesPath(),
this.IotHub,
this.device.NestedEdge.DeviceHostname,
token,
Option.None<string>(),
this.device.NestedEdge.IsNestedEdge);
await TryFinally.DoAsync(
async () =>
{
DateTime seekTime = DateTime.Now;
await leaf.SendEventAsync(token);
await leaf.WaitForEventsReceivedAsync(seekTime, token);
await leaf.InvokeDirectMethodAsync(token);
},
async () =>
{
await leaf.CloseAsync();
await leaf.DeleteIdentityAsync(token);
});
// Re-create the leaf with the same device ID, for our purposes this is
// the equivalent of updating the SAS keys
using var leafUpdated = await LeafDevice.CreateAsync(
leafDeviceId,
Protocol.Amqp,
AuthenticationType.Sas,
Option.Some(this.runtime.DeviceId),
false,
this.ca,
this.daemon.GetCertificatesPath(),
this.IotHub,
this.device.NestedEdge.DeviceHostname,
token,
Option.None<string>(),
this.device.NestedEdge.IsNestedEdge);
await TryFinally.DoAsync(
async () =>
{
DateTime seekTime = DateTime.Now;
await leafUpdated.SendEventAsync(token);
await leafUpdated.WaitForEventsReceivedAsync(seekTime, token);
await leafUpdated.InvokeDirectMethodAsync(token);
},
async () =>
{
await leafUpdated.CloseAsync();
await leafUpdated.DeleteIdentityAsync(token);
});
}
[Test]
[Category("NestedEdgeOnly")]
[Category("NestedEdgeAmqpOnly")]
public async Task RouteMessageL3LeafToL4Module()
{
CancellationToken token = this.TestToken;
await this.runtime.DeployConfigurationAsync(this.cli, token, Context.Current.NestedEdge);
// These must match the IDs in nestededge_middleLayerBaseDeployment_amqp.json,
// which defines a route that filters by deviceId to forwards the message
// to the relayer module
string parentDeviceId = Context.Current.ParentDeviceId.Expect(() => new InvalidOperationException("No parent device ID set!"));
string leafDeviceId = "L3LeafToRelayer1";
string relayerModuleId = "relayer1";
// Create leaf and send message
using var leaf = await LeafDevice.CreateAsync(
leafDeviceId,
Protocol.Amqp,
AuthenticationType.Sas,
Option.Some(this.runtime.DeviceId),
false,
this.ca,
this.daemon.GetCertificatesPath(),
this.IotHub,
Context.Current.Hostname.GetOrElse(Dns.GetHostName().ToLower()),
token,
Option.None<string>(),
Context.Current.NestedEdge);
await TryFinally.DoAsync(
async () =>
{
// Send a message from the leaf device
DateTime seekTime = DateTime.Now;
await leaf.SendEventAsync(token);
Log.Verbose($"Sent message from {leafDeviceId}");
// Verify that the message was received/resent by the relayer module on L4
await Profiler.Run(
() => this.IotHub.ReceiveEventsAsync(
parentDeviceId,
seekTime,
data =>
{
data.SystemProperties.TryGetValue("iothub-connection-device-id", out object devId);
data.SystemProperties.TryGetValue("iothub-connection-module-id", out object modId);
data.Properties.TryGetValue("leaf-message-id", out object msgId);
Log.Verbose($"Received event for '{devId + "/" + modId}' with message ID '{msgId}'");
return devId != null && devId.ToString().Equals(parentDeviceId)
&& modId.ToString().Equals(relayerModuleId);
},
token),
"Received events from module '{Device}' on Event Hub '{EventHub}'",
parentDeviceId + "/" + relayerModuleId,
this.IotHub.EntityPath);
},
async () =>
{
await leaf.CloseAsync();
await leaf.DeleteIdentityAsync(token);
});
}
[Test]
[Category("Flaky")]
public async Task DisableReenableParentEdge()
{
CancellationToken token = this.TestToken;
Log.Verbose("Deploying L3 Edge");
await this.runtime.DeployConfigurationAsync(this.cli, token, this.device.NestedEdge.IsNestedEdge);
// Disable the parent Edge device
Log.Verbose("Disabling Edge device");
await this.IotHub.UpdateEdgeEnableStatus(this.runtime.DeviceId, false);
await Task.Delay(TimeSpan.FromSeconds(10));
// Re-enable parent Edge
Log.Verbose("Re-enabling Edge device");
await this.IotHub.UpdateEdgeEnableStatus(this.runtime.DeviceId, true);
await Task.Delay(TimeSpan.FromSeconds(10));
// Try connecting
string leafDeviceId = DeviceId.Current.Generate();
using var leaf = await LeafDevice.CreateAsync(
leafDeviceId,
Protocol.Amqp,
AuthenticationType.Sas,
Option.Some(this.runtime.DeviceId),
false,
this.ca,
this.daemon.GetCertificatesPath(),
this.IotHub,
this.device.NestedEdge.DeviceHostname,
token,
Option.None<string>(),
this.device.NestedEdge.IsNestedEdge);
await TryFinally.DoAsync(
async () =>
{
DateTime seekTime = DateTime.Now;
await leaf.SendEventAsync(token);
await leaf.WaitForEventsReceivedAsync(seekTime, token);
await leaf.InvokeDirectMethodAsync(token);
},
async () =>
{
await leaf.CloseAsync();
await leaf.DeleteIdentityAsync(token);
});
}
}
}