Skip to content

Commit

Permalink
fix: build and image validation unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wangeguo committed Jan 28, 2024
1 parent 18774d7 commit 7f451e4
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
16 changes: 15 additions & 1 deletion pkg/apis/build/v1alpha1/build_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,17 @@ func testBuildValidation(t *testing.T, when spec.G, it spec.S) {
Image: "registry.com/image",
}
assertValidationError(build, apis.ErrMultipleOneOf("git", "blob", "registry").ViaField("spec", "source"))

build.Spec.Source.Volume = &corev1alpha1.Volume{
ClaimName: "some-claim",
}
assertValidationError(build, apis.ErrMultipleOneOf("git", "blob", "registry", "volume").ViaField("spec", "source"))
})

it("missing source", func() {
build.Spec.Source = corev1alpha1.SourceConfig{}

assertValidationError(build, apis.ErrMissingOneOf("git", "blob", "registry").ViaField("spec", "source"))
assertValidationError(build, apis.ErrMissingOneOf("git", "blob", "registry", "volume").ViaField("spec", "source"))
})

it("validates git url", func() {
Expand Down Expand Up @@ -142,6 +147,15 @@ func testBuildValidation(t *testing.T, when spec.G, it spec.S) {
assertValidationError(build, apis.ErrMissingField("image").ViaField("spec", "source", "registry"))
})

it("validates volume claim name", func() {
build.Spec.Source.Git = nil
build.Spec.Source.Blob = nil
build.Spec.Source.Registry = nil
build.Spec.Source.Volume = &corev1alpha1.Volume{ClaimName: ""}

assertValidationError(build, apis.ErrMissingField("persistentVolumeClaimName").ViaField("spec", "source", "volume"))
})

it("validates valid lastBuilt Image", func() {
build.Spec.LastBuild = &LastBuild{Image: "invalid@@"}

Expand Down
16 changes: 15 additions & 1 deletion pkg/apis/build/v1alpha1/image_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,17 @@ func testImageValidation(t *testing.T, when spec.G, it spec.S) {
Image: "registry.com/image",
}
assertValidationError(image, ctx, apis.ErrMultipleOneOf("git", "blob", "registry").ViaField("spec", "source"))

image.Spec.Source.Volume = &corev1alpha1.Volume{
ClaimName: "some-claim",
}
assertValidationError(image, ctx, apis.ErrMultipleOneOf("git", "blob", "registry", "volume").ViaField("spec", "source"))
})

it("missing source", func() {
image.Spec.Source = corev1alpha1.SourceConfig{}

assertValidationError(image, ctx, apis.ErrMissingOneOf("git", "blob", "registry").ViaField("spec", "source"))
assertValidationError(image, ctx, apis.ErrMissingOneOf("git", "blob", "registry", "volume").ViaField("spec", "source"))
})

it("validates git url", func() {
Expand Down Expand Up @@ -219,6 +224,15 @@ func testImageValidation(t *testing.T, when spec.G, it spec.S) {
assertValidationError(image, ctx, apis.ErrInvalidValue(image.Spec.Source.Registry.Image, "image").ViaField("spec", "source", "registry"))
})

it("validate volume claim name", func() {
image.Spec.Source.Git = nil
image.Spec.Source.Blob = nil
image.Spec.Source.Registry = nil
image.Spec.Source.Volume = &corev1alpha1.Volume{ClaimName: ""}

assertValidationError(image, ctx, apis.ErrMissingField("persistentVolumeClaimName").ViaField("spec", "source", "volume"))
})

it("validates build bindings", func() {
image.Spec.Build.Bindings = []corev1alpha1.CNBBinding{
{MetadataRef: &corev1.LocalObjectReference{Name: "metadata"}},
Expand Down
16 changes: 15 additions & 1 deletion pkg/apis/build/v1alpha2/build_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,17 @@ func testBuildValidation(t *testing.T, when spec.G, it spec.S) {
Image: "registry.com/image",
}
assertValidationError(build, context.TODO(), apis.ErrMultipleOneOf("git", "blob", "registry").ViaField("spec", "source"))

build.Spec.Source.Volume = &corev1alpha1.Volume{
ClaimName: "some-claim",
}
assertValidationError(build, context.TODO(), apis.ErrMultipleOneOf("git", "blob", "registry", "volume").ViaField("spec", "source"))
})

it("missing source", func() {
build.Spec.Source = corev1alpha1.SourceConfig{}

assertValidationError(build, context.TODO(), apis.ErrMissingOneOf("git", "blob", "registry").ViaField("spec", "source"))
assertValidationError(build, context.TODO(), apis.ErrMissingOneOf("git", "blob", "registry", "volume").ViaField("spec", "source"))
})

it("validates git url", func() {
Expand Down Expand Up @@ -143,6 +148,15 @@ func testBuildValidation(t *testing.T, when spec.G, it spec.S) {
assertValidationError(build, context.TODO(), apis.ErrMissingField("image").ViaField("spec", "source", "registry"))
})

it("validates volume claim name", func() {
build.Spec.Source.Git = nil
build.Spec.Source.Blob = nil
build.Spec.Source.Registry = nil
build.Spec.Source.Volume = &corev1alpha1.Volume{ClaimName: ""}

assertValidationError(build, context.TODO(), apis.ErrMissingField("persistentVolumeClaimName").ViaField("spec", "source", "volume"))
})

it("validates valid lastBuilt Image", func() {
build.Spec.LastBuild = &LastBuild{Image: "invalid@@"}

Expand Down
16 changes: 15 additions & 1 deletion pkg/apis/build/v1alpha2/image_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,17 @@ func testImageValidation(t *testing.T, when spec.G, it spec.S) {
Image: "registry.com/image",
}
assertValidationError(image, ctx, apis.ErrMultipleOneOf("git", "blob", "registry").ViaField("spec", "source"))

image.Spec.Source.Volume = &corev1alpha1.Volume{
ClaimName: "some-claim",
}
assertValidationError(image, ctx, apis.ErrMultipleOneOf("git", "blob", "registry", "volume").ViaField("spec", "source"))
})

it("missing source", func() {
image.Spec.Source = corev1alpha1.SourceConfig{}

assertValidationError(image, ctx, apis.ErrMissingOneOf("git", "blob", "registry").ViaField("spec", "source"))
assertValidationError(image, ctx, apis.ErrMissingOneOf("git", "blob", "registry", "volume").ViaField("spec", "source"))
})

it("validates git url", func() {
Expand Down Expand Up @@ -253,6 +258,15 @@ func testImageValidation(t *testing.T, when spec.G, it spec.S) {
assertValidationError(image, ctx, apis.ErrInvalidValue(image.Spec.Source.Registry.Image, "image").ViaField("spec", "source", "registry"))
})

it("validate volume claim name", func() {
image.Spec.Source.Git = nil
image.Spec.Source.Blob = nil
image.Spec.Source.Registry = nil
image.Spec.Source.Volume = &corev1alpha1.Volume{ClaimName: ""}

assertValidationError(image, ctx, apis.ErrMissingField("persistentVolumeClaimName").ViaField("spec", "source", "volume"))
})

it("validates service bindings", func() {
image.Spec.Build.Services = Services{
{Kind: "Secret"},
Expand Down

0 comments on commit 7f451e4

Please sign in to comment.