...

Source file src/k8s.io/kubectl/pkg/util/storage/storage_test.go

Documentation: k8s.io/kubectl/pkg/util/storage

     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  	v1 "k8s.io/api/core/v1"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  )
    25  
    26  func TestIsDefaultAnnotationText(t *testing.T) {
    27  	tests := []struct {
    28  		name         string
    29  		obj          metav1.ObjectMeta
    30  		expectResult string
    31  	}{
    32  		{
    33  			name: "The annotation is not set",
    34  			obj: metav1.ObjectMeta{
    35  				Annotations: map[string]string{},
    36  			},
    37  			expectResult: "No",
    38  		},
    39  		{
    40  			name: "The annotation is set",
    41  			obj: metav1.ObjectMeta{
    42  				Annotations: map[string]string{IsDefaultStorageClassAnnotation: "true"},
    43  			},
    44  			expectResult: "Yes",
    45  		},
    46  		{
    47  			name: "The annotation is set",
    48  			obj: metav1.ObjectMeta{
    49  				Annotations: map[string]string{BetaIsDefaultStorageClassAnnotation: "true"},
    50  			},
    51  			expectResult: "Yes",
    52  		},
    53  	}
    54  	for _, tt := range tests {
    55  		t.Run(tt.name, func(t *testing.T) {
    56  			got := IsDefaultAnnotationText(tt.obj)
    57  			if got != tt.expectResult {
    58  				t.Errorf("expected result %v; got %v", tt.expectResult, got)
    59  			}
    60  		})
    61  	}
    62  }
    63  
    64  func TestGetAccessModesAsString(t *testing.T) {
    65  	tests := []struct {
    66  		name         string
    67  		modes        []v1.PersistentVolumeAccessMode
    68  		expectResult string
    69  	}{
    70  		{
    71  			name: "Contains access mode",
    72  			modes: []v1.PersistentVolumeAccessMode{
    73  				v1.ReadWriteOnce,
    74  				v1.ReadOnlyMany,
    75  				v1.ReadOnlyMany,
    76  				v1.ReadWriteMany,
    77  				v1.ReadWriteOncePod,
    78  			},
    79  			expectResult: "RWO,ROX,RWX,RWOP",
    80  		},
    81  	}
    82  	for _, tt := range tests {
    83  		t.Run(tt.name, func(t *testing.T) {
    84  			got := GetAccessModesAsString(tt.modes)
    85  			if got != tt.expectResult {
    86  				t.Errorf("expected result %v; got %v", tt.expectResult, got)
    87  			}
    88  		})
    89  	}
    90  }
    91  
    92  func TestGetPersistentVolumeClass(t *testing.T) {
    93  	tests := []struct {
    94  		name         string
    95  		volume       *v1.PersistentVolume
    96  		expectResult string
    97  	}{
    98  		{
    99  			name: "Get beta storage class annotation",
   100  			volume: &v1.PersistentVolume{
   101  				ObjectMeta: metav1.ObjectMeta{
   102  					Annotations: map[string]string{v1.BetaStorageClassAnnotation: "class"},
   103  				},
   104  			},
   105  			expectResult: "class",
   106  		},
   107  		{
   108  			name: "Get storage class name",
   109  			volume: &v1.PersistentVolume{
   110  				Spec: v1.PersistentVolumeSpec{
   111  					StorageClassName: "class1",
   112  				},
   113  			},
   114  			expectResult: "class1",
   115  		},
   116  	}
   117  	for _, tt := range tests {
   118  		t.Run(tt.name, func(t *testing.T) {
   119  			got := GetPersistentVolumeClass(tt.volume)
   120  			if got != tt.expectResult {
   121  				t.Errorf("expected result %v; got %v", tt.expectResult, got)
   122  			}
   123  		})
   124  	}
   125  }
   126  
   127  func TestGetPersistentVolumeClaimClass(t *testing.T) {
   128  	StorageClassName := "class1"
   129  	tests := []struct {
   130  		name         string
   131  		claim        *v1.PersistentVolumeClaim
   132  		expectResult string
   133  	}{
   134  		{
   135  			name: "Get beta storage class annotation",
   136  			claim: &v1.PersistentVolumeClaim{
   137  				ObjectMeta: metav1.ObjectMeta{
   138  					Annotations: map[string]string{v1.BetaStorageClassAnnotation: "class"},
   139  				},
   140  			},
   141  			expectResult: "class",
   142  		},
   143  		{
   144  			name: "Get storage class name",
   145  			claim: &v1.PersistentVolumeClaim{
   146  				Spec: v1.PersistentVolumeClaimSpec{
   147  					StorageClassName: &StorageClassName,
   148  				},
   149  			},
   150  			expectResult: StorageClassName,
   151  		},
   152  		{
   153  			name:         "No storage class",
   154  			claim:        &v1.PersistentVolumeClaim{},
   155  			expectResult: "",
   156  		},
   157  	}
   158  	for _, tt := range tests {
   159  		t.Run(tt.name, func(t *testing.T) {
   160  			got := GetPersistentVolumeClaimClass(tt.claim)
   161  			if got != tt.expectResult {
   162  				t.Errorf("expected result %v; got %v", tt.expectResult, got)
   163  			}
   164  		})
   165  	}
   166  }
   167  

View as plain text