k8s.io/perf-tests/clusterloader2@v0.0.0-20240304094227-64bdb12da87e/pkg/util/cluster_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 util
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  	corev1 "k8s.io/api/core/v1"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  )
    26  
    27  func TestLegacyIsMasterNode(t *testing.T) {
    28  	testcases := map[string]struct {
    29  		Name   string
    30  		Labels map[string]string
    31  		expect bool
    32  	}{
    33  		"node with node label key": {
    34  			Labels: map[string]string{keyMasterNodeLabel: ""},
    35  			expect: true,
    36  		},
    37  		"node with node label key value": {
    38  			Labels: map[string]string{keyMasterNodeLabel: "true"},
    39  			expect: true,
    40  		},
    41  		"node without node label": {
    42  			Labels: map[string]string{},
    43  			expect: false,
    44  		},
    45  	}
    46  
    47  	for _, tc := range testcases {
    48  		node := &corev1.Node{
    49  			ObjectMeta: metav1.ObjectMeta{Labels: tc.Labels},
    50  		}
    51  		result := LegacyIsMasterNode(node)
    52  		assert.Equal(t, tc.expect, result)
    53  	}
    54  }
    55  
    56  func TestIsControlPlaneNode(t *testing.T) {
    57  	testcases := map[string]struct {
    58  		Name   string
    59  		Labels map[string]string
    60  		expect bool
    61  	}{
    62  		"node with controlplane node-role key": {
    63  			Labels: map[string]string{keyControlPlaneNodeLabel: ""},
    64  			expect: true,
    65  		},
    66  		"node with controlplane node-role key and value as true": {
    67  			Labels: map[string]string{keyControlPlaneNodeLabel: "true"},
    68  			expect: true,
    69  		},
    70  		"node with controlplane node-role key and value as false": {
    71  			Labels: map[string]string{keyControlPlaneNodeLabel: "false"},
    72  			expect: true,
    73  		},
    74  		"node without controlplane node-role": {
    75  			Labels: map[string]string{},
    76  			expect: false,
    77  		},
    78  	}
    79  	for _, tc := range testcases {
    80  		node := &corev1.Node{
    81  			ObjectMeta: metav1.ObjectMeta{Labels: tc.Labels},
    82  		}
    83  		result := IsControlPlaneNode(node)
    84  		assert.Equal(t, tc.expect, result)
    85  	}
    86  }