From b5fce5021f130746c1c3da20b76f9deeeaea3025 Mon Sep 17 00:00:00 2001 From: Deyuan Deng Date: Sun, 23 Nov 2014 23:03:11 -0500 Subject: [PATCH] gitrepo validation --- pkg/api/validation/validation.go | 20 ++++++++++++++++---- pkg/api/validation/validation_test.go | 3 ++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/pkg/api/validation/validation.go b/pkg/api/validation/validation.go index 7bb8f1153c7..6cba612952b 100644 --- a/pkg/api/validation/validation.go +++ b/pkg/api/validation/validation.go @@ -72,7 +72,11 @@ func validateSource(source *api.VolumeSource) errs.ValidationErrorList { } if source.EmptyDir != nil { numVolumes++ - //EmptyDirs have nothing to validate + // EmptyDirs have nothing to validate + } + if source.GitRepo != nil { + numVolumes++ + allErrs = append(allErrs, validateGitRepo(source.GitRepo)...) } if source.GCEPersistentDisk != nil { numVolumes++ @@ -92,15 +96,21 @@ func validateHostDir(hostDir *api.HostDir) errs.ValidationErrorList { return allErrs } -var supportedPortProtocols = util.NewStringSet(string(api.ProtocolTCP), string(api.ProtocolUDP)) +func validateGitRepo(gitRepo *api.GitRepo) errs.ValidationErrorList { + allErrs := errs.ValidationErrorList{} + if gitRepo.Repository == "" { + allErrs = append(allErrs, errs.NewFieldRequired("gitRepo.Repository", gitRepo.Repository)) + } + return allErrs +} func validateGCEPersistentDisk(PD *api.GCEPersistentDisk) errs.ValidationErrorList { allErrs := errs.ValidationErrorList{} if PD.PDName == "" { - allErrs = append(allErrs, errs.NewFieldInvalid("PD.PDName", PD.PDName)) + allErrs = append(allErrs, errs.NewFieldRequired("PD.PDName", PD.PDName)) } if PD.FSType == "" { - allErrs = append(allErrs, errs.NewFieldInvalid("PD.FSType", PD.FSType)) + allErrs = append(allErrs, errs.NewFieldRequired("PD.FSType", PD.FSType)) } if PD.Partition < 0 || PD.Partition > 255 { allErrs = append(allErrs, errs.NewFieldInvalid("PD.Partition", PD.Partition)) @@ -108,6 +118,8 @@ func validateGCEPersistentDisk(PD *api.GCEPersistentDisk) errs.ValidationErrorLi return allErrs } +var supportedPortProtocols = util.NewStringSet(string(api.ProtocolTCP), string(api.ProtocolUDP)) + func validatePorts(ports []api.Port) errs.ValidationErrorList { allErrs := errs.ValidationErrorList{} diff --git a/pkg/api/validation/validation_test.go b/pkg/api/validation/validation_test.go index 6985e35f3e8..bedc22d226d 100644 --- a/pkg/api/validation/validation_test.go +++ b/pkg/api/validation/validation_test.go @@ -42,12 +42,13 @@ func TestValidateVolumes(t *testing.T) { {Name: "abc-123", Source: &api.VolumeSource{HostDir: &api.HostDir{"/mnt/path3"}}}, {Name: "empty", Source: &api.VolumeSource{EmptyDir: &api.EmptyDir{}}}, {Name: "gcepd", Source: &api.VolumeSource{GCEPersistentDisk: &api.GCEPersistentDisk{"my-PD", "ext4", 1, false}}}, + {Name: "gitrepo", Source: &api.VolumeSource{GitRepo: &api.GitRepo{"my-repo", "hashstring"}}}, } names, errs := validateVolumes(successCase) if len(errs) != 0 { t.Errorf("expected success: %v", errs) } - if len(names) != 5 || !names.HasAll("abc", "123", "abc-123", "empty", "gcepd") { + if len(names) != 6 || !names.HasAll("abc", "123", "abc-123", "empty", "gcepd", "gitrepo") { t.Errorf("wrong names result: %v", names) }