Exemple #1
0
        public void KubernetesConfigMapProvider_AddsEnvSpecificJsonFileToDictionaryOnSuccess()
        {
            // arrange
            var mockHttpMessageHandler = new MockHttpMessageHandler();

            mockHttpMessageHandler
            .Expect(HttpMethod.Get, "*")
            .Respond(new StringContent("{\"kind\":\"ConfigMap\",\"apiVersion\":\"v1\",\"metadata\":{\"name\":\"testconfigmap\",\"namespace\":\"default\",\"selfLink\":\"/api/v1/namespaces/default/configmaps/testconfigmap\",\"uid\":\"8582b94c-f4fa-47fa-bacc-47019223775c\",\"resourceVersion\":\"1320622\",\"creationTimestamp\":\"2020-04-15T18:33:49Z\",\"annotations\":{\"kubectl.kubernetes.io/last-applied-configuration\":\"{\\\"apiVersion\\\":\\\"v1\\\",\\\"data\\\":{\\\"ConfigMapName\\\":\\\"testconfigmap\\\"},\\\"kind\\\":\\\"ConfigMap\\\",\\\"metadata\\\":{\\\"annotations\\\":{},\\\"name\\\":\\\"kubernetes1\\\",\\\"namespace\\\":\\\"default\\\"}}\\n\"}},\"data\":{" +
                                       "\"appsettings.demo.json\": \"{\n  \\\"Test0\\\": \\\"Value0\\\",\n  \\\"Test1\\\": [\n    {\n      \\\"Test2\\\": \\\"Value1\\\"\n    }\n  ]\n}\"" +
                                       "}\n}\n"));

            using var client = new k8s.Kubernetes(new KubernetesClientConfiguration { Host = "http://localhost" }, httpClient: mockHttpMessageHandler.ToHttpClient());
            var settings = new KubernetesConfigSourceSettings("default", "testconfigmap", new ReloadSettings());
            var provider = new KubernetesConfigMapProvider(client, settings);

            // act
            provider.Load();

            // assert
            Assert.True(provider.TryGet("Test0", out var testValue));
            Assert.Equal("Value0", testValue);

            Assert.True(provider.TryGet("Test1:0:Test2", out var testValue2));
            Assert.Equal("Value1", testValue2);
        }
