public async Task HandleShutdownTest() { // Arrange var mockConfigSource = new Mock <IConfigSource>(); IModule mod1 = new TestModule("mod1", "1.0", "docker", ModuleStatus.Running, new TestConfig("boo"), RestartPolicy.OnUnhealthy, ImagePullPolicy.OnCreate, new ConfigurationInfo("1"), null); IModule mod2 = new TestModule("mod2", "1.0", "docker", ModuleStatus.Running, new TestConfig("boo"), RestartPolicy.OnUnhealthy, ImagePullPolicy.OnCreate, new ConfigurationInfo("1"), null); var modules = new Dictionary <string, IModule> { [mod1.Name] = mod1, [mod2.Name] = mod2 }; var mockEnvironment = new Mock <IEnvironment>(); mockEnvironment.Setup(m => m.GetModulesAsync(It.IsAny <CancellationToken>())) .ReturnsAsync(new ModuleSet(modules)); var mockPlanner = new Mock <IPlanner>(); mockPlanner.Setup(p => p.CreateShutdownPlanAsync(It.IsAny <ModuleSet>())) .ReturnsAsync(new Plan(new ICommand[0])); var mockPlanRunner = new Mock <IPlanRunner>(); mockPlanRunner.Setup(m => m.ExecuteAsync(It.IsAny <long>(), It.IsAny <Plan>(), It.IsAny <CancellationToken>())) .Returns( async() => { await Task.Delay(TimeSpan.FromSeconds(5)); return(true); }); var mockReporter = new Mock <IReporter>(); mockReporter.Setup( m => m.ReportAsync( It.IsAny <CancellationToken>(), It.IsAny <ModuleSet>(), It.IsAny <IRuntimeInfo>(), It.IsAny <long>(), It.IsAny <DeploymentStatus>())) .Returns(Task.Delay(TimeSpan.FromSeconds(5))); var mockModuleIdentityLifecycleManager = new Mock <IModuleIdentityLifecycleManager>(); var configStore = Mock.Of <IEntityStore <string, string> >(); var mockEnvironmentProvider = Mock.Of <IEnvironmentProvider>(m => m.Create(It.IsAny <DeploymentConfig>()) == mockEnvironment.Object); var serde = Mock.Of <ISerde <DeploymentConfigInfo> >(); var encryptionDecryptionProvider = Mock.Of <IEncryptionProvider>(); var availabilityMetric = Mock.Of <IAvailabilityMetric>(); var deploymentConfig = new DeploymentConfig("1.0", Mock.Of <IRuntimeInfo>(), new SystemModules(null, null), modules); var deploymentConfigInfo = new DeploymentConfigInfo(0, deploymentConfig); var token = new CancellationToken(); mockConfigSource.Setup(cs => cs.GetDeploymentConfigInfoAsync()) .ReturnsAsync(deploymentConfigInfo); // Act var agent = new Agent(mockConfigSource.Object, mockEnvironmentProvider, mockPlanner.Object, mockPlanRunner.Object, mockReporter.Object, mockModuleIdentityLifecycleManager.Object, configStore, DeploymentConfigInfo.Empty, serde, encryptionDecryptionProvider, availabilityMetric); var shutdownTask = agent.HandleShutdown(token); var waitTask = Task.Delay(TimeSpan.FromSeconds(6)); var completedTask = await Task.WhenAny(shutdownTask, waitTask); // Assert Assert.Equal(completedTask, shutdownTask); mockReporter.Verify(r => r.ReportShutdown(It.IsAny <DeploymentStatus>(), token), Times.Once); mockPlanRunner.Verify(r => r.ExecuteAsync(It.IsAny <long>(), It.IsAny <Plan>(), It.IsAny <CancellationToken>()), Times.Once); mockPlanner.Verify(r => r.CreateShutdownPlanAsync(It.IsAny <ModuleSet>()), Times.Once); }
public async void ReconcileAsyncOnSetPlan() { var desiredModule = new TestModule("desired", "v1", "test", ModuleStatus.Running, new TestConfig("image"), RestartPolicy.OnUnhealthy, ImagePullPolicy.OnCreate, new ConfigurationInfo("1"), null); var currentModule = new TestModule("current", "v1", "test", ModuleStatus.Running, new TestConfig("image"), RestartPolicy.OnUnhealthy, ImagePullPolicy.OnCreate, new ConfigurationInfo("1"), null); var recordKeeper = Option.Some(new TestPlanRecorder()); var moduleExecutionList = new List <TestRecordType> { new TestRecordType(TestCommandType.TestCreate, desiredModule), new TestRecordType(TestCommandType.TestRemove, currentModule) }; var commandList = new List <ICommand> { new TestCommand(TestCommandType.TestCreate, desiredModule, recordKeeper), new TestCommand(TestCommandType.TestRemove, currentModule, recordKeeper) }; var testPlan = new Plan(commandList); var token = new CancellationToken(); var runtimeInfo = Mock.Of <IRuntimeInfo>(); var deploymentConfig = new DeploymentConfig("1.0", runtimeInfo, new SystemModules(null, null), new Dictionary <string, IModule> { ["desired"] = desiredModule }); var deploymentConfigInfo = new DeploymentConfigInfo(0, deploymentConfig); ModuleSet desiredSet = deploymentConfig.GetModuleSet(); ModuleSet currentSet = ModuleSet.Create(currentModule); var mockConfigSource = new Mock <IConfigSource>(); var mockEnvironment = new Mock <IEnvironment>(); var mockPlanner = new Mock <IPlanner>(); var planRunner = new OrderedPlanRunner(); var mockReporter = new Mock <IReporter>(); var mockModuleIdentityLifecycleManager = new Mock <IModuleIdentityLifecycleManager>(); var configStore = Mock.Of <IEntityStore <string, string> >(); var mockEnvironmentProvider = Mock.Of <IEnvironmentProvider>(m => m.Create(It.IsAny <DeploymentConfig>()) == mockEnvironment.Object); var serde = Mock.Of <ISerde <DeploymentConfigInfo> >(); var encryptionDecryptionProvider = Mock.Of <IEncryptionProvider>(); var availabilityMetric = Mock.Of <IAvailabilityMetric>(); mockConfigSource.Setup(cs => cs.GetDeploymentConfigInfoAsync()) .ReturnsAsync(deploymentConfigInfo); mockEnvironment.Setup(env => env.GetModulesAsync(token)) .ReturnsAsync(currentSet); mockModuleIdentityLifecycleManager.Setup(m => m.GetModuleIdentitiesAsync(desiredSet, currentSet)) .ReturnsAsync(ImmutableDictionary <string, IModuleIdentity> .Empty); mockPlanner.Setup(pl => pl.PlanAsync(It.IsAny <ModuleSet>(), currentSet, runtimeInfo, ImmutableDictionary <string, IModuleIdentity> .Empty)) .Returns(Task.FromResult(testPlan)); mockModuleIdentityLifecycleManager.Setup(m => m.GetModuleIdentitiesAsync(It.IsAny <ModuleSet>(), currentSet)) .Returns(Task.FromResult((IImmutableDictionary <string, IModuleIdentity>)ImmutableDictionary <string, IModuleIdentity> .Empty)); mockReporter.Setup(r => r.ReportAsync(token, It.IsAny <ModuleSet>(), It.IsAny <IRuntimeInfo>(), It.IsAny <long>(), DeploymentStatus.Success)) .Returns(Task.CompletedTask); var agent = new Agent(mockConfigSource.Object, mockEnvironmentProvider, mockPlanner.Object, planRunner, mockReporter.Object, mockModuleIdentityLifecycleManager.Object, configStore, DeploymentConfigInfo.Empty, serde, encryptionDecryptionProvider, availabilityMetric); await agent.ReconcileAsync(token); mockEnvironment.Verify(env => env.GetModulesAsync(token), Times.Exactly(1)); mockPlanner.Verify(pl => pl.PlanAsync(It.IsAny <ModuleSet>(), currentSet, runtimeInfo, ImmutableDictionary <string, IModuleIdentity> .Empty), Times.Once); mockReporter.VerifyAll(); recordKeeper.ForEach(r => Assert.Equal(moduleExecutionList, r.ExecutionList)); }