public async Task TestGetModulesIdentity_IIdentityManagerException_ShouldReturnEmptyIdentities()
        {
            // Arrange
            var identityManager = Mock.Of <IIdentityManager>();

            Mock.Get(identityManager).Setup(m => m.GetIdentities()).ThrowsAsync(new InvalidOperationException());
            var moduleIdentityLifecycleManager = new ModuleIdentityLifecycleManager(identityManager, ModuleIdentityProviderServiceBuilder, EdgeletUri);
            var envVar = new Dictionary <string, EnvVal>();

            var       module1 = new TestModule("mod1", "v1", "test", ModuleStatus.Running, new TestConfig("image"), RestartPolicy.OnUnhealthy, DefaultConfigurationInfo, envVar);
            var       module2 = new TestModule("mod2", "v1", "test", ModuleStatus.Running, new TestConfig("image"), RestartPolicy.OnUnhealthy, DefaultConfigurationInfo, envVar);
            var       module3 = new TestModule("mod3", "v1", "test", ModuleStatus.Running, new TestConfig("image"), RestartPolicy.OnUnhealthy, DefaultConfigurationInfo, envVar);
            var       module4 = new TestModule("$edgeHub", "v1", "test", ModuleStatus.Running, new TestConfig("image"), RestartPolicy.OnUnhealthy, DefaultConfigurationInfo, envVar);
            ModuleSet desired = ModuleSet.Create(module1, module4);
            ModuleSet current = ModuleSet.Create(module2, module3, module4);

            // Act
            IImmutableDictionary <string, IModuleIdentity> modulesIdentities = await moduleIdentityLifecycleManager.GetModuleIdentitiesAsync(desired, current);

            // Assert
            Assert.False(modulesIdentities.Any());
            Mock.Get(identityManager).Verify();
        }
Example #2
0
        public async Task TestGetModulesIdentity_WithUpdatedModules_ShouldUpdateIdentities()
        {
            // Arrange
            const string Module1   = "module1";
            var          identity1 = new Identity
            {
                ModuleId     = Module1,
                ManagedBy    = "IotEdge",
                GenerationId = Guid.NewGuid().ToString()
            };

            const string Module2   = "module2";
            var          identity2 = new Identity
            {
                ModuleId     = Module2,
                ManagedBy    = "Me",
                GenerationId = Guid.NewGuid().ToString()
            };

            const string Module3   = "module3";
            var          identity3 = new Identity
            {
                ModuleId     = Module3,
                ManagedBy    = Constants.ModuleIdentityEdgeManagedByValue,
                GenerationId = Guid.NewGuid().ToString()
            };

            const string Module4   = "$edgeHub";
            var          identity4 = new Identity
            {
                ModuleId     = Module4,
                ManagedBy    = Constants.ModuleIdentityEdgeManagedByValue,
                GenerationId = Guid.NewGuid().ToString()
            };

            // We should NOT get an update request for this identity
            const string Module5   = "$edgeAgent";
            var          identity5 = new Identity
            {
                ModuleId     = Module5,
                ManagedBy    = Constants.ModuleIdentityEdgeManagedByValue,
                GenerationId = Guid.NewGuid().ToString()
            };

            var identityManager = Mock.Of <IIdentityManager>(
                m =>
                m.GetIdentities() == Task.FromResult(new List <Identity>()
            {
                identity2, identity3, identity4, identity5
            }.AsEnumerable()) &&
                m.CreateIdentityAsync(Module1, Constants.ModuleIdentityEdgeManagedByValue) == Task.FromResult(identity1) &&
                m.UpdateIdentityAsync(identity2.ModuleId, identity2.GenerationId, identity2.ManagedBy) == Task.FromResult(identity2) &&
                m.UpdateIdentityAsync(identity3.ModuleId, identity3.GenerationId, identity3.ManagedBy) == Task.FromResult(identity3) &&
                m.UpdateIdentityAsync(identity4.ModuleId, identity4.GenerationId, identity4.ManagedBy) == Task.FromResult(identity4));

            var       moduleIdentityLifecycleManager = new ModuleIdentityLifecycleManager(identityManager, ModuleIdentityProviderServiceBuilder, EdgeletUri);
            var       envVar  = new Dictionary <string, EnvVal>();
            var       module1 = new TestModule(Module1, "v1", "test", ModuleStatus.Running, new TestConfig("image"), RestartPolicy.OnUnhealthy, DefaultConfigurationInfo, envVar);
            var       module2 = new TestModule(Module2, "v1", "test", ModuleStatus.Running, new TestConfig("image"), RestartPolicy.OnUnhealthy, DefaultConfigurationInfo, envVar);
            var       module3 = new TestModule(Module3, "v1", "test", ModuleStatus.Running, new TestConfig("image"), RestartPolicy.OnUnhealthy, DefaultConfigurationInfo, envVar);
            var       module4 = new TestModule(Module4, "v1", "test", ModuleStatus.Running, new TestConfig("image"), RestartPolicy.OnUnhealthy, DefaultConfigurationInfo, envVar);
            var       module5 = new TestModule(Module5, "v1", "test", ModuleStatus.Running, new TestConfig("image"), RestartPolicy.OnUnhealthy, DefaultConfigurationInfo, envVar);
            ModuleSet desired = ModuleSet.Create(module1, module2.CloneWithImage("image2"), module3.CloneWithImage("image2"), module4.CloneWithImage("image2"), module5.CloneWithImage("image2"));
            ModuleSet current = ModuleSet.Create(module2, module3, module4, module5);

            // Act
            IImmutableDictionary <string, IModuleIdentity> modulesIdentities = await moduleIdentityLifecycleManager.GetModuleIdentitiesAsync(desired, current);

            // Assert
            Assert.Equal(5, modulesIdentities.Count);
            Assert.True(modulesIdentities.TryGetValue(Module1, out IModuleIdentity moduleIdentity1));
            Assert.Equal(Module1, moduleIdentity1.ModuleId);
            Assert.True(modulesIdentities.TryGetValue(Module2, out IModuleIdentity moduleIdentity2));
            Assert.Equal(Module2, moduleIdentity2.ModuleId);
            Assert.True(modulesIdentities.TryGetValue(Module3, out IModuleIdentity moduleIdentity3));
            Assert.Equal(Module3, moduleIdentity3.ModuleId);
            Assert.True(modulesIdentities.TryGetValue("edgeHub", out IModuleIdentity moduleIdentity4));
            Assert.Equal(Module4, moduleIdentity4.ModuleId);
            Assert.IsType <IdentityProviderServiceCredentials>(moduleIdentity1.Credentials);
            Mock.Get(identityManager).Verify();
        }