mirror of https://github.com/k3s-io/k3s
add comments to validation testcases, and use const in util.go
parent
54154f8ebb
commit
60d991e59a
|
@ -22,6 +22,11 @@ import (
|
||||||
"k8s.io/kubernetes/pkg/features"
|
"k8s.io/kubernetes/pkg/features"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
pvc string = "PersistentVolumeClaim"
|
||||||
|
volumeSnapshot string = "VolumeSnapshot"
|
||||||
|
)
|
||||||
|
|
||||||
// DropDisabledFields removes disabled fields from the pvc spec.
|
// DropDisabledFields removes disabled fields from the pvc spec.
|
||||||
// This should be called from PrepareForCreate/PrepareForUpdate for all resources containing a pvc spec.
|
// This should be called from PrepareForCreate/PrepareForUpdate for all resources containing a pvc spec.
|
||||||
func DropDisabledFields(pvcSpec, oldPVCSpec *core.PersistentVolumeClaimSpec) {
|
func DropDisabledFields(pvcSpec, oldPVCSpec *core.PersistentVolumeClaimSpec) {
|
||||||
|
@ -55,13 +60,13 @@ func dataSourceInUse(oldPVCSpec *core.PersistentVolumeClaimSpec) bool {
|
||||||
|
|
||||||
func dataSourceIsEnabled(pvcSpec *core.PersistentVolumeClaimSpec) bool {
|
func dataSourceIsEnabled(pvcSpec *core.PersistentVolumeClaimSpec) bool {
|
||||||
if pvcSpec.DataSource != nil {
|
if pvcSpec.DataSource != nil {
|
||||||
if pvcSpec.DataSource.Kind == "PersistentVolumeClaim" &&
|
if pvcSpec.DataSource.Kind == pvc &&
|
||||||
*pvcSpec.DataSource.APIGroup == "" &&
|
*pvcSpec.DataSource.APIGroup == "" &&
|
||||||
utilfeature.DefaultFeatureGate.Enabled(features.VolumePVCDataSource) {
|
utilfeature.DefaultFeatureGate.Enabled(features.VolumePVCDataSource) {
|
||||||
return true
|
return true
|
||||||
|
|
||||||
}
|
}
|
||||||
if pvcSpec.DataSource.Kind == "VolumeSnapshot" &&
|
if pvcSpec.DataSource.Kind == volumeSnapshot &&
|
||||||
*pvcSpec.DataSource.APIGroup == "snapshot.storage.k8s.io" &&
|
*pvcSpec.DataSource.APIGroup == "snapshot.storage.k8s.io" &&
|
||||||
utilfeature.DefaultFeatureGate.Enabled(features.VolumeSnapshotDataSource) {
|
utilfeature.DefaultFeatureGate.Enabled(features.VolumeSnapshotDataSource) {
|
||||||
return true
|
return true
|
||||||
|
|
|
@ -13312,24 +13312,48 @@ func testDataSourceInSpec(name string, kind string, apiGroup string) *core.Persi
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAlphaVolumePVCDataSource(t *testing.T) {
|
func TestAlphaVolumePVCDataSource(t *testing.T) {
|
||||||
successTestCases := []core.PersistentVolumeClaimSpec{
|
|
||||||
*testDataSourceInSpec("test_snapshot", "VolumeSnapshot", "snapshot.storage.k8s.io"),
|
testCases := []struct {
|
||||||
*testDataSourceInSpec("test_pvc", "PersistentVolumeClaim", ""),
|
testName string
|
||||||
}
|
claimSpec core.PersistentVolumeClaimSpec
|
||||||
failedTestCases := []core.PersistentVolumeClaimSpec{
|
expectedFail bool
|
||||||
*testDataSourceInSpec("", "VolumeSnapshot", "snapshot.storage.k8s.io"),
|
}{
|
||||||
*testDataSourceInSpec("test_snapshot", "PersistentVolumeClaim", "snapshot.storage.k8s.io"),
|
{
|
||||||
*testDataSourceInSpec("test_snapshot", "VolumeSnapshot", "storage.k8s.io"),
|
testName: "test create from valid snapshot source",
|
||||||
|
claimSpec: *testDataSourceInSpec("test_snapshot", "VolumeSnapshot", "snapshot.storage.k8s.io"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
testName: "test create from valid pvc source",
|
||||||
|
claimSpec: *testDataSourceInSpec("test_pvc", "PersistentVolumeClaim", ""),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
testName: "test missing name in snapshot datasource should fail",
|
||||||
|
claimSpec: *testDataSourceInSpec("", "VolumeSnapshot", "snapshot.storage.k8s.io"),
|
||||||
|
expectedFail: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
testName: "test specifying pvc with snapshot api group should fail",
|
||||||
|
claimSpec: *testDataSourceInSpec("test_snapshot", "PersistentVolumeClaim", "snapshot.storage.k8s.io"),
|
||||||
|
expectedFail: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
testName: "test invalid group name in snapshot datasource should fail",
|
||||||
|
claimSpec: *testDataSourceInSpec("test_snapshot", "VolumeSnapshot", "storage.k8s.io"),
|
||||||
|
expectedFail: true,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range successTestCases {
|
for _, tc := range testCases {
|
||||||
if errs := ValidatePersistentVolumeClaimSpec(&tc, field.NewPath("spec")); len(errs) != 0 {
|
if tc.expectedFail {
|
||||||
t.Errorf("expected success: %v", errs)
|
if errs := ValidatePersistentVolumeClaimSpec(&tc.claimSpec, field.NewPath("spec")); len(errs) == 0 {
|
||||||
}
|
t.Errorf("expected failure: %v", errs)
|
||||||
}
|
}
|
||||||
for _, tc := range failedTestCases {
|
|
||||||
if errs := ValidatePersistentVolumeClaimSpec(&tc, field.NewPath("spec")); len(errs) == 0 {
|
} else {
|
||||||
t.Errorf("expected failure: %v", errs)
|
if errs := ValidatePersistentVolumeClaimSpec(&tc.claimSpec, field.NewPath("spec")); len(errs) != 0 {
|
||||||
|
t.Errorf("expected success: %v", errs)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue