k8s.io/perf-tests/clusterloader2@v0.0.0-20240304094227-64bdb12da87e/pkg/measurement/common/container_restarts_test.go (about)

     1  /*
     2  Copyright 2021 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 common
    18  
    19  import (
    20  	"fmt"
    21  	"strings"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/stretchr/testify/assert"
    26  
    27  	"k8s.io/perf-tests/clusterloader2/pkg/measurement"
    28  	"k8s.io/perf-tests/clusterloader2/pkg/measurement/common/executors"
    29  )
    30  
    31  func TestContainerRestartsMeasurement(t *testing.T) {
    32  	splitter := func(yamlLines []string) string {
    33  		return strings.Join(yamlLines, "\n")
    34  	}
    35  
    36  	cases := []struct {
    37  		name               string
    38  		config             *measurement.Config
    39  		hasError           bool
    40  		testSeriesFile     string
    41  		testSeriesDuration time.Duration
    42  	}{
    43  		{
    44  			name:               "no_restarts",
    45  			hasError:           false,
    46  			testSeriesFile:     "no_restarts.yaml",
    47  			testSeriesDuration: 10 * time.Minute,
    48  			config: &measurement.Config{
    49  				Params: map[string]interface{}{},
    50  			},
    51  		},
    52  		{
    53  			name:               "double_restart_of_apiserver/violation",
    54  			hasError:           true,
    55  			testSeriesFile:     "double_restart_of_apiserver.yaml",
    56  			testSeriesDuration: 10 * time.Minute,
    57  			config: &measurement.Config{
    58  				Params: map[string]interface{}{},
    59  			},
    60  		},
    61  		{
    62  			name:               "double_restart_of_apiserver/default_allowed_restarts",
    63  			hasError:           false,
    64  			testSeriesFile:     "double_restart_of_apiserver.yaml",
    65  			testSeriesDuration: 10 * time.Minute,
    66  			config: &measurement.Config{
    67  				Params: map[string]interface{}{
    68  					"defaultAllowedRestarts": 2,
    69  				},
    70  			},
    71  		},
    72  		{
    73  			name:               "double_restart_of_apiserver/custom_allowed_restarts",
    74  			hasError:           false,
    75  			testSeriesFile:     "double_restart_of_apiserver.yaml",
    76  			testSeriesDuration: 10 * time.Minute,
    77  			config: &measurement.Config{
    78  				Params: map[string]interface{}{
    79  					"customAllowedRestarts": splitter([]string{
    80  						"- container: kube-apiserver",
    81  						"  pod: kube-apiserver",
    82  						"  namespace: kube-system",
    83  						"  allowedRestarts: 2",
    84  					}),
    85  				},
    86  			},
    87  		},
    88  		{
    89  			name:               "two_apiserver_replicas_restarts/violation",
    90  			hasError:           true,
    91  			testSeriesFile:     "two_apiserver_replicas_restarts.yaml",
    92  			testSeriesDuration: 10 * time.Minute,
    93  			config: &measurement.Config{
    94  				Params: map[string]interface{}{},
    95  			},
    96  		},
    97  		{
    98  			name:               "two_apiserver_replicas_restarts/custom_allowed_restarts_with_regex",
    99  			hasError:           false,
   100  			testSeriesFile:     "two_apiserver_replicas_restarts.yaml",
   101  			testSeriesDuration: 10 * time.Minute,
   102  			config: &measurement.Config{
   103  				Params: map[string]interface{}{
   104  					"customAllowedRestarts": splitter([]string{
   105  						"- container: kube-apiserver",
   106  						"  pod: kube-apiserver-*",
   107  						"  namespace: kube-system",
   108  						"  allowedRestarts: 2",
   109  					}),
   110  				},
   111  			},
   112  		},
   113  	}
   114  
   115  	for _, tc := range cases {
   116  		t.Run(tc.name, func(t *testing.T) {
   117  			executor, err := executors.NewPromqlExecutor(fmt.Sprintf("testdata/container_restarts/%s", tc.testSeriesFile))
   118  			if err != nil {
   119  				t.Fatalf("failed to create PromQL executor: %v", err)
   120  			}
   121  			defer executor.Close()
   122  			gatherer := &containerRestartsGatherer{}
   123  			start := time.Unix(0, 0).UTC()
   124  			end := start.Add(tc.testSeriesDuration)
   125  			_, err = gatherer.Gather(executor, start, end, tc.config)
   126  			if tc.hasError {
   127  				assert.NotNil(t, err, "wanted error, but got none")
   128  			} else {
   129  				assert.Nil(t, err, "wanted no error, but got %v", err)
   130  			}
   131  		})
   132  	}
   133  }