Пример #1
0
        public void Multiple()
        {
            LabelSelector <TestItem> selector;

            var items = new List <TestItem>();

            // Test multple selector conditions.

            items.Clear();
            items.Add(new TestItem("one",
                                   new KeyValuePair <string, string>("test", "aa"),
                                   new KeyValuePair <string, string>("cloud", "false"),
                                   new KeyValuePair <string, string>("onpremise", "true")));
            items.Add(new TestItem("two",
                                   new KeyValuePair <string, string>("test", "bb"),
                                   new KeyValuePair <string, string>("cloud", "true"),
                                   new KeyValuePair <string, string>("foobar", "true"),
                                   new KeyValuePair <string, string>("onpremise", "true")));

            selector = new LabelSelector <TestItem>(items);

            Assert.Empty(selector.Select("test==aa,test=foo"));
            Assert.Single(selector.Select("test==aa,onpremise=true"));
            Assert.Equal(2, selector.Select("test in (aa, bb),onpremise=true").Count());
        }
Пример #2
0
        public void Constraints_Explicit()
        {
            // Verify that explicit Kubernetes client label checks work.

            var maxDnsLabel = new string('a', 63);
            var maxName     = new string('z', 63);
            var maxValue    = new string('b', 63);
            var maxDnsName  = new string('c', 63) + '.' + new string('d', 63) + '.' + new string('e', 63) + '.' + new string('f', 61);

            Assert.Equal(253, maxDnsName.Length);

            // Verify label keys

            LabelSelector.ValidateLabelKey($"{maxDnsName}/test");                                                // This is OK
            LabelSelector.ValidateLabelKey($"a.b.c/test");                                                       // This is OK
            LabelSelector.ValidateLabelKey($"0.test-foo.com/test");                                              // This is OK
            LabelSelector.ValidateLabelKey($"0.test_foo.com/test");                                              // This is OK
            LabelSelector.ValidateLabelKey($"0.test_foo.com/{maxName}");                                         // This is OK
            Assert.Throws <FormatException>(() => LabelSelector.ValidateLabelKey($"_test.com/test"));            // Invalid first prefix character
            Assert.Throws <FormatException>(() => LabelSelector.ValidateLabelKey($"test.com_/test"));            // Invalid last prefix character
            Assert.Throws <FormatException>(() => LabelSelector.ValidateLabelKey($"{maxDnsName}g/test"));        // Prefix too long
            Assert.Throws <FormatException>(() => LabelSelector.ValidateLabelKey($"a{maxDnsName}/test"));        // DNS label too long
            Assert.Throws <FormatException>(() => LabelSelector.ValidateLabelKey($"com/test"));                  // Not enough DNS labels
            Assert.Throws <FormatException>(() => LabelSelector.ValidateLabelKey($"test..com/test"));            // Missing DNS label
            Assert.Throws <FormatException>(() => LabelSelector.ValidateLabelKey($"0.test_foo.com/{maxName}q")); // Name part is too long

            // Verify label values

            LabelSelector.ValidateLabelValue($"{maxValue}");                                                    // This is OK
            LabelSelector.ValidateLabelValue($"0123456789_abcdefghijklmnopqrstuvwxyz.test_test");               // This is OK
            LabelSelector.ValidateLabelValue($"f0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZ.TEST_TEST");              // This is OK
            Assert.Throws <FormatException>(() => LabelSelector.ValidateLabelValue($"{maxValue}g"));            // Value is too long
            Assert.Throws <FormatException>(() => LabelSelector.ValidateLabelValue($"*"));                      // Value has invalid character
        }
