...

Source file src/k8s.io/kubernetes/pkg/volume/csimigration/plugin_manager_test.go

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

     1  /*
     2  Copyright 2019 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 csimigration
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	v1 "k8s.io/api/core/v1"
    24  	utilfeature "k8s.io/apiserver/pkg/util/feature"
    25  	"k8s.io/component-base/featuregate"
    26  	featuregatetesting "k8s.io/component-base/featuregate/testing"
    27  	csitrans "k8s.io/csi-translation-lib"
    28  	"k8s.io/kubernetes/pkg/features"
    29  	"k8s.io/kubernetes/pkg/volume"
    30  )
    31  
    32  func TestIsMigratable(t *testing.T) {
    33  	testCases := []struct {
    34  		name                 string
    35  		pluginFeature        featuregate.Feature
    36  		pluginFeatureEnabled bool
    37  		csiMigrationEnabled  bool
    38  		isMigratable         bool
    39  		spec                 *volume.Spec
    40  	}{
    41  		{
    42  			name:                 "RBD PV source with CSIMigrationGCE enabled",
    43  			pluginFeature:        features.CSIMigrationRBD,
    44  			pluginFeatureEnabled: true,
    45  			isMigratable:         true,
    46  			csiMigrationEnabled:  true,
    47  			spec: &volume.Spec{
    48  				PersistentVolume: &v1.PersistentVolume{
    49  					Spec: v1.PersistentVolumeSpec{
    50  						PersistentVolumeSource: v1.PersistentVolumeSource{
    51  							RBD: &v1.RBDPersistentVolumeSource{
    52  								RBDImage: "test-disk",
    53  							},
    54  						},
    55  					},
    56  				},
    57  			},
    58  		},
    59  		{
    60  			name:                 "RBD PD PV Source with CSIMigrationGCE disabled",
    61  			pluginFeature:        features.CSIMigrationRBD,
    62  			pluginFeatureEnabled: false,
    63  			isMigratable:         false,
    64  			csiMigrationEnabled:  true,
    65  			spec: &volume.Spec{
    66  				PersistentVolume: &v1.PersistentVolume{
    67  					Spec: v1.PersistentVolumeSpec{
    68  						PersistentVolumeSource: v1.PersistentVolumeSource{
    69  							RBD: &v1.RBDPersistentVolumeSource{
    70  								RBDImage: "test-disk",
    71  							},
    72  						},
    73  					},
    74  				},
    75  			},
    76  		},
    77  	}
    78  	csiTranslator := csitrans.New()
    79  	for _, test := range testCases {
    80  		pm := NewPluginManager(csiTranslator, utilfeature.DefaultFeatureGate)
    81  		t.Run(fmt.Sprintf("Testing %v", test.name), func(t *testing.T) {
    82  			defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, test.pluginFeature, test.pluginFeatureEnabled)()
    83  			migratable, err := pm.IsMigratable(test.spec)
    84  			if migratable != test.isMigratable {
    85  				t.Errorf("Expected migratability of spec: %v does not match obtained migratability: %v", test.isMigratable, migratable)
    86  			}
    87  			if err != nil {
    88  				t.Errorf("Unexpected error: %v", err)
    89  			}
    90  		})
    91  	}
    92  }
    93  
    94  func TestMigrationFeatureFlagStatus(t *testing.T) {
    95  	testCases := []struct {
    96  		name                          string
    97  		pluginName                    string
    98  		csiMigrationEnabled           bool
    99  		pluginFeature                 featuregate.Feature
   100  		pluginFeatureEnabled          bool
   101  		inTreePluginUnregister        featuregate.Feature
   102  		inTreePluginUnregisterEnabled bool
   103  		csiMigrationResult            bool
   104  		csiMigrationCompleteResult    bool
   105  	}{
   106  		{
   107  			name:                          "gce-pd migration flag enabled and migration-complete flag disabled with CSI migration flag",
   108  			pluginName:                    "kubernetes.io/gce-pd",
   109  			pluginFeatureEnabled:          true,
   110  			csiMigrationEnabled:           true,
   111  			inTreePluginUnregister:        features.InTreePluginGCEUnregister,
   112  			inTreePluginUnregisterEnabled: false,
   113  			csiMigrationResult:            true,
   114  			csiMigrationCompleteResult:    false,
   115  		},
   116  		{
   117  			name:                          "gce-pd migration flag enabled and migration-complete flag enabled with CSI migration flag",
   118  			pluginName:                    "kubernetes.io/gce-pd",
   119  			pluginFeatureEnabled:          true,
   120  			csiMigrationEnabled:           true,
   121  			inTreePluginUnregister:        features.InTreePluginGCEUnregister,
   122  			inTreePluginUnregisterEnabled: true,
   123  			csiMigrationResult:            true,
   124  			csiMigrationCompleteResult:    true,
   125  		},
   126  	}
   127  	csiTranslator := csitrans.New()
   128  	for _, test := range testCases {
   129  		pm := NewPluginManager(csiTranslator, utilfeature.DefaultFeatureGate)
   130  		t.Run(fmt.Sprintf("Testing %v", test.name), func(t *testing.T) {
   131  			// CSIMigrationGCE is locked to on, so it cannot be enabled or disabled. There are a couple
   132  			// of test cases that check correct behavior when CSIMigrationGCE is enabled, but there are
   133  			// no longer any tests cases for CSIMigrationGCE being disabled as that is not possible.
   134  			if len(test.pluginFeature) > 0 {
   135  				defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, test.pluginFeature, test.pluginFeatureEnabled)()
   136  			}
   137  			defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, test.inTreePluginUnregister, test.inTreePluginUnregisterEnabled)()
   138  
   139  			csiMigrationResult := pm.IsMigrationEnabledForPlugin(test.pluginName)
   140  			if csiMigrationResult != test.csiMigrationResult {
   141  				t.Errorf("Expected migratability of plugin %v: %v does not match obtained migratability: %v", test.pluginName, test.csiMigrationResult, csiMigrationResult)
   142  			}
   143  			csiMigrationCompleteResult := pm.IsMigrationCompleteForPlugin(test.pluginName)
   144  			if csiMigrationCompleteResult != test.csiMigrationCompleteResult {
   145  				t.Errorf("Expected migration complete status of plugin: %v: %v does not match obtained migratability: %v", test.pluginName, test.csiMigrationCompleteResult, csiMigrationResult)
   146  			}
   147  		})
   148  	}
   149  }
   150  

View as plain text