Exemplo n.º 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());
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
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"));
        }
Exemplo n.º 4
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)"));
        }
Exemplo n.º 5
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());
        }
Exemplo n.º 6
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());
        }
Exemplo n.º 7
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
        }
Exemplo n.º 8
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
        }