Skip to content

Commit

Permalink
Tests for Custom Scheduler Name 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 Jan 21, 2025
1 parent 114d06a commit 02dfeeb
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 0 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 @@ -645,5 +645,20 @@ var _ = Describe("Reconcile Build", func() {
Expect(statusWriter.UpdateCallCount()).To(Equal(1))
})
})

Context("when SchedulerName is specified", func() {
It("should fail to validate when the SchedulerName is invalid", func() {
// set SchedulerName to be invalid
buildSample.Spec.SchedulerName = strings.Repeat("s", 64)
buildSample.Spec.Output.PushSecret = nil

statusCall := ctl.StubFunc(corev1.ConditionFalse, build.SchedulerNameNotValid, "name part "+validation.MaxLenError(63))
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 @@ -1660,5 +1660,19 @@ var _ = Describe("Reconcile BuildRun", func() {
Expect(statusWriter.UpdateCallCount()).To(Equal(1))
})
})

Context("when SchedulerName is specified", func() {
It("should fail to validate when the SchedulerName is invalid", func() {
// set SchedulerName to be invalid
buildRunSample.Spec.SchedulerName = strings.Repeat("s", 64)

statusCall := ctl.StubFunc(corev1.ConditionFalse, build.SchedulerNameNotValid, validation.MaxLenError(64))
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 @@ -678,5 +678,27 @@ var _ = Describe("GenerateTaskrun", func() {
Expect(got.Spec.PodTemplate.Tolerations[0].Value).To(Equal(buildRun.Spec.Tolerations[0].Value))
})
})

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

buildRun, err = ctl.LoadBuildRunFromBytes([]byte(test.MinimalBuildRunWithSchedulerName))
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 SchedulerName value specified in the buildRun", func() {
Expect(got.Spec.PodTemplate.SchedulerName).To(Equal(buildRun.Spec.SchedulerName))
})
})
})
})
42 changes: 42 additions & 0 deletions test/integration/build_to_taskruns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,46 @@ var _ = Describe("Integration tests Build and TaskRun", func() {
})
})
})

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

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

buildObject, err = tb.GetBuildTillValidation(buildObject.Name)
Expect(err).To(BeNil())
Expect(*buildObject.Status.Message).To(Equal(v1beta1.AllValidationsSucceeded))
Expect(*buildObject.Status.Registered).To(Equal(corev1.ConditionTrue))
Expect(*buildObject.Status.Reason).To(Equal(v1beta1.SucceedStatus))

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.SchedulerName).To(Equal(tr.Spec.PodTemplate.SchedulerName))
})
})

Context("when the SchedulerName is invalid", func() {
It("fails the build with a proper error in Reason", func() {
// set SchedulerName to be invalid
buildObject.Spec.SchedulerName = 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.SchedulerNameNotValid))
})
})
})
})
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 @@ -633,4 +633,50 @@ var _ = Describe("Integration tests BuildRuns and TaskRuns", func() {
})
})
})

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

Context("when the taskrun is created", func() {
It("should have the SchedulerName 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.GetBRTillStartTime(buildRunObject.Name)
Expect(err).To(BeNil())

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

Context("when the SchedulerName 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 SchedulerName to be invalid
buildRunObject.Spec.SchedulerName = strings.Repeat("s", 64)
Expect(tb.CreateBR(buildRunObject)).To(BeNil())

br, err := tb.GetBRTillStartTime(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(validation.MaxLenError(63)))
})
})
})
})
13 changes: 13 additions & 0 deletions test/v1beta1_samples/build_samples.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,19 @@ spec:
value: "build-test-value"
`

// MinimalBuildWithSchedulerName defines a simple
// Build with a strategy, output, and a SchedulerName specified
const MinimalBuildWithSchedulerName = `
apiVersion: shipwright.io/v1beta1
kind: Build
spec:
strategy:
kind: ClusterBuildStrategy
output:
image: image-registry.openshift-image-registry.svc:5000/example/buildpacks-app
schedulerName: "build-test-schedulername"
`

// BuildWithUndefinedParameter defines a param that was not declared under the
// strategy parameters
const BuildWithUndefinedParam = `
Expand Down
12 changes: 12 additions & 0 deletions test/v1beta1_samples/buildrun_samples.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,18 @@ spec:
value: "buildrun-test-value"
`

// MinimalBuildRunWithSchedulerName defines a minimal BuildRun
// with a reference to a not existing Build,
// and a SchedulerName specified
const MinimalBuildRunWithSchedulerName = `
apiVersion: shipwright.io/v1beta1
kind: BuildRun
spec:
build:
name: foobar
schedulerName: "buildrun-test-schedulername"
`

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

0 comments on commit 02dfeeb

Please sign in to comment.