diff --git a/pkg/reconciler/build/build_test.go b/pkg/reconciler/build/build_test.go index 1d9a21db5..1b0ecdf31 100644 --- a/pkg/reconciler/build/build_test.go +++ b/pkg/reconciler/build/build_test.go @@ -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)) + }) + }) }) }) diff --git a/pkg/reconciler/buildrun/buildrun_test.go b/pkg/reconciler/buildrun/buildrun_test.go index 18771f3a9..3270212bc 100644 --- a/pkg/reconciler/buildrun/buildrun_test.go +++ b/pkg/reconciler/buildrun/buildrun_test.go @@ -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)) + }) + }) }) }) diff --git a/pkg/reconciler/buildrun/resources/taskrun_test.go b/pkg/reconciler/buildrun/resources/taskrun_test.go index c81b07320..4380f03b0 100644 --- a/pkg/reconciler/buildrun/resources/taskrun_test.go +++ b/pkg/reconciler/buildrun/resources/taskrun_test.go @@ -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)) + }) + }) }) }) diff --git a/test/integration/build_to_taskruns_test.go b/test/integration/build_to_taskruns_test.go index 675305e14..871abd79c 100644 --- a/test/integration/build_to_taskruns_test.go +++ b/test/integration/build_to_taskruns_test.go @@ -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)) + }) + }) + }) }) diff --git a/test/integration/buildruns_to_taskruns_test.go b/test/integration/buildruns_to_taskruns_test.go index 56e412cb2..e3e76d6f7 100644 --- a/test/integration/buildruns_to_taskruns_test.go +++ b/test/integration/buildruns_to_taskruns_test.go @@ -633,4 +633,45 @@ 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.BuildRunWithSchedulerName) + }) + + 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() { + // set SchedulerName to be invalid + buildRunObject.Spec.SchedulerName = 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("BuildRegistrationFailed")) + Expect(condition.Message).To(ContainSubstring(validation.MaxLenError(63))) + }) + }) + }) }) diff --git a/test/v1beta1_samples/build_samples.go b/test/v1beta1_samples/build_samples.go index eff97c325..538f6df6a 100644 --- a/test/v1beta1_samples/build_samples.go +++ b/test/v1beta1_samples/build_samples.go @@ -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 = ` diff --git a/test/v1beta1_samples/buildrun_samples.go b/test/v1beta1_samples/buildrun_samples.go index cb728e0dc..722565749 100644 --- a/test/v1beta1_samples/buildrun_samples.go +++ b/test/v1beta1_samples/buildrun_samples.go @@ -255,6 +255,34 @@ 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" +` + +// BuildRunWithSchedulerName defines a BuildRun +// with a Build spec defined and a SchedulerName specified +const BuildRunWithSchedulerName = ` +apiVersion: shipwright.io/v1beta1 +kind: BuildRun +spec: + build: + spec: + strategy: + kind: ClusterBuildStrategy + output: + image: image-registry.openshift-image-registry.svc:5000/example/buildpacks-app + name: foobar + schedulerName: "buildrun-test-schedulername" +` + // MinimalBuildRunWithVulnerabilityScan defines a BuildRun with // an override for the Build Output const MinimalBuildRunWithVulnerabilityScan = `