...

Source file src/k8s.io/kubernetes/pkg/apis/storagemigration/validation/validation_test.go

Documentation: k8s.io/kubernetes/pkg/apis/storagemigration/validation

     1  /*
     2  Copyright 2024 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 validation
    18  
    19  import (
    20  	"testing"
    21  
    22  	"k8s.io/kubernetes/pkg/apis/storagemigration"
    23  
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  )
    26  
    27  // TestValidateStorageVersionMigration tests the ValidateStorageVersionMigration function
    28  func TestValidateStorageVersionMigration(t *testing.T) {
    29  	tests := []struct {
    30  		name        string
    31  		svm         *storagemigration.StorageVersionMigration
    32  		errorString string
    33  	}{
    34  		{
    35  			name: "when all fields are non-empty",
    36  			svm: &storagemigration.StorageVersionMigration{
    37  				ObjectMeta: metav1.ObjectMeta{
    38  					Name: "test-svm",
    39  				},
    40  				Spec: storagemigration.StorageVersionMigrationSpec{
    41  					Resource: storagemigration.GroupVersionResource{
    42  						Group:    "non-empty",
    43  						Version:  "non-empty",
    44  						Resource: "non-empty",
    45  					},
    46  				},
    47  			},
    48  			errorString: "",
    49  		},
    50  		{
    51  			name: "when all fields are empty",
    52  			svm: &storagemigration.StorageVersionMigration{
    53  				ObjectMeta: metav1.ObjectMeta{
    54  					Name: "test-svm",
    55  				},
    56  				Spec: storagemigration.StorageVersionMigrationSpec{
    57  					Resource: storagemigration.GroupVersionResource{
    58  						Group:    "",
    59  						Version:  "",
    60  						Resource: "",
    61  					},
    62  				},
    63  			},
    64  			errorString: "[spec.resource.resource: Required value: resource is required, spec.resource.version: Required value: version is required]",
    65  		},
    66  		{
    67  			name: "when resource is empty",
    68  			svm: &storagemigration.StorageVersionMigration{
    69  				ObjectMeta: metav1.ObjectMeta{
    70  					Name: "test-svm",
    71  				},
    72  				Spec: storagemigration.StorageVersionMigrationSpec{
    73  					Resource: storagemigration.GroupVersionResource{
    74  						Group:    "non-empty",
    75  						Version:  "non-empty",
    76  						Resource: "",
    77  					},
    78  				},
    79  			},
    80  			errorString: "spec.resource.resource: Required value: resource is required",
    81  		},
    82  		{
    83  			name: "when version is empty",
    84  			svm: &storagemigration.StorageVersionMigration{
    85  				ObjectMeta: metav1.ObjectMeta{
    86  					Name: "test-svm",
    87  				},
    88  				Spec: storagemigration.StorageVersionMigrationSpec{
    89  					Resource: storagemigration.GroupVersionResource{
    90  						Group:    "non-empty",
    91  						Version:  "",
    92  						Resource: "non-empty",
    93  					},
    94  				},
    95  			},
    96  			errorString: "spec.resource.version: Required value: version is required",
    97  		},
    98  	}
    99  
   100  	for _, test := range tests {
   101  		t.Run(test.name, func(t *testing.T) {
   102  			errors := ValidateStorageVersionMigration(test.svm)
   103  
   104  			errorString := ""
   105  			if len(errors) == 0 {
   106  				errorString = ""
   107  			} else {
   108  				errorString = errors.ToAggregate().Error()
   109  			}
   110  
   111  			if errorString != test.errorString {
   112  				t.Errorf("Expected error string %s, got %s", test.errorString, errorString)
   113  			}
   114  		})
   115  	}
   116  }
   117  

View as plain text