Skip to content

Commit

Permalink
Tests for NodeSelectors on Build and BuildRun objects
Browse files Browse the repository at this point in the history
Signed-off-by: Dylan Orzel <[email protected]>
  • Loading branch information
dorzel committed Oct 9, 2024
1 parent f0e066c commit 90becbe
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 2 deletions.
16 changes: 16 additions & 0 deletions pkg/reconciler/build/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package build_test
import (
"context"
"fmt"
"strings"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -613,5 +614,20 @@ var _ = Describe("Reconcile Build", func() {
Expect(err).ToNot(HaveOccurred())
})
})

Context("when nodeSelector is specified", func() {
It("should fail to validate when the nodeSelector is invalid", func() {
// set nodeSelector to be invalid
buildSample.Spec.NodeSelector = map[string]string{strings.Repeat("s", 64): "amd64"}
buildSample.Spec.Output.PushSecret = nil

statusCall := ctl.StubFunc(corev1.ConditionFalse, build.NodeSelectorNotValid, "must be no more than 63 characters")
statusWriter.UpdateCalls(statusCall)

_, err := reconciler.Reconcile(context.TODO(), request)
Expect(err).To(BeNil())
Expect(statusWriter.UpdateCallCount()).To(Equal(1))
})
})
})
})
14 changes: 14 additions & 0 deletions pkg/reconciler/buildrun/buildrun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1631,5 +1631,19 @@ var _ = Describe("Reconcile BuildRun", func() {
Expect(statusWriter.UpdateCallCount()).To(Equal(1))
})
})

Context("when nodeSelector is specified", func() {
It("fails when the nodeSelector is invalid", func() {
// set nodeSelector to be invalid
buildSample.Spec.NodeSelector = map[string]string{strings.Repeat("s", 64): "amd64"}

statusCall := ctl.StubFunc(corev1.ConditionFalse, build.NodeSelectorNotValid, "must be no more than 63 characters")
statusWriter.UpdateCalls(statusCall)

_, err := reconciler.Reconcile(context.TODO(), buildRunRequest)
Expect(err).To(BeNil())
Expect(statusWriter.UpdateCallCount()).To(Equal(1))
})
})
})
})
22 changes: 22 additions & 0 deletions pkg/reconciler/buildrun/resources/taskrun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,5 +632,27 @@ var _ = Describe("GenerateTaskrun", func() {
Expect(paramOutputImageFound).To(BeTrue())
})
})

Context("when the build and buildrun both specify a nodeSelector", func() {
BeforeEach(func() {
build, err = ctl.LoadBuildYAML([]byte(test.MinimalBuildRunWithNodeSelector))
Expect(err).To(BeNil())

buildRun, err = ctl.LoadBuildRunFromBytes([]byte(test.MinimalBuildRunWithNodeSelector))
Expect(err).To(BeNil())

buildStrategy, err = ctl.LoadBuildStrategyFromBytes([]byte(test.ClusterBuildStrategyNoOp))
Expect(err).To(BeNil())
})

JustBeforeEach(func() {
got, err = resources.GenerateTaskRun(config.NewDefaultConfig(), build, buildRun, serviceAccountName, buildStrategy)
Expect(err).To(BeNil())
})

It("should give precedence to the nodeSelector specified in the buildRun", func() {
Expect(got.Spec.PodTemplate.NodeSelector).To(Equal(buildRun.Spec.NodeSelector))
})
})
})
})
41 changes: 41 additions & 0 deletions test/integration/build_to_taskruns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package integration_test

import (
"strings"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -202,4 +204,43 @@ var _ = Describe("Integration tests Build and TaskRun", func() {
})
})
})

Context("when a build with nodeSelector is defined", func() {
BeforeEach(func() {
buildSample = []byte(test.MinimalBuildWithNodeSelector)
buildRunSample = []byte(test.MinimalBuildRun)
})

Context("when the TaskRun is created", func() {
It("should have the nodeSelector specified in the PodTemplate", func() {
Expect(tb.CreateBuild(buildObject)).To(BeNil())

buildObject, err = tb.GetBuildTillValidation(buildObject.Name)
Expect(err).To(BeNil())

Expect(tb.CreateBR(buildRunObject)).To(BeNil())

_, err = tb.GetBRTillStartTime(buildRunObject.Name)
Expect(err).To(BeNil())

tr, err := tb.GetTaskRunFromBuildRun(buildRunObject.Name)
Expect(err).To(BeNil())
Expect(buildObject.Spec.NodeSelector).To(Equal(tr.Spec.PodTemplate.NodeSelector))
})
})

Context("when the nodeSelector is invalid", func() {
It("fails the build with a proper error in Reason", func() {
// set nodeSelector label to be invalid
buildObject.Spec.NodeSelector = map[string]string{strings.Repeat("s", 64): ""}
Expect(tb.CreateBuild(buildObject)).To(BeNil())

buildObject, err = tb.GetBuildTillValidation(buildObject.Name)
Expect(err).To(BeNil())

Expect(*buildObject.Status.Registered).To(Equal(corev1.ConditionFalse))
Expect(*buildObject.Status.Reason).To(Equal(v1beta1.NodeSelectorNotValid))
})
})
})
})
46 changes: 46 additions & 0 deletions test/integration/buildruns_to_taskruns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,4 +536,50 @@ var _ = Describe("Integration tests BuildRuns and TaskRuns", func() {
Expect(err).To(HaveOccurred())
})
})

