public async Task TestEnvVars()
        {
            const string Image                = "hello-world:latest";
            const string Name                 = "test-env";
            string       sharedAccessKey      = Convert.ToBase64String(Encoding.UTF8.GetBytes("deviceKey"));
            string       fakeConnectionString = $"Hostname=fakeiothub;Deviceid=test;SharedAccessKey={sharedAccessKey}";

            try
            {
                using (var cts = new CancellationTokenSource(Timeout))
                {
                    await Client.CleanupContainerAsync(Name, Image);

                    string createOptions = @"{""Env"": [ ""k1=v1"", ""k2=v2""]}";
                    var    config        = new DockerConfig(Image, createOptions);
                    var    loggingConfig = new DockerLoggingConfig("json-file");
                    var    module        = new DockerModule(Name, "1.0", ModuleStatus.Running, global::Microsoft.Azure.Devices.Edge.Agent.Core.RestartPolicy.OnUnhealthy, config, null, null);

                    IConfigurationRoot configRoot = new ConfigurationBuilder().AddInMemoryCollection(
                        new Dictionary <string, string>
                    {
                        { "EdgeDeviceConnectionString", fakeConnectionString }
                    }).Build();

                    var deploymentConfigModules = new Dictionary <string, IModule> {
                        [Name] = module
                    };
                    var systemModules        = new SystemModules(null, null);
                    var deploymentConfigInfo = new DeploymentConfigInfo(1, new DeploymentConfig("1.0", new DockerRuntimeInfo("docker", new DockerRuntimeConfig("1.25", string.Empty)), systemModules, deploymentConfigModules));
                    var configSource         = new Mock <IConfigSource>();
                    configSource.Setup(cs => cs.Configuration).Returns(configRoot);
                    configSource.Setup(cs => cs.GetDeploymentConfigInfoAsync()).ReturnsAsync(deploymentConfigInfo);

                    var credential = new ConnectionStringCredentials("fake");
                    var identity   = new Mock <IModuleIdentity>();
                    identity.Setup(id => id.Credentials).Returns(credential);

                    ICommand create = await CreateCommand.BuildAsync(Client, module, identity.Object, loggingConfig, configSource.Object, false);

                    await Client.PullImageAsync(Image, cts.Token);

                    // create module using command
                    await create.ExecuteAsync(cts.Token);

                    // check that the environment variables are being returned
                    RuntimeInfoProvider runtimeInfoProvider = await RuntimeInfoProvider.CreateAsync(Client);

                    IEnumerable <ModuleRuntimeInfo> modules = await runtimeInfoProvider.GetModules(cts.Token);

                    var returnedModule = modules.First(m => m.Name == Name) as ModuleRuntimeInfo <DockerReportedConfig>;
                    Assert.NotNull(returnedModule);
                }
            }
            finally
            {
                await Client.CleanupContainerAsync(Name, Image);

                await Client.CleanupContainerAsync("test-filters-external", Image);
            }
        }
