public void TryUpdateEnvironmentParentAsync_should_return_true_and_set_new_parent_for_existent_environment()
        {
            const string oldParent = "root";

            CreateEnvironmentNode("environment", oldParent, GetProperties());

            var serviceDiscoveryManager = new ServiceDiscoveryManager(GetZooKeeperClient(), log: Log);

            serviceDiscoveryManager.GetEnvironmentAsync("environment")
            .GetAwaiter()
            .GetResult()
            .ParentEnvironment
            .Should()
            .BeEquivalentTo(oldParent);

            const string newParent = "newWorldRoot";

            serviceDiscoveryManager.TryUpdateEnvironmentParentAsync("environment", newParent)
            .GetAwaiter()
            .GetResult()
            .Should()
            .BeTrue();

            serviceDiscoveryManager.GetEnvironmentAsync("environment")
            .GetAwaiter()
            .GetResult()
            .ParentEnvironment
            .Should()
            .BeEquivalentTo(newParent);
        }
Example #2
0
        public void GetServices_NotPass_NullInput()
        {   //Arrange
            var expected = false;
            var actual   = false;
            ICollection <ServiceDisplayResp> registeredServices;

            //Act
            using (var context = new ApiGatewayContext())
            {
                var serviceDiscoveryService = new ServiceDiscoveryService(context);
                var serviceDiscoveryManager = new ServiceDiscoveryManager(serviceDiscoveryService);
                registeredServices = serviceDiscoveryManager.GetAvailableServices(null);
            }



            //Since input is invalid, the registeredServices should be null
            if (registeredServices != null)
            {
                actual = true;
            }

            //Assert
            Assert.AreEqual(expected, actual);
        }
Example #3
0
        //Fail condition for ServiceDiscoveryManager.GetAvailableServices(), when input is too short, return no data
        public void GetServices_NotPass_InputTooShort()
        {   //Arrange
            var expected = false;
            var actual   = false;
            ICollection <ServiceDisplayResp> registeredServices;

            //Act
            using (var context = new ApiGatewayContext())
            {
                var serviceDiscoveryService = new ServiceDiscoveryService(context);
                var serviceDiscoveryManager = new ServiceDiscoveryManager(serviceDiscoveryService);
                //Generate a key that is shorter than requirement
                var randomId = GenerateRandomKey(Constants.clientIdLength - 1);
                registeredServices = serviceDiscoveryManager.GetAvailableServices(randomId);
            }

            //Since input is invalid, the registeredServices should be null
            if (registeredServices != null)
            {
                actual = true;
            }

            //Assert
            Assert.AreEqual(expected, actual);
        }
        public void TryCreatePermanentReplicaAsync_should_return_true_and_create_new_replica_environment_and_application_if_they_do_not_exist()
        {
            var replicaInfo             = new ReplicaInfo("default", "vostok", "replica1", GetProperties());
            var serviceDiscoveryManager = new ServiceDiscoveryManager(GetZooKeeperClient(), log: Log);

            serviceDiscoveryManager.TryCreatePermanentReplicaAsync(replicaInfo)
            .GetAwaiter()
            .GetResult()
            .Should()
            .BeTrue();

            serviceDiscoveryManager
            .GetAllReplicasAsync(replicaInfo.Environment, replicaInfo.Application)
            .GetAwaiter()
            .GetResult()
            .Should()
            .BeEquivalentTo("replica1");

            serviceDiscoveryManager
            .GetEnvironmentAsync("default")
            .GetAwaiter()
            .GetResult()
            .Should()
            .NotBeNull();

            serviceDiscoveryManager
            .GetApplicationAsync("default", "vostok")
            .GetAwaiter()
            .GetResult()
            .Should()
            .NotBeNull();
        }
