Esempio n. 1
0
        public async Task ReconcileAsync_FeedbackLoopReturnsNull_DoesNothing_Async()
        {
            var kubernetes = new Mock <KubernetesClient>();
            var sessions   = kubernetes.WithClient <WebDriverSession>();
            var pods       = kubernetes.WithClient <V1Pod>();

            Feedback <WebDriverSession, V1Pod> feedback = null;

            var parent = new WebDriverSession();
            var child  = new V1Pod();

            var feedbackLoops = new Collection <FeedbackLoop <WebDriverSession, V1Pod> >()
            {
                new FeedbackLoop <WebDriverSession, V1Pod>((context, cancellationToken) =>
                {
                    return(Task.FromResult(feedback));
                }),
            };

            using (var @operator = new ChildOperator <WebDriverSession, V1Pod>(
                       kubernetes.Object,
                       this.configuration,
                       this.filter,
                       (session, pod) => { },
                       feedbackLoops,
                       this.logger,
                       this.services))
            {
                await @operator.ReconcileAsync(parent, child, default).ConfigureAwait(false);
            }

            sessions.Verify();
        }
Esempio n. 2
0
        public async Task ReconcileAsync_Exception_LogsError_Async()
        {
            var kubernetes = new Mock <KubernetesClient>();
            var sessions   = kubernetes.WithClient <WebDriverSession>();
            var pods       = kubernetes.WithClient <V1Pod>();

            pods.Setup(p => p.CreateAsync(It.IsAny <V1Pod>(), default)).ThrowsAsync(new NotSupportedException());

            var loggerFactory = this.services.GetRequiredService <ILoggerFactory>();
            var sink          = this.services.GetRequiredService <ITestLoggerSink>();

            using (var @operator = new ChildOperator <WebDriverSession, V1Pod>(
                       kubernetes.Object,
                       this.configuration,
                       this.filter,
                       (session, pod) => { },
                       this.feedbackLoops,
                       loggerFactory.CreateLogger <ChildOperator <WebDriverSession, V1Pod> >(),
                       this.services))
            {
                var parent = new WebDriverSession()
                {
                    Metadata = new V1ObjectMeta(),
                };

                await @operator.ReconcileAsync(parent, null, default).ConfigureAwait(false);
            }

            Assert.Collection(
                sink.LogEntries,
                e => Assert.Equal("Scheduling reconciliation for parent (null) and child (null) for operator ChildOperatorTests", e.Message),
                e => Assert.Equal("Caught error Specified method is not supported. while executing reconciliation for operator ChildOperatorTests", e.Message));
        }
Esempio n. 3
0
        public async Task ReconcileAsync_Child_NoOp_Async()
        {
            var kubernetes = new Mock <KubernetesClient>();
            var sessions   = kubernetes.WithClient <WebDriverSession>();
            var pods       = kubernetes.WithClient <V1Pod>();

            using (var @operator = new ChildOperator <WebDriverSession, V1Pod>(
                       kubernetes.Object,
                       this.configuration,
                       this.filter,
                       (session, pod) =>
            {
                pod.Spec = new V1PodSpec(
                    new V1Container[]
                {
                    new V1Container()
                    {
                        Name = "fake-driver",
                        Image = "quay.io/kaponata/fake-driver:2.0.1",
                    },
                });
            },
                       this.feedbackLoops,
                       this.logger,
                       this.services))
            {
                var parent = new WebDriverSession()
                {
                    Metadata = new V1ObjectMeta()
                    {
                        Name = "my-session",
                        NamespaceProperty = "default",
                        Uid = "my-uid",
                    },
                };

                var child = new V1Pod()
                {
                    Metadata = new V1ObjectMeta()
                    {
                        Name = "my-session",
                        NamespaceProperty = "default",
                    },
                };

                await @operator.ReconcileAsync(
                    parent, child, default).ConfigureAwait(false);
            }
        }
