k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/api/storage/util_test.go (about)

     1  /*
     2  Copyright 2022 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 storage
    18  
    19  import (
    20  	"testing"
    21  
    22  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    23  	"k8s.io/apimachinery/pkg/util/sets"
    24  
    25  	"k8s.io/kubernetes/pkg/apis/core"
    26  	"k8s.io/kubernetes/pkg/apis/storage"
    27  )
    28  
    29  func TestStorageClassWarnings(t *testing.T) {
    30  	testcases := []struct {
    31  		name     string
    32  		template *storage.StorageClass
    33  		expected []string
    34  	}{
    35  		{
    36  			name:     "null",
    37  			template: nil,
    38  			expected: nil,
    39  		},
    40  		{
    41  			name: "no warning",
    42  			template: &storage.StorageClass{
    43  				ObjectMeta: metav1.ObjectMeta{
    44  					Name: "foo",
    45  				},
    46  			},
    47  			expected: nil,
    48  		},
    49  		{
    50  			name: "warning",
    51  			template: &storage.StorageClass{
    52  				ObjectMeta: metav1.ObjectMeta{
    53  					Name: "foo",
    54  				},
    55  				AllowedTopologies: []core.TopologySelectorTerm{
    56  					{
    57  						MatchLabelExpressions: []core.TopologySelectorLabelRequirement{
    58  							{
    59  								Key:    "beta.kubernetes.io/arch",
    60  								Values: []string{"amd64"},
    61  							},
    62  							{
    63  								Key:    "beta.kubernetes.io/os",
    64  								Values: []string{"linux"},
    65  							},
    66  						},
    67  					},
    68  				},
    69  			},
    70  			expected: []string{
    71  				`allowedTopologies[0].matchLabelExpressions[0].key: deprecated since v1.14; use "kubernetes.io/arch" instead`,
    72  				`allowedTopologies[0].matchLabelExpressions[1].key: deprecated since v1.14; use "kubernetes.io/os" instead`,
    73  			},
    74  		},
    75  	}
    76  
    77  	for _, tc := range testcases {
    78  		t.Run("podspec_"+tc.name, func(t *testing.T) {
    79  			actual := sets.New[string](GetWarningsForStorageClass(tc.template)...)
    80  			expected := sets.New[string](tc.expected...)
    81  			for _, missing := range sets.List[string](expected.Difference(actual)) {
    82  				t.Errorf("missing: %s", missing)
    83  			}
    84  			for _, extra := range sets.List[string](actual.Difference(expected)) {
    85  				t.Errorf("extra: %s", extra)
    86  			}
    87  		})
    88  
    89  	}
    90  }
    91  
    92  func TestCSIStorageCapacityWarnings(t *testing.T) {
    93  	testcases := []struct {
    94  		name     string
    95  		template *storage.CSIStorageCapacity
    96  		expected []string
    97  	}{
    98  		{
    99  			name:     "null",
   100  			template: nil,
   101  			expected: nil,
   102  		},
   103  		{
   104  			name: "no warning",
   105  			template: &storage.CSIStorageCapacity{
   106  				ObjectMeta: metav1.ObjectMeta{
   107  					Name: "foo",
   108  				},
   109  			},
   110  			expected: nil,
   111  		},
   112  		{
   113  			name: "MatchLabels warning",
   114  			template: &storage.CSIStorageCapacity{
   115  				ObjectMeta: metav1.ObjectMeta{
   116  					Name: "foo",
   117  				},
   118  				NodeTopology: &metav1.LabelSelector{
   119  					MatchLabels: map[string]string{
   120  						"beta.kubernetes.io/arch": "amd64",
   121  						"beta.kubernetes.io/os":   "linux",
   122  					},
   123  				},
   124  			},
   125  			expected: []string{
   126  				`nodeTopology.matchLabels.beta.kubernetes.io/arch: deprecated since v1.14; use "kubernetes.io/arch" instead`,
   127  				`nodeTopology.matchLabels.beta.kubernetes.io/os: deprecated since v1.14; use "kubernetes.io/os" instead`,
   128  			},
   129  		},
   130  		{
   131  			name: "MatchExpressions warning",
   132  			template: &storage.CSIStorageCapacity{
   133  				ObjectMeta: metav1.ObjectMeta{
   134  					Name: "foo",
   135  				},
   136  				NodeTopology: &metav1.LabelSelector{
   137  					MatchExpressions: []metav1.LabelSelectorRequirement{
   138  						{
   139  							Key:      "beta.kubernetes.io/arch",
   140  							Operator: metav1.LabelSelectorOpIn,
   141  							Values:   []string{"amd64"},
   142  						},
   143  						{
   144  							Key:      "beta.kubernetes.io/os",
   145  							Operator: metav1.LabelSelectorOpIn,
   146  							Values:   []string{"linux"},
   147  						},
   148  					},
   149  				},
   150  			},
   151  			expected: []string{
   152  				`nodeTopology.matchExpressions[0].key: beta.kubernetes.io/arch is deprecated since v1.14; use "kubernetes.io/arch" instead`,
   153  				`nodeTopology.matchExpressions[1].key: beta.kubernetes.io/os is deprecated since v1.14; use "kubernetes.io/os" instead`,
   154  			},
   155  		},
   156  	}
   157  
   158  	for _, tc := range testcases {
   159  		t.Run("podspec_"+tc.name, func(t *testing.T) {
   160  			actual := sets.New[string](GetWarningsForCSIStorageCapacity(tc.template)...)
   161  			expected := sets.New[string](tc.expected...)
   162  			for _, missing := range sets.List[string](expected.Difference(actual)) {
   163  				t.Errorf("missing: %s", missing)
   164  			}
   165  			for _, extra := range sets.List[string](actual.Difference(expected)) {
   166  				t.Errorf("extra: %s", extra)
   167  			}
   168  		})
   169  
   170  	}
   171  }