Exemplo n.º 1
0
        public Task <int> RunAsync(Args args) => Profiler.Run(
            "Running tempSensor test",
            async() =>
        {
            using (var cts = new CancellationTokenSource(TimeSpan.FromMinutes(5)))
            {
                CancellationToken token = cts.Token;

                // ** setup
                var iotHub = new IotHub(args.ConnectionString, args.Endpoint, args.Proxy);
                var device = await EdgeDevice.GetOrCreateIdentityAsync(
                    args.DeviceId,
                    iotHub,
                    token);

                var daemon = new EdgeDaemon(args.InstallerPath);
                await daemon.UninstallAsync(token);
                await daemon.InstallAsync(
                    device.ConnectionString,
                    args.PackagesPath,
                    args.Proxy,
                    token);

                await args.Proxy.Match(
                    async p =>
                {
                    await daemon.StopAsync(token);
                    var yaml = new DaemonConfiguration();
                    yaml.AddHttpsProxy(p);
                    yaml.Update();
                    await daemon.StartAsync(token);
                },
                    () => daemon.WaitForStatusAsync(EdgeDaemonStatus.Running, token));

                var agent = new EdgeAgent(device.Id, iotHub);
                await agent.WaitForStatusAsync(EdgeModuleStatus.Running, token);
                await agent.PingAsync(token);

                // ** test
                var config = new EdgeConfiguration(device.Id, args.AgentImage, iotHub);
                args.Registry.ForEach(
                    r => config.AddRegistryCredentials(r.address, r.username, r.password));
                config.AddEdgeHub(args.HubImage);
                args.Proxy.ForEach(p => config.AddProxy(p));
                config.AddTempSensor(args.SensorImage);
                await config.DeployAsync(token);

                var hub    = new EdgeModule("edgeHub", device.Id, iotHub);
                var sensor = new EdgeModule("tempSensor", device.Id, iotHub);
                await EdgeModule.WaitForStatusAsync(
                    new[] { hub, sensor },
                    EdgeModuleStatus.Running,
                    token);
                await sensor.WaitForEventsReceivedAsync(token);

                var sensorTwin = new ModuleTwin(sensor.Id, device.Id, iotHub);
                await sensorTwin.UpdateDesiredPropertiesAsync(
                    new
                {
                    properties = new
                    {
                        desired = new
                        {
                            SendData     = true,
                            SendInterval = 10
                        }
                    }
                },
                    token);
                await sensorTwin.WaitForReportedPropertyUpdatesAsync(
                    new
                {
                    properties = new
                    {
                        reported = new
                        {
                            SendData     = true,
                            SendInterval = 10
                        }
                    }
                },
                    token);

                // ** teardown
                await daemon.StopAsync(token);
                await device.MaybeDeleteIdentityAsync(token);
            }

            return(0);
        },
            "Completed tempSensor test");
