Пример #1
0
        static async Task UpdateDeploymentEnvironmentVariablesAsync(TestResultReportingClient apiClient, CancellationTokenSource cts)
        {
            RegistryManager registryManager = null;

            try
            {
                registryManager = RegistryManager.CreateFromConnectionString(Settings.Current.IoTHubConnectionString.OrDefault());
                JObject deploymentJson = await GetEdgeAgentDeploymentManifestJsonAsync(registryManager, Settings.Current.DeviceId);

                DateTime testStartAt = DateTime.UtcNow;
                long     count       = 1;
                var      envVars     = new Dictionary <string, string>();

                while (!cts.IsCancellationRequested && DateTime.UtcNow - testStartAt < Settings.Current.TestDuration)
                {
                    KeyValuePair <string, string> newEnvVar     = AddEnvironmentValue(deploymentJson, Settings.Current.TargetModuleId.OrDefault(), count);
                    ConfigurationContent          configContent = JsonConvert.DeserializeObject <ConfigurationContent>(deploymentJson.ToString());
                    await registryManager.ApplyConfigurationContentOnDeviceAsync(Settings.Current.DeviceId, configContent);

                    envVars.Add(newEnvVar.Key, newEnvVar.Value);
                    var testResult = new DeploymentTestResult(Settings.Current.TrackingId, Settings.Current.ModuleId + ".send", envVars, DateTime.UtcNow);
                    await ModuleUtil.ReportTestResultAsync(apiClient, Logger, testResult);

                    Logger.LogInformation($"Successfully report to TRC for new deployment: tracking id={Settings.Current.TrackingId}, new environment variable={newEnvVar.Key}:{newEnvVar.Value}, EnvVars Count={envVars.Count}.");

                    await Task.Delay(Settings.Current.DeploymentUpdatePeriod, cts.Token);

                    count++;
                }
            }
            finally
            {
                registryManager?.Dispose();
            }
        }
Пример #2
0
        public async Task TestCreateReportAsync(
            IEnumerable <string> expectedStoreValues,
            IEnumerable <string> actualStoreValues,
            int batchSize,
            ulong totalExpectedDeployments,
            ulong totalActualDeployments,
            ulong totalMatchedDeployments,
            int expectedMissingResultsCount,
            DeploymentTestResult lastActualDeploymentTestResult)
        {
            string expectedSource = "expectedSource";
            string actualSource   = "actualSource";
            string resultType     = TestOperationResultType.Deployment.ToString();

            var mockExpectedStore = new Mock <ISequentialStore <TestOperationResult> >();
            var expectedResults   = new StoreTestResultCollection <TestOperationResult>(mockExpectedStore.Object, batchSize);
            var mockActualStore   = new Mock <ISequentialStore <TestOperationResult> >();
            var actualResults     = new StoreTestResultCollection <TestOperationResult>(mockActualStore.Object, batchSize);

            string trackingId      = Guid.NewGuid().ToString();
            var    reportGenerator = new DeploymentTestReportGenerator(
                TestDescription,
                trackingId,
                expectedSource,
                expectedResults,
                actualSource,
                actualResults,
                UnmatchedResultsMaxSize);

            var expectedStoreData = GetStoreData(expectedSource, resultType, expectedStoreValues);

            for (int i = 0; i < expectedStoreData.Count; i += batchSize)
            {
                int startingOffset = i;
                mockExpectedStore.Setup(s => s.GetBatch(startingOffset, batchSize)).ReturnsAsync(expectedStoreData.Skip(startingOffset).Take(batchSize));
            }

            var actualStoreData = GetStoreData(actualSource, resultType, actualStoreValues);

            for (int j = 0; j < expectedStoreData.Count; j += batchSize)
            {
                int startingOffset = j;
                mockActualStore.Setup(s => s.GetBatch(startingOffset, batchSize)).ReturnsAsync(actualStoreData.Skip(startingOffset).Take(batchSize));
            }

            var report = (DeploymentTestReport)await reportGenerator.CreateReportAsync();

            Assert.Equal(totalExpectedDeployments, report.TotalExpectedDeployments);
            Assert.Equal(totalActualDeployments, report.TotalActualDeployments);
            Assert.Equal(totalMatchedDeployments, report.TotalMatchedDeployments);
            Assert.Equal(expectedMissingResultsCount, report.UnmatchedResults.Count);

            if (lastActualDeploymentTestResult != null)
            {
                Assert.True(report.LastActualDeploymentTestResult.HasValue);

                var comparer = new DeploymentTestResultComparer();
                Assert.True(comparer.Matches(lastActualDeploymentTestResult.ToTestOperationResult(), report.LastActualDeploymentTestResult.OrDefault()));
            }
        }