Пример #3
0
        public void NotEqual()
        {
            LabelSelector <TestItem> selector;

            var items = new List <TestItem>();

            // Test NULL items

            selector = new LabelSelector <TestItem>(items);

            Assert.Empty(selector.Select("test != true"));
            Assert.Empty(selector.Select("\ttest\r\n!=true"));
            Assert.Empty(selector.Select("test!=true"));

            // Test empty items

            selector = new LabelSelector <TestItem>(items);

            Assert.Empty(selector.Select("test != true"));
            Assert.Empty(selector.Select("test!=true"));
            Assert.Empty(selector.Select("test!=true"));

            // Test selecting from a single item

            items.Clear();
            items.Add(new TestItem("one",
                                   new KeyValuePair <string, string>("test", "true"),
                                   new KeyValuePair <string, string>("cloud", "false"),
                                   new KeyValuePair <string, string>("onpremise", "true")));

            selector = new LabelSelector <TestItem>(items);

            Assert.Single(selector.Select("test != false"));
            Assert.Empty(selector.Select("test != true"));
            Assert.Single(selector.Select("test!=false"));
            Assert.Empty(selector.Select("test!=true"));

            Assert.Equal("one", selector.Select("test=true").First().Name);

            // Test selecting from a multiple items

            selector = new LabelSelector <TestItem>(items);

            items.Clear();
            items.Add(new TestItem("one",
                                   new KeyValuePair <string, string>("test", "true"),
                                   new KeyValuePair <string, string>("cloud", "false"),
                                   new KeyValuePair <string, string>("onpremise", "true")));
            items.Add(new TestItem("two",
                                   new KeyValuePair <string, string>("test", "false"),
                                   new KeyValuePair <string, string>("cloud", "true"),
                                   new KeyValuePair <string, string>("onpremise", "true")));

            Assert.Single(selector.Select("test != true"));
            Assert.Single(selector.Select("test!=true"));

            Assert.Equal("one", selector.Select("test!=false").First().Name);
            Assert.Equal("two", selector.Select("test!=true").First().Name);
        }
Пример #4
0
        public void Constraints_Enabled()
        {
            LabelSelector <TestItem> selector;

            var items = new List <TestItem>();

            selector = new LabelSelector <TestItem>(items);

            items.Clear();
            items.Add(new TestItem("one",
                                   new KeyValuePair <string, string>("neonkube.com/test", "true")));
            items.Add(new TestItem("two",
                                   new KeyValuePair <string, string>("neonkube.com/test", "false")));

            Assert.Single(selector.Select("neonkube.com/test == false"));

            Assert.Equal("one", selector.Select("neonkube.com/test=true").First().Name);
            Assert.Equal("two", selector.Select("neonkube.com/test=false").First().Name);

            // Verify that we can various kinds of invalid label keys.

            Assert.Throws <FormatException>(() => selector.Select("/==true"));
            Assert.Throws <FormatException>(() => selector.Select("/test==true"));
            Assert.Throws <FormatException>(() => selector.Select("com/test==true"));
            Assert.Throws <FormatException>(() => selector.Select("*test.com/test==true"));
            Assert.Throws <FormatException>(() => selector.Select("test.com*/test==true"));

            var maxDnsLabel = new string('a', 63);
            var maxName     = new string('z', 63);
            var maxValue    = new string('b', 63);
            var maxDnsName  = new string('c', 63) + '.' + new string('d', 63) + '.' + new string('e', 63) + '.' + new string('f', 61);

            Assert.Equal(253, maxDnsName.Length);

            // Verify label keys

            selector.Select($"{maxDnsName}/test==true");                                                    // This is OK
            selector.Select($"a.b.c/test==true");                                                           // This is OK
            selector.Select($"0.test-foo.com/test==true");                                                  // This is OK
            selector.Select($"0.test_foo.com/test==true");                                                  // This is OK
            selector.Select($"0.test_foo.com/{maxName}==true");                                             // This is OK
            Assert.Throws <FormatException>(() => selector.Select($"_test.com/test==true"));                // Invalid first character
            Assert.Throws <FormatException>(() => selector.Select($"test.com_/test==true"));                // Invalid last character
            Assert.Throws <FormatException>(() => selector.Select($"{maxDnsName}g/test==true"));            // DNS name too long
            Assert.Throws <FormatException>(() => selector.Select($"a{maxDnsName}/test==true"));            // DNS label too long
            Assert.Throws <FormatException>(() => selector.Select($"com/test==true"));                      // Not enough DNS labels
            Assert.Throws <FormatException>(() => selector.Select($"test..com/test==true"));                // Missing DNS label
            Assert.Throws <FormatException>(() => selector.Select($"0.test_foo.com/{maxName}q==true"));     // Name part is too long

            // Verify label values

            selector.Select($"foo.com/test=={maxValue}");                                                   // This is OK
            selector.Select($"foo.com/test==0123456789_abcdefghijklmnopqrstuvwxyz.test_test");              // This is OK
            selector.Select($"foo.com/test==0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZ.TEST_TEST");              // This is OK
            selector.Select($"foo.com/test=={maxValue}");                                                   // This is OK
            Assert.Throws <FormatException>(() => selector.Select($"foo.com/test=={maxValue}g"));           // Value is too long
            Assert.Throws <FormatException>(() => selector.Select($"foo.com/test==*"));                     // Value has invalid character
        }