Example #5
0
        public void GetServices_NotPass_EmptyDatabase()
        {   //Arrange
            var expected = false;
            var actual   = false;
            ICollection <ServiceDisplayResp> registeredServices;
            var option = new DbContextOptionsBuilder <ApiGatewayContext>()
                         .UseInMemoryDatabase(databaseName: "ServiceDiscoveryManager.GetServices_Empty_database")
                         .Options;

            //Act
            using (var context = new ApiGatewayContext(option))
            {
                var serviceDiscoveryService = new ServiceDiscoveryService(context);
                var serviceDiscoveryManager = new ServiceDiscoveryManager(serviceDiscoveryService);
                registeredServices = serviceDiscoveryManager.GetAvailableServices(GenerateRandomKey(Int32.Parse(Environment.GetEnvironmentVariable("APIKeyInputLength", EnvironmentVariableTarget.User))));
            }

            //Since input is invalid, the registeredServices should be null
            if (registeredServices != null)
            {
                actual = true;
            }

            //Assert
            Assert.AreEqual(expected, actual);
        }
        public void TryUpdateEnvironmentPropertiesAsync_should_return_false_and_do_not_create_non_existent_environment()
        {
            const string environment = "environment";

            var updatedProperties = new Dictionary <string, string>()
            {
                ["updatedKey"] = "updatedValue"
            };

            IEnvironmentInfoProperties UpdatePropertiesFunc(IEnvironmentInfoProperties props)
            {
                foreach (var property in updatedProperties)
                {
                    props.Set(property.Key, property.Value);
                }
                return(props);
            }

            var serviceDiscoveryManager = new ServiceDiscoveryManager(GetZooKeeperClient(), log: Log);

            serviceDiscoveryManager.TryUpdateEnvironmentPropertiesAsync(environment, UpdatePropertiesFunc)
            .GetAwaiter()
            .GetResult()
            .Should()
            .BeFalse();

            serviceDiscoveryManager.GetEnvironmentAsync(environment)
            .GetAwaiter()
            .GetResult()
            .Should()
            .BeNull();
        }
        public void GetAllEnvironmentsAsync_should_return_empty_array_if_no_environment_exists()
        {
            var serviceDiscoveryManager = new ServiceDiscoveryManager(GetZooKeeperClient(), log: Log);

            serviceDiscoveryManager.GetAllEnvironmentsAsync()
            .GetAwaiter()
            .GetResult()
            .Should()
            .BeEmpty();
        }
        public void GetAllReplicasAsync_should_return_empty_array_if_no_replicas_exists()
        {
            CreateApplicationNode("default", "vostok", GetProperties());

            var serviceDiscoveryManager = new ServiceDiscoveryManager(GetZooKeeperClient(), log: Log);

            serviceDiscoveryManager.GetAllReplicasAsync("default", "vostok")
            .GetAwaiter()
            .GetResult()
            .Should()
            .BeEmpty();
        }
        public void GetApplicationAsync_should_return_null_if_application_does_not_exist()
        {
            CreateApplicationNode("notMyEnv", "notMyApp");

            var serviceDiscoveryManager = new ServiceDiscoveryManager(GetZooKeeperClient(), log: Log);

            serviceDiscoveryManager.GetApplicationAsync("default", "vostok")
            .GetAwaiter()
            .GetResult()
            .Should()
            .BeNull();
        }
        public void GetAllApplicationAsync_should_return_empty_array_if_environment_does_not_exists()
        {
            CreateEnvironmentNode("env1");

            var serviceDiscoveryManager = new ServiceDiscoveryManager(GetZooKeeperClient(), log: Log);

            serviceDiscoveryManager.GetAllApplicationsAsync("env2")
            .GetAwaiter()
            .GetResult()
            .Should()
            .BeEmpty();
        }
        public void GetAllApplicationAsync_should_return_empty_array_if_there_are_no_application_in_environment()
        {
            CreateEnvironmentNode("env1");
            CreateApplicationNode("env2", "vostok");

            var serviceDiscoveryManager = new ServiceDiscoveryManager(GetZooKeeperClient(), log: Log);

            serviceDiscoveryManager.GetAllApplicationsAsync("env1")
            .GetAwaiter()
            .GetResult()
            .Should()
            .BeEmpty();
        }
        public void GetAllApplicationAsync_should_return_all_applications_from_environment()
        {
            CreateApplicationNode("env1", "vostok1");
            CreateApplicationNode("env1", "vostok2");
            CreateApplicationNode("env2", "zapad1");

            var serviceDiscoveryManager = new ServiceDiscoveryManager(GetZooKeeperClient(), log: Log);

            serviceDiscoveryManager.GetAllApplicationsAsync("env1")
            .GetAwaiter()
            .GetResult()
            .Should()
            .BeEquivalentTo("vostok1", "vostok2");
        }
        public void GetApplicationAsync_should_return_not_null_if_application_exists()
        {
            var replica = new ReplicaInfo("default", "vostok", "https://github.com/vostok");

            CreateApplicationNode(replica.Environment, replica.Application);

            var serviceDiscoveryManager = new ServiceDiscoveryManager(GetZooKeeperClient(), log: Log);

            serviceDiscoveryManager.GetApplicationAsync(replica.Environment, replica.Application)
            .GetAwaiter()
            .GetResult()
            .Should()
            .BeEquivalentTo(new ApplicationInfo(replica.Environment, replica.Application, null));
        }
        public void GetEnvironmentAsync_should_return_not_null_if_environment_exists()
        {
            var replica = new ReplicaInfo("default", "vostok", "https://github.com/vostok");

            CreateEnvironmentNode(replica.Environment);

            var serviceDiscoveryManager = new ServiceDiscoveryManager(GetZooKeeperClient(), log: Log);

            serviceDiscoveryManager.GetEnvironmentAsync(replica.Environment)
            .GetAwaiter()
            .GetResult()
            .Should()
            .NotBeNull();
        }
        public void GetAllEnvironmentsAsync_should_return_all_existent_environments()
        {
            CreateEnvironmentNode("env1");
            CreateEnvironmentNode("env2");
            CreateEnvironmentNode("env3");

            var serviceDiscoveryManager = new ServiceDiscoveryManager(GetZooKeeperClient(), log: Log);

            serviceDiscoveryManager.GetAllEnvironmentsAsync()
            .GetAwaiter()
            .GetResult()
            .Should()
            .BeEquivalentTo("env1", "env2", "env3");
        }
        public void TryDeleteEnvironmentAsync_should_return_true_for_non_existent_environment()
        {
            var serviceDiscoveryManager = new ServiceDiscoveryManager(GetZooKeeperClient(), log: Log);

            serviceDiscoveryManager.TryDeleteEnvironmentAsync("default")
            .GetAwaiter()
            .GetResult()
            .Should()
            .BeTrue();

            serviceDiscoveryManager.GetEnvironmentAsync("default")
            .GetAwaiter()
            .GetResult()
            .Should()
            .BeNull();
        }
        public void TryDeletePermanentReplicaAsync_should_return_true_for_non_existent_replica()
        {
            var serviceDiscoveryManager = new ServiceDiscoveryManager(GetZooKeeperClient(), log: Log);

            serviceDiscoveryManager.TryDeletePermanentReplicaAsync("default", "vostok", "replica1")
            .GetAwaiter()
            .GetResult()
            .Should()
            .BeTrue();

            serviceDiscoveryManager.GetAllReplicasAsync("default", "vostok")
            .GetAwaiter()
            .GetResult()
            .Should()
            .BeEmpty();
        }
        public void GetAllReplicasAsync_should_return_all_existent_replicas_from_environment_and_application()
        {
            CreateReplicaNode(new ReplicaInfo("default", "vostok", "vr1", GetProperties()));
            CreateReplicaNode(new ReplicaInfo("default", "vostok", "vr2", GetProperties()));
            CreateReplicaNode(new ReplicaInfo("default", "vostok", "vr3", GetProperties()));
            CreateReplicaNode(new ReplicaInfo("default", "zapad", "zzzz111", GetProperties()));
            CreateReplicaNode(new ReplicaInfo("worldofpain", "vostok", "665", GetProperties()));

            var serviceDiscoveryManager = new ServiceDiscoveryManager(GetZooKeeperClient(), log: Log);

            serviceDiscoveryManager.GetAllReplicasAsync("default", "vostok")
            .GetAwaiter()
            .GetResult()
            .Should()
            .BeEquivalentTo("vr1", "vr2", "vr3");
        }
        public void TryDeleteEnvironmentAsync_should_return_true_and_delete_existent_environment_without_children()
        {
            CreateEnvironmentNode("default", "parent", GetProperties());
            var serviceDiscoveryManager = new ServiceDiscoveryManager(GetZooKeeperClient(), log: Log);

            serviceDiscoveryManager.TryDeleteEnvironmentAsync("default")
            .GetAwaiter()
            .GetResult()
            .Should()
            .BeTrue();

            serviceDiscoveryManager.GetEnvironmentAsync("default")
            .GetAwaiter()
            .GetResult()
            .Should()
            .BeNull();
        }
        public void TryCreateEnvironmentAsync_should_return_true_and_create_new_environment()
        {
            var serviceDiscoveryManager = new ServiceDiscoveryManager(GetZooKeeperClient(), log: Log);

            var environmentInfo = new EnvironmentInfo("default", "parent", GetProperties());

            serviceDiscoveryManager.TryCreateEnvironmentAsync(environmentInfo)
            .GetAwaiter()
            .GetResult()
            .Should()
            .BeTrue();

            serviceDiscoveryManager.GetEnvironmentAsync("default")
            .GetAwaiter()
            .GetResult()
            .Should()
            .BeEquivalentTo(environmentInfo);
        }
        public void TryUpdateEnvironmentParentAsync_should_return_false_if_environment_does_not_exist()
        {
            var serviceDiscoveryManager = new ServiceDiscoveryManager(GetZooKeeperClient(), log: Log);

            const string newParent = "newWorldRoot";

            serviceDiscoveryManager.TryUpdateEnvironmentParentAsync("environment", newParent)
            .GetAwaiter()
            .GetResult()
            .Should()
            .BeFalse();

            serviceDiscoveryManager.GetEnvironmentAsync("environment")
            .GetAwaiter()
            .GetResult()
            .Should()
            .BeNull();
        }
        public void TryUpdateEnvironmentPropertiesAsync_should_return_true_and_set_new_properties_for_existent_environment()
        {
            var initProperties = GetProperties();
            var environment    = "environment";

            CreateEnvironmentNode(environment, properties: initProperties);
            var updatedProperties = new Dictionary <string, string>()
            {
                ["updatedKey"] = "updatedValue"
            };

            IEnvironmentInfoProperties UpdatePropertiesFunc(IEnvironmentInfoProperties props)
            {
                foreach (var property in updatedProperties)
                {
                    props = props.Set(property.Key, property.Value);
                }
                return(props);
            }

            var serviceDiscoveryManager = new ServiceDiscoveryManager(GetZooKeeperClient(), log: Log);

            serviceDiscoveryManager.GetEnvironmentAsync(environment)
            .GetAwaiter()
            .GetResult()
            .Properties
            .Should()
            .BeEquivalentTo(initProperties);

            serviceDiscoveryManager.TryUpdateEnvironmentPropertiesAsync(environment, UpdatePropertiesFunc)
            .GetAwaiter()
            .GetResult()
            .Should()
            .BeTrue();

            serviceDiscoveryManager.GetEnvironmentAsync(environment)
            .GetAwaiter()
            .GetResult()
            .Properties
            .Should()
            .BeEquivalentTo(initProperties.Concat(updatedProperties));
        }
        public void TryUpdateApplicationPropertiesAsync_should_return_true_and_set_new_properties_for_existent_application()
        {
            var initProperties = GetProperties();

            CreateApplicationNode("environment", "vostok", properties: initProperties);
            var updatedProperties = new Dictionary <string, string>
            {
                ["updatedKey"] = "updatedValue"
            };

            IApplicationInfoProperties UpdatePropertiesFunc(IApplicationInfoProperties props)
            {
                foreach (var property in updatedProperties)
                {
                    props = props.Set(property.Key, property.Value);
                }
                return(props);
            }

            var serviceDiscoveryManager = new ServiceDiscoveryManager(GetZooKeeperClient(), log: Log);

            serviceDiscoveryManager.GetApplicationAsync("environment", "vostok")
            .GetAwaiter()
            .GetResult()
            .Properties
            .Should()
            .BeEquivalentTo(initProperties);

            serviceDiscoveryManager.TryUpdateApplicationPropertiesAsync("environment", "vostok", UpdatePropertiesFunc)
            .GetAwaiter()
            .GetResult()
            .Should()
            .BeTrue();

            Action assert = () => serviceDiscoveryManager.GetApplicationAsync("environment", "vostok")
                            .GetAwaiter()
                            .GetResult()
                            .Properties
                            .Should()
                            .BeEquivalentTo(initProperties.Concat(updatedProperties));
        }
        public void TryCreateEnvironmentAsync_should_return_false_and_not_create_environment_if_it_exists()
        {
            CreateEnvironmentNode("default", "parent", GetProperties());
            var serviceDiscoveryManager = new ServiceDiscoveryManager(GetZooKeeperClient(), log: Log);

            var environmentInfo = new EnvironmentInfo("default", "parent", new Dictionary <string, string> {
                ["prop"] = "propValue"
            });

            serviceDiscoveryManager.TryCreateEnvironmentAsync(environmentInfo)
            .GetAwaiter()
            .GetResult()
            .Should()
            .BeFalse();

            serviceDiscoveryManager.GetEnvironmentAsync("default")
            .GetAwaiter()
            .GetResult()
            .Should()
            .BeEquivalentTo(new EnvironmentInfo("default", "parent", GetProperties()));
        }
        public void TryDeletePermanentReplicaAsync_should_return_false_and_do_not_delete_ephemeral_replica()
        {
            var replicaToDelete = new ReplicaInfo("default", "vostok", "replica1");

            CreateReplicaNode(replicaToDelete, false);
            CreateReplicaNode(new ReplicaInfo("default", "vostok", "replica2"));
            CreateReplicaNode(new ReplicaInfo("default", "vostok", "replica3"));

            var serviceDiscoveryManager = new ServiceDiscoveryManager(GetZooKeeperClient(), log: Log);

            serviceDiscoveryManager.TryDeletePermanentReplicaAsync(replicaToDelete.Environment, replicaToDelete.Application, replicaToDelete.Replica)
            .GetAwaiter()
            .GetResult()
            .Should()
            .BeFalse();

            serviceDiscoveryManager.GetAllReplicasAsync("default", "vostok")
            .GetAwaiter()
            .GetResult()
            .Should()
            .BeEquivalentTo("replica1", "replica2", "replica3");
        }