Exemple #2
0
        public async Task KubernetesSecretProvider_ReloadsDictionaryOnInterval()
        {
            // arrange
            var foundKey = false;
            var mockHttpMessageHandler = new MockHttpMessageHandler();

            mockHttpMessageHandler
            .Expect(HttpMethod.Get, "*")
            .Respond(new StringContent("{\"kind\":\"Secret\",\"apiVersion\":\"v1\",\"metadata\":{\"name\":\"testsecret\",\"namespace\":\"default\",\"selfLink\":\"/api/v1/namespaces/default/secrets/testsecret\",\"uid\":\"04a256d5-5480-4e6a-ab1a-81b1df2b1f15\",\"resourceVersion\":\"724153\",\"creationTimestamp\":\"2020-04-17T14:32:42Z\",\"annotations\":{\"kubectl.kubernetes.io/last-applied-configuration\":\"{\\\"apiVersion\\\":\\\"v1\\\",\\\"data\\\":{\\\"testKey\\\":\\\"dGVzdFZhbHVl\\\"},\\\"kind\\\":\\\"Secret\\\",\\\"metadata\\\":{\\\"annotations\\\":{},\\\"name\\\":\\\"testsecret\\\",\\\"namespace\\\":\\\"default\\\"},\\\"type\\\":\\\"Opaque\\\"}\\n\"}},\"data\":{\"testKey\":\"dGVzdFZhbHVl\"},\"type\":\"Opaque\"}\n"));
            mockHttpMessageHandler
            .Expect(HttpMethod.Get, "*")
            .Respond(new StringContent("{\"kind\":\"Secret\",\"apiVersion\":\"v1\",\"metadata\":{\"name\":\"testsecret\",\"namespace\":\"default\",\"selfLink\":\"/api/v1/namespaces/default/secrets/testsecret\",\"uid\":\"04a256d5-5480-4e6a-ab1a-81b1df2b1f15\",\"resourceVersion\":\"724153\",\"creationTimestamp\":\"2020-04-17T14:32:42Z\",\"annotations\":{\"kubectl.kubernetes.io/last-applied-configuration\":\"{\\\"apiVersion\\\":\\\"v1\\\",\\\"data\\\":{\\\"testKey\\\":\\\"dGVzdFZhbHVl\\\"},\\\"kind\\\":\\\"Secret\\\",\\\"metadata\\\":{\\\"annotations\\\":{},\\\"name\\\":\\\"testsecret\\\",\\\"namespace\\\":\\\"default\\\"},\\\"type\\\":\\\"Opaque\\\"}\\n\"}},\"data\":{\"updatedKey\":\"dGVzdFZhbHVl\"},\"type\":\"Opaque\"}\n"));
            mockHttpMessageHandler
            .Expect(HttpMethod.Get, "*")
            .Respond(new StringContent("{\"kind\":\"Secret\",\"apiVersion\":\"v1\",\"metadata\":{\"name\":\"testsecret\",\"namespace\":\"default\",\"selfLink\":\"/api/v1/namespaces/default/secrets/testsecret\",\"uid\":\"04a256d5-5480-4e6a-ab1a-81b1df2b1f15\",\"resourceVersion\":\"724153\",\"creationTimestamp\":\"2020-04-17T14:32:42Z\",\"annotations\":{\"kubectl.kubernetes.io/last-applied-configuration\":\"{\\\"apiVersion\\\":\\\"v1\\\",\\\"data\\\":{\\\"testKey\\\":\\\"dGVzdFZhbHVl\\\"},\\\"kind\\\":\\\"Secret\\\",\\\"metadata\\\":{\\\"annotations\\\":{},\\\"name\\\":\\\"testsecret\\\",\\\"namespace\\\":\\\"default\\\"},\\\"type\\\":\\\"Opaque\\\"}\\n\"}},\"data\":{\"updatedAgain\":\"dGVzdFZhbHVl\"},\"type\":\"Opaque\"}\n"));

            var client = new k8s.Kubernetes(new KubernetesClientConfiguration()
            {
                Host = "http://localhost"
            }, httpClient: mockHttpMessageHandler.ToHttpClient());
            var settings = new KubernetesConfigSourceSettings("default", "testsecret", new ReloadSettings()
            {
                Period = 1, Secrets = true
            });
            var provider = new KubernetesSecretProvider(client, settings, new CancellationTokenSource(20000).Token);

            // act
            provider.Load();

            // assert
            Assert.True(provider.TryGet("testKey", out var testValue), "TryGet testKey");
            Assert.Equal("testValue", testValue);
            while (!foundKey)
            {
                await Task.Delay(100);

                foundKey = provider.TryGet("updatedKey", out testValue);
                if (foundKey)
                {
                    Assert.Equal("testValue", testValue);
                }
            }

            foundKey = false;
            while (!foundKey)
            {
                await Task.Delay(100);

                foundKey = provider.TryGet("updatedAgain", out testValue);
                if (foundKey)
                {
                    Assert.Equal("testValue", testValue);
                }
            }

            Assert.False(provider.TryGet("testKey", out _), "TryGet testKey after update");
            Assert.False(provider.TryGet("updatedKey", out _), "TryGet updatedKey after update");
        }
