mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-21 10:51:29 +00:00
Merge pull request #113166 from alculquicondor/simply_job_metrics_test
Simplify tests for job metrics by resetting them
This commit is contained in:
commit
05b46d7ce2
@ -84,8 +84,8 @@ func TestMetrics(t *testing.T) {
|
|||||||
|
|
||||||
testCases := map[string]struct {
|
testCases := map[string]struct {
|
||||||
job *batchv1.Job
|
job *batchv1.Job
|
||||||
wantJobFinishedNumMetricDelta metricLabelsWithValue
|
wantJobFinishedNumMetric metricLabelsWithValue
|
||||||
wantJobPodsFinishedMetricDelta metricLabelsWithValue
|
wantJobPodsFinishedMetric metricLabelsWithValue
|
||||||
}{
|
}{
|
||||||
"non-indexed job": {
|
"non-indexed job": {
|
||||||
job: &batchv1.Job{
|
job: &batchv1.Job{
|
||||||
@ -95,11 +95,11 @@ func TestMetrics(t *testing.T) {
|
|||||||
CompletionMode: &nonIndexedCompletion,
|
CompletionMode: &nonIndexedCompletion,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
wantJobFinishedNumMetricDelta: metricLabelsWithValue{
|
wantJobFinishedNumMetric: metricLabelsWithValue{
|
||||||
Labels: []string{"NonIndexed", "succeeded"},
|
Labels: []string{"NonIndexed", "succeeded"},
|
||||||
Value: 1,
|
Value: 1,
|
||||||
},
|
},
|
||||||
wantJobPodsFinishedMetricDelta: metricLabelsWithValue{
|
wantJobPodsFinishedMetric: metricLabelsWithValue{
|
||||||
Labels: []string{"NonIndexed", "succeeded"},
|
Labels: []string{"NonIndexed", "succeeded"},
|
||||||
Value: 2,
|
Value: 2,
|
||||||
},
|
},
|
||||||
@ -112,11 +112,11 @@ func TestMetrics(t *testing.T) {
|
|||||||
CompletionMode: &indexedCompletion,
|
CompletionMode: &indexedCompletion,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
wantJobFinishedNumMetricDelta: metricLabelsWithValue{
|
wantJobFinishedNumMetric: metricLabelsWithValue{
|
||||||
Labels: []string{"Indexed", "succeeded"},
|
Labels: []string{"Indexed", "succeeded"},
|
||||||
Value: 1,
|
Value: 1,
|
||||||
},
|
},
|
||||||
wantJobPodsFinishedMetricDelta: metricLabelsWithValue{
|
wantJobPodsFinishedMetric: metricLabelsWithValue{
|
||||||
Labels: []string{"Indexed", "succeeded"},
|
Labels: []string{"Indexed", "succeeded"},
|
||||||
Value: 2,
|
Value: 2,
|
||||||
},
|
},
|
||||||
@ -125,17 +125,7 @@ func TestMetrics(t *testing.T) {
|
|||||||
job_index := 0 // job index to avoid collisions between job names created by different test cases
|
job_index := 0 // job index to avoid collisions between job names created by different test cases
|
||||||
for name, tc := range testCases {
|
for name, tc := range testCases {
|
||||||
t.Run(name, func(t *testing.T) {
|
t.Run(name, func(t *testing.T) {
|
||||||
|
resetMetrics()
|
||||||
// record the metrics after the job is created
|
|
||||||
jobFinishedNumBefore, err := getCounterMetricValueForLabels(metrics.JobFinishedNum, tc.wantJobFinishedNumMetricDelta.Labels)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to collect the JobFinishedNum metric before creating the job: %q", err)
|
|
||||||
}
|
|
||||||
jobPodsFinishedBefore, err := getCounterMetricValueForLabels(metrics.JobPodsFinished, tc.wantJobPodsFinishedMetricDelta.Labels)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to collect the JobPodsFinished metric before creating the job: %q", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// create a single job and wait for its completion
|
// create a single job and wait for its completion
|
||||||
job := tc.job.DeepCopy()
|
job := tc.job.DeepCopy()
|
||||||
job.Name = fmt.Sprintf("job-%v", job_index)
|
job.Name = fmt.Sprintf("job-%v", job_index)
|
||||||
@ -154,25 +144,23 @@ func TestMetrics(t *testing.T) {
|
|||||||
validateJobSucceeded(ctx, t, clientSet, jobObj)
|
validateJobSucceeded(ctx, t, clientSet, jobObj)
|
||||||
|
|
||||||
// verify metric values after the job is finished
|
// verify metric values after the job is finished
|
||||||
validateMetricValueDeltas(t, metrics.JobFinishedNum, tc.wantJobFinishedNumMetricDelta, jobFinishedNumBefore)
|
validateMetricValue(t, metrics.JobFinishedNum, tc.wantJobFinishedNumMetric)
|
||||||
validateMetricValueDeltas(t, metrics.JobPodsFinished, tc.wantJobPodsFinishedMetricDelta, jobPodsFinishedBefore)
|
validateMetricValue(t, metrics.JobPodsFinished, tc.wantJobPodsFinishedMetric)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func validateMetricValueDeltas(t *testing.T, counterVer *basemetrics.CounterVec, wantMetricDelta metricLabelsWithValue, metricValuesBefore metricLabelsWithValue) {
|
func validateMetricValue(t *testing.T, counterVec *basemetrics.CounterVec, wantMetric metricLabelsWithValue) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
var cmpErr error
|
var cmpErr error
|
||||||
err := wait.PollImmediate(10*time.Millisecond, 10*time.Second, func() (bool, error) {
|
err := wait.PollImmediate(10*time.Millisecond, 10*time.Second, func() (bool, error) {
|
||||||
cmpErr = nil
|
cmpErr = nil
|
||||||
metricValuesAfter, err := getCounterMetricValueForLabels(counterVer, wantMetricDelta.Labels)
|
value, err := testutil.GetCounterMetricValue(counterVec.WithLabelValues(wantMetric.Labels...))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return true, fmt.Errorf("Failed to collect the %q metric after the job is finished: %q", counterVer.Name, err)
|
return true, fmt.Errorf("collecting the %q metric: %q", counterVec.Name, err)
|
||||||
}
|
}
|
||||||
wantDelta := wantMetricDelta.Value
|
if wantMetric.Value != int(value) {
|
||||||
gotDelta := metricValuesAfter.Value - metricValuesBefore.Value
|
cmpErr = fmt.Errorf("Unexpected metric delta for %q metric with labels %q. want: %v, got: %v", counterVec.Name, wantMetric.Labels, wantMetric.Value, int(value))
|
||||||
if wantDelta != gotDelta {
|
|
||||||
cmpErr = fmt.Errorf("Unexepected metric delta for %q metric with labels %q. want: %v, got: %v", counterVer.Name, wantMetricDelta.Labels, wantDelta, gotDelta)
|
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
return true, nil
|
return true, nil
|
||||||
@ -185,16 +173,6 @@ func validateMetricValueDeltas(t *testing.T, counterVer *basemetrics.CounterVec,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getCounterMetricValueForLabels(counterVec *basemetrics.CounterVec, labels []string) (metricLabelsWithValue, error) {
|
|
||||||
var result metricLabelsWithValue = metricLabelsWithValue{Labels: labels}
|
|
||||||
value, err := testutil.GetCounterMetricValue(counterVec.WithLabelValues(labels...))
|
|
||||||
if err != nil {
|
|
||||||
return result, err
|
|
||||||
}
|
|
||||||
result.Value = int(value)
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestJobPodFailurePolicyWithFailedPodDeletedDuringControllerRestart verifies that the job is properly marked as Failed
|
// TestJobPodFailurePolicyWithFailedPodDeletedDuringControllerRestart verifies that the job is properly marked as Failed
|
||||||
// in a scenario when the job controller crashes between removing pod finalizers and marking the job as Failed (based on
|
// in a scenario when the job controller crashes between removing pod finalizers and marking the job as Failed (based on
|
||||||
// the pod failure policy). After the finalizer for the failed pod is removed we remove the failed pod. This step is
|
// the pod failure policy). After the finalizer for the failed pod is removed we remove the failed pod. This step is
|
||||||
@ -1697,6 +1675,11 @@ func startJobControllerAndWaitForCaches(restConfig *restclient.Config) (context.
|
|||||||
return ctx, cancel
|
return ctx, cancel
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func resetMetrics() {
|
||||||
|
metrics.JobFinishedNum.Reset()
|
||||||
|
metrics.JobPodsFinished.Reset()
|
||||||
|
}
|
||||||
|
|
||||||
func createJobControllerWithSharedInformers(restConfig *restclient.Config, informerSet informers.SharedInformerFactory) (*jobcontroller.Controller, context.Context, context.CancelFunc) {
|
func createJobControllerWithSharedInformers(restConfig *restclient.Config, informerSet informers.SharedInformerFactory) (*jobcontroller.Controller, context.Context, context.CancelFunc) {
|
||||||
clientSet := clientset.NewForConfigOrDie(restclient.AddUserAgent(restConfig, "job-controller"))
|
clientSet := clientset.NewForConfigOrDie(restclient.AddUserAgent(restConfig, "job-controller"))
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
Loading…
Reference in New Issue
Block a user