Exemplo n.º 1
0
 public async Task ModulesProcessedInPriorityOrderAsync(TestConfig testConfig)
 {
     await this.AgentExecutionTestAsync(testConfig);
 }
Exemplo n.º 2
0
        protected async Task AgentExecutionTestAsync(TestConfig testConfig)
        {
            string             sharedAccessKey = Convert.ToBase64String(Encoding.UTF8.GetBytes("test"));
            IConfigurationRoot configRoot      = new ConfigurationBuilder().AddInMemoryCollection(
                new Dictionary <string, string>
            {
                { "DeviceConnectionString", $"Hostname=fakeiothub;Deviceid=test;SharedAccessKey={sharedAccessKey}" }
            }).Build();

            var deploymentConfigInfo = new DeploymentConfigInfo(1, testConfig.DeploymentConfig);

            var configSource = new Mock <IConfigSource>();

            configSource.Setup(cs => cs.Configuration).Returns(configRoot);
            configSource.Setup(cs => cs.GetDeploymentConfigInfoAsync()).ReturnsAsync(deploymentConfigInfo);
            NullReporter reporter = NullReporter.Instance;

            var restartStateStore                = Mock.Of <IEntityStore <string, ModuleState> >();
            var configStore                      = Mock.Of <IEntityStore <string, string> >();
            var deploymentConfigInfoSerde        = Mock.Of <ISerde <DeploymentConfigInfo> >();
            IRestartPolicyManager restartManager = new Mock <IRestartPolicyManager>().Object;

            var environment = new Mock <IEnvironment>();

            environment.Setup(e => e.GetModulesAsync(It.IsAny <CancellationToken>())).ReturnsAsync(testConfig.RuntimeInfo);
            environment.Setup(e => e.GetRuntimeInfoAsync()).ReturnsAsync(UnknownRuntimeInfo.Instance);

            var environmentProvider = new Mock <IEnvironmentProvider>();

            environmentProvider.Setup(ep => ep.Create(It.IsAny <DeploymentConfig>())).Returns(environment.Object);

            var commandFactory = new TestCommandFactory();

            var credential = new ConnectionStringCredentials("fake");
            IDictionary <string, IModuleIdentity> identities = new Dictionary <string, IModuleIdentity>();

            var identity = new Mock <IModuleIdentity>();

            identity.Setup(id => id.Credentials).Returns(credential);
            identity.Setup(id => id.ModuleId).Returns(Constants.EdgeAgentModuleName);
            identities.Add(Constants.EdgeAgentModuleName, identity.Object);

            if (testConfig.DeploymentConfig.SystemModules.EdgeHub.HasValue)
            {
                identity = new Mock <IModuleIdentity>();
                identity.Setup(id => id.Credentials).Returns(credential);
                identity.Setup(id => id.ModuleId).Returns(Constants.EdgeHubModuleName);
                identities.Add(Constants.EdgeHubModuleName, identity.Object);
            }

            foreach (var module in testConfig.DeploymentConfig.Modules)
            {
                identity = new Mock <IModuleIdentity>();
                identity.Setup(id => id.Credentials).Returns(credential);
                identity.Setup(id => id.ModuleId).Returns(module.Key);
                identities.Add(module.Key, identity.Object);
            }

            foreach (var module in testConfig.RuntimeInfo.Modules)
            {
                if (identities.ContainsKey(module.Key))
                {
                    continue;
                }

                identity = new Mock <IModuleIdentity>();
                identity.Setup(id => id.Credentials).Returns(credential);
                identity.Setup(id => id.ModuleId).Returns(module.Key);
                identities.Add(module.Key, identity.Object);
            }

            IImmutableDictionary <string, IModuleIdentity> immutableIdentities = identities.ToImmutableDictionary();
            var moduleIdentityLifecycleManager = new Mock <IModuleIdentityLifecycleManager>();

            moduleIdentityLifecycleManager.Setup(m => m.GetModuleIdentitiesAsync(It.IsAny <ModuleSet>(), It.IsAny <ModuleSet>())).Returns(Task.FromResult(immutableIdentities));
            var availabilityMetric = Mock.Of <IDeploymentMetrics>();

            var store = Mock.Of <IEntityStore <string, ModuleState> >();
            HealthRestartPlanner restartPlanner = new HealthRestartPlanner(commandFactory, store, TimeSpan.FromSeconds(10), restartManager);

            Agent agent = await Agent.Create(
                configSource.Object,
                restartPlanner,
                new OrderedPlanRunner(),
                reporter,
                moduleIdentityLifecycleManager.Object,
                environmentProvider.Object,
                configStore,
                deploymentConfigInfoSerde,
                NullEncryptionProvider.Instance,
                availabilityMetric);

            await agent.ReconcileAsync(CancellationToken.None);

            Assert.True(testConfig.Validator.Validate(commandFactory));
        }