示例#2
0
        public async Task GetModulesTest()
        {
            // Arrange
            string module1Hash = Guid.NewGuid().ToString();
            var    module1     = new ModuleRuntimeInfo <TestConfig>(
                "module1",
                "docker",
                ModuleStatus.Running,
                "running",
                0,
                Option.Some(new DateTime(2010, 01, 02, 03, 04, 05)),
                Option.None <DateTime>(),
                new TestConfig(module1Hash));

            string module2Hash = Guid.NewGuid().ToString();
            var    module2     = new ModuleRuntimeInfo <TestConfig>(
                "module2",
                "docker",
                ModuleStatus.Stopped,
                "stopped",
                5,
                Option.Some(new DateTime(2011, 02, 03, 04, 05, 06)),
                Option.Some(new DateTime(2011, 02, 03, 05, 06, 07)),
                new TestConfig(module2Hash));

            var modules = new List <ModuleRuntimeInfo> {
                module1, module2
            };
            var moduleManager = Mock.Of <IModuleManager>(m => m.GetModules <TestConfig>(It.IsAny <CancellationToken>()) == Task.FromResult(modules.AsEnumerable()));
            IRuntimeInfoProvider runtimeInfoProvider = new RuntimeInfoProvider <TestConfig>(moduleManager);

            // Act
            List <ModuleRuntimeInfo> runtimeInfos = (await runtimeInfoProvider.GetModules(CancellationToken.None)).ToList();

            // Assert
            Assert.NotNull(runtimeInfos);
            Assert.Equal(2, runtimeInfos.Count);

            ModuleRuntimeInfo runtimeInfo1 = runtimeInfos[0];

            Assert.Equal("module1", runtimeInfo1.Name);
            Assert.Equal(new DateTime(2010, 01, 02, 03, 04, 05), runtimeInfo1.StartTime.OrDefault());
            Assert.Equal(ModuleStatus.Running, runtimeInfo1.ModuleStatus);
            Assert.Equal("running", runtimeInfo1.Description);
            Assert.Equal(0, runtimeInfo1.ExitCode);
            Assert.False(runtimeInfo1.ExitTime.HasValue);
            Assert.Equal((runtimeInfo1 as ModuleRuntimeInfo <TestConfig>)?.Config.ImageHash, module1Hash);

            ModuleRuntimeInfo runtimeInfo2 = runtimeInfos[1];

            Assert.Equal("module2", runtimeInfo2.Name);
            Assert.Equal(new DateTime(2011, 02, 03, 04, 05, 06), runtimeInfo2.StartTime.OrDefault());
            Assert.Equal(ModuleStatus.Stopped, runtimeInfo2.ModuleStatus);
            Assert.Equal("stopped", runtimeInfo2.Description);
            Assert.Equal(5, runtimeInfo2.ExitCode);
            Assert.Equal(new DateTime(2011, 02, 03, 05, 06, 07), runtimeInfo2.ExitTime.OrDefault());
            Assert.Equal((runtimeInfo2 as ModuleRuntimeInfo <TestConfig>)?.Config.ImageHash, module2Hash);
        }
        public async Task TestEmptyEnvironment()
        {
            using (var cts = new CancellationTokenSource(Timeout))
            {
                RuntimeInfoProvider runtimeInfoProvider = await RuntimeInfoProvider.CreateAsync(Client);

                IEnumerable <ModuleRuntimeInfo> modules = await runtimeInfoProvider.GetModules(cts.Token);

                Assert.Empty(modules);
            }
        }
示例#4
0
        public async Task TestFilters()
        {
            const string Image                = "hello-world:latest";
            const string Name                 = "test-filters";
            string       sharedAccessKey      = Convert.ToBase64String(Encoding.UTF8.GetBytes("test"));
            string       fakeConnectionString = $"Hostname=fakeiothub;Deviceid=test;SharedAccessKey={sharedAccessKey}";

            try
            {
                using (var cts = new CancellationTokenSource(Timeout))
                {
                    await Client.CleanupContainerAsync(Name, Image);

                    await Client.CleanupContainerAsync("test-filters-external", Image);

                    var loggingConfig = new DockerLoggingConfig("json-file");
                    var config        = new DockerConfig(Image);
                    var module        = new DockerModule(Name, "1.0", ModuleStatus.Running, global::Microsoft.Azure.Devices.Edge.Agent.Core.RestartPolicy.OnUnhealthy, config, ImagePullPolicy.OnCreate, Constants.DefaultStartupOrder, null, null);

                    IConfigurationRoot configRoot = new ConfigurationBuilder().AddInMemoryCollection(
                        new Dictionary <string, string>
                    {
                        { "EdgeDeviceConnectionString", fakeConnectionString }
                    }).Build();

                    var deploymentConfigModules = new Dictionary <string, IModule> {
                        [Name] = module
                    };
                    var systemModules        = new SystemModules(null, null);
                    var deploymentConfigInfo = new DeploymentConfigInfo(1, new DeploymentConfig("1.0", new DockerRuntimeInfo("docker", new DockerRuntimeConfig("1.25", string.Empty)), systemModules, deploymentConfigModules, null));
                    var configSource         = new Mock <IConfigSource>();
                    configSource.Setup(cs => cs.Configuration).Returns(configRoot);
                    configSource.Setup(cs => cs.GetDeploymentConfigInfoAsync()).ReturnsAsync(deploymentConfigInfo);

                    var credential = new ConnectionStringCredentials("fake");
                    var identity   = new Mock <IModuleIdentity>();
                    identity.Setup(id => id.Credentials).Returns(credential);

                    ICommand create = await CreateCommand.BuildAsync(Client, module, identity.Object, loggingConfig, configSource.Object, false);

                    // pull the image for both containers
                    await Client.PullImageAsync(Image, cts.Token);

                    // pull and create module using commands
                    await create.ExecuteAsync(cts.Token);

                    var createParams = new CreateContainerParameters
                    {
                        Name  = "test-filters-external",
                        Image = Image,
                    };
                    await Client.Containers.CreateContainerAsync(createParams);

                    // Check that only containers created via command are listed in the environment
                    RuntimeInfoProvider runtimeInfoProvider = await RuntimeInfoProvider.CreateAsync(Client);

                    IEnumerable <ModuleRuntimeInfo> modules = await runtimeInfoProvider.GetModules(cts.Token);

                    Assert.Single(modules);
                    Assert.Equal(module.Name, modules.First().Name);
                }
            }
            finally
            {
                await Client.CleanupContainerAsync(Name, Image);

                await Client.CleanupContainerAsync("test-filters-external", Image);
            }
        }
