...

Source file src/k8s.io/apiextensions-apiserver/pkg/apiserver/conversion/metrics_test.go

Documentation: k8s.io/apiextensions-apiserver/pkg/apiserver/conversion

     1  /*
     2  Copyright 2023 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package conversion
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"testing"
    23  	"time"
    24  
    25  	"k8s.io/component-base/metrics"
    26  	"k8s.io/component-base/metrics/testutil"
    27  )
    28  
    29  func TestConversionWebhookMetrics_ObserveConversionWebhookSuccess(t *testing.T) {
    30  	type fields struct {
    31  		conversionWebhookRequest *metrics.CounterVec
    32  		conversionWebhookLatency *metrics.HistogramVec
    33  	}
    34  	type args struct {
    35  		elapsed time.Duration
    36  	}
    37  	tests := []struct {
    38  		name                 string
    39  		fields               fields
    40  		args                 args
    41  		wantLabels           map[string]string
    42  		expectedRequestValue int
    43  	}{
    44  		{
    45  			name: "test_conversion_success",
    46  			fields: fields{
    47  				conversionWebhookRequest: Metrics.conversionWebhookRequest,
    48  				conversionWebhookLatency: Metrics.conversionWebhookLatency,
    49  			},
    50  			args: args{
    51  				elapsed: 2 * time.Second,
    52  			},
    53  			wantLabels: map[string]string{
    54  				"result":       "success",
    55  				"failure_type": "",
    56  			},
    57  			expectedRequestValue: 1,
    58  		}, {
    59  			name: "test_conversion_success_2",
    60  			fields: fields{
    61  				conversionWebhookRequest: Metrics.conversionWebhookRequest,
    62  				conversionWebhookLatency: Metrics.conversionWebhookLatency,
    63  			},
    64  			args: args{
    65  				elapsed: 2 * time.Second,
    66  			},
    67  			wantLabels: map[string]string{
    68  				"result":       "success",
    69  				"failure_type": "",
    70  			},
    71  			expectedRequestValue: 2,
    72  		},
    73  	}
    74  	for _, tt := range tests {
    75  		t.Run(tt.name, func(t *testing.T) {
    76  			m := &ConversionWebhookMetrics{
    77  				conversionWebhookRequest: tt.fields.conversionWebhookRequest,
    78  				conversionWebhookLatency: tt.fields.conversionWebhookLatency,
    79  			}
    80  			m.ObserveConversionWebhookSuccess(context.TODO(), tt.args.elapsed)
    81  			testutil.AssertVectorCount(t, fmt.Sprintf("%s_conversion_webhook_request_total", namespace), tt.wantLabels, tt.expectedRequestValue)
    82  			testutil.AssertHistogramTotalCount(t, fmt.Sprintf("%s_conversion_webhook_duration_seconds", namespace), tt.wantLabels, tt.expectedRequestValue)
    83  		})
    84  	}
    85  }
    86  
    87  func TestConversionWebhookMetrics_ObserveConversionWebhookFailure(t *testing.T) {
    88  	type fields struct {
    89  		conversionWebhookRequest *metrics.CounterVec
    90  		conversionWebhookLatency *metrics.HistogramVec
    91  	}
    92  	type args struct {
    93  		elapsed   time.Duration
    94  		errorType ConversionWebhookErrorType
    95  	}
    96  	tests := []struct {
    97  		name                 string
    98  		fields               fields
    99  		args                 args
   100  		wantLabels           map[string]string
   101  		expectedRequestValue int
   102  		expectedLatencyCount int
   103  	}{
   104  		{
   105  			name: "test_conversion_failure",
   106  			fields: fields{
   107  				conversionWebhookRequest: Metrics.conversionWebhookRequest,
   108  				conversionWebhookLatency: Metrics.conversionWebhookLatency,
   109  			},
   110  			args: args{
   111  				elapsed:   2 * time.Second,
   112  				errorType: ConversionWebhookCallFailure,
   113  			},
   114  			wantLabels: map[string]string{
   115  				"result":       "failure",
   116  				"failure_type": string(ConversionWebhookCallFailure),
   117  			},
   118  			expectedRequestValue: 1,
   119  			expectedLatencyCount: 1,
   120  		}, {
   121  			name: "test_conversion_failure_2",
   122  			fields: fields{
   123  				conversionWebhookRequest: Metrics.conversionWebhookRequest,
   124  				conversionWebhookLatency: Metrics.conversionWebhookLatency,
   125  			},
   126  			args: args{
   127  				elapsed:   2 * time.Second,
   128  				errorType: ConversionWebhookMalformedResponseFailure,
   129  			},
   130  			wantLabels: map[string]string{
   131  				"result":       "failure",
   132  				"failure_type": string(ConversionWebhookMalformedResponseFailure),
   133  			},
   134  			expectedRequestValue: 1,
   135  			expectedLatencyCount: 2,
   136  		}, {
   137  			name: "test_conversion_failure_3",
   138  			fields: fields{
   139  				conversionWebhookRequest: Metrics.conversionWebhookRequest,
   140  				conversionWebhookLatency: Metrics.conversionWebhookLatency,
   141  			},
   142  			args: args{
   143  				elapsed:   2 * time.Second,
   144  				errorType: ConversionWebhookPartialResponseFailure,
   145  			},
   146  			wantLabels: map[string]string{
   147  				"result":       "failure",
   148  				"failure_type": string(ConversionWebhookPartialResponseFailure),
   149  			},
   150  			expectedRequestValue: 1,
   151  			expectedLatencyCount: 3,
   152  		}, {
   153  			name: "test_conversion_failure_4",
   154  			fields: fields{
   155  				conversionWebhookRequest: Metrics.conversionWebhookRequest,
   156  				conversionWebhookLatency: Metrics.conversionWebhookLatency,
   157  			},
   158  			args: args{
   159  				elapsed:   2 * time.Second,
   160  				errorType: ConversionWebhookInvalidConvertedObjectFailure,
   161  			},
   162  			wantLabels: map[string]string{
   163  				"result":       "failure",
   164  				"failure_type": string(ConversionWebhookInvalidConvertedObjectFailure),
   165  			},
   166  			expectedRequestValue: 1,
   167  			expectedLatencyCount: 4,
   168  		}, {
   169  			name: "test_conversion_failure_5",
   170  			fields: fields{
   171  				conversionWebhookRequest: Metrics.conversionWebhookRequest,
   172  				conversionWebhookLatency: Metrics.conversionWebhookLatency,
   173  			},
   174  			args: args{
   175  				elapsed:   2 * time.Second,
   176  				errorType: ConversionWebhookNoObjectsReturnedFailure,
   177  			},
   178  			wantLabels: map[string]string{
   179  				"result":       "failure",
   180  				"failure_type": string(ConversionWebhookNoObjectsReturnedFailure),
   181  			},
   182  			expectedRequestValue: 1,
   183  			expectedLatencyCount: 5,
   184  		},
   185  	}
   186  	for _, tt := range tests {
   187  		t.Run(tt.name, func(t *testing.T) {
   188  			m := &ConversionWebhookMetrics{
   189  				conversionWebhookRequest: tt.fields.conversionWebhookRequest,
   190  				conversionWebhookLatency: tt.fields.conversionWebhookLatency,
   191  			}
   192  			m.ObserveConversionWebhookFailure(context.TODO(), tt.args.elapsed, tt.args.errorType)
   193  			testutil.AssertVectorCount(t, fmt.Sprintf("%s_conversion_webhook_request_total", namespace), tt.wantLabels, tt.expectedRequestValue)
   194  			testutil.AssertHistogramTotalCount(t, fmt.Sprintf("%s_conversion_webhook_duration_seconds", namespace), tt.wantLabels, tt.expectedRequestValue)
   195  		})
   196  	}
   197  }
   198  

View as plain text