Exemple #3
0
        public async Task KubernetesConfigMapProvider_ReloadsDictionaryOnInterval()
        {
            // arrange
            var foundKey = false;
            var mockHttpMessageHandler = new MockHttpMessageHandler();

            mockHttpMessageHandler
            .Expect(HttpMethod.Get, "*")
            .Respond(new StringContent("{\"kind\":\"ConfigMap\",\"apiVersion\":\"v1\",\"metadata\":{\"name\":\"testconfigmap\",\"namespace\":\"default\",\"selfLink\":\"/api/v1/namespaces/default/configmaps/testconfigmap\",\"uid\":\"8582b94c-f4fa-47fa-bacc-47019223775c\",\"resourceVersion\":\"1320622\",\"creationTimestamp\":\"2020-04-15T18:33:49Z\",\"annotations\":{\"kubectl.kubernetes.io/last-applied-configuration\":\"{\\\"apiVersion\\\":\\\"v1\\\",\\\"data\\\":{\\\"ConfigMapName\\\":\\\"testconfigmap\\\"},\\\"kind\\\":\\\"ConfigMap\\\",\\\"metadata\\\":{\\\"annotations\\\":{},\\\"name\\\":\\\"kubernetes1\\\",\\\"namespace\\\":\\\"default\\\"}}\\n\"}},\"data\":{\"TestKey\":\"TestValue\"}}\n"));
            mockHttpMessageHandler
            .Expect(HttpMethod.Get, "*")
            .Respond(new StringContent("{\"kind\":\"ConfigMap\",\"apiVersion\":\"v1\",\"metadata\":{\"name\":\"testconfigmap\",\"namespace\":\"default\",\"selfLink\":\"/api/v1/namespaces/default/configmaps/testconfigmap\",\"uid\":\"8582b94c-f4fa-47fa-bacc-47019223775c\",\"resourceVersion\":\"1320622\",\"creationTimestamp\":\"2020-04-15T18:33:49Z\",\"annotations\":{\"kubectl.kubernetes.io/last-applied-configuration\":\"{\\\"apiVersion\\\":\\\"v1\\\",\\\"data\\\":{\\\"ConfigMapName\\\":\\\"testconfigmap\\\"},\\\"kind\\\":\\\"ConfigMap\\\",\\\"metadata\\\":{\\\"annotations\\\":{},\\\"name\\\":\\\"kubernetes1\\\",\\\"namespace\\\":\\\"default\\\"}}\\n\"}},\"data\":{\"TestKey2\":\"UpdatedValue\"}}\n"));
            mockHttpMessageHandler
            .Expect(HttpMethod.Get, "*")
            .Respond(new StringContent("{\"kind\":\"ConfigMap\",\"apiVersion\":\"v1\",\"metadata\":{\"name\":\"testconfigmap\",\"namespace\":\"default\",\"selfLink\":\"/api/v1/namespaces/default/configmaps/testconfigmap\",\"uid\":\"8582b94c-f4fa-47fa-bacc-47019223775c\",\"resourceVersion\":\"1320622\",\"creationTimestamp\":\"2020-04-15T18:33:49Z\",\"annotations\":{\"kubectl.kubernetes.io/last-applied-configuration\":\"{\\\"apiVersion\\\":\\\"v1\\\",\\\"data\\\":{\\\"ConfigMapName\\\":\\\"testconfigmap\\\"},\\\"kind\\\":\\\"ConfigMap\\\",\\\"metadata\\\":{\\\"annotations\\\":{},\\\"name\\\":\\\"kubernetes1\\\",\\\"namespace\\\":\\\"default\\\"}}\\n\"}},\"data\":{\"TestKey3\":\"UpdatedAgain\"}}\n"));

            using var client = new k8s.Kubernetes(new KubernetesClientConfiguration { Host = "http://localhost" }, httpClient: mockHttpMessageHandler.ToHttpClient());
            var settings = new KubernetesConfigSourceSettings("default", "testconfigmap", new ReloadSettings()
            {
                Period = 1, ConfigMaps = true
            });
            var provider = new KubernetesConfigMapProvider(client, settings, new CancellationTokenSource(20000).Token);

            // act
            provider.Load();

            // assert
            Assert.True(provider.TryGet("TestKey", out var testValue), "TryGet TestKey");
            Assert.Equal("TestValue", testValue);

            while (!foundKey)
            {
                await Task.Delay(100);

                foundKey = provider.TryGet("TestKey2", out var testValue2);
                if (foundKey)
                {
                    Assert.Equal("UpdatedValue", testValue2);
                }
            }

            foundKey = false;
            while (!foundKey)
            {
                await Task.Delay(100);

                foundKey = provider.TryGet("TestKey3", out var testValue3);
                if (foundKey)
                {
                    Assert.Equal("UpdatedAgain", testValue3);
                }
            }

            Assert.False(provider.TryGet("TestKey", out _), "TryGet TestKey after update");
            Assert.False(provider.TryGet("TestKey2", out _), "TryGet TestKey2 after update");
        }
