예제 #1
0
        public void TestMqttDeviceState()
        {
            var deviceId = "rsa-device-mqtt-state-" + _fixture.TestId;

            try
            {
                //Setup screnario
                CloudIotSample.CreateRsaDevice(_fixture.ProjectId, _fixture.RegionId, _fixture.RegistryId, deviceId, "test/data/rsa_cert.pem");
                var mqttExampleOut = Run("startMqtt",
                                         _fixture.ProjectId,
                                         _fixture.RegionId,
                                         _fixture.RegistryId,
                                         deviceId,
                                         _fixture.PrivateKeyPath,
                                         "RS256",
                                         _fixture.CertPath,
                                         "state",
                                         "--waittime",
                                         "5",
                                         "--nummessages",
                                         "1");
                Assert.Contains("Publishing state message", mqttExampleOut.Stdout);
                Assert.Contains("On Publish", mqttExampleOut.Stdout);
            }
            catch (Google.GoogleApiException e)
            {
                Console.WriteLine("Failure on exception: {0}", e.Message);
            }
            finally
            {
                //Clean up
                CloudIotSample.DeleteDevice(_fixture.ProjectId, _fixture.RegionId, _fixture.RegistryId, deviceId);
            }
        }
예제 #2
0
        public void TestGatewayListenForDevice()
        {
            var gatewayId = "rsa-listen-gateway";
            var deviceId  = "rsa-listen-device";

            try
            {
                //Setup
                CloudIotSample.CreateGateway(_fixture.ProjectId, _fixture.RegionId,
                                             _fixture.RegistryId, gatewayId, "test/data/rsa_cert.pem", "RS256");
                CloudIotSample.BindDeviceToGateway(_fixture.ProjectId, _fixture.RegionId, _fixture.RegistryId,
                                                   deviceId, gatewayId);

                //Connect
                var listenConfigMsgOut = Run("listenForConfigMessages", _fixture.ProjectId,
                                             _fixture.RegionId, _fixture.RegistryId, gatewayId, deviceId, _fixture.CertPath,
                                             _fixture.PrivateKeyPath, "RS256", "--listentime", "10");

                //Assertions
                Assert.DoesNotContain("error occurred", listenConfigMsgOut.Stdout);
                Assert.Contains("On Subscribe", listenConfigMsgOut.Stdout);
                Assert.Contains("On Publish", listenConfigMsgOut.Stdout);
            }
            finally
            {
                //Clean up
                CloudIotSample.UnbindDeviceFromGateway(_fixture.ProjectId, _fixture.RegionId,
                                                       _fixture.RegistryId, deviceId, gatewayId);
                CloudIotSample.DeleteDevice(_fixture.ProjectId, _fixture.RegionId, _fixture.RegistryId, deviceId);
                CloudIotSample.DeleteDevice(_fixture.ProjectId, _fixture.RegionId, _fixture.RegistryId, gatewayId);
            }
        }
예제 #3
0
        public void TestSendDataForBoundDevice()
        {
            var gatewayId = string.Format("test-gateway-{0}", "RS256");
            var deviceId  = string.Format("test-device-{0}", TestUtil.RandomName());

            try
            {
                //Setup
                CloudIotSample.CreateGateway(_fixture.ProjectId, _fixture.RegionId, _fixture.RegistryId,
                                             gatewayId, "test/data/rsa_cert.pem", "RS256");
                CloudIotSample.BindDeviceToGateway(_fixture.ProjectId, _fixture.RegionId,
                                                   _fixture.RegistryId, deviceId, gatewayId);

                //Connect the gateway
                var sendDataBoundDeviceOut = Run("sendDataFromBoundDevice", _fixture.ProjectId,
                                                 _fixture.RegionId, _fixture.RegistryId,
                                                 deviceId, gatewayId, _fixture.PrivateKeyPath, "RS256", _fixture.CertPath,
                                                 "state", "test-message");
                Assert.Contains("Data sent", sendDataBoundDeviceOut.Stdout);
                Assert.Contains("On Publish", sendDataBoundDeviceOut.Stdout);
                Assert.DoesNotContain("An error occured", sendDataBoundDeviceOut.Stdout);
            }
            finally
            {
                //Clean up
                CloudIotSample.UnbindDeviceFromGateway(_fixture.ProjectId, _fixture.RegionId,
                                                       _fixture.RegistryId, deviceId, gatewayId);
                CloudIotSample.DeleteDevice(_fixture.ProjectId, _fixture.RegionId,
                                            _fixture.RegistryId, deviceId);
                CloudIotSample.DeleteDevice(_fixture.ProjectId, _fixture.RegionId,
                                            _fixture.RegistryId, gatewayId);
            }
        }
예제 #4
0
        public IotTestFixture()
        {
            RegionId  = "us-central1";
            ProjectId = Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID");
            string privateKeyPath = Environment.GetEnvironmentVariable("IOT_PRIVATE_KEY_PATH");

            if (privateKeyPath.Length == 0 || !File.Exists(privateKeyPath))
            {
                throw new NullReferenceException("Private key path is not for unit tests.");
            }
            CertPath       = Environment.GetEnvironmentVariable("IOT_CERT_KEY_PATH");
            PrivateKeyPath = privateKeyPath;
            ServiceAccount = "serviceAccount:[email protected]";
            TestId         = ((DateTimeOffset)DateTime.UtcNow).ToUnixTimeSeconds().ToString();
            TopicName      = new TopicName(ProjectId, "iot-test-" + TestId);
            RegistryId     = "iot-test-" + TestId;
            CreatePubSubTopic(this.TopicName);
            // Check if the number of registries does not exceed 90.
            CheckRegistriesLimit(ProjectId, RegionId);
            Assert.Equal(0, CloudIotSample.CreateRegistry(ProjectId, RegionId,
                                                          RegistryId, TopicName.TopicId));
        }
예제 #5
0
        public void CheckRegistriesLimit(string projectId, string regionId)
        {
            List <DeviceRegistry> listRegistries = (List <DeviceRegistry>)CloudIotMqttExample.GetRegistries(projectId, regionId);

            if (listRegistries != null && listRegistries.Count > 90)
            {
                //Clean 20 oldest registries with testing prefix in the project.
                Console.WriteLine("The maximum number of registries is about to exceed.");
                Console.WriteLine("Deleting the oldest 20 registries with IoT Test prefix");
                var count = 20;
                var index = 0;
                while (count > 0)
                {
                    if (listRegistries[index].Id.Contains("iot-test-"))
                    {
                        CloudIotSample.UnbindAllDevices(projectId, regionId, listRegistries[index].Id);
                        CloudIotSample.ClearRegistry(projectId, regionId, listRegistries[index].Id);
                        count--;
                    }
                    index++;
                }
            }
        }
예제 #6
0
        public void Dispose()
        {
            var deleteRegOutput = CloudIotSample.DeleteRegistry(ProjectId, RegionId, RegistryId);

            DeletePubSubTopic(this.TopicName);
        }