Exemplo n.º 2
0
        public async Task <int> RunAsync(Args args)
        {
            LogEventLevel consoleLevel = args.Verbose
                ? LogEventLevel.Verbose
                : LogEventLevel.Information;
            var loggerConfig = new LoggerConfiguration()
                               .MinimumLevel.Verbose()
                               .WriteTo.Console(consoleLevel);

            args.LogFile.ForEach(f => loggerConfig.WriteTo.File(f));
            Log.Logger = loggerConfig.CreateLogger();

            try
            {
                using (var cts = new CancellationTokenSource(args.Timeout))
                {
                    Log.Information("Running tempSensor test");
                    await Profiler.Run(
                        async() =>
                    {
                        CancellationToken token = cts.Token;

                        // ** setup
                        var iotHub        = new IotHub(args.ConnectionString, args.Endpoint, args.Proxy);
                        EdgeDevice device = await EdgeDevice.GetOrCreateIdentityAsync(
                            args.DeviceId,
                            iotHub,
                            token);

                        var daemon = Platform.CreateEdgeDaemon(args.InstallerPath);
                        await daemon.UninstallAsync(token);
                        await daemon.InstallAsync(
                            device.ConnectionString,
                            args.PackagesPath,
                            args.Proxy,
                            token);
                        await daemon.WaitForStatusAsync(EdgeDaemonStatus.Running, token);

                        var agent = new EdgeAgent(device.Id, iotHub);
                        await agent.WaitForStatusAsync(EdgeModuleStatus.Running, token);
                        await agent.PingAsync(token);

                        // ** test
                        var config = new EdgeConfiguration(device.Id, args.AgentImage, iotHub);
                        args.Registry.ForEach(
                            r => config.AddRegistryCredentials(r.address, r.username, r.password));
                        config.AddEdgeHub(args.HubImage);
                        args.Proxy.ForEach(p => config.AddProxy(p));
                        config.AddTempSensor(args.SensorImage);
                        await config.DeployAsync(token);

                        var hub    = new EdgeModule("edgeHub", device.Id, iotHub);
                        var sensor = new EdgeModule("tempSensor", device.Id, iotHub);
                        await EdgeModule.WaitForStatusAsync(
                            new[] { hub, sensor },
                            EdgeModuleStatus.Running,
                            token);
                        await sensor.WaitForEventsReceivedAsync(token);

                        var sensorTwin = new ModuleTwin(sensor.Id, device.Id, iotHub);
                        await sensorTwin.UpdateDesiredPropertiesAsync(
                            new
                        {
                            properties = new
                            {
                                desired = new
                                {
                                    SendData     = true,
                                    SendInterval = 10
                                }
                            }
                        },
                            token);
                        await sensorTwin.WaitForReportedPropertyUpdatesAsync(
                            new
                        {
                            properties = new
                            {
                                reported = new
                                {
                                    SendData     = true,
                                    SendInterval = 10
                                }
                            }
                        },
                            token);

                        // ** teardown
                        await daemon.StopAsync(token);
                        await device.MaybeDeleteIdentityAsync(token);
                    },
                        "Completed tempSensor test");
                }
            }
            catch (OperationCanceledException e)
            {
                Log.Error(e, "Cancelled tempSensor test after {Timeout} minutes", args.Timeout.TotalMinutes);
            }
            catch (Exception e)
            {
                Log.Error(e, "Failed tempSensor test");
                return(1);
            }
            finally
            {
                Log.CloseAndFlush();
            }

            return(0);
        }
Exemplo n.º 3
0
        public async Task TempSensor()
        {
            string       agentImage             = Context.Current.EdgeAgentImage.Expect(() => new ArgumentException());
            string       hubImage               = Context.Current.EdgeHubImage.Expect(() => new ArgumentException());
            string       sensorImage            = Context.Current.TempSensorImage.Expect(() => new ArgumentException());
            bool         optimizeForPerformance = Context.Current.OptimizeForPerformance;
            Option <Uri> proxy = Context.Current.Proxy;

            CancellationToken token = this.cts.Token;

            string name = "temp sensor";

            Log.Information("Running test '{Name}'", name);
            await Profiler.Run(
                async() =>
            {
                var iotHub = new IotHub(
                    Context.Current.ConnectionString,
                    Context.Current.EventHubEndpoint,
                    proxy);

                EdgeDevice device = (await EdgeDevice.GetIdentityAsync(
                                         Context.Current.DeviceId,
                                         iotHub,
                                         token)).Expect(() => new Exception("Device should have already been created in setup fixture"));

                var builder = new EdgeConfigBuilder(device.Id);
                foreach ((string address, string username, string password) in Context.Current.Registries)
                {
                    builder.AddRegistryCredentials(address, username, password);
                }

                builder.AddEdgeAgent(agentImage).WithProxy(proxy);
                builder.AddEdgeHub(hubImage, optimizeForPerformance).WithProxy(proxy);
                builder.AddModule("tempSensor", sensorImage);
                await builder.Build().DeployAsync(iotHub, token);

                var hub    = new EdgeModule("edgeHub", device.Id, iotHub);
                var sensor = new EdgeModule("tempSensor", device.Id, iotHub);
                await EdgeModule.WaitForStatusAsync(
                    new[] { hub, sensor },
                    EdgeModuleStatus.Running,
                    token);
                await sensor.WaitForEventsReceivedAsync(token);

                var sensorTwin = new ModuleTwin(sensor.Id, device.Id, iotHub);
                await sensorTwin.UpdateDesiredPropertiesAsync(
                    new
                {
                    properties = new
                    {
                        desired = new
                        {
                            SendData     = true,
                            SendInterval = 10
                        }
                    }
                },
                    token);
                await sensorTwin.WaitForReportedPropertyUpdatesAsync(
                    new
                {
                    properties = new
                    {
                        reported = new
                        {
                            SendData     = true,
                            SendInterval = 10
                        }
                    }
                },
                    token);
            },
                "Completed test '{Name}'",
                name);
        }