Skip to content

Commit

Permalink
Tests for NodeSelectors on Build and BuildRun objects
Browse files Browse the repository at this point in the history
  • Loading branch information
dorzel committed Sep 24, 2024
1 parent a26faab commit dd9194a
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 2 deletions.
15 changes: 15 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,19 @@ 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"}

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 in the PodTemplate", 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))
})
})
})
})
45 changes: 45 additions & 0 deletions test/integration/build_to_taskruns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
package integration_test

import (
"strings"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"

"github.com/shipwright-io/build/pkg/apis/build/v1beta1"
"github.com/shipwright-io/build/pkg/reconciler/buildrun/resources"
utils "github.com/shipwright-io/build/test/utils/v1beta1"
test "github.com/shipwright-io/build/test/v1beta1_samples"
)
Expand Down Expand Up @@ -202,4 +205,46 @@ var _ = Describe("Integration tests Build and TaskRun", func() {
})
})
})

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

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())

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() {
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): ""}
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(resources.ConditionBuildRegistrationFailed))
Expect(buildObject.Status.Reason).To(Equal(v1beta1.NodeSelectorNotValid))
})
})
})
})
42 changes: 42 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,46 @@ 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.MinimalBuildahBuild)
buildRunSample = []byte(test.MinimalBuildahBuildRunWithNodeSelector)
})

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())

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 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(resources.ConditionBuildRegistrationFailed))
Expect(*buildObject.Status.Reason).To(Equal(v1beta1.NodeSelectorNotValid))
})
})
})
})
40 changes: 38 additions & 2 deletions test/v1beta1_samples/build_samples.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,28 @@ 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
paramValues:
- name: dockerfile
value: Dockerfile
nodeSelector:
kubernetes.io/arch: amd64
`

// BuildahBuildWithOutput defines a simple
// Build with a source, strategy and output
const BuildahBuildWithOutput = `
Expand Down Expand Up @@ -520,8 +542,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 +554,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 dd9194a

Please sign in to comment.