k8s.io/perf-tests/clusterloader2@v0.0.0-20240304094227-64bdb12da87e/pkg/util/util_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 util
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  	"k8s.io/apimachinery/pkg/labels"
    24  )
    25  
    26  type test struct {
    27  	Field1 string
    28  	Field2 int
    29  	*ObjectSelector
    30  }
    31  
    32  func TestToStruct(t *testing.T) {
    33  	tests := []struct {
    34  		name    string
    35  		dict    map[string]interface{}
    36  		in      interface{}
    37  		want    interface{}
    38  		wantErr bool
    39  	}{
    40  		{
    41  			name: "basic",
    42  			dict: map[string]interface{}{
    43  				"field1": "string1",
    44  				"field2": 1234,
    45  			},
    46  			in: &test{},
    47  			want: &test{
    48  				Field1: "string1",
    49  				Field2: 1234,
    50  			},
    51  		},
    52  		{
    53  			name: "preserves default values",
    54  			dict: map[string]interface{}{
    55  				"field2": 1234,
    56  			},
    57  			in: &test{
    58  				Field1: "default value",
    59  			},
    60  			want: &test{
    61  				Field1: "default value",
    62  				Field2: 1234,
    63  			},
    64  		},
    65  		{
    66  			name: "With embed selector (WaitForControlledPodsRunning case)",
    67  			dict: map[string]interface{}{
    68  				"field1":        "string1",
    69  				"namespace":     "namespace-1",
    70  				"fieldSelector": "spec.nodeName=abcd",
    71  				"labelSelector": "group = load",
    72  			},
    73  			in: &test{},
    74  			want: &test{
    75  				Field1: "string1",
    76  				ObjectSelector: &ObjectSelector{
    77  					Namespace:     "namespace-1",
    78  					FieldSelector: "spec.nodeName=abcd",
    79  					LabelSelector: "group = load",
    80  				},
    81  			},
    82  		},
    83  		{
    84  			name: "type mismatch",
    85  			dict: map[string]interface{}{
    86  				"field1": 1234, // should be string
    87  			},
    88  			in:      &test{},
    89  			wantErr: true,
    90  		},
    91  	}
    92  	for _, tt := range tests {
    93  		t.Run(tt.name, func(t *testing.T) {
    94  			if err := ToStruct(tt.dict, tt.in); (err != nil) != tt.wantErr {
    95  				t.Errorf("ToStruct() error = %v, wantErr %v", err, tt.wantErr)
    96  			}
    97  			if !tt.wantErr {
    98  				assert.Equal(t, tt.want, tt.in)
    99  			}
   100  		})
   101  	}
   102  }
   103  
   104  func TestGetLabelSelector(t *testing.T) {
   105  	tests := []struct {
   106  		name    string
   107  		value   interface{}
   108  		matches labels.Labels
   109  		wantErr bool
   110  	}{
   111  		{
   112  			name:    "error with non-string value",
   113  			value:   1,
   114  			wantErr: true,
   115  		},
   116  		{
   117  			name:    "error with bad label selector value",
   118  			value:   "?i am a bad label selector?",
   119  			wantErr: true,
   120  		},
   121  		{
   122  			name:    "no error with good label selector value",
   123  			value:   "app=test",
   124  			matches: labels.Set{"app": "test"},
   125  		},
   126  	}
   127  
   128  	for _, tt := range tests {
   129  		t.Run(tt.name, func(t *testing.T) {
   130  			l, err := GetLabelSelector(map[string]interface{}{"key": tt.value}, "key")
   131  			if (err != nil) != tt.wantErr {
   132  				t.Errorf("GetLabelSelector() error = %v, wantErr %v", err, tt.wantErr)
   133  			}
   134  			if !tt.wantErr {
   135  				assert.Equal(t, true, (*l).Matches(tt.matches))
   136  			}
   137  		})
   138  	}
   139  }