...

Source file src/k8s.io/kubernetes/pkg/volume/util/storageclass_test.go

Documentation: k8s.io/kubernetes/pkg/volume/util

     1  /*
     2  Copyright 2023 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  	"time"
    22  
    23  	storagev1 "k8s.io/api/storage/v1"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	"k8s.io/client-go/informers"
    26  	"k8s.io/kubernetes/pkg/controller"
    27  )
    28  
    29  func TestGetDefaultClass(t *testing.T) {
    30  
    31  	var (
    32  		t1 = time.Now()
    33  		t2 = time.Now().Add(1 * time.Hour)
    34  
    35  		sc1 = &storagev1.StorageClass{
    36  			ObjectMeta: metav1.ObjectMeta{
    37  				Name: "my-storage-class1",
    38  				Annotations: map[string]string{
    39  					"a": "b",
    40  				},
    41  			},
    42  		}
    43  		sc2 = &storagev1.StorageClass{
    44  			ObjectMeta: metav1.ObjectMeta{
    45  				Name: "my-storage-class2",
    46  				Annotations: map[string]string{
    47  					"a": "b",
    48  				},
    49  			},
    50  		}
    51  
    52  		sc3 = &storagev1.StorageClass{
    53  			ObjectMeta: metav1.ObjectMeta{
    54  				Name: "my-storage-class3",
    55  				Annotations: map[string]string{
    56  					IsDefaultStorageClassAnnotation: "true",
    57  				},
    58  				CreationTimestamp: metav1.Time{Time: t1},
    59  			},
    60  		}
    61  
    62  		sc4 = &storagev1.StorageClass{
    63  			ObjectMeta: metav1.ObjectMeta{
    64  				Name: "my-storage-class4",
    65  				Annotations: map[string]string{
    66  					IsDefaultStorageClassAnnotation: "true",
    67  				},
    68  				CreationTimestamp: metav1.Time{Time: t2},
    69  			},
    70  		}
    71  
    72  		sc5 = &storagev1.StorageClass{
    73  			ObjectMeta: metav1.ObjectMeta{
    74  				Name: "my-storage-class5",
    75  				Annotations: map[string]string{
    76  					IsDefaultStorageClassAnnotation: "true",
    77  				},
    78  				CreationTimestamp: metav1.Time{Time: t2},
    79  			},
    80  		}
    81  	)
    82  
    83  	testCases := []struct {
    84  		name    string
    85  		classes []*storagev1.StorageClass
    86  		expect  *storagev1.StorageClass
    87  	}{
    88  
    89  		{
    90  			name: "no storage class",
    91  		},
    92  
    93  		{
    94  			name:    "no default storage class",
    95  			classes: []*storagev1.StorageClass{sc1, sc2},
    96  			expect:  nil,
    97  		},
    98  
    99  		{
   100  			name:    "one default storage class",
   101  			classes: []*storagev1.StorageClass{sc1, sc2, sc3},
   102  			expect:  sc3,
   103  		},
   104  
   105  		{
   106  			name:    "two default storage class with different creation timestamp",
   107  			classes: []*storagev1.StorageClass{sc3, sc4},
   108  			expect:  sc4,
   109  		},
   110  
   111  		{
   112  			name:    "two default storage class with same creation timestamp",
   113  			classes: []*storagev1.StorageClass{sc4, sc5},
   114  			expect:  sc4,
   115  		},
   116  	}
   117  
   118  	for _, tc := range testCases {
   119  		t.Run(tc.name, func(t *testing.T) {
   120  			informerFactory := informers.NewSharedInformerFactory(nil, controller.NoResyncPeriodFunc())
   121  			for _, c := range tc.classes {
   122  				informerFactory.Storage().V1().StorageClasses().Informer().GetStore().Add(c)
   123  			}
   124  			lister := informerFactory.Storage().V1().StorageClasses().Lister()
   125  			actual, err := GetDefaultClass(lister)
   126  			if err != nil {
   127  				t.Errorf("Expected no error, got %v", err)
   128  				return
   129  			}
   130  			if tc.expect != actual {
   131  				t.Errorf("Expected %v, got %v", tc.expect, actual)
   132  			}
   133  		})
   134  	}
   135  
   136  }
   137  

View as plain text