Context("when a buildrun is created with a nodeSelector defined", func() {
BeforeEach(func() {
buildSample = []byte(test.MinimalBuild)
buildRunSample = []byte(test.MinimalBuildRunWithNodeSelector)
})

Context("when the taskrun is created", func() {
It("should have the nodeSelector specified in the PodTemplate", func() {
Expect(tb.CreateBuild(buildObject)).To(BeNil())

buildObject, err = tb.GetBuildTillValidation(buildObject.Name)
Expect(err).To(BeNil())

Expect(tb.CreateBR(buildRunObject)).To(BeNil())

br, err := tb.GetBRTillCompletion(buildRunObject.Name)
Expect(err).To(BeNil())

tr, err := tb.GetTaskRunFromBuildRun(buildRunObject.Name)
Expect(err).To(BeNil())
Expect(br.Spec.NodeSelector).To(Equal(tr.Spec.PodTemplate.NodeSelector))
})
})

Context("when the nodeSelector is invalid", func() {
It("fails the buildrun with a proper error in Reason", func() {
Expect(tb.CreateBuild(buildObject)).To(BeNil())

buildObject, err = tb.GetBuildTillValidation(buildObject.Name)
Expect(err).To(BeNil())

// set nodeSelector label to be invalid
buildRunObject.Spec.NodeSelector = map[string]string{strings.Repeat("s", 64): "amd64"}
Expect(tb.CreateBR(buildRunObject)).To(BeNil())

br, err := tb.GetBRTillCompletion(buildRunObject.Name)
Expect(err).To(BeNil())

condition := br.Status.GetCondition(v1beta1.Succeeded)
Expect(condition.Status).To(Equal(corev1.ConditionFalse))
Expect(condition.Reason).To(Equal("PodCreationFailed"))
Expect(condition.Message).To(ContainSubstring("must be no more than 63 characters"))
})
})
})
})
37 changes: 35 additions & 2 deletions test/v1beta1_samples/build_samples.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,25 @@ spec:
value: Dockerfile
`

// MinimalBuildahBuildWithNodeSelector defines a simple
// Build with a source, strategy, and nodeSelector
const MinimalBuildahBuildWithNodeSelector = `
apiVersion: shipwright.io/v1beta1
kind: Build
metadata:
name: buildah
spec:
source:
type: Git
git:
url: "https://github.com/shipwright-io/sample-go"
strategy:
name: buildah
kind: ClusterBuildStrategy
nodeSelector:
kubernetes.io/arch: amd64
`

// BuildahBuildWithOutput defines a simple
// Build with a source, strategy and output
const BuildahBuildWithOutput = `
Expand Down Expand Up @@ -520,8 +539,8 @@ spec:
image: image-registry.openshift-image-registry.svc:5000/example/buildpacks-app
`

// BuildWithRestrictedParam defines a Build using params that are reserved only
// for shipwright
// MinimalBuild defines a simple
// Build with a strategy output
const MinimalBuild = `
apiVersion: shipwright.io/v1beta1
kind: Build
Expand All @@ -532,6 +551,20 @@ spec:
image: image-registry.openshift-image-registry.svc:5000/example/buildpacks-app
`

// MinimalBuildWithNodeSelector defines a simple
// Build with a strategy, output, and NodeSelector
const MinimalBuildWithNodeSelector = `
apiVersion: shipwright.io/v1beta1
kind: Build
spec:
strategy:
kind: ClusterBuildStrategy
output:
image: image-registry.openshift-image-registry.svc:5000/example/buildpacks-app
nodeSelector:
kubernetes.io/arch: amd64
`

// BuildWithUndefinedParameter defines a param that was not declared under the
// strategy parameters
const BuildWithUndefinedParam = `
Expand Down
27 changes: 27 additions & 0 deletions test/v1beta1_samples/buildrun_samples.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ spec:
name: buildah
`

// MinimalBuildahBuildRunWithNodeSelector defines a simple
// BuildRun with a referenced Build and nodeSelector
const MinimalBuildahBuildRunWithNodeSelector = `
apiVersion: shipwright.io/v1beta1
kind: BuildRun
metadata:
name: buildah-run
spec:
build:
name: buildah
nodeSelector:
kubernetes.io/arch: amd64
`

// BuildahBuildRunWithSA defines a BuildRun
// with a service-account
const BuildahBuildRunWithSA = `
Expand Down Expand Up @@ -213,6 +227,19 @@ spec:
name: foobar
`

// MinimalBuildRunWithNodeSelector defines a minimal BuildRun
// with a reference to a not existing Build,
// and a nodeSelector
const MinimalBuildRunWithNodeSelector = `
apiVersion: shipwright.io/v1beta1
kind: BuildRun
spec:
build:
name: foobar
nodeSelector:
kubernetes.io/arch: amd64
`

// MinimalBuildRunWithVulnerabilityScan defines a BuildRun with
// an override for the Build Output
const MinimalBuildRunWithVulnerabilityScan = `
Expand Down

0 comments on commit 90becbe

Please sign in to comment.