gyfora commented on code in PR #311: URL: https://github.com/apache/flink-kubernetes-operator/pull/311#discussion_r918283173
########## flink-kubernetes-operator/src/test/java/org/apache/flink/kubernetes/operator/metrics/lifecycle/ResourceLifecycleMetricsTest.java: ########## @@ -0,0 +1,150 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.kubernetes.operator.metrics.lifecycle; + +import org.apache.flink.api.common.JobStatus; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.kubernetes.operator.TestUtils; +import org.apache.flink.kubernetes.operator.crd.spec.JobState; +import org.apache.flink.kubernetes.operator.crd.status.ReconciliationState; +import org.apache.flink.kubernetes.operator.reconciler.ReconciliationUtils; +import org.apache.flink.metrics.Histogram; +import org.apache.flink.runtime.metrics.DescriptiveStatisticsHistogram; + +import org.junit.jupiter.api.Test; + +import java.time.Instant; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import static org.apache.flink.kubernetes.operator.metrics.lifecycle.ResourceLifecycleState.CREATED; +import static org.apache.flink.kubernetes.operator.metrics.lifecycle.ResourceLifecycleState.DEPLOYED; +import static org.apache.flink.kubernetes.operator.metrics.lifecycle.ResourceLifecycleState.FAILED; +import static org.apache.flink.kubernetes.operator.metrics.lifecycle.ResourceLifecycleState.ROLLED_BACK; +import static org.apache.flink.kubernetes.operator.metrics.lifecycle.ResourceLifecycleState.ROLLING_BACK; +import static org.apache.flink.kubernetes.operator.metrics.lifecycle.ResourceLifecycleState.STABLE; +import static org.apache.flink.kubernetes.operator.metrics.lifecycle.ResourceLifecycleState.SUSPENDED; +import static org.apache.flink.kubernetes.operator.metrics.lifecycle.ResourceLifecycleState.UPGRADING; +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** Test for resource lifecycle metrics. */ +public class ResourceLifecycleMetricsTest { + + @Test + public void lifecycleStateTest() { + var application = TestUtils.buildApplicationCluster(); + assertEquals(CREATED, application.getStatus().getLifecycleState()); + + ReconciliationUtils.updateStatusBeforeDeploymentAttempt(application, new Configuration()); + assertEquals(UPGRADING, application.getStatus().getLifecycleState()); + + ReconciliationUtils.updateStatusForDeployedSpec(application, new Configuration()); + assertEquals(DEPLOYED, application.getStatus().getLifecycleState()); + + application.getStatus().getReconciliationStatus().markReconciledSpecAsStable(); + assertEquals(STABLE, application.getStatus().getLifecycleState()); + + application.getStatus().setError("errr"); + assertEquals(STABLE, application.getStatus().getLifecycleState()); + + application.getStatus().getJobStatus().setState(JobStatus.FAILED.name()); + assertEquals(FAILED, application.getStatus().getLifecycleState()); + + application.getStatus().setError(""); + + application + .getStatus() + .getReconciliationStatus() + .setState(ReconciliationState.ROLLING_BACK); + assertEquals(ROLLING_BACK, application.getStatus().getLifecycleState()); + + application.getStatus().getJobStatus().setState(JobStatus.RECONCILING.name()); + application.getStatus().getReconciliationStatus().setState(ReconciliationState.ROLLED_BACK); + assertEquals(ROLLED_BACK, application.getStatus().getLifecycleState()); + + application.getStatus().getJobStatus().setState(JobStatus.FAILED.name()); + assertEquals(FAILED, application.getStatus().getLifecycleState()); + + application.getStatus().getJobStatus().setState(JobStatus.RUNNING.name()); + application.getSpec().getJob().setState(JobState.SUSPENDED); + ReconciliationUtils.updateStatusForDeployedSpec(application, new Configuration()); + assertEquals(SUSPENDED, application.getStatus().getLifecycleState()); + } + + @Test + public void testLifecycleTracker() { + var histos = initHistos(); + var lifecycleTracker = + new ResourceLifecycleMetricTracker(CREATED, Instant.ofEpochMilli(1000), histos); + + long ts = 1000; + lifecycleTracker.onUpdate(UPGRADING, Instant.ofEpochMilli(ts += 1000)); Review Comment: The ts is always increasing this way, I think we cannot easily simplify this. Also it's pretty simple already :) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org