Пример #5
0
        public async Task Session_CreatesAndDeletesPod_Async()
        {
            const string name = "session-createsanddeletespod-async";

            Debug.Assert(FormatName(nameof(this.Session_CreatesAndDeletesPod_Async)) == name, "Use naming conventions");

            var configuration = new ChildOperatorConfiguration(name)
            {
                ParentLabelSelector = LabelSelector.Create <WebDriverSession>(
                    s => s.Metadata.Labels[Annotations.ManagedBy] == name &&
                    s.Metadata.Labels[Annotations.AutomationName] == Annotations.AutomationNames.Fake),
            };

            var podCreated = new TaskCompletionSource <V1Pod>();
            var podRunning = new TaskCompletionSource <V1Pod>();
            var podDeleted = new TaskCompletionSource <V1Pod>();

            var kubernetes = this.host.Services.GetRequiredService <KubernetesClient>();
            var podClient  = kubernetes.GetClient <V1Pod>();

            var sessionClient = kubernetes.GetClient <WebDriverSession>();
            var logger        = this.host.Services.GetRequiredService <ILoggerFactory>().CreateLogger(name);

            // Delete all objects which may have been previously created by this test.
            await Task.WhenAll(
                sessionClient.TryDeleteAsync($"{name}-empty", TimeSpan.FromMinutes(1), default),
                sessionClient.TryDeleteAsync($"{name}-fake", TimeSpan.FromMinutes(1), default),
                podClient.TryDeleteAsync($"{name}-empty", TimeSpan.FromMinutes(1), default),
                podClient.TryDeleteAsync($"{name}-fake", TimeSpan.FromMinutes(1), default)).ConfigureAwait(false);

            var podWatcher =
                podClient.WatchAsync(
                    fieldSelector: null,
                    labelSelector: $"{Annotations.ManagedBy}={name}",
                    resourceVersion: null,
                    resourceVersionMatch: null,
                    (eventType, pod) =>
            {
                logger.LogInformation($"Got an added {eventType}  event for pod {pod}", eventType, pod.Metadata.Name);
                switch (eventType)
                {
                case k8s.WatchEventType.Added:
                    podCreated.TrySetResult(pod);
                    break;

                case k8s.WatchEventType.Modified when pod.Status.Phase == "Running" && pod.Status.ContainerStatuses.All(c => c.Ready):
                    podRunning.TrySetResult(pod);
                    break;

                case k8s.WatchEventType.Deleted:
                    podDeleted.TrySetResult(pod);
                    return(Task.FromResult(WatchResult.Stop));
                }

                return(Task.FromResult(WatchResult.Continue));
            },
Пример #6
0
 public void FromDictionary_SingleValue_ReturnsSelector()
 {
     Assert.Equal(
         "app.kubernetes.io/managed-by=test",
         LabelSelector.Create(
             new Dictionary <string, string>()
     {
         { Annotations.ManagedBy, "test" },
     }));
 }
Пример #7
0
        public void NotHas()
        {
            LabelSelector <TestItem> selector;

            var items = new List <TestItem>();

            // Test NULL items

            selector = new LabelSelector <TestItem>(items);

            Assert.Empty(selector.Select("!test"));
            Assert.Empty(selector.Select("! test"));
            Assert.Empty(selector.Select("\t!test\r\n"));

            // Test empty items

            selector = new LabelSelector <TestItem>(items);

            Assert.Empty(selector.Select("!test"));
            Assert.Empty(selector.Select("! test"));
            Assert.Empty(selector.Select("\t!test\r\n"));

            // Test selecting from a single item

            items.Clear();
            items.Add(new TestItem("one",
                                   new KeyValuePair <string, string>("test", "true"),
                                   new KeyValuePair <string, string>("cloud", "false"),
                                   new KeyValuePair <string, string>("onpremise", "true")));

            selector = new LabelSelector <TestItem>(items);

            Assert.Single(selector.Select("!foo"));
            Assert.Empty(selector.Select("!onpremise"));

            // Test selecting from a multiple items

            selector = new LabelSelector <TestItem>(items);

            items.Clear();
            items.Add(new TestItem("one",
                                   new KeyValuePair <string, string>("test", "true"),
                                   new KeyValuePair <string, string>("cloud", "false"),
                                   new KeyValuePair <string, string>("onpremise", "true")));
            items.Add(new TestItem("two",
                                   new KeyValuePair <string, string>("test", "false"),
                                   new KeyValuePair <string, string>("cloud", "true"),
                                   new KeyValuePair <string, string>("foobar", "true"),
                                   new KeyValuePair <string, string>("onpremise", "true")));

            Assert.Equal(2, selector.Select("!foo").Count());
            Assert.Single(selector.Select("!foobar"));
            Assert.Empty(selector.Select("!onpremise"));
        }
Пример #8
0
 public void FromDictionary_TwoValues_ReturnsSelector()
 {
     Assert.Equal(
         "app.kubernetes.io/managed-by=test,kubernetes.io/arch=arm64",
         LabelSelector.Create(
             new Dictionary <string, string>()
     {
         { Annotations.ManagedBy, "test" },
         { Annotations.Arch, "arm64" },
     }));
 }
Пример #9
0
        public void Constraints_Disabled()
        {
            LabelSelector <TestItem> selector;

            var items = new List <TestItem>();

            // Verify selectors with valid Kubernetes label keys still works in unconstrained mode.

            selector = new LabelSelector <TestItem>(items, LabelSelectorOptions.UnConstraintedLabels);

            items.Clear();
            items.Add(new TestItem("one",
                                   new KeyValuePair <string, string>("neonKUBE.com/test", "true")));
            items.Add(new TestItem("two",
                                   new KeyValuePair <string, string>("neonKUBE.com/test", "false")));

            Assert.Single(selector.Select("neonKUBE.com/test == false"));

            Assert.Equal("one", selector.Select("neonKUBE.com/test=true").First().Name);
            Assert.Equal("two", selector.Select("neonKUBE.com/test=false").First().Name);

            var maxDnsLabel = new string('a', 63);
            var maxName     = new string('z', 63);
            var maxValue    = new string('b', 63);
            var maxDnsName  = new string('c', 63) + '.' + new string('d', 63) + '.' + new string('e', 63) + '.' + new string('f', 61);

            Assert.Equal(253, maxDnsName.Length);

            // Verify label keys

            selector.Select($"{maxDnsName}/test==true");                                                        // This is OK
            selector.Select($"a.b.c/test==true");                                                               // This is OK
            selector.Select($"0.test-foo.com/test==true");                                                      // This is OK
            selector.Select($"0.test_foo.com/test==true");                                                      // This is OK
            selector.Select($"0.test_foo.com/{maxName}==true");                                                 // This is OK
            selector.Select($"_test.com/test==true");                                                           // Invalid first character
            selector.Select($"test.com_/test==true");                                                           // Invalid last character
            selector.Select($"{maxDnsName}g/test==true");                                                       // DNS name too long
            selector.Select($"a{maxDnsName}/test==true");                                                       // DNS label too long
            selector.Select($"com/test==true");                                                                 // Not enough DNS labels
            selector.Select($"test..com/test==true");                                                           // Missing DNS label
            selector.Select($"0.test_foo.com/{maxName}q==true");                                                // Name part is too long

            // Verify label values

            selector.Select($"foo.com/test=={maxValue}");                                                       // This is OK
            selector.Select($"foo.com/test==0123456789_abcdefghijklmnopqrstuvwxyz.test_test");                  // This is OK
            selector.Select($"foo.com/test==0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZ.TEST_TEST");                  // This is OK
            selector.Select($"foo.com/test=={maxValue}");                                                       // This is OK
            selector.Select($"foo.com/test=={maxValue}g");                                                      // Value is too long
            selector.Select($"foo.com/test==*");                                                                // Value has invalid character
        }
Пример #10
0
        public void NotIn()
        {
            LabelSelector <TestItem> selector;

            var items = new List <TestItem>();

            // Test NULL items

            selector = new LabelSelector <TestItem>(items);

            Assert.Empty(selector.Select("test in (aaa)"));
            Assert.Empty(selector.Select("test in\t\r\n(aaa)"));

            // Test empty items

            selector = new LabelSelector <TestItem>(items);

            Assert.Empty(selector.Select("test in (aaa)"));
            Assert.Empty(selector.Select("test in\t\r\n(aaa)"));

            // Test selecting from a single item

            items.Clear();
            items.Add(new TestItem("one",
                                   new KeyValuePair <string, string>("test", "aa"),
                                   new KeyValuePair <string, string>("cloud", "false"),
                                   new KeyValuePair <string, string>("onpremise", "true")));

            selector = new LabelSelector <TestItem>(items);

            Assert.Single(selector.Select("test notin (cc)"));
            Assert.Empty(selector.Select("test notin (aa)"));

            // Test selecting from a multiple items

            selector = new LabelSelector <TestItem>(items);

            items.Clear();
            items.Add(new TestItem("one",
                                   new KeyValuePair <string, string>("test", "aa"),
                                   new KeyValuePair <string, string>("cloud", "false"),
                                   new KeyValuePair <string, string>("onpremise", "true")));
            items.Add(new TestItem("two",
                                   new KeyValuePair <string, string>("test", "bb"),
                                   new KeyValuePair <string, string>("cloud", "true"),
                                   new KeyValuePair <string, string>("foobar", "true"),
                                   new KeyValuePair <string, string>("onpremise", "true")));

            Assert.Single(selector.Select("test notin (aa)"));
            Assert.Equal(2, selector.Select("test notin (cc, dd)").Count());
            Assert.Empty(selector.Select("test notin (aa, bb)"));
        }
Пример #11
0
        public void CaseInsensitive_Labels()
        {
            LabelSelector <TestItemCaseInsensitive> selector;

            var items = new List <TestItemCaseInsensitive>();

            selector = new LabelSelector <TestItemCaseInsensitive>(items);

            items.Clear();
            items.Add(new TestItemCaseInsensitive("one",
                                                  new KeyValuePair <string, string>("test", "true"),
                                                  new KeyValuePair <string, string>("cloud", "false"),
                                                  new KeyValuePair <string, string>("onpremise", "true")));
            items.Add(new TestItemCaseInsensitive("two",
                                                  new KeyValuePair <string, string>("TEST", "false"),
                                                  new KeyValuePair <string, string>("CLOUD", "true"),
                                                  new KeyValuePair <string, string>("ONPREMISE", "true")));

            Assert.Single(selector.Select("test == false"));
            Assert.Single(selector.Select("TEST == false"));
            Assert.Equal(2, selector.Select("test in (true,false)").Count());
            Assert.Equal(2, selector.Select("TEST in (true,false)").Count());
        }
Пример #12
0
        public void CaseInsensitive_Values()
        {
            LabelSelector <TestItem> selector;

            var items = new List <TestItem>();

            selector = new LabelSelector <TestItem>(items, LabelSelectorOptions.CaseInsensitiveValues);

            items.Clear();
            items.Add(new TestItem("one",
                                   new KeyValuePair <string, string>("test", "true"),
                                   new KeyValuePair <string, string>("cloud", "false"),
                                   new KeyValuePair <string, string>("onpremise", "true")));
            items.Add(new TestItem("two",
                                   new KeyValuePair <string, string>("test", "FALSE"),
                                   new KeyValuePair <string, string>("cloud", "TRUE"),
                                   new KeyValuePair <string, string>("onpremise", "TRUE")));

            Assert.Single(selector.Select("test == false"));
            Assert.Single(selector.Select("test == FALSE"));
            Assert.Equal(2, selector.Select("test in (TRUE,false)").Count());
            Assert.Equal(2, selector.Select("test in (true,FALSE)").Count());
        }
Пример #13
0
 public void Create_MethodCallOnWrongType_ReturnsNull()
 {
     Assert.Null(LabelSelector.Create <V1Pod>(p => p.Metadata.Labels.ToString() == "a"));
 }
Пример #14
0
 public void FromDictionary_EmptyDictionary_ReturnsEmptySelector()
 {
     Assert.Equal(string.Empty, LabelSelector.Create(new Dictionary <string, string>()));
 }
Пример #15
0
 public void FromDictionary_ValidatesArgument()
 {
     Assert.Throws <ArgumentNullException>("values", () => LabelSelector.Create((Dictionary <string, string>)null));
 }
Пример #16
0
 public void NotAKubeType_ReturnsNull()
 {
     Assert.Null(LabelSelector.Create <CustomObject>(s => s.Child.Metadata.Labels[Annotations.ManagedBy] == "test"));
 }
Пример #17
0
 public void Create_CustomType_Works()
 {
     Assert.Equal("app.kubernetes.io/managed-by=test", LabelSelector.Create <WebDriverSession>(s => s.Metadata.Labels[Annotations.ManagedBy] == "test"));
 }
Пример #18
0
 public void Create_TwoLabels_Works()
 {
     Assert.Equal("kubernetes.io/os=android,kubernetes.io/arch=arm64", LabelSelector.Create <V1Pod>(
                      p => p.Metadata.Labels[Annotations.Os] == Annotations.OperatingSystem.Android &&
                      p.Metadata.Labels[Annotations.Arch] == Annotations.Architecture.Arm64));
 }
Пример #19
0
 public void Create_NoLabel_ReturnsNull()
 {
     Assert.Null(LabelSelector.Create <V1Pod>(p => p.Spec.Subdomain == "fake"));
 }
Пример #20
0
 public void Create_AlwaysFalse_Throws()
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => LabelSelector.Create <V1Pod>(s => false));
 }
