12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649 |
- /*
- Copyright 2016 The Kubernetes Authors.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package validation
- import (
- "fmt"
- "strings"
- "testing"
- "k8s.io/apimachinery/pkg/api/resource"
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- utilfeature "k8s.io/apiserver/pkg/util/feature"
- featuregatetesting "k8s.io/component-base/featuregate/testing"
- api "k8s.io/kubernetes/pkg/apis/core"
- "k8s.io/kubernetes/pkg/apis/storage"
- "k8s.io/kubernetes/pkg/features"
- )
- var (
- deleteReclaimPolicy = api.PersistentVolumeReclaimDelete
- immediateMode1 = storage.VolumeBindingImmediate
- immediateMode2 = storage.VolumeBindingImmediate
- waitingMode = storage.VolumeBindingWaitForFirstConsumer
- invalidMode = storage.VolumeBindingMode("foo")
- inlineSpec = api.PersistentVolumeSpec{
- AccessModes: []api.PersistentVolumeAccessMode{api.ReadWriteOnce},
- PersistentVolumeSource: api.PersistentVolumeSource{
- CSI: &api.CSIPersistentVolumeSource{
- Driver: "com.test.foo",
- VolumeHandle: "foobar",
- },
- },
- }
- )
- func TestValidateStorageClass(t *testing.T) {
- deleteReclaimPolicy := api.PersistentVolumeReclaimPolicy("Delete")
- retainReclaimPolicy := api.PersistentVolumeReclaimPolicy("Retain")
- recycleReclaimPolicy := api.PersistentVolumeReclaimPolicy("Recycle")
- successCases := []storage.StorageClass{
- {
- // empty parameters
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Provisioner: "kubernetes.io/foo-provisioner",
- Parameters: map[string]string{},
- ReclaimPolicy: &deleteReclaimPolicy,
- VolumeBindingMode: &immediateMode1,
- },
- {
- // nil parameters
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Provisioner: "kubernetes.io/foo-provisioner",
- ReclaimPolicy: &deleteReclaimPolicy,
- VolumeBindingMode: &immediateMode1,
- },
- {
- // some parameters
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Provisioner: "kubernetes.io/foo-provisioner",
- Parameters: map[string]string{
- "kubernetes.io/foo-parameter": "free/form/string",
- "foo-parameter": "free-form-string",
- "foo-parameter2": "{\"embedded\": \"json\", \"with\": {\"structures\":\"inside\"}}",
- },
- ReclaimPolicy: &deleteReclaimPolicy,
- VolumeBindingMode: &immediateMode1,
- },
- {
- // retain reclaimPolicy
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Provisioner: "kubernetes.io/foo-provisioner",
- ReclaimPolicy: &retainReclaimPolicy,
- VolumeBindingMode: &immediateMode1,
- },
- }
- // Success cases are expected to pass validation.
- for k, v := range successCases {
- if errs := ValidateStorageClass(&v); len(errs) != 0 {
- t.Errorf("Expected success for %d, got %v", k, errs)
- }
- }
- // generate a map longer than maxProvisionerParameterSize
- longParameters := make(map[string]string)
- totalSize := 0
- for totalSize < maxProvisionerParameterSize {
- k := fmt.Sprintf("param/%d", totalSize)
- v := fmt.Sprintf("value-%d", totalSize)
- longParameters[k] = v
- totalSize = totalSize + len(k) + len(v)
- }
- errorCases := map[string]storage.StorageClass{
- "namespace is present": {
- ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "bar"},
- Provisioner: "kubernetes.io/foo-provisioner",
- ReclaimPolicy: &deleteReclaimPolicy,
- },
- "invalid provisioner": {
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Provisioner: "kubernetes.io/invalid/provisioner",
- ReclaimPolicy: &deleteReclaimPolicy,
- },
- "invalid empty parameter name": {
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Provisioner: "kubernetes.io/foo",
- Parameters: map[string]string{
- "": "value",
- },
- ReclaimPolicy: &deleteReclaimPolicy,
- },
- "provisioner: Required value": {
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Provisioner: "",
- ReclaimPolicy: &deleteReclaimPolicy,
- },
- "too long parameters": {
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Provisioner: "kubernetes.io/foo",
- Parameters: longParameters,
- ReclaimPolicy: &deleteReclaimPolicy,
- },
- "invalid reclaimpolicy": {
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Provisioner: "kubernetes.io/foo",
- ReclaimPolicy: &recycleReclaimPolicy,
- },
- }
- // Error cases are not expected to pass validation.
- for testName, storageClass := range errorCases {
- if errs := ValidateStorageClass(&storageClass); len(errs) == 0 {
- t.Errorf("Expected failure for test: %s", testName)
- }
- }
- }
- func TestVolumeAttachmentValidation(t *testing.T) {
- defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CSIMigration, true)()
- volumeName := "pv-name"
- empty := ""
- migrationEnabledSuccessCases := []storage.VolumeAttachment{
- {
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Spec: storage.VolumeAttachmentSpec{
- Attacher: "myattacher",
- Source: storage.VolumeAttachmentSource{
- PersistentVolumeName: &volumeName,
- },
- NodeName: "mynode",
- },
- },
- {
- ObjectMeta: metav1.ObjectMeta{Name: "foo-with-inlinespec"},
- Spec: storage.VolumeAttachmentSpec{
- Attacher: "myattacher",
- Source: storage.VolumeAttachmentSource{
- InlineVolumeSpec: &inlineSpec,
- },
- NodeName: "mynode",
- },
- },
- {
- ObjectMeta: metav1.ObjectMeta{Name: "foo-with-status"},
- Spec: storage.VolumeAttachmentSpec{
- Attacher: "myattacher",
- Source: storage.VolumeAttachmentSource{
- PersistentVolumeName: &volumeName,
- },
- NodeName: "mynode",
- },
- Status: storage.VolumeAttachmentStatus{
- Attached: true,
- AttachmentMetadata: map[string]string{
- "foo": "bar",
- },
- AttachError: &storage.VolumeError{
- Time: metav1.Time{},
- Message: "hello world",
- },
- DetachError: &storage.VolumeError{
- Time: metav1.Time{},
- Message: "hello world",
- },
- },
- },
- {
- ObjectMeta: metav1.ObjectMeta{Name: "foo-with-inlinespec-and-status"},
- Spec: storage.VolumeAttachmentSpec{
- Attacher: "myattacher",
- Source: storage.VolumeAttachmentSource{
- InlineVolumeSpec: &inlineSpec,
- },
- NodeName: "mynode",
- },
- Status: storage.VolumeAttachmentStatus{
- Attached: true,
- AttachmentMetadata: map[string]string{
- "foo": "bar",
- },
- AttachError: &storage.VolumeError{
- Time: metav1.Time{},
- Message: "hello world",
- },
- DetachError: &storage.VolumeError{
- Time: metav1.Time{},
- Message: "hello world",
- },
- },
- },
- }
- for _, volumeAttachment := range migrationEnabledSuccessCases {
- if errs := ValidateVolumeAttachment(&volumeAttachment); len(errs) != 0 {
- t.Errorf("expected success: %v %v", volumeAttachment, errs)
- }
- }
- migrationEnabledErrorCases := []storage.VolumeAttachment{
- {
- // Empty attacher name
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Spec: storage.VolumeAttachmentSpec{
- Attacher: "",
- NodeName: "mynode",
- Source: storage.VolumeAttachmentSource{
- PersistentVolumeName: &volumeName,
- },
- },
- },
- {
- // Empty node name
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Spec: storage.VolumeAttachmentSpec{
- Attacher: "myattacher",
- NodeName: "",
- Source: storage.VolumeAttachmentSource{
- PersistentVolumeName: &volumeName,
- },
- },
- },
- {
- // No volume name
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Spec: storage.VolumeAttachmentSpec{
- Attacher: "myattacher",
- NodeName: "node",
- Source: storage.VolumeAttachmentSource{
- PersistentVolumeName: nil,
- },
- },
- },
- {
- // Empty volume name
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Spec: storage.VolumeAttachmentSpec{
- Attacher: "myattacher",
- NodeName: "node",
- Source: storage.VolumeAttachmentSource{
- PersistentVolumeName: &empty,
- },
- },
- },
- {
- // Too long error message
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Spec: storage.VolumeAttachmentSpec{
- Attacher: "myattacher",
- NodeName: "node",
- Source: storage.VolumeAttachmentSource{
- PersistentVolumeName: &volumeName,
- },
- },
- Status: storage.VolumeAttachmentStatus{
- Attached: true,
- AttachmentMetadata: map[string]string{
- "foo": "bar",
- },
- AttachError: &storage.VolumeError{
- Time: metav1.Time{},
- Message: "hello world",
- },
- DetachError: &storage.VolumeError{
- Time: metav1.Time{},
- Message: strings.Repeat("a", maxVolumeErrorMessageSize+1),
- },
- },
- },
- {
- // Too long metadata
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Spec: storage.VolumeAttachmentSpec{
- Attacher: "myattacher",
- NodeName: "node",
- Source: storage.VolumeAttachmentSource{
- PersistentVolumeName: &volumeName,
- },
- },
- Status: storage.VolumeAttachmentStatus{
- Attached: true,
- AttachmentMetadata: map[string]string{
- "foo": strings.Repeat("a", maxAttachedVolumeMetadataSize),
- },
- AttachError: &storage.VolumeError{
- Time: metav1.Time{},
- Message: "hello world",
- },
- DetachError: &storage.VolumeError{
- Time: metav1.Time{},
- Message: "hello world",
- },
- },
- },
- {
- // VolumeAttachmentSource with no PersistentVolumeName nor InlineSpec
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Spec: storage.VolumeAttachmentSpec{
- Attacher: "myattacher",
- NodeName: "node",
- Source: storage.VolumeAttachmentSource{},
- },
- },
- {
- // VolumeAttachmentSource with PersistentVolumeName and InlineSpec
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Spec: storage.VolumeAttachmentSpec{
- Attacher: "myattacher",
- NodeName: "node",
- Source: storage.VolumeAttachmentSource{
- PersistentVolumeName: &volumeName,
- InlineVolumeSpec: &inlineSpec,
- },
- },
- },
- {
- // VolumeAttachmentSource with InlineSpec without CSI PV Source
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Spec: storage.VolumeAttachmentSpec{
- Attacher: "myattacher",
- NodeName: "node",
- Source: storage.VolumeAttachmentSource{
- PersistentVolumeName: &volumeName,
- InlineVolumeSpec: &api.PersistentVolumeSpec{
- Capacity: api.ResourceList{
- api.ResourceName(api.ResourceStorage): resource.MustParse("10G"),
- },
- AccessModes: []api.PersistentVolumeAccessMode{api.ReadWriteOnce},
- PersistentVolumeSource: api.PersistentVolumeSource{
- FlexVolume: &api.FlexPersistentVolumeSource{
- Driver: "kubernetes.io/blue",
- FSType: "ext4",
- },
- },
- StorageClassName: "test-storage-class",
- },
- },
- },
- },
- }
- for _, volumeAttachment := range migrationEnabledErrorCases {
- if errs := ValidateVolumeAttachment(&volumeAttachment); len(errs) == 0 {
- t.Errorf("expected failure for test: %v", volumeAttachment)
- }
- }
- // validate with CSIMigration disabled
- defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CSIMigration, false)()
- migrationDisabledSuccessCases := []storage.VolumeAttachment{
- {
- // PVName specified with migration disabled
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Spec: storage.VolumeAttachmentSpec{
- Attacher: "myattacher",
- NodeName: "node",
- Source: storage.VolumeAttachmentSource{
- PersistentVolumeName: &volumeName,
- },
- },
- },
- {
- // InlineSpec specified with migration disabled
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Spec: storage.VolumeAttachmentSpec{
- Attacher: "myattacher",
- NodeName: "node",
- Source: storage.VolumeAttachmentSource{
- InlineVolumeSpec: &inlineSpec,
- },
- },
- },
- }
- for _, volumeAttachment := range migrationDisabledSuccessCases {
- if errs := ValidateVolumeAttachment(&volumeAttachment); len(errs) != 0 {
- t.Errorf("expected success: %v %v", volumeAttachment, errs)
- }
- }
- }
- func TestVolumeAttachmentUpdateValidation(t *testing.T) {
- defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CSIMigration, true)()
- volumeName := "foo"
- newVolumeName := "bar"
- old := storage.VolumeAttachment{
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Spec: storage.VolumeAttachmentSpec{
- Attacher: "myattacher",
- Source: storage.VolumeAttachmentSource{},
- NodeName: "mynode",
- },
- }
- successCases := []storage.VolumeAttachment{
- {
- // no change
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Spec: storage.VolumeAttachmentSpec{
- Attacher: "myattacher",
- Source: storage.VolumeAttachmentSource{},
- NodeName: "mynode",
- },
- },
- {
- // modify status
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Spec: storage.VolumeAttachmentSpec{
- Attacher: "myattacher",
- Source: storage.VolumeAttachmentSource{},
- NodeName: "mynode",
- },
- Status: storage.VolumeAttachmentStatus{
- Attached: true,
- AttachmentMetadata: map[string]string{
- "foo": "bar",
- },
- AttachError: &storage.VolumeError{
- Time: metav1.Time{},
- Message: "hello world",
- },
- DetachError: &storage.VolumeError{
- Time: metav1.Time{},
- Message: "hello world",
- },
- },
- },
- }
- for _, volumeAttachment := range successCases {
- volumeAttachment.Spec.Source = storage.VolumeAttachmentSource{}
- old.Spec.Source = storage.VolumeAttachmentSource{}
- // test scenarios with PersistentVolumeName set
- volumeAttachment.Spec.Source.PersistentVolumeName = &volumeName
- old.Spec.Source.PersistentVolumeName = &volumeName
- if errs := ValidateVolumeAttachmentUpdate(&volumeAttachment, &old); len(errs) != 0 {
- t.Errorf("expected success: %+v", errs)
- }
- volumeAttachment.Spec.Source = storage.VolumeAttachmentSource{}
- old.Spec.Source = storage.VolumeAttachmentSource{}
- // test scenarios with InlineVolumeSpec set
- volumeAttachment.Spec.Source.InlineVolumeSpec = &inlineSpec
- old.Spec.Source.InlineVolumeSpec = &inlineSpec
- if errs := ValidateVolumeAttachmentUpdate(&volumeAttachment, &old); len(errs) != 0 {
- t.Errorf("expected success: %+v", errs)
- }
- }
- // reset old's source with volumeName in case it was left with something else by earlier tests
- old.Spec.Source = storage.VolumeAttachmentSource{}
- old.Spec.Source.PersistentVolumeName = &volumeName
- errorCases := []storage.VolumeAttachment{
- {
- // change attacher
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Spec: storage.VolumeAttachmentSpec{
- Attacher: "another-attacher",
- Source: storage.VolumeAttachmentSource{
- PersistentVolumeName: &volumeName,
- },
- NodeName: "mynode",
- },
- },
- {
- // change source volume name
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Spec: storage.VolumeAttachmentSpec{
- Attacher: "myattacher",
- Source: storage.VolumeAttachmentSource{
- PersistentVolumeName: &newVolumeName,
- },
- NodeName: "mynode",
- },
- },
- {
- // change node
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Spec: storage.VolumeAttachmentSpec{
- Attacher: "myattacher",
- Source: storage.VolumeAttachmentSource{
- PersistentVolumeName: &volumeName,
- },
- NodeName: "anothernode",
- },
- },
- {
- // change source
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Spec: storage.VolumeAttachmentSpec{
- Attacher: "myattacher",
- Source: storage.VolumeAttachmentSource{
- InlineVolumeSpec: &inlineSpec,
- },
- NodeName: "mynode",
- },
- },
- {
- // add invalid status
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Spec: storage.VolumeAttachmentSpec{
- Attacher: "myattacher",
- Source: storage.VolumeAttachmentSource{
- PersistentVolumeName: &volumeName,
- },
- NodeName: "mynode",
- },
- Status: storage.VolumeAttachmentStatus{
- Attached: true,
- AttachmentMetadata: map[string]string{
- "foo": "bar",
- },
- AttachError: &storage.VolumeError{
- Time: metav1.Time{},
- Message: strings.Repeat("a", maxAttachedVolumeMetadataSize),
- },
- DetachError: &storage.VolumeError{
- Time: metav1.Time{},
- Message: "hello world",
- },
- },
- },
- }
- for _, volumeAttachment := range errorCases {
- if errs := ValidateVolumeAttachmentUpdate(&volumeAttachment, &old); len(errs) == 0 {
- t.Errorf("Expected failure for test: %+v", volumeAttachment)
- }
- }
- }
- func TestVolumeAttachmentValidationV1(t *testing.T) {
- volumeName := "pv-name"
- invalidVolumeName := "-invalid-@#$%^&*()-"
- successCases := []storage.VolumeAttachment{
- {
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Spec: storage.VolumeAttachmentSpec{
- Attacher: "myattacher",
- Source: storage.VolumeAttachmentSource{
- PersistentVolumeName: &volumeName,
- },
- NodeName: "mynode",
- },
- },
- }
- for _, volumeAttachment := range successCases {
- if errs := ValidateVolumeAttachmentV1(&volumeAttachment); len(errs) != 0 {
- t.Errorf("expected success: %+v", errs)
- }
- }
- errorCases := []storage.VolumeAttachment{
- {
- // Invalid attacher name
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Spec: storage.VolumeAttachmentSpec{
- Attacher: "invalid-@#$%^&*()",
- NodeName: "mynode",
- Source: storage.VolumeAttachmentSource{
- PersistentVolumeName: &volumeName,
- },
- },
- },
- {
- // Invalid PV name
- ObjectMeta: metav1.ObjectMeta{Name: "foo"},
- Spec: storage.VolumeAttachmentSpec{
- Attacher: "myattacher",
- NodeName: "mynode",
- Source: storage.VolumeAttachmentSource{
- PersistentVolumeName: &invalidVolumeName,
- },
- },
- },
- }
- for _, volumeAttachment := range errorCases {
- if errs := ValidateVolumeAttachmentV1(&volumeAttachment); len(errs) == 0 {
- t.Errorf("Expected failure for test: %+v", volumeAttachment)
- }
- }
- }
- func makeClass(mode *storage.VolumeBindingMode, topologies []api.TopologySelectorTerm) *storage.StorageClass {
- return &storage.StorageClass{
- ObjectMeta: metav1.ObjectMeta{Name: "foo", ResourceVersion: "foo"},
- Provisioner: "kubernetes.io/foo-provisioner",
- ReclaimPolicy: &deleteReclaimPolicy,
- VolumeBindingMode: mode,
- AllowedTopologies: topologies,
- }
- }
- type bindingTest struct {
- class *storage.StorageClass
- shouldSucceed bool
- }
- func TestValidateVolumeBindingMode(t *testing.T) {
- cases := map[string]bindingTest{
- "no mode": {
- class: makeClass(nil, nil),
- shouldSucceed: false,
- },
- "immediate mode": {
- class: makeClass(&immediateMode1, nil),
- shouldSucceed: true,
- },
- "waiting mode": {
- class: makeClass(&waitingMode, nil),
- shouldSucceed: true,
- },
- "invalid mode": {
- class: makeClass(&invalidMode, nil),
- shouldSucceed: false,
- },
- }
- for testName, testCase := range cases {
- errs := ValidateStorageClass(testCase.class)
- if testCase.shouldSucceed && len(errs) != 0 {
- t.Errorf("Expected success for test %q, got %v", testName, errs)
- }
- if !testCase.shouldSucceed && len(errs) == 0 {
- t.Errorf("Expected failure for test %q, got success", testName)
- }
- }
- }
- type updateTest struct {
- oldClass *storage.StorageClass
- newClass *storage.StorageClass
- shouldSucceed bool
- }
- func TestValidateUpdateVolumeBindingMode(t *testing.T) {
- noBinding := makeClass(nil, nil)
- immediateBinding1 := makeClass(&immediateMode1, nil)
- immediateBinding2 := makeClass(&immediateMode2, nil)
- waitBinding := makeClass(&waitingMode, nil)
- cases := map[string]updateTest{
- "old and new no mode": {
- oldClass: noBinding,
- newClass: noBinding,
- shouldSucceed: true,
- },
- "old and new same mode ptr": {
- oldClass: immediateBinding1,
- newClass: immediateBinding1,
- shouldSucceed: true,
- },
- "old and new same mode value": {
- oldClass: immediateBinding1,
- newClass: immediateBinding2,
- shouldSucceed: true,
- },
- "old no mode, new mode": {
- oldClass: noBinding,
- newClass: waitBinding,
- shouldSucceed: false,
- },
- "old mode, new no mode": {
- oldClass: waitBinding,
- newClass: noBinding,
- shouldSucceed: false,
- },
- "old and new different modes": {
- oldClass: waitBinding,
- newClass: immediateBinding1,
- shouldSucceed: false,
- },
- }
- for testName, testCase := range cases {
- errs := ValidateStorageClassUpdate(testCase.newClass, testCase.oldClass)
- if testCase.shouldSucceed && len(errs) != 0 {
- t.Errorf("Expected success for %v, got %v", testName, errs)
- }
- if !testCase.shouldSucceed && len(errs) == 0 {
- t.Errorf("Expected failure for %v, got success", testName)
- }
- }
- }
- func TestValidateAllowedTopologies(t *testing.T) {
- validTopology := []api.TopologySelectorTerm{
- {
- MatchLabelExpressions: []api.TopologySelectorLabelRequirement{
- {
- Key: "failure-domain.beta.kubernetes.io/zone",
- Values: []string{"zone1"},
- },
- {
- Key: "kubernetes.io/hostname",
- Values: []string{"node1"},
- },
- },
- },
- {
- MatchLabelExpressions: []api.TopologySelectorLabelRequirement{
- {
- Key: "failure-domain.beta.kubernetes.io/zone",
- Values: []string{"zone2"},
- },
- {
- Key: "kubernetes.io/hostname",
- Values: []string{"node2"},
- },
- },
- },
- }
- topologyInvalidKey := []api.TopologySelectorTerm{
- {
- MatchLabelExpressions: []api.TopologySelectorLabelRequirement{
- {
- Key: "/invalidkey",
- Values: []string{"zone1"},
- },
- },
- },
- }
- topologyLackOfValues := []api.TopologySelectorTerm{
- {
- MatchLabelExpressions: []api.TopologySelectorLabelRequirement{
- {
- Key: "kubernetes.io/hostname",
- Values: []string{},
- },
- },
- },
- }
- topologyDupValues := []api.TopologySelectorTerm{
- {
- MatchLabelExpressions: []api.TopologySelectorLabelRequirement{
- {
- Key: "kubernetes.io/hostname",
- Values: []string{"node1", "node1"},
- },
- },
- },
- }
- topologyMultiValues := []api.TopologySelectorTerm{
- {
- MatchLabelExpressions: []api.TopologySelectorLabelRequirement{
- {
- Key: "kubernetes.io/hostname",
- Values: []string{"node1", "node2"},
- },
- },
- },
- }
- topologyEmptyMatchLabelExpressions := []api.TopologySelectorTerm{
- {
- MatchLabelExpressions: nil,
- },
- }
- topologyDupKeys := []api.TopologySelectorTerm{
- {
- MatchLabelExpressions: []api.TopologySelectorLabelRequirement{
- {
- Key: "kubernetes.io/hostname",
- Values: []string{"node1"},
- },
- {
- Key: "kubernetes.io/hostname",
- Values: []string{"node2"},
- },
- },
- },
- }
- topologyMultiTerm := []api.TopologySelectorTerm{
- {
- MatchLabelExpressions: []api.TopologySelectorLabelRequirement{
- {
- Key: "kubernetes.io/hostname",
- Values: []string{"node1"},
- },
- },
- },
- {
- MatchLabelExpressions: []api.TopologySelectorLabelRequirement{
- {
- Key: "kubernetes.io/hostname",
- Values: []string{"node2"},
- },
- },
- },
- }
- topologyDupTermsIdentical := []api.TopologySelectorTerm{
- {
- MatchLabelExpressions: []api.TopologySelectorLabelRequirement{
- {
- Key: "failure-domain.beta.kubernetes.io/zone",
- Values: []string{"zone1"},
- },
- {
- Key: "kubernetes.io/hostname",
- Values: []string{"node1"},
- },
- },
- },
- {
- MatchLabelExpressions: []api.TopologySelectorLabelRequirement{
- {
- Key: "failure-domain.beta.kubernetes.io/zone",
- Values: []string{"zone1"},
- },
- {
- Key: "kubernetes.io/hostname",
- Values: []string{"node1"},
- },
- },
- },
- }
- topologyExprsOneSameOneDiff := []api.TopologySelectorTerm{
- {
- MatchLabelExpressions: []api.TopologySelectorLabelRequirement{
- {
- Key: "failure-domain.beta.kubernetes.io/zone",
- Values: []string{"zone1"},
- },
- {
- Key: "kubernetes.io/hostname",
- Values: []string{"node1"},
- },
- },
- },
- {
- MatchLabelExpressions: []api.TopologySelectorLabelRequirement{
- {
- Key: "failure-domain.beta.kubernetes.io/zone",
- Values: []string{"zone1"},
- },
- {
- Key: "kubernetes.io/hostname",
- Values: []string{"node2"},
- },
- },
- },
- }
- topologyValuesOneSameOneDiff := []api.TopologySelectorTerm{
- {
- MatchLabelExpressions: []api.TopologySelectorLabelRequirement{
- {
- Key: "kubernetes.io/hostname",
- Values: []string{"node1", "node2"},
- },
- },
- },
- {
- MatchLabelExpressions: []api.TopologySelectorLabelRequirement{
- {
- Key: "kubernetes.io/hostname",
- Values: []string{"node1", "node3"},
- },
- },
- },
- }
- topologyDupTermsDiffExprOrder := []api.TopologySelectorTerm{
- {
- MatchLabelExpressions: []api.TopologySelectorLabelRequirement{
- {
- Key: "kubernetes.io/hostname",
- Values: []string{"node1"},
- },
- {
- Key: "failure-domain.beta.kubernetes.io/zone",
- Values: []string{"zone1"},
- },
- },
- },
- {
- MatchLabelExpressions: []api.TopologySelectorLabelRequirement{
- {
- Key: "failure-domain.beta.kubernetes.io/zone",
- Values: []string{"zone1"},
- },
- {
- Key: "kubernetes.io/hostname",
- Values: []string{"node1"},
- },
- },
- },
- }
- topologyDupTermsDiffValueOrder := []api.TopologySelectorTerm{
- {
- MatchLabelExpressions: []api.TopologySelectorLabelRequirement{
- {
- Key: "failure-domain.beta.kubernetes.io/zone",
- Values: []string{"zone1", "zone2"},
- },
- },
- },
- {
- MatchLabelExpressions: []api.TopologySelectorLabelRequirement{
- {
- Key: "failure-domain.beta.kubernetes.io/zone",
- Values: []string{"zone2", "zone1"},
- },
- },
- },
- }
- cases := map[string]bindingTest{
- "no topology": {
- class: makeClass(&waitingMode, nil),
- shouldSucceed: true,
- },
- "valid topology": {
- class: makeClass(&waitingMode, validTopology),
- shouldSucceed: true,
- },
- "topology invalid key": {
- class: makeClass(&waitingMode, topologyInvalidKey),
- shouldSucceed: false,
- },
- "topology lack of values": {
- class: makeClass(&waitingMode, topologyLackOfValues),
- shouldSucceed: false,
- },
- "duplicate TopologySelectorRequirement values": {
- class: makeClass(&waitingMode, topologyDupValues),
- shouldSucceed: false,
- },
- "multiple TopologySelectorRequirement values": {
- class: makeClass(&waitingMode, topologyMultiValues),
- shouldSucceed: true,
- },
- "empty MatchLabelExpressions": {
- class: makeClass(&waitingMode, topologyEmptyMatchLabelExpressions),
- shouldSucceed: false,
- },
- "duplicate MatchLabelExpression keys": {
- class: makeClass(&waitingMode, topologyDupKeys),
- shouldSucceed: false,
- },
- "duplicate MatchLabelExpression keys but across separate terms": {
- class: makeClass(&waitingMode, topologyMultiTerm),
- shouldSucceed: true,
- },
- "duplicate AllowedTopologies terms - identical": {
- class: makeClass(&waitingMode, topologyDupTermsIdentical),
- shouldSucceed: false,
- },
- "two AllowedTopologies terms, with a pair of the same MatchLabelExpressions and a pair of different ones": {
- class: makeClass(&waitingMode, topologyExprsOneSameOneDiff),
- shouldSucceed: true,
- },
- "two AllowedTopologies terms, with a pair of the same Values and a pair of different ones": {
- class: makeClass(&waitingMode, topologyValuesOneSameOneDiff),
- shouldSucceed: true,
- },
- "duplicate AllowedTopologies terms - different MatchLabelExpressions order": {
- class: makeClass(&waitingMode, topologyDupTermsDiffExprOrder),
- shouldSucceed: false,
- },
- "duplicate AllowedTopologies terms - different TopologySelectorRequirement values order": {
- class: makeClass(&waitingMode, topologyDupTermsDiffValueOrder),
- shouldSucceed: false,
- },
- }
- for testName, testCase := range cases {
- errs := ValidateStorageClass(testCase.class)
- if testCase.shouldSucceed && len(errs) != 0 {
- t.Errorf("Expected success for test %q, got %v", testName, errs)
- }
- if !testCase.shouldSucceed && len(errs) == 0 {
- t.Errorf("Expected failure for test %q, got success", testName)
- }
- }
- }
- func TestCSINodeValidation(t *testing.T) {
- driverName := "driver-name"
- driverName2 := "1io.kubernetes-storage-2-csi-driver3"
- longName := "my-a-b-c-d-c-f-g-h-i-j-k-l-m-n-o-p-q-r-s-t-u-v-w-x-y-z-ABCDEFGHIJKLMNOPQRSTUVWXYZ-driver"
- nodeID := "nodeA"
- successCases := []storage.CSINode{
- {
- // driver name: dot only
- ObjectMeta: metav1.ObjectMeta{Name: "foo1"},
- Spec: storage.CSINodeSpec{
- Drivers: []storage.CSINodeDriver{
- {
- Name: "io.kubernetes.storage.csi.driver",
- NodeID: nodeID,
- TopologyKeys: []string{"company.com/zone1", "company.com/zone2"},
- },
- },
- },
- },
- {
- // driver name: dash only
- ObjectMeta: metav1.ObjectMeta{Name: "foo2"},
- Spec: storage.CSINodeSpec{
- Drivers: []storage.CSINodeDriver{
- {
- Name: "io-kubernetes-storage-csi-driver",
- NodeID: nodeID,
- TopologyKeys: []string{"company.com/zone1", "company.com/zone2"},
- },
- },
- },
- },
- {
- // driver name: numbers
- ObjectMeta: metav1.ObjectMeta{Name: "foo3"},
- Spec: storage.CSINodeSpec{
- Drivers: []storage.CSINodeDriver{
- {
- Name: "1io-kubernetes-storage-2-csi-driver3",
- NodeID: nodeID,
- TopologyKeys: []string{"company.com/zone1", "company.com/zone2"},
- },
- },
- },
- },
- {
- // driver name: dot, dash
- ObjectMeta: metav1.ObjectMeta{Name: "foo4"},
- Spec: storage.CSINodeSpec{
- Drivers: []storage.CSINodeDriver{
- {
- Name: "io.kubernetes.storage-csi-driver",
- NodeID: nodeID,
- TopologyKeys: []string{"company.com/zone1", "company.com/zone2"},
- },
- },
- },
- },
- {
- // driver name: dot, dash, and numbers
- ObjectMeta: metav1.ObjectMeta{Name: "foo5"},
- Spec: storage.CSINodeSpec{
- Drivers: []storage.CSINodeDriver{
- {
- Name: driverName2,
- NodeID: nodeID,
- TopologyKeys: []string{"company.com/zone1", "company.com/zone2"},
- },
- },
- },
- },
- {
- // Driver name length 1
- ObjectMeta: metav1.ObjectMeta{Name: "foo2"},
- Spec: storage.CSINodeSpec{
- Drivers: []storage.CSINodeDriver{
- {
- Name: "a",
- NodeID: nodeID,
- TopologyKeys: []string{"company.com/zone1", "company.com/zone2"},
- },
- },
- },
- },
- {
- // multiple drivers with different node IDs, topology keys
- ObjectMeta: metav1.ObjectMeta{Name: "foo6"},
- Spec: storage.CSINodeSpec{
- Drivers: []storage.CSINodeDriver{
- {
- Name: "driver1",
- NodeID: "node1",
- TopologyKeys: []string{"key1", "key2"},
- },
- {
- Name: "driverB",
- NodeID: "nodeA",
- TopologyKeys: []string{"keyA", "keyB"},
- },
- },
- },
- },
- {
- // multiple drivers with same node IDs, topology keys
- ObjectMeta: metav1.ObjectMeta{Name: "foo7"},
- Spec: storage.CSINodeSpec{
- Drivers: []storage.CSINodeDriver{
- {
- Name: "driver1",
- NodeID: "node1",
- TopologyKeys: []string{"key1"},
- },
- {
- Name: "driver2",
- NodeID: "node1",
- TopologyKeys: []string{"key1"},
- },
- },
- },
- },
- {
- // topology key names with -, _, and dot .
- ObjectMeta: metav1.ObjectMeta{Name: "foo8"},
- Spec: storage.CSINodeSpec{
- Drivers: []storage.CSINodeDriver{
- {
- Name: "driver1",
- NodeID: "node1",
- TopologyKeys: []string{"zone_1", "zone.2"},
- },
- {
- Name: "driver2",
- NodeID: "node1",
- TopologyKeys: []string{"zone-3", "zone.4"},
- },
- },
- },
- },
- {
- // topology prefix with - and dot.
- ObjectMeta: metav1.ObjectMeta{Name: "foo9"},
- Spec: storage.CSINodeSpec{
- Drivers: []storage.CSINodeDriver{
- {
- Name: "driver1",
- NodeID: "node1",
- TopologyKeys: []string{"company-com/zone1", "company.com/zone2"},
- },
- },
- },
- },
- {
- // No topology keys
- ObjectMeta: metav1.ObjectMeta{Name: "foo10"},
- Spec: storage.CSINodeSpec{
- Drivers: []storage.CSINodeDriver{
- {
- Name: driverName,
- NodeID: nodeID,
- },
- },
- },
- },
- }
- for _, csiNode := range successCases {
- if errs := ValidateCSINode(&csiNode); len(errs) != 0 {
- t.Errorf("expected success: %v", errs)
- }
- }
- errorCases := []storage.CSINode{
- {
- // Empty driver name
- ObjectMeta: metav1.ObjectMeta{Name: "foo1"},
- Spec: storage.CSINodeSpec{
- Drivers: []storage.CSINodeDriver{
- {
- Name: "",
- NodeID: nodeID,
- TopologyKeys: []string{"company.com/zone1", "company.com/zone2"},
- },
- },
- },
- },
- {
- // Invalid start char in driver name
- ObjectMeta: metav1.ObjectMeta{Name: "foo3"},
- Spec: storage.CSINodeSpec{
- Drivers: []storage.CSINodeDriver{
- {
- Name: "_io.kubernetes.storage.csi.driver",
- NodeID: nodeID,
- TopologyKeys: []string{"company.com/zone1", "company.com/zone2"},
- },
- },
- },
- },
- {
- // Invalid end char in driver name
- ObjectMeta: metav1.ObjectMeta{Name: "foo4"},
- Spec: storage.CSINodeSpec{
- Drivers: []storage.CSINodeDriver{
- {
- Name: "io.kubernetes.storage.csi.driver/",
- NodeID: nodeID,
- TopologyKeys: []string{"company.com/zone1", "company.com/zone2"},
- },
- },
- },
- },
- {
- // Invalid separators in driver name
- ObjectMeta: metav1.ObjectMeta{Name: "foo5"},
- Spec: storage.CSINodeSpec{
- Drivers: []storage.CSINodeDriver{
- {
- Name: "io/kubernetes/storage/csi~driver",
- NodeID: nodeID,
- TopologyKeys: []string{"company.com/zone1", "company.com/zone2"},
- },
- },
- },
- },
- {
- // driver name: underscore only
- ObjectMeta: metav1.ObjectMeta{Name: "foo6"},
- Spec: storage.CSINodeSpec{
- Drivers: []storage.CSINodeDriver{
- {
- Name: "io_kubernetes_storage_csi_driver",
- NodeID: nodeID,
- TopologyKeys: []string{"company.com/zone1", "company.com/zone2"},
- },
- },
- },
- },
- {
- // Driver name length > 63
- ObjectMeta: metav1.ObjectMeta{Name: "foo7"},
- Spec: storage.CSINodeSpec{
- Drivers: []storage.CSINodeDriver{
- {
- Name: longName,
- NodeID: nodeID,
- TopologyKeys: []string{"company.com/zone1", "company.com/zone2"},
- },
- },
- },
- },
- {
- // No driver name
- ObjectMeta: metav1.ObjectMeta{Name: "foo8"},
- Spec: storage.CSINodeSpec{
- Drivers: []storage.CSINodeDriver{
- {
- NodeID: nodeID,
- TopologyKeys: []string{"company.com/zone1", "company.com/zone2"},
- },
- },
- },
- },
- {
- // Empty individual topology key
- ObjectMeta: metav1.ObjectMeta{Name: "foo9"},
- Spec: storage.CSINodeSpec{
- Drivers: []storage.CSINodeDriver{
- {
- Name: driverName,
- NodeID: nodeID,
- TopologyKeys: []string{"company.com/zone1", ""},
- },
- },
- },
- },
- {
- // duplicate drivers in driver specs
- ObjectMeta: metav1.ObjectMeta{Name: "foo10"},
- Spec: storage.CSINodeSpec{
- Drivers: []storage.CSINodeDriver{
- {
- Name: "driver1",
- NodeID: "node1",
- TopologyKeys: []string{"key1", "key2"},
- },
- {
- Name: "driver1",
- NodeID: "nodeX",
- TopologyKeys: []string{"keyA", "keyB"},
- },
- },
- },
- },
- {
- // single driver with duplicate topology keys in driver specs
- ObjectMeta: metav1.ObjectMeta{Name: "foo11"},
- Spec: storage.CSINodeSpec{
- Drivers: []storage.CSINodeDriver{
- {
- Name: "driver1",
- NodeID: "node1",
- TopologyKeys: []string{"key1", "key1"},
- },
- },
- },
- },
- {
- // multiple drivers with one set of duplicate topology keys in driver specs
- ObjectMeta: metav1.ObjectMeta{Name: "foo12"},
- Spec: storage.CSINodeSpec{
- Drivers: []storage.CSINodeDriver{
- {
- Name: "driver1",
- NodeID: "node1",
- TopologyKeys: []string{"key1"},
- },
- {
- Name: "driver2",
- NodeID: "nodeX",
- TopologyKeys: []string{"keyA", "keyA"},
- },
- },
- },
- },
- {
- // Empty NodeID
- ObjectMeta: metav1.ObjectMeta{Name: "foo13"},
- Spec: storage.CSINodeSpec{
- Drivers: []storage.CSINodeDriver{
- {
- Name: driverName,
- NodeID: "",
- TopologyKeys: []string{"company.com/zone1", "company.com/zone2"},
- },
- },
- },
- },
- {
- // topology prefix should be lower case
- ObjectMeta: metav1.ObjectMeta{Name: "foo14"},
- Spec: storage.CSINodeSpec{
- Drivers: []storage.CSINodeDriver{
- {
- Name: driverName,
- NodeID: "node1",
- TopologyKeys: []string{"Company.Com/zone1", "company.com/zone2"},
- },
- },
- },
- },
- }
- for _, csiNode := range errorCases {
- if errs := ValidateCSINode(&csiNode); len(errs) == 0 {
- t.Errorf("Expected failure for test: %v", csiNode)
- }
- }
- }
- func TestCSINodeUpdateValidation(t *testing.T) {
- //driverName := "driver-name"
- //driverName2 := "1io.kubernetes-storage-2-csi-driver3"
- //longName := "my-a-b-c-d-c-f-g-h-i-j-k-l-m-n-o-p-q-r-s-t-u-v-w-x-y-z-ABCDEFGHIJKLMNOPQRSTUVWXYZ-driver"
- nodeID := "nodeA"
- old := storage.CSINode{
- ObjectMeta: metav1.ObjectMeta{Name: "foo1"},
- Spec: storage.CSINodeSpec{
- Drivers: []storage.CSINodeDriver{
- {
- Name: "io.kubernetes.storage.csi.driver-1",
- NodeID: nodeID,
- TopologyKeys: []string{"company.com/zone1", "company.com/zone2"},
- },
- {
- Name: "io.kubernetes.storage.csi.driver-2",
- NodeID: nodeID,
- TopologyKeys: []string{"company.com/zone1", "company.com/zone2"},
- },
- },
- },
- }
- successCases := []storage.CSINode{
- {
- // no change
- ObjectMeta: metav1.ObjectMeta{Name: "foo1"},
- Spec: storage.CSINodeSpec{
- Drivers: []storage.CSINodeDriver{
- {
- Name: "io.kubernetes.storage.csi.driver-1",
- NodeID: nodeID,
- TopologyKeys: []string{"company.com/zone1", "company.com/zone2"},
- },
- {
- Name: "io.kubernetes.storage.csi.driver-2",
- NodeID: nodeID,
- TopologyKeys: []string{"company.com/zone1", "company.com/zone2"},
- },
- },
- },
- },
- {
- // remove a driver
- ObjectMeta: metav1.ObjectMeta{Name: "foo1"},
- Spec: storage.CSINodeSpec{
- Drivers: []storage.CSINodeDriver{
- {
- Name: "io.kubernetes.storage.csi.driver-1",
- NodeID: nodeID,
- TopologyKeys: []string{"company.com/zone1", "company.com/zone2"},
- },
- },
- },
- },
- {
- // add a driver
- ObjectMeta: metav1.ObjectMeta{Name: "foo1"},
- Spec: storage.CSINodeSpec{
- Drivers: []storage.CSINodeDriver{
- {
- Name: "io.kubernetes.storage.csi.driver-1",
- NodeID: nodeID,
- TopologyKeys: []string{"company.com/zone1", "company.com/zone2"},
- },
- {
- Name: "io.kubernetes.storage.csi.driver-2",
- NodeID: nodeID,
- TopologyKeys: []string{"company.com/zone1", "company.com/zone2"},
- },
- {
- Name: "io.kubernetes.storage.csi.driver-3",
- NodeID: nodeID,
- TopologyKeys: []string{"company.com/zone1", "company.com/zone2"},
- },
- },
- },
- },
- {
- // remove a driver and add a driver
- ObjectMeta: metav1.ObjectMeta{Name: "foo1"},
- Spec: storage.CSINodeSpec{
- Drivers: []storage.CSINodeDriver{
- {
- Name: "io.kubernetes.storage.csi.driver-1",
- NodeID: nodeID,
- TopologyKeys: []string{"company.com/zone1", "company.com/zone2"},
- },
- {
- Name: "io.kubernetes.storage.csi.new-driver",
- NodeID: nodeID,
- TopologyKeys: []string{"company.com/zone1", "company.com/zone2"},
- },
- },
- },
- },
- }
- for _, csiNode := range successCases {
- if errs := ValidateCSINodeUpdate(&csiNode, &old); len(errs) != 0 {
- t.Errorf("expected success: %+v", errs)
- }
- }
- errorCases := []storage.CSINode{
- {
- // invalid change node id
- ObjectMeta: metav1.ObjectMeta{Name: "foo1"},
- Spec: storage.CSINodeSpec{
- Drivers: []storage.CSINodeDriver{
- {
- Name: "io.kubernetes.storage.csi.driver-1",
- NodeID: "nodeB",
- TopologyKeys: []string{"company.com/zone1", "company.com/zone2"},
- },
- {
- Name: "io.kubernetes.storage.csi.driver-2",
- NodeID: nodeID,
- TopologyKeys: []string{"company.com/zone1", "company.com/zone2"},
- },
- },
- },
- },
- {
- // invalid change topology keys
- ObjectMeta: metav1.ObjectMeta{Name: "foo1"},
- Spec: storage.CSINodeSpec{
- Drivers: []storage.CSINodeDriver{
- {
- Name: "io.kubernetes.storage.csi.driver-1",
- NodeID: "nodeB",
- TopologyKeys: []string{"company.com/zone1", "company.com/zone2"},
- },
- {
- Name: "io.kubernetes.storage.csi.driver-2",
- NodeID: nodeID,
- TopologyKeys: []string{"company.com/zone2"},
- },
- },
- },
- },
- }
- for _, csiNode := range errorCases {
- if errs := ValidateCSINodeUpdate(&csiNode, &old); len(errs) == 0 {
- t.Errorf("Expected failure for test: %+v", csiNode)
- }
- }
- }
- func TestCSIDriverValidation(t *testing.T) {
- driverName := "test-driver"
- longName := "my-a-b-c-d-c-f-g-h-i-j-k-l-m-n-o-p-q-r-s-t-u-v-w-x-y-z-ABCDEFGHIJKLMNOPQRSTUVWXYZ-driver"
- invalidName := "-invalid-@#$%^&*()-"
- attachRequired := true
- attachNotRequired := false
- podInfoOnMount := true
- notPodInfoOnMount := false
- successCases := []storage.CSIDriver{
- {
- ObjectMeta: metav1.ObjectMeta{Name: driverName},
- Spec: storage.CSIDriverSpec{
- AttachRequired: &attachRequired,
- PodInfoOnMount: &podInfoOnMount,
- },
- },
- {
- // driver name: dot only
- ObjectMeta: metav1.ObjectMeta{Name: "io.kubernetes.storage.csi.driver"},
- Spec: storage.CSIDriverSpec{
- AttachRequired: &attachRequired,
- PodInfoOnMount: &podInfoOnMount,
- },
- },
- {
- // driver name: dash only
- ObjectMeta: metav1.ObjectMeta{Name: "io-kubernetes-storage-csi-driver"},
- Spec: storage.CSIDriverSpec{
- AttachRequired: &attachRequired,
- PodInfoOnMount: ¬PodInfoOnMount,
- },
- },
- {
- // driver name: numbers
- ObjectMeta: metav1.ObjectMeta{Name: "1csi2driver3"},
- Spec: storage.CSIDriverSpec{
- AttachRequired: &attachRequired,
- PodInfoOnMount: &podInfoOnMount,
- },
- },
- {
- // driver name: dot and dash
- ObjectMeta: metav1.ObjectMeta{Name: "io.kubernetes.storage.csi-driver"},
- Spec: storage.CSIDriverSpec{
- AttachRequired: &attachRequired,
- PodInfoOnMount: &podInfoOnMount,
- },
- },
- {
- ObjectMeta: metav1.ObjectMeta{Name: driverName},
- Spec: storage.CSIDriverSpec{
- AttachRequired: &attachRequired,
- PodInfoOnMount: ¬PodInfoOnMount,
- },
- },
- {
- ObjectMeta: metav1.ObjectMeta{Name: driverName},
- Spec: storage.CSIDriverSpec{
- AttachRequired: &attachRequired,
- PodInfoOnMount: &podInfoOnMount,
- },
- },
- {
- ObjectMeta: metav1.ObjectMeta{Name: driverName},
- Spec: storage.CSIDriverSpec{
- AttachRequired: &attachNotRequired,
- PodInfoOnMount: ¬PodInfoOnMount,
- },
- },
- }
- for _, csiDriver := range successCases {
- if errs := ValidateCSIDriver(&csiDriver); len(errs) != 0 {
- t.Errorf("expected success: %v", errs)
- }
- }
- errorCases := []storage.CSIDriver{
- {
- ObjectMeta: metav1.ObjectMeta{Name: invalidName},
- Spec: storage.CSIDriverSpec{
- AttachRequired: &attachRequired,
- PodInfoOnMount: &podInfoOnMount,
- },
- },
- {
- ObjectMeta: metav1.ObjectMeta{Name: longName},
- Spec: storage.CSIDriverSpec{
- AttachRequired: &attachNotRequired,
- PodInfoOnMount: ¬PodInfoOnMount,
- },
- },
- {
- // AttachRequired not set
- ObjectMeta: metav1.ObjectMeta{Name: driverName},
- Spec: storage.CSIDriverSpec{
- AttachRequired: nil,
- PodInfoOnMount: &podInfoOnMount,
- },
- },
- {
- // AttachRequired not set
- ObjectMeta: metav1.ObjectMeta{Name: driverName},
- Spec: storage.CSIDriverSpec{
- AttachRequired: &attachNotRequired,
- PodInfoOnMount: nil,
- },
- },
- }
- for _, csiDriver := range errorCases {
- if errs := ValidateCSIDriver(&csiDriver); len(errs) == 0 {
- t.Errorf("Expected failure for test: %v", csiDriver)
- }
- }
- }
|