Esempio n. 4
0
        public async Task ReconcileAsync_FeedbackLoopHasChildPatch_ExecutesPatch_Async()
        {
            var kubernetes = new Mock <KubernetesClient>();
            var sessions   = kubernetes.WithClient <WebDriverSession>();
            var pods       = kubernetes.WithClient <V1Pod>();

            var feedback = new Feedback <WebDriverSession, V1Pod>();

            feedback.ChildFeedback = new JsonPatchDocument <V1Pod>();
            feedback.ChildFeedback.Add(s => s.Status, new V1PodStatus());

            var parent = new WebDriverSession();
            var child  = new V1Pod();

            pods.Setup(p => p.PatchAsync(child, feedback.ChildFeedback, default))
            .ReturnsAsync(child)
            .Verifiable();

            var feedbackLoops = new Collection <FeedbackLoop <WebDriverSession, V1Pod> >()
            {
                new FeedbackLoop <WebDriverSession, V1Pod>((context, cancellationToken) =>
                {
                    return(Task.FromResult(feedback));
                }),
            };

            using (var @operator = new ChildOperator <WebDriverSession, V1Pod>(
                       kubernetes.Object,
                       this.configuration,
                       this.filter,
                       (session, pod) => { },
                       feedbackLoops,
                       this.logger,
                       this.services))
            {
                await @operator.ReconcileAsync(parent, child, default).ConfigureAwait(false);
            }

            sessions.Verify();
        }
Esempio n. 5
0
        public async Task ReconcileAsync_FeedbackLoop_IsPassedScopedServices_Async()
        {
            var kubernetes = new Mock <KubernetesClient>();
            var sessions   = kubernetes.WithClient <WebDriverSession>();
            var pods       = kubernetes.WithClient <V1Pod>();

            var feedbackLoops = new Collection <FeedbackLoop <WebDriverSession, V1Pod> >()
            {
                new FeedbackLoop <WebDriverSession, V1Pod>((context, cancellationToken) =>
                {
                    Assert.NotNull(context.Services);

                    // Get a scope service, and assert that the scoped service passed to the feedback
                    // loop is different than the scoped service at the root.
                    var value     = context.Services.GetRequiredService <NamespacedKubernetesClient <V1Job> >();
                    var rootValue = this.services.GetRequiredService <NamespacedKubernetesClient <V1Job> >();
                    Assert.NotSame(rootValue, value);

                    return(Task.FromResult((Feedback <WebDriverSession, V1Pod>)null));
                }),
            };

            var parent = new WebDriverSession();
            var child  = new V1Pod();

            using (var @operator = new ChildOperator <WebDriverSession, V1Pod>(
                       kubernetes.Object,
                       this.configuration,
                       this.filter,
                       (session, pod) => { },
                       feedbackLoops,
                       this.logger,
                       this.services))
            {
                await @operator.ReconcileAsync(parent, child, default).ConfigureAwait(false);
            }

            sessions.Verify();
        }
Esempio n. 6
0
        public async Task ReconcileAsync_NoChild_CreatesChild_Async()
        {
            var kubernetes = new Mock <KubernetesClient>();
            var sessions   = kubernetes.WithClient <WebDriverSession>();
            var pods       = kubernetes.WithClient <V1Pod>();

            (var createdPods, var _) = pods.TrackCreatedItems();

            using (var @operator = new ChildOperator <WebDriverSession, V1Pod>(
                       kubernetes.Object,
                       this.configuration,
                       this.filter,
                       (session, pod) =>
            {
                pod.Spec = new V1PodSpec(
                    new V1Container[]
                {
                    new V1Container()
                    {
                        Name = "fake-driver",
                        Image = "quay.io/kaponata/fake-driver:2.0.1",
                    },
                });
            },
                       this.feedbackLoops,
                       this.logger,
                       this.services))
            {
                var parent = new WebDriverSession()
                {
                    Metadata = new V1ObjectMeta()
                    {
                        Name = "my-session",
                        NamespaceProperty = "default",
                        Uid = "my-uid",
                    },
                };

                await @operator.ReconcileAsync(parent, null, default).ConfigureAwait(false);
            }

            Assert.Collection(
                createdPods,
                p =>
            {
                // The name, namespace and owner references are initialized correctly
                Assert.Equal("my-session", p.Metadata.Name);
                Assert.Equal("default", p.Metadata.NamespaceProperty);

                Assert.Collection(
                    p.Metadata.OwnerReferences,
                    r =>
                {
                    Assert.Equal("my-session", r.Name);
                    Assert.Equal("WebDriverSession", r.Kind);
                    Assert.Equal("kaponata.io/v1alpha1", r.ApiVersion);
                    Assert.Equal("my-uid", r.Uid);
                });

                // The labels are copied from the configuration
                Assert.Collection(
                    p.Metadata.Labels,
                    p =>
                {
                    Assert.Equal(Annotations.ManagedBy, p.Key);
                    Assert.Equal(nameof(ChildOperatorTests), p.Value);
                });
            });
        }