Пример #21
0
 public void Create_Null_ReturnsNull()
 {
     Assert.Null(LabelSelector.Create <V1Pod>(null));
 }
Пример #22
0
 public void Create_ValueNotConstant_Throws()
 {
     Assert.Throws <ArgumentOutOfRangeException>(
         () => LabelSelector.Create <V1Pod>(
             p => p.Metadata.Labels[Annotations.Os] == p.ToString()));
 }
Пример #23
0
 public void Create_AlwaysTrue_ReturnsNull()
 {
     Assert.Null(LabelSelector.Create <V1Pod>(s => true));
 }
Пример #24
0
 public void Create_MethodCallOnWrongProperty_ReturnsNull()
 {
     Assert.Null(LabelSelector.Create <V1Pod>(p => p.Spec.NodeSelector["a"] == "a"));
 }
Пример #25
0
 public void Create_Complex_Throws()
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => LabelSelector.Create <V1Pod>(s => s.HasFinalizer("test")));
 }
Пример #26
0
 public void Create_LabelOnNonParam_ReturnsNull()
 {
     Assert.Null(LabelSelector.Create <V1Pod>(p => new V1Pod().Metadata.Labels["a"] == "b"));
 }
Пример #27
0
 public void Create_SingleLabel_Works()
 {
     Assert.Equal("kubernetes.io/os=android", LabelSelector.Create <V1Pod>(p => p.Metadata.Labels[Annotations.Os] == Annotations.OperatingSystem.Android));
 }
Пример #28
0
 public void Create_MethodCallOnWrongProperty2_ReturnsNull()
 {
     Assert.Null(LabelSelector.Create <V1Pod>(p => p.Metadata.ManagedFields.ToString() == "b"));
 }
Пример #29
0
 public void Create_ThreeLabels_Works()
 {
     Assert.Equal("kubernetes.io/os=android,kubernetes.io/arch=arm64,app.kubernetes.io/managed-by=my-operator", LabelSelector.Create <V1Pod>(
                      p => p.Metadata.Labels[Annotations.Os] == Annotations.OperatingSystem.Android &&
                      p.Metadata.Labels[Annotations.Arch] == Annotations.Architecture.Arm64 &&
                      p.Metadata.Labels[Annotations.ManagedBy] == "my-operator"));
 }
Пример #30
0
 public void Create_LabelNameNotConstant_ReturnsNull()
 {
     Assert.Null(LabelSelector.Create <V1Pod>(p => p.Metadata.Labels["a".ToString()] == "b"));
 }