Example #26
0
        public void GetAvailableServices_InMemory_Pass()
        {   //Arrange
            var expected = true;
            var actual   = false;
            var option   = new DbContextOptionsBuilder <ApiGatewayContext>()
                           .UseInMemoryDatabase(databaseName: "ServiceDiscoveryManager.GetAvailableServices_Pass_Database")
                           .Options;
            ICollection <ServiceDisplayResp> registeredServices;

            //Act
            using (var context = new ApiGatewayContext(option))
            {
                // Create a team first
                var teamForTesting = new Team();
                var randomId       = GenerateRandomKey(Int32.Parse(Environment.GetEnvironmentVariable("APIKeyInputLength", EnvironmentVariableTarget.User)));
                teamForTesting.ClientId    = randomId;
                teamForTesting.WebsiteUrl  = "testingWebSiteUrl";
                teamForTesting.Secret      = "testingSecret";
                teamForTesting.CallbackUrl = "testingCallBackUrl";
                teamForTesting.Digest      = "testingDigest";
                teamForTesting.Username    = "******";
                context.Team.Add(teamForTesting);

                //Service

                var serviceForTesting = new Service();
                serviceForTesting.Endpoint    = "testingEndPoint";
                serviceForTesting.Owner       = randomId;//need to be the same as team's ClineId
                serviceForTesting.Id          = 12345678;
                serviceForTesting.Input       = "int";
                serviceForTesting.Output      = "int";
                serviceForTesting.Dataformat  = "xml";
                serviceForTesting.Description = "Some description for testing";
                context.Service.Add(serviceForTesting);

                //Configuration

                var configForTesting = new Configuration();
                configForTesting.EndPoint = "testingEndPoint";//need to be the same as service
                configForTesting.OpenTo   = randomId;
                configForTesting.Steps    = "some steps for testing";
                context.Configuration.Add(configForTesting);
                context.SaveChanges();

                var serviceDiscoveryService = new ServiceDiscoveryService(context);
                var serviceDiscoveryManager = new ServiceDiscoveryManager(serviceDiscoveryService);

                registeredServices = serviceDiscoveryManager.GetAvailableServices(randomId);
                if (registeredServices.Count > 0)
                {
                    actual = true;
                }
            }

            foreach (var service in registeredServices)
            {
                Trace.WriteLine(service.Endpoint + " " + service.Username + " " + service.Input + " " + service.Output + " " + service.Dataformat + " " + service.Description + Environment.NewLine);
            }

            //Assert
            Assert.AreEqual(expected, actual);
        }
 public ServiceDiscoveryController(ServiceDiscoveryManager serviceDiscoveryManager)
 {
     _serviceDiscoveryManager = serviceDiscoveryManager;
 }