Exemple #4
0
        public void KubernetesSecretProvider_ThrowsOnNulls()
        {
            var client   = new Mock <k8s.Kubernetes>();
            var settings = new KubernetesConfigSourceSettings("default", "test", new ReloadSettings());

            var ex1 = Assert.Throws <ArgumentNullException>(() => new KubernetesSecretProvider(null, settings));
            var ex2 = Assert.Throws <ArgumentNullException>(() => new KubernetesSecretProvider(client.Object, null));

            Assert.Equal("kubernetes", ex1.ParamName);
            Assert.Equal("settings", ex2.ParamName);
        }
Exemple #5
0
        public void KubernetesConfigMapProvider_ThrowsOn403()
        {
            var mockHttpMessageHandler = new MockHttpMessageHandler();

            mockHttpMessageHandler.Expect(HttpMethod.Get, "*").Respond(HttpStatusCode.Forbidden);

            using var client = new k8s.Kubernetes(new KubernetesClientConfiguration { Host = "http://localhost" }, httpClient: mockHttpMessageHandler.ToHttpClient());
            var settings = new KubernetesConfigSourceSettings("default", "test", new ReloadSettings());
            var provider = new KubernetesConfigMapProvider(client, settings);

            var ex = Assert.Throws <HttpOperationException>(() => provider.Load());

            Assert.Equal(HttpStatusCode.Forbidden, ex.Response.StatusCode);
        }
Exemple #6
0
        public void KubernetesProviderGetsNewLoggerFactory()
        {
            // arrange
            using var client = new k8s.Kubernetes(new KubernetesClientConfiguration { Host = "http://localhost" }, new HttpClient());
            var settings = new KubernetesConfigSourceSettings("default", "testconfigmap", new ReloadSettings());
            var provider = new KubernetesConfigMapProvider(client, settings);
            var originalLoggerFactory = settings.LoggerFactory;
            var newFactory            = new LoggerFactory();

            provider.ProvideRuntimeReplacements(newFactory);

            Assert.Equal(newFactory, settings.LoggerFactory);
            Assert.NotEqual(originalLoggerFactory, settings.LoggerFactory);
        }
Exemple #7
0
        public void KubernetesProviderGetsNewLogger()
        {
            // arrange
            using var client = new k8s.Kubernetes(new KubernetesClientConfiguration { Host = "http://localhost" }, new HttpClient());
            var settings = new KubernetesConfigSourceSettings("default", "testconfigmap", new ReloadSettings());
            var provider = new KubernetesConfigMapProvider(client, settings);

            settings.LoggerFactory ??= new LoggerFactory();
            var firstLogger = settings.LoggerFactory.CreateLogger <KubernetesConfigMapProviderTest>();

            provider.ProvideRuntimeReplacements(new LoggerFactory());
            var secondLogger = settings.LoggerFactory.CreateLogger <KubernetesConfigMapProviderTest>();

            Assert.NotEqual(firstLogger.GetHashCode(), secondLogger.GetHashCode());
        }
