k8s.io/perf-tests/clusterloader2@v0.0.0-20240304094227-64bdb12da87e/pkg/config/template_provider_test.go (about)

     1  /*
     2  Copyright 2018 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 config
    18  
    19  import (
    20  	"os"
    21  	"reflect"
    22  	"testing"
    23  
    24  	"k8s.io/perf-tests/clusterloader2/api"
    25  )
    26  
    27  func TestValidateTestSuite(t *testing.T) {
    28  	tests := []struct {
    29  		name    string
    30  		suite   api.TestSuite
    31  		wantErr bool
    32  	}{
    33  		{
    34  			name:    "empty-suite",
    35  			suite:   api.TestSuite{},
    36  			wantErr: false,
    37  		},
    38  		{
    39  			name: "valid-id",
    40  			suite: api.TestSuite{
    41  				api.TestScenario{
    42  					Identifier:    "some-id",
    43  					ConfigPath:    "",
    44  					OverridePaths: []string{},
    45  				},
    46  			},
    47  			wantErr: false,
    48  		},
    49  		{
    50  			name: "id-with-underscore",
    51  			suite: api.TestSuite{
    52  				api.TestScenario{
    53  					Identifier:    "some_id",
    54  					ConfigPath:    "",
    55  					OverridePaths: []string{},
    56  				},
    57  			},
    58  			wantErr: true,
    59  		},
    60  	}
    61  	for _, tt := range tests {
    62  		t.Run(tt.name, func(t *testing.T) {
    63  			if err := validateTestSuite(tt.suite); (err != nil) != tt.wantErr {
    64  				t.Errorf("validateTestSuite() error = %v, wantErr %v", err, tt.wantErr)
    65  			}
    66  		})
    67  	}
    68  }
    69  
    70  func TestLoadCL2Envs(t *testing.T) {
    71  	tests := []struct {
    72  		name          string
    73  		env           map[string]string
    74  		wantedMapping map[string]interface{}
    75  	}{
    76  		{
    77  			name: "One CL2 env, one non-CL2 env",
    78  			env: map[string]string{
    79  				"CL2_MY_PARAM": "100",
    80  				"NODE_SIZE":    "n1-standard-1",
    81  			},
    82  			wantedMapping: map[string]interface{}{
    83  				"CL2_MY_PARAM": int64(100),
    84  			},
    85  		},
    86  		{
    87  			name: "Multiple CL2 envs",
    88  			env: map[string]string{
    89  				"CL2_MY_PARAM1": "100",
    90  				"CL2_MY_PARAM2": "true",
    91  				"CL2_MY_PARAM3": "99.99",
    92  				"CL2_MY_PARAM4": "XXX",
    93  				"CL2_MY_PARAM5": "1",
    94  				"CL2_MY_PARAM6": "a=b",
    95  			},
    96  			wantedMapping: map[string]interface{}{
    97  				"CL2_MY_PARAM1": int64(100),
    98  				"CL2_MY_PARAM2": true,
    99  				"CL2_MY_PARAM3": 99.99,
   100  				"CL2_MY_PARAM4": "XXX",
   101  				"CL2_MY_PARAM5": int64(1),
   102  				"CL2_MY_PARAM6": "a=b",
   103  			},
   104  		},
   105  		{
   106  			name: "No CL2 envs",
   107  			env: map[string]string{
   108  				"NODE_SIZE": "n1-standard-1",
   109  				"CLUSTER":   "my-cluster",
   110  			},
   111  			wantedMapping: map[string]interface{}{},
   112  		},
   113  		{
   114  			name: "Env prefix is case sensitive",
   115  			env: map[string]string{
   116  				"cl2_my_param": "123",
   117  			},
   118  			wantedMapping: map[string]interface{}{},
   119  		},
   120  	}
   121  
   122  	for _, tt := range tests {
   123  		t.Run(tt.name, func(t *testing.T) {
   124  			os.Clearenv()
   125  			for k, v := range tt.env {
   126  				os.Setenv(k, v)
   127  			}
   128  			mapping, err := LoadCL2Envs()
   129  			if err != nil {
   130  				t.Error(err)
   131  			}
   132  			if !reflect.DeepEqual(mapping, tt.wantedMapping) {
   133  				t.Errorf("wanted: %v, got: %v", tt.wantedMapping, mapping)
   134  			}
   135  		})
   136  	}
   137  }
   138  
   139  func TestMergeMappings(t *testing.T) {
   140  	tests := []struct {
   141  		name    string
   142  		a       map[string]interface{}
   143  		b       map[string]interface{}
   144  		wantedA map[string]interface{}
   145  		wantErr bool
   146  	}{
   147  		{
   148  			name: "Different keys",
   149  			a:    map[string]interface{}{"ENABLE_XXX": true},
   150  			b:    map[string]interface{}{"CL2_PARAM1": 123},
   151  			wantedA: map[string]interface{}{
   152  				"ENABLE_XXX": true,
   153  				"CL2_PARAM1": 123,
   154  			},
   155  		},
   156  		{
   157  			name: "Same keys, no conflict",
   158  			a:    map[string]interface{}{"CL2_PARAM1": 100},
   159  			b:    map[string]interface{}{"CL2_PARAM1": 100},
   160  			wantedA: map[string]interface{}{
   161  				"CL2_PARAM1": 100,
   162  			},
   163  		},
   164  		{
   165  			name:    "Same keys, conflict",
   166  			a:       map[string]interface{}{"CL2_PARAM1": 100},
   167  			b:       map[string]interface{}{"CL2_PARAM1": 105},
   168  			wantErr: true,
   169  		},
   170  	}
   171  
   172  	for _, tt := range tests {
   173  		t.Run(tt.name, func(t *testing.T) {
   174  			if err := MergeMappings(tt.a, tt.b); err != nil {
   175  				if !tt.wantErr {
   176  					t.Errorf("unexpceted MergeMappings() error: %v", err)
   177  				}
   178  				return
   179  			}
   180  			if !reflect.DeepEqual(tt.a, tt.wantedA) {
   181  				t.Errorf("wanted: %v, got: %v", tt.wantedA, tt.a)
   182  			}
   183  		})
   184  	}
   185  }