Skip to content

Commit b1b1fc8

Browse files
authored
Merge pull request kubernetes#81961 from pohly/ephemeral-unit-tests
storage: enhance test for ValidateCSIDriverUpdate
2 parents 3d912dd + c20721a commit b1b1fc8

File tree

1 file changed

+60
-38
lines changed

1 file changed

+60
-38
lines changed

pkg/apis/storage/validation/validation_test.go

Lines changed: 60 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1800,7 +1800,7 @@ func TestCSIDriverValidation(t *testing.T) {
18001800
},
18011801
},
18021802
{
1803-
// AttachRequired not set
1803+
// PodInfoOnMount not set
18041804
ObjectMeta: metav1.ObjectMeta{Name: driverName},
18051805
Spec: storage.CSIDriverSpec{
18061806
AttachRequired: &attachNotRequired,
@@ -1856,64 +1856,86 @@ func TestCSIDriverValidationUpdate(t *testing.T) {
18561856
}
18571857
}
18581858

1859-
errorCases := []storage.CSIDriver{
1859+
// Each test case changes exactly one field. None of that is valid.
1860+
errorCases := []struct {
1861+
name string
1862+
modify func(new *storage.CSIDriver)
1863+
}{
18601864
{
1861-
ObjectMeta: metav1.ObjectMeta{Name: invalidName},
1862-
Spec: storage.CSIDriverSpec{
1863-
AttachRequired: &attachRequired,
1864-
PodInfoOnMount: &podInfoOnMount,
1865+
name: "invalid name",
1866+
modify: func(new *storage.CSIDriver) {
1867+
new.Name = invalidName
18651868
},
18661869
},
18671870
{
1868-
ObjectMeta: metav1.ObjectMeta{Name: longName},
1869-
Spec: storage.CSIDriverSpec{
1870-
AttachRequired: &attachNotRequired,
1871-
PodInfoOnMount: &notPodInfoOnMount,
1871+
name: "long name",
1872+
modify: func(new *storage.CSIDriver) {
1873+
new.Name = longName
18721874
},
18731875
},
18741876
{
1875-
// AttachRequired not set
1876-
ObjectMeta: metav1.ObjectMeta{Name: driverName},
1877-
Spec: storage.CSIDriverSpec{
1878-
AttachRequired: nil,
1879-
PodInfoOnMount: &podInfoOnMount,
1877+
name: "AttachRequired not set",
1878+
modify: func(new *storage.CSIDriver) {
1879+
new.Spec.AttachRequired = nil
18801880
},
18811881
},
18821882
{
1883-
// AttachRequired not set
1884-
ObjectMeta: metav1.ObjectMeta{Name: driverName},
1885-
Spec: storage.CSIDriverSpec{
1886-
AttachRequired: &attachNotRequired,
1887-
PodInfoOnMount: nil,
1883+
name: "PodInfoOnMount not set",
1884+
modify: func(new *storage.CSIDriver) {
1885+
new.Spec.PodInfoOnMount = nil
18881886
},
18891887
},
18901888
{
1891-
// invalid mode
1892-
ObjectMeta: metav1.ObjectMeta{Name: driverName},
1893-
Spec: storage.CSIDriverSpec{
1894-
AttachRequired: &attachNotRequired,
1895-
PodInfoOnMount: &notPodInfoOnMount,
1896-
VolumeLifecycleModes: []storage.VolumeLifecycleMode{
1889+
name: "AttachRequired changed",
1890+
modify: func(new *storage.CSIDriver) {
1891+
new.Spec.AttachRequired = &attachRequired
1892+
},
1893+
},
1894+
{
1895+
name: "PodInfoOnMount changed",
1896+
modify: func(new *storage.CSIDriver) {
1897+
new.Spec.PodInfoOnMount = &podInfoOnMount
1898+
},
1899+
},
1900+
{
1901+
name: "invalid volume lifecycle mode",
1902+
modify: func(new *storage.CSIDriver) {
1903+
new.Spec.VolumeLifecycleModes = []storage.VolumeLifecycleMode{
18971904
"no-such-mode",
1898-
},
1905+
}
18991906
},
19001907
},
19011908
{
1902-
// different modes
1903-
ObjectMeta: metav1.ObjectMeta{Name: driverName},
1904-
Spec: storage.CSIDriverSpec{
1905-
AttachRequired: &attachNotRequired,
1906-
PodInfoOnMount: &notPodInfoOnMount,
1907-
VolumeLifecycleModes: []storage.VolumeLifecycleMode{
1909+
name: "volume lifecycle modes not set",
1910+
modify: func(new *storage.CSIDriver) {
1911+
new.Spec.VolumeLifecycleModes = nil
1912+
},
1913+
},
1914+
{
1915+
name: "VolumeLifecyclePersistent removed",
1916+
modify: func(new *storage.CSIDriver) {
1917+
new.Spec.VolumeLifecycleModes = []storage.VolumeLifecycleMode{
19081918
storage.VolumeLifecycleEphemeral,
1909-
},
1919+
}
1920+
},
1921+
},
1922+
{
1923+
name: "VolumeLifecycleEphemeral removed",
1924+
modify: func(new *storage.CSIDriver) {
1925+
new.Spec.VolumeLifecycleModes = []storage.VolumeLifecycleMode{
1926+
storage.VolumeLifecyclePersistent,
1927+
}
19101928
},
19111929
},
19121930
}
19131931

1914-
for _, csiDriver := range errorCases {
1915-
if errs := ValidateCSIDriverUpdate(&csiDriver, &old); len(errs) == 0 {
1916-
t.Errorf("Expected failure for test: %v", csiDriver)
1917-
}
1932+
for _, test := range errorCases {
1933+
t.Run(test.name, func(t *testing.T) {
1934+
new := old.DeepCopy()
1935+
test.modify(new)
1936+
if errs := ValidateCSIDriverUpdate(new, &old); len(errs) == 0 {
1937+
t.Errorf("Expected failure for test: %v", new)
1938+
}
1939+
})
19181940
}
19191941
}

0 commit comments

Comments
 (0)