public async Task ProcessRemove()
        {
            bool calledActual = false;
            string nameActual = null;

            MockReliableStateManager stateManager = new MockReliableStateManager();
            MockClusterOperator clusterOperator = new MockClusterOperator()
            {
                DeleteClusterAsyncFunc = name =>
                {
                    nameActual = name;
                    calledActual = true;
                    return Task.FromResult(true);
                }
            };

            ClusterConfig config = new ClusterConfig();
            ClusterService target = new ClusterService(
                clusterOperator,
                null,
                new MockApplicationDeployService(),
                stateManager,
                this.CreateServiceContext(),
                config);

            Cluster cluster = this.CreateCluster(ClusterStatus.Remove);

            Cluster actual = await target.ProcessClusterStatusAsync(cluster);

            Assert.IsTrue(calledActual);
            Assert.AreEqual(ClusterStatus.Deleting, actual.Status);
        }
        public async Task ProcessDeletingSuccessful()
        {
            MockReliableStateManager stateManager = new MockReliableStateManager();
            MockClusterOperator clusterOperator = new MockClusterOperator()
            {
                GetClusterStatusAsyncFunc = domain => Task.FromResult(ClusterOperationStatus.ClusterNotFound)
            };

            ClusterConfig config = new ClusterConfig();
            ClusterService target = new ClusterService(
                clusterOperator,
                null,
                new MockApplicationDeployService(),
                stateManager,
                this.CreateServiceContext(),
                config);

            Cluster cluster = this.CreateCluster(ClusterStatus.Deleting);

            Cluster actual = await target.ProcessClusterStatusAsync(cluster);

            Assert.AreEqual(ClusterStatus.Deleted, actual.Status);
        }
        public async Task ProcessCreatingClusterFailed()
        {
            MockReliableStateManager stateManager = new MockReliableStateManager();
            MockClusterOperator clusterOperator = new MockClusterOperator()
            {
                GetClusterStatusAsyncFunc = name => Task.FromResult(ClusterOperationStatus.CreateFailed)
            };

            ClusterConfig config = new ClusterConfig();
            ClusterService target = new ClusterService(
                clusterOperator,
                null,
                new MockApplicationDeployService(),
                stateManager,
                this.CreateServiceContext(),
                config);
            Cluster cluster = this.CreateCluster(ClusterStatus.Creating);

            Cluster actual = await target.ProcessClusterStatusAsync(cluster);

            Assert.AreEqual(ClusterStatus.Remove, actual.Status);
            Assert.AreEqual(0, actual.Ports.Count());
            Assert.AreEqual(0, actual.Users.Count());
        }
        public async Task ProcessCreatingClusterSuccess()
        {
            MockReliableStateManager stateManager = new MockReliableStateManager();
            MockClusterOperator clusterOperator = new MockClusterOperator()
            {
                GetClusterStatusAsyncFunc = name => Task.FromResult(ClusterOperationStatus.Ready)
            };

            ClusterConfig config = new ClusterConfig();
            ClusterService target = new ClusterService(
                clusterOperator,
                null,
                new MockApplicationDeployService(),
                stateManager,
                this.CreateServiceContext(),
                config);
            Cluster cluster = this.CreateCluster(ClusterStatus.Creating);

            Cluster actual = await target.ProcessClusterStatusAsync(cluster);

            Assert.AreEqual(ClusterStatus.Ready, actual.Status);
            Assert.IsTrue(actual.CreatedOn.ToUniversalTime() <= DateTimeOffset.UtcNow);
        }
        public async Task ProcessNewCluster()
        {
            bool calledActual = false;
            string nameTemplate = "Test:{0}";
            string nameActual = null;
            IEnumerable<int> portsExpected = Enumerable.Empty<int>();

            MockReliableStateManager stateManager = new MockReliableStateManager();
            MockClusterOperator clusterOperator = new MockClusterOperator()
            {
                CreateClusterAsyncFunc = (name, ports) =>
                {
                    nameActual = name;
                    portsExpected = ports;
                    calledActual = true;
                    return Task.FromResult(String.Format(nameTemplate, name));
                }
            };

            ClusterConfig config = new ClusterConfig();
            ClusterService target = new ClusterService(
                clusterOperator,
                null,
                new MockApplicationDeployService(),
                stateManager,
                this.CreateServiceContext(),
                config);

            Cluster cluster = this.CreateCluster(ClusterStatus.New);

            Cluster actual = await target.ProcessClusterStatusAsync(cluster);

            Assert.IsTrue(calledActual);
            Assert.AreEqual(ClusterStatus.Creating, actual.Status);
            Assert.AreEqual(String.Format(nameTemplate, nameActual), actual.Address);
            Assert.AreEqual(5, actual.Ports.Count());
            Enumerable.SequenceEqual(portsExpected, actual.Ports);
        }
        public async Task ProcessClustersAsyncDelete()
        {
            int key = 1;
            MockReliableStateManager stateManager = new MockReliableStateManager();
            ClusterConfig config = new ClusterConfig();
            MockClusterOperator clusterOperator = new MockClusterOperator()
            {
                GetClusterStatusAsyncFunc = (name) => Task.FromResult(ClusterOperationStatus.ClusterNotFound)
            };

            IReliableDictionary<int, Cluster> dictionary =
                await stateManager.GetOrAddAsync<IReliableDictionary<int, Cluster>>(ClusterService.ClusterDictionaryName);

            Cluster original = this.CreateCluster(ClusterStatus.Deleting);
            using (ITransaction tx = stateManager.CreateTransaction())
            {
                await dictionary.SetAsync(tx, key, original);
            }

            ClusterService target = new ClusterService(
                clusterOperator,
                null,
                new MockApplicationDeployService(),
                stateManager,
                this.CreateServiceContext(),
                config);

            await target.ProcessClustersAsync(CancellationToken.None);

            using (ITransaction tx = stateManager.CreateTransaction())
            {
                ConditionalValue<Cluster> actual = await dictionary.TryGetValueAsync(tx, key);

                Assert.IsFalse(actual.HasValue);
            }
        }
        public async Task ProcessClustersAsyncSaveChanges()
        {
            int key = 1;
            string nameTemplate = "Test:{0}";
            MockReliableStateManager stateManager = new MockReliableStateManager();
            ClusterConfig config = new ClusterConfig();
            MockClusterOperator clusterOperator = new MockClusterOperator()
            {
                CreateClusterAsyncFunc = (name, ports) => { return Task.FromResult(String.Format(nameTemplate, name)); }
            };

            IReliableDictionary<int, Cluster> dictionary =
                await stateManager.GetOrAddAsync<IReliableDictionary<int, Cluster>>(ClusterService.ClusterDictionaryName);

            Cluster original = this.CreateCluster(ClusterStatus.New);
            using (ITransaction tx = stateManager.CreateTransaction())
            {
                await dictionary.SetAsync(tx, key, original);
            }

            ClusterService target = new ClusterService(
                clusterOperator,
                null,
                new MockApplicationDeployService(),
                stateManager,
                this.CreateServiceContext(),
                config);

            await target.ProcessClustersAsync(CancellationToken.None);

            using (ITransaction tx = stateManager.CreateTransaction())
            {
                Cluster actual = (await dictionary.TryGetValueAsync(tx, key)).Value;

                Assert.AreNotEqual(original, actual);
            }
        }