|
| 1 | +package controller |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/go-test/deep" |
| 7 | + corev1 "k8s.io/api/core/v1" |
| 8 | +) |
| 9 | + |
| 10 | +func TestVolumeNameIs(t *testing.T) { |
| 11 | + type TestCase struct { |
| 12 | + Description string |
| 13 | + Volume corev1.Volume |
| 14 | + Name string |
| 15 | + Expected bool |
| 16 | + } |
| 17 | + |
| 18 | + tests := []TestCase{ |
| 19 | + { |
| 20 | + Description: "Should return false if name does not match", |
| 21 | + Volume: corev1.Volume{Name: "my-volume"}, |
| 22 | + Name: "abc", |
| 23 | + Expected: false, |
| 24 | + }, |
| 25 | + { |
| 26 | + Description: "Should return true if name matches", |
| 27 | + Volume: corev1.Volume{Name: "my-volume"}, |
| 28 | + Name: "my-volume", |
| 29 | + Expected: true, |
| 30 | + }, |
| 31 | + } |
| 32 | + |
| 33 | + for _, tt := range tests { |
| 34 | + t.Run(tt.Description, func(t *testing.T) { |
| 35 | + if res := volumeNameIs(tt.Name)(tt.Volume); res != tt.Expected { |
| 36 | + t.Errorf("volumeNameIs=%v, expected=%v", res, tt.Expected) |
| 37 | + } |
| 38 | + }) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func TestVolumeMountNameIs(t *testing.T) { |
| 43 | + type TestCase struct { |
| 44 | + Description string |
| 45 | + VolumeMount corev1.VolumeMount |
| 46 | + Name string |
| 47 | + Expected bool |
| 48 | + } |
| 49 | + |
| 50 | + tests := []TestCase{ |
| 51 | + { |
| 52 | + Description: "Should return false if name does not match", |
| 53 | + VolumeMount: corev1.VolumeMount{Name: "my-volume"}, |
| 54 | + Name: "abc", |
| 55 | + Expected: false, |
| 56 | + }, |
| 57 | + { |
| 58 | + Description: "Should return true if name matches", |
| 59 | + VolumeMount: corev1.VolumeMount{Name: "my-volume"}, |
| 60 | + Name: "my-volume", |
| 61 | + Expected: true, |
| 62 | + }, |
| 63 | + } |
| 64 | + |
| 65 | + for _, tt := range tests { |
| 66 | + t.Run(tt.Description, func(t *testing.T) { |
| 67 | + if res := volumeMountNameIs(tt.Name)(tt.VolumeMount); res != tt.Expected { |
| 68 | + t.Errorf("volumeMountNameIs=%v, expected=%v", res, tt.Expected) |
| 69 | + } |
| 70 | + }) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestRemoveBucketsFromPodSpec(t *testing.T) { |
| 75 | + type TestCase struct { |
| 76 | + Description string |
| 77 | + PodSpec corev1.PodSpec |
| 78 | + ExpectedPodSpec corev1.PodSpec |
| 79 | + ExpectedModified bool |
| 80 | + } |
| 81 | + |
| 82 | + tests := []TestCase{ |
| 83 | + { |
| 84 | + Description: "Should not change anything if no gcsfuse volumes/sidecars", |
| 85 | + PodSpec: corev1.PodSpec{ |
| 86 | + Volumes: []corev1.Volume{ |
| 87 | + { |
| 88 | + Name: "not-gcsfuse", |
| 89 | + }, |
| 90 | + }, |
| 91 | + Containers: []corev1.Container{ |
| 92 | + { |
| 93 | + Name: "not-gcsfuse", |
| 94 | + VolumeMounts: []corev1.VolumeMount{ |
| 95 | + { |
| 96 | + Name: "not-gcsfuse", |
| 97 | + }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + ExpectedPodSpec: corev1.PodSpec{ |
| 103 | + Volumes: []corev1.Volume{ |
| 104 | + { |
| 105 | + Name: "not-gcsfuse", |
| 106 | + }, |
| 107 | + }, |
| 108 | + Containers: []corev1.Container{ |
| 109 | + { |
| 110 | + Name: "not-gcsfuse", |
| 111 | + VolumeMounts: []corev1.VolumeMount{ |
| 112 | + { |
| 113 | + Name: "not-gcsfuse", |
| 114 | + }, |
| 115 | + }, |
| 116 | + }, |
| 117 | + }, |
| 118 | + }, |
| 119 | + ExpectedModified: false, |
| 120 | + }, |
| 121 | + { |
| 122 | + Description: "Should remove only relevant volumes and containers", |
| 123 | + PodSpec: corev1.PodSpec{ |
| 124 | + Volumes: []corev1.Volume{ |
| 125 | + { |
| 126 | + Name: "gcsfuse-1", |
| 127 | + }, |
| 128 | + { |
| 129 | + Name: "not-gcsfuse", |
| 130 | + }, |
| 131 | + { |
| 132 | + Name: "gcsfuse-2", |
| 133 | + }, |
| 134 | + }, |
| 135 | + Containers: []corev1.Container{ |
| 136 | + { |
| 137 | + Name: "not-gcsfuse", |
| 138 | + VolumeMounts: []corev1.VolumeMount{ |
| 139 | + { |
| 140 | + Name: "gcsfuse-1", |
| 141 | + }, |
| 142 | + { |
| 143 | + Name: "not-gcsfuse", |
| 144 | + }, |
| 145 | + { |
| 146 | + Name: "gcsfuse-2", |
| 147 | + }, |
| 148 | + }, |
| 149 | + }, |
| 150 | + { |
| 151 | + Name: precreatorContainerName, |
| 152 | + }, |
| 153 | + }, |
| 154 | + }, |
| 155 | + ExpectedPodSpec: corev1.PodSpec{ |
| 156 | + Volumes: []corev1.Volume{ |
| 157 | + { |
| 158 | + Name: "not-gcsfuse", |
| 159 | + }, |
| 160 | + }, |
| 161 | + Containers: []corev1.Container{ |
| 162 | + { |
| 163 | + Name: "not-gcsfuse", |
| 164 | + VolumeMounts: []corev1.VolumeMount{ |
| 165 | + { |
| 166 | + Name: "not-gcsfuse", |
| 167 | + }, |
| 168 | + }, |
| 169 | + }, |
| 170 | + }, |
| 171 | + }, |
| 172 | + ExpectedModified: true, |
| 173 | + }, |
| 174 | + } |
| 175 | + |
| 176 | + for _, tt := range tests { |
| 177 | + t.Run(tt.Description, func(t *testing.T) { |
| 178 | + if modified := removeBucketsFromPodSpec(&tt.PodSpec, &tt.PodSpec.Containers[0]); modified != tt.ExpectedModified { |
| 179 | + t.Errorf("modified=%v, expected=%v", modified, tt.ExpectedModified) |
| 180 | + } |
| 181 | + if diff := deep.Equal(tt.PodSpec, tt.ExpectedPodSpec); diff != nil { |
| 182 | + t.Errorf("modified PodSpec does not equal expected: %v", diff) |
| 183 | + } |
| 184 | + }) |
| 185 | + } |
| 186 | + |
| 187 | +} |
0 commit comments