Exemple #8
0
        public void KubernetesConfigMapProvider_SeesDoubleUnderscore()
        {
            var mockHttpMessageHandler = new MockHttpMessageHandler();

            mockHttpMessageHandler
            .Expect(HttpMethod.Get, "*")
            .Respond(new StringContent("{\"kind\":\"ConfigMap\",\"apiVersion\":\"v1\",\"metadata\":{\"name\":\"testconfigmap\",\"namespace\":\"default\",\"selfLink\":\"/api/v1/namespaces/default/configmaps/testconfigmap\",\"uid\":\"8582b94c-f4fa-47fa-bacc-47019223775c\",\"resourceVersion\":\"1320622\",\"creationTimestamp\":\"2020-04-15T18:33:49Z\",\"annotations\":{\"kubectl.kubernetes.io/last-applied-configuration\":\"{\\\"apiVersion\\\":\\\"v1\\\",\\\"data\\\":{\\\"ConfigMapName\\\":\\\"testconfigmap\\\"},\\\"kind\\\":\\\"ConfigMap\\\",\\\"metadata\\\":{\\\"annotations\\\":{},\\\"name\\\":\\\"kubernetes1\\\",\\\"namespace\\\":\\\"default\\\"}}\\n\"}},\"data\":{\"several__layers__deep__TestKey\":\"TestValue\"}}\n"));

            using var client = new k8s.Kubernetes(new KubernetesClientConfiguration { Host = "http://localhost" }, httpClient: mockHttpMessageHandler.ToHttpClient());
            var settings = new KubernetesConfigSourceSettings("default", "testconfigmap", new ReloadSettings());
            var provider = new KubernetesConfigMapProvider(client, settings);

            provider.Load();

            Assert.True(provider.TryGet("several:layers:deep:TestKey", out var testValue));
            Assert.Equal("TestValue", testValue);
        }
Exemple #9
0
        public async Task KubernetesSecretProvider_ContinuesOn404()
        {
            var mockHttpMessageHandler = new MockHttpMessageHandler();

            mockHttpMessageHandler.Expect(HttpMethod.Get, "*").Respond(HttpStatusCode.NotFound);

            using var client = new k8s.Kubernetes(new KubernetesClientConfiguration { Host = "http://localhost" }, httpClient: mockHttpMessageHandler.ToHttpClient());
            var settings = new KubernetesConfigSourceSettings("default", "test", new ReloadSettings()
            {
                ConfigMaps = true, Period = 0
            });
            var provider = new KubernetesConfigMapProvider(client, settings);

            provider.Load();
            await Task.Delay(50);

            Assert.True(provider.Polling, "Provider has begun polling");
        }
Exemple #10
0
        public void KubernetesSecretProvider_SeesDoubleUnderscore()
        {
            var mockHttpMessageHandler = new MockHttpMessageHandler();

            mockHttpMessageHandler
            .Expect(HttpMethod.Get, "*")
            .Respond(new StringContent("{\"kind\":\"Secret\",\"apiVersion\":\"v1\",\"metadata\":{\"name\":\"testsecret\",\"namespace\":\"default\",\"selfLink\":\"/api/v1/namespaces/default/secrets/testsecret\",\"uid\":\"04a256d5-5480-4e6a-ab1a-81b1df2b1f15\",\"resourceVersion\":\"724153\",\"creationTimestamp\":\"2020-04-17T14:32:42Z\",\"annotations\":{\"kubectl.kubernetes.io/last-applied-configuration\":\"{\\\"apiVersion\\\":\\\"v1\\\",\\\"data\\\":{\\\"testKey\\\":\\\"dGVzdFZhbHVl\\\"},\\\"kind\\\":\\\"Secret\\\",\\\"metadata\\\":{\\\"annotations\\\":{},\\\"name\\\":\\\"testsecret\\\",\\\"namespace\\\":\\\"default\\\"},\\\"type\\\":\\\"Opaque\\\"}\\n\"}},\"data\":{\"several__layers__deep__testKey\":\"dGVzdFZhbHVl\"},\"type\":\"Opaque\"}\n"));

            var client = new k8s.Kubernetes(new KubernetesClientConfiguration()
            {
                Host = "http://localhost"
            }, httpClient: mockHttpMessageHandler.ToHttpClient());
            var settings = new KubernetesConfigSourceSettings("default", "testsecret", new ReloadSettings());
            var provider = new KubernetesSecretProvider(client, settings);

            provider.Load();

            Assert.True(provider.TryGet("several:layers:deep:testKey", out var testValue));
            Assert.Equal("testValue", testValue);
        }