示例#5
0
        public async Task GetModulesTest()
        {
            // Arrange
            string module1Hash = Guid.NewGuid().ToString();
            var    module1     = new ModuleDetails
            {
                Id     = Guid.NewGuid().ToString(),
                Name   = "module1",
                Status = new Status
                {
                    StartTime     = new DateTime(2010, 01, 02, 03, 04, 05),
                    RuntimeStatus = new RuntimeStatus {
                        Status = "Running", Description = "running"
                    },
                    ExitStatus = null
                },
                Type   = "docker",
                Config = new Config
                {
                    Env = new ObservableCollection <EnvVar>(new List <EnvVar> {
                        new EnvVar {
                            Key = "k1", Value = "v1"
                        }
                    }),
                    Settings = JObject.FromObject(new TestConfig(module1Hash))
                }
            };

            string module2Hash = Guid.NewGuid().ToString();
            var    module2     = new ModuleDetails
            {
                Id     = Guid.NewGuid().ToString(),
                Name   = "module2",
                Status = new Status
                {
                    StartTime     = new DateTime(2011, 02, 03, 04, 05, 06),
                    RuntimeStatus = new RuntimeStatus {
                        Status = "Stopped", Description = "stopped"
                    },
                    ExitStatus = new ExitStatus {
                        ExitTime = new DateTime(2011, 02, 03, 05, 06, 07), StatusCode = "5"
                    }
                },
                Type   = "docker",
                Config = new Config
                {
                    Env = new ObservableCollection <EnvVar>(new List <EnvVar> {
                        new EnvVar {
                            Key = "k2", Value = "v2"
                        }
                    }),
                    Settings = JObject.FromObject(new TestConfig(module2Hash))
                }
            };

            var modules = new List <ModuleDetails> {
                module1, module2
            };
            var moduleManager = Mock.Of <IModuleManager>(m => m.GetModules(It.IsAny <CancellationToken>()) == Task.FromResult(modules.AsEnumerable()));
            IRuntimeInfoProvider runtimeInfoProvider = new RuntimeInfoProvider <TestConfig>(moduleManager);

            // Act
            List <ModuleRuntimeInfo> runtimeInfos = (await runtimeInfoProvider.GetModules(CancellationToken.None)).ToList();

            // Assert
            Assert.NotNull(runtimeInfos);
            Assert.Equal(2, runtimeInfos.Count);

            ModuleRuntimeInfo runtimeInfo1 = runtimeInfos[0];

            Assert.Equal("module1", runtimeInfo1.Name);
            Assert.Equal(new DateTime(2010, 01, 02, 03, 04, 05), runtimeInfo1.StartTime.OrDefault());
            Assert.Equal(ModuleStatus.Running, runtimeInfo1.ModuleStatus);
            Assert.Equal("running", runtimeInfo1.Description);
            Assert.Equal(0, runtimeInfo1.ExitCode);
            Assert.False(runtimeInfo1.ExitTime.HasValue);
            Assert.Equal((runtimeInfo1 as ModuleRuntimeInfo <TestConfig>)?.Config.ImageHash, module1Hash);

            ModuleRuntimeInfo runtimeInfo2 = runtimeInfos[1];

            Assert.Equal("module2", runtimeInfo2.Name);
            Assert.Equal(new DateTime(2011, 02, 03, 04, 05, 06), runtimeInfo2.StartTime.OrDefault());
            Assert.Equal(ModuleStatus.Stopped, runtimeInfo2.ModuleStatus);
            Assert.Equal("stopped", runtimeInfo2.Description);
            Assert.Equal(5, runtimeInfo2.ExitCode);
            Assert.Equal(new DateTime(2011, 02, 03, 05, 06, 07), runtimeInfo2.ExitTime.OrDefault());
            Assert.Equal((runtimeInfo2 as ModuleRuntimeInfo <TestConfig>)?.Config.ImageHash, module2Hash);
        }