Example #1
0
        public void TestMetadata()
        {
            // test getters on null metadata
            var pod = new V1Pod();

            Assert.Null(pod.Annotations());
            Assert.Null(pod.ApiGroup());
            var(g, v) = pod.ApiGroupAndVersion();
            Assert.Null(g);
            Assert.Null(v);
            Assert.Null(pod.ApiGroupVersion());
            Assert.Null(pod.CreationTimestamp());
            Assert.Null(pod.DeletionTimestamp());
            Assert.Null(pod.Finalizers());
            Assert.Equal(-1, pod.FindOwnerReference(r => true));
            Assert.Null(pod.Generation());
            Assert.Null(pod.GetAnnotation("x"));
            Assert.Null(pod.GetController());
            Assert.Null(pod.GetLabel("x"));
            Assert.Null(pod.GetOwnerReference(r => true));
            Assert.False(pod.HasFinalizer("x"));
            Assert.Null(pod.Labels());
            Assert.Null(pod.Name());
            Assert.Null(pod.Namespace());
            Assert.Null(pod.OwnerReferences());
            Assert.Null(pod.ResourceVersion());
            Assert.Null(pod.Uid());
            Assert.Null(pod.Metadata);

            // test API version stuff
            pod = new V1Pod()
            {
                ApiVersion = "v1"
            };
            Assert.Equal("", pod.ApiGroup());
            (g, v) = pod.ApiGroupAndVersion();
            Assert.Equal("", g);
            Assert.Equal("v1", v);
            Assert.Equal("v1", pod.ApiGroupVersion());
            pod.ApiVersion = "abc/v2";
            Assert.Equal("abc", pod.ApiGroup());
            (g, v) = pod.ApiGroupAndVersion();
            Assert.Equal("abc", g);
            Assert.Equal("v2", v);
            Assert.Equal("v2", pod.ApiGroupVersion());

            // test the Ensure*() functions
            Assert.NotNull(pod.EnsureMetadata());
            Assert.NotNull(pod.Metadata);
            Assert.NotNull(pod.Metadata.EnsureAnnotations());
            Assert.NotNull(pod.Metadata.Annotations);
            Assert.NotNull(pod.Metadata.EnsureFinalizers());
            Assert.NotNull(pod.Metadata.Finalizers);
            Assert.NotNull(pod.Metadata.EnsureLabels());
            Assert.NotNull(pod.Metadata.Labels);

            // test getters with non-null values
            DateTime ts = DateTime.UtcNow, ts2 = DateTime.Now;

            pod.Metadata = new V1ObjectMeta()
            {
                CreationTimestamp = ts,
                DeletionTimestamp = ts2,
                Generation        = 1,
                Name = "name",
                NamespaceProperty = "ns",
                ResourceVersion   = "42",
                Uid = "id"
            };
            Assert.Equal(ts, pod.CreationTimestamp().Value);
            Assert.Equal(ts2, pod.DeletionTimestamp().Value);
            Assert.Equal(1, pod.Generation().Value);
            Assert.Equal("name", pod.Name());
            Assert.Equal("ns", pod.Namespace());
            Assert.Equal("42", pod.ResourceVersion());
            Assert.Equal("id", pod.Uid());

            // test annotations and labels
            pod.SetAnnotation("x", "y");
            pod.SetLabel("a", "b");
            Assert.Equal(1, pod.Annotations().Count);
            Assert.Equal(1, pod.Labels().Count);
            Assert.Equal("y", pod.GetAnnotation("x"));
            Assert.Equal("y", pod.Metadata.Annotations["x"]);
            Assert.Null(pod.GetAnnotation("a"));
            Assert.Equal("b", pod.GetLabel("a"));
            Assert.Equal("b", pod.Metadata.Labels["a"]);
            Assert.Null(pod.GetLabel("x"));
            pod.SetAnnotation("x", null);
            Assert.Equal(0, pod.Annotations().Count);
            pod.SetLabel("a", null);
            Assert.Equal(0, pod.Labels().Count);

            // test finalizers
            Assert.False(pod.HasFinalizer("abc"));
            Assert.True(pod.AddFinalizer("abc"));
            Assert.True(pod.HasFinalizer("abc"));
            Assert.False(pod.AddFinalizer("abc"));
            Assert.False(pod.HasFinalizer("xyz"));
            Assert.False(pod.RemoveFinalizer("xyz"));
            Assert.True(pod.RemoveFinalizer("abc"));
            Assert.False(pod.HasFinalizer("abc"));
            Assert.False(pod.RemoveFinalizer("abc"));
        }