Пример #3
0
        public bool Matches(TestOperationResult expected, TestOperationResult actual)
        {
            if ((expected == null && actual != null) || (expected != null && actual == null))
            {
                return(false);
            }

            if (expected == null && actual == null)
            {
                return(true);
            }

            if (!string.Equals(expected.Type, actual.Type, StringComparison.OrdinalIgnoreCase))
            {
                return(false);
            }

            DeploymentTestResult expectedResult = JsonConvert.DeserializeObject <DeploymentTestResult>(expected.Result);
            DeploymentTestResult actualResult   = JsonConvert.DeserializeObject <DeploymentTestResult>(actual.Result);

            if (!string.Equals(expectedResult.TrackingId, actualResult.TrackingId, StringComparison.OrdinalIgnoreCase))
            {
                return(false);
            }

            if (expectedResult.EnvironmentVariables.Count > actualResult.EnvironmentVariables.Count)
            {
                return(false);
            }

            // Actual result should have key and value matched to expected result
            foreach (string key in expectedResult.EnvironmentVariables.Keys)
            {
                if (!actualResult.EnvironmentVariables.ContainsKey(key))
                {
                    return(false);
                }

                if (!string.Equals(expectedResult.EnvironmentVariables[key], actualResult.EnvironmentVariables[key], StringComparison.OrdinalIgnoreCase))
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #4
0
        static async Task ReportDeploymentEnvironmentVariablesAsync(TestResultReportingClient trcClient)
        {
            // Report all environment variable with predefined prefix to Test Result Coordinator
            var envVars = new Dictionary <string, string>();

            foreach (DictionaryEntry envVariable in Environment.GetEnvironmentVariables())
            {
                if (envVariable.Key.ToString().StartsWith(Settings.EnvironmentVariablePrefix, StringComparison.OrdinalIgnoreCase))
                {
                    envVars.Add(envVariable.Key.ToString(), envVariable.Value.ToString());
                }
            }

            var testResult = new DeploymentTestResult(Settings.Current.TrackingId, Settings.Current.ModuleId + ".receive", envVars, DateTime.UtcNow);
            await ModuleUtil.ReportTestResultAsync(trcClient, Logger, testResult);

            Logger.LogInformation($"Successfully report to TRC for new deployment: tracking id={Settings.Current.TrackingId}, environment variable count={testResult.EnvironmentVariables.Count}.");
        }
Пример #5
0
        public void TestConstructorSuccess()
        {
            DeploymentTestResult testResult = GetDeploymentTestResult();

            var report = new DeploymentTestReport(
                TestDescription,
                "trackingId123",
                "expectedSource",
                "actualSource",
                "resultType1",
                15,
                10,
                13,
                Option.Some(testResult.ToTestOperationResult()),
                new List <TestOperationResult>
            {
                new TestOperationResult("expectedSource", "resultType1", "FakeValue1", new DateTime(2019, 12, 4, 10, 15, 15)),
                new TestOperationResult("expectedSource", "resultType1", "FakeValue2", new DateTime(2019, 12, 4, 10, 15, 18)),
            });

            Assert.Equal(TestDescription, report.TestDescription);
            Assert.Equal("trackingId123", report.TrackingId);
            Assert.Equal("actualSource", report.ActualSource);
            Assert.Equal("expectedSource", report.ExpectedSource);
            Assert.Equal("resultType1", report.ResultType);
            Assert.Equal(15UL, report.TotalExpectedDeployments);
            Assert.Equal(10UL, report.TotalActualDeployments);
            Assert.Equal(13UL, report.TotalMatchedDeployments);
            Assert.Equal(2, report.UnmatchedResults.Count);

            Assert.Equal("expectedSource", report.UnmatchedResults[0].Source);
            Assert.Equal("resultType1", report.UnmatchedResults[0].Type);
            Assert.Equal("FakeValue1", report.UnmatchedResults[0].Result);
            Assert.Equal(new DateTime(2019, 12, 4, 10, 15, 15), report.UnmatchedResults[0].CreatedAt);

            Assert.Equal("expectedSource", report.UnmatchedResults[1].Source);
            Assert.Equal("resultType1", report.UnmatchedResults[1].Type);
            Assert.Equal("FakeValue2", report.UnmatchedResults[1].Result);
            Assert.Equal(new DateTime(2019, 12, 4, 10, 15, 18), report.UnmatchedResults[1].CreatedAt);
        }
        public void TestConstructorThrowsWhenExpectedSourceIsNotProvided(string expectedSource)
        {
            DeploymentTestResult testResult = GetDeploymentTestResult();

            ArgumentException ex = Assert.Throws <ArgumentException>(
                () => new DeploymentTestReport(
                    "trackingId123",
                    expectedSource,
                    "actualSource",
                    "resultType1",
                    15,
                    10,
                    13,
                    Option.Some(testResult.ToTestOperationResult()),
                    new List <TestOperationResult>
            {
                new TestOperationResult("expectedSource", "resultType1", "FakeValue1", new DateTime(2019, 12, 4, 10, 15, 15)),
                new TestOperationResult("expectedSource", "resultType1", "FakeValue2", new DateTime(2019, 12, 4, 10, 15, 18)),
            }));

            Assert.StartsWith("expectedSource", ex.Message);
        }