Ejemplo n.º 1
0
        public void ExtantParametersTest()
        {
            string           environment = "Staging";
            List <Parameter> parameters  = new List <Parameter>(_parameters);

            JObject parameterizedDashboard = GrafanaSerialization.ParameterizeDashboard(_valueReplacementDashboard, parameters, _environments, environment);

            // Expect that the parameters list has not changed
            parameters.Select(p => p.Values[environment]).Should()
            .BeEquivalentTo(_parameters
                            .SelectMany(p => p.Values
                                        .Where(kvp => kvp.Key == environment)
                                        .Select(kvp => kvp.Value)));

            // Expect that the dashboard values are replaced with the preset parameter names
            parameterizedDashboard.SelectToken("$.dashboard.panels[0].targets[0].azureMonitor.resourceGroup")?.Value <string>()
            .Should().NotBeNull()
            .And.StartWith("[parameter(MyNamedValue1Parameter)]");
            parameterizedDashboard.SelectToken("$.dashboard.panels[0].targets[0].azureMonitor.resourceName")?.Value <string>()
            .Should().NotBeNull()
            .And.StartWith("[parameter(MyNamedValue2Parameter)]");
            parameterizedDashboard.SelectToken("$.dashboard.panels[1].targets[0].azureLogAnalytics.resource")?.Value <string>()
            .Should().NotBeNull()
            .And.StartWith("[parameter(MyNamedValue1Parameter)]");
        }
Ejemplo n.º 2
0
        public void NoExtantParametersTest()
        {
            List <Parameter> parameters = new List <Parameter>();

            string environment = "Staging";

            JObject parameterizedDashboard = GrafanaSerialization.ParameterizeDashboard(_valueReplacementDashboard, parameters, _environments, environment);

            parameters.Should()
            .HaveCount(2);
            parameters.Select(p => p.Values.Keys).Should()
            .AllBeEquivalentTo(_environments);
            parameters.Select(p => p.Values[environment]).Should()
            .BeEquivalentTo(_parameters
                            .SelectMany(p => p.Values
                                        .Where(kvp => kvp.Key == environment)
                                        .Select(kvp => kvp.Value)));

            parameterizedDashboard.SelectToken("$.dashboard.panels[0].targets[0].azureMonitor.resourceGroup")?.Value <string>()
            .Should().NotBeNull().And.StartWith("[parameter(");
            parameterizedDashboard.SelectToken("$.dashboard.panels[0].targets[0].azureMonitor.resourceName")?.Value <string>()
            .Should().NotBeNull().And.StartWith("[parameter(");
            parameterizedDashboard.SelectToken("$.dashboard.panels[1].targets[0].azureLogAnalytics.resource")?.Value <string>()
            .Should().NotBeNull().And.StartWith("[parameter(");
        }
Ejemplo n.º 3
0
        public void Deparameterize_ThrowIfUnknownEnvironmentTest()
        {
            // Cause exception by specifying environment not used in the definition list
            string environment = "MadeUpEnvironment";

            Action act = () => GrafanaSerialization.DeparameterizeDashboard(_dashboardWithParameters, _parameters, environment);

            act.Should().Throw <ArgumentException>();
        }
Ejemplo n.º 4
0
        public void Deparameterize_ThrowIfUnknownNameTest()
        {
            string environment = "Staging";

            // Cause exception by including only some of the definitions used in the dashboard
            IEnumerable <Parameter> parameters = _parameters.Take(1);

            Action act = () => GrafanaSerialization.DeparameterizeDashboard(_dashboardWithParameters, parameters, environment);

            act.Should().Throw <ArgumentException>();
        }
Ejemplo n.º 5
0
        public void ExtractDataSourceIdentifiersTest()
        {
            //$.dashboard.[*].datasource
            var dashboard = new JObject
            {
                {
                    "dashboard",
                    new JObject
                    {
                        { "annotations",
                          new JObject
                          {
                              {
                                  "list", new JArray
                                  {
                                      new JObject
                                      {
                                          { "datasource", "Test Datasource Name 1" },
                                      }
                                  }
                              }
                          } }
                    }
                },
                {
                    "panels",
                    new JArray
                    {
                        new JObject
                        {
                            {
                                "datasource",
                                new JObject
                                {
                                    { "type", "Test Datasource Type" },
                                    { "uid", "1234" }
                                }
                            },
                            { "other-property", "IGNORED" },
                        }
                    }
                },
                { "other-property", "IGNORED" }
            };

            var expected = new List <string> {
                "Test Datasource Name 1",
                "1234"
            };

            IEnumerable <string> actual = GrafanaSerialization.ExtractDataSourceIdentifiers(dashboard);

            actual.Should().Equal(expected);
        }
Ejemplo n.º 6
0
        public void SanitizeDataSourceTest()
        {
            var datasource = new JObject
            {
                { "id", "removed" },
                { "orgId", "removed" },
                { "url", "" },
                { "name", "datasource name" },
                {
                    "jsonData",
                    new JObject
                    {
                        { "safeData1", "value 1" },
                        { "safeData2", "value 2" },
                    }
                },
                {
                    "secureJsonFields",
                    new JObject
                    {
                        { "dangerousField1", "REMOVED" },
                        { "dangerousField2", "REMOVED" },
                    }
                },
            };

            var result = GrafanaSerialization.SanitizeDataSource(datasource);

            // These are instance dependent, so need to be stripped
            result["id"].Should().BeNull();
            result["orgId"].Should().BeNull();
            result["url"].Should().BeNull();
            result["secureJsonFields"].Should().BeNull();

            // These are secure, so they shouldn't be exported
            string df1 = result.Value <JObject>("secureJsonData")?.Value <string>("dangerousField1");

            df1.Should().StartWith("[vault(");
            df1.Should().Contain("dangerousField1");
            df1.Should().NotContain("REMOVED");

            string df2 = result.Value <JObject>("secureJsonData")?.Value <string>("dangerousField2");

            df2.Should().StartWith("[vault(");
            df2.Should().Contain("dangerousField2");
            df2.Should().NotContain("REMOVED");

            // This is safe, so it should be preserved
            (result.Value <JObject>("jsonData")?.Value <string>("safeData1")).Should().Be("value 1");
        }
Ejemplo n.º 7
0
        public void DeparamaterizeTest()
        {
            string environment = "Staging";

            JObject deparameterizedDashboard = GrafanaSerialization.DeparameterizeDashboard(_dashboardWithParameters, _parameters, environment);

            deparameterizedDashboard.SelectToken("$.dashboard.panels[0].targets[0].azureMonitor.resourceGroup")?.Value <string>()
            .Should().Be(_parameters[0].Values[environment]);
            deparameterizedDashboard.SelectToken("$.dashboard.panels[0].targets[0].azureMonitor.resourceName")?.Value <string>()
            .Should().Be(_parameters[1].Values[environment]);
            deparameterizedDashboard.SelectToken("$.dashboard.panels[0].targets[0].azureMonitor.duplicateTest")?.Value <string>()
            .Should().Be(_parameters[0].Values[environment]);
            deparameterizedDashboard.SelectToken("$.dashboard.panels[1].targets[0].azureLogAnalytics.resource")?.Value <string>()
            .Should().Be(_parameters[0].Values[environment]);
        }
Ejemplo n.º 8
0
        public void Deparameterize_ThrowIfPlaceholderPresentTest()
        {
            // Cause exception by specifying a parameter with the placeholder string in it.
            string           environment = _environments[0];
            List <Parameter> parameters  = new List <Parameter>(_parameters);

            parameters.Add(new Parameter()
            {
                Name   = "PLACEHOLDER:12345678-1234-1234-1234-1234567890AB",
                Values = new Dictionary <string, string>()
                {
                    { environment, "MadeUpValue" }
                }
            });

            Action act = () => GrafanaSerialization.DeparameterizeDashboard(_dashboardWithParameters, parameters, environment);

            act.Should().Throw <ArgumentException>();
        }
Ejemplo n.º 9
0
        public void ExtractDataSourceNamesTest()
        {
            //$.dashboard.panels[*]..datasource
            var dashboard = new JObject
            {
                {
                    "dashboard", new JObject
                    {
                        {
                            "panels",
                            new JArray
                            {
                                new JObject
                                {
                                    { "datasource", "Test Datasource 1" },
                                    { "other-property", "IGNORED" },
                                },
                                new JObject
                                {
                                    { "datasource", "Test Datasource 2" },
                                },
                            }
                        },
                        { "other-property", "IGNORED" },
                    }
                },
            };

            var expected = new List <string> {
                "Test Datasource 1",
                "Test Datasource 2",
            };

            IEnumerable <string> actual = GrafanaSerialization.ExtractDataSourceNames(dashboard);

            Assert.Equal(expected, actual);
        }