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 Feb 7, 2025
1 parent 9d22ecb commit 7377fd4
Show file tree
Hide file tree
Showing 12 changed files with 561 additions and 3 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, "Scheduler name not valid: name part "+validation.MaxLenError(63))
statusWriter.UpdateCalls(statusCall)

_, err := reconciler.Reconcile(context.TODO(), request)
Expect(err).To(BeNil())
Expect(statusWriter.UpdateCallCount()).To(Equal(1))
})
})
})
})
59 changes: 59 additions & 0 deletions pkg/reconciler/buildrun/buildrun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1660,5 +1660,64 @@ 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))
})
})

Context("when a buildrun has a buildSpec defined and overrides nodeSelector", func() {
BeforeEach(func() {
buildRunSample = ctl.BuildRunWithNodeSelectorOverride(buildRunName, buildName, map[string]string{"Key": "testkey", "Value": "testvalue"})
})

It("should fail to register", func() {
statusCall := ctl.StubFunc(corev1.ConditionFalse, build.BuildReason(resources.BuildRunBuildFieldOverrideForbidden), "cannot use 'nodeSelector' override and 'buildSpec' simultaneously")
statusWriter.UpdateCalls(statusCall)

_, err := reconciler.Reconcile(context.TODO(), buildRunRequest)
Expect(err).To(BeNil())
Expect(statusWriter.UpdateCallCount()).To(Equal(1))
})
})

Context("when a buildrun has a buildSpec defined and overrides Tolerations", func() {
BeforeEach(func() {
buildRunSample = ctl.BuildRunWithTolerationsOverride(buildRunName, buildName, []corev1.Toleration{{Key: "testkey", Value: "testvalue", Operator: corev1.TolerationOpEqual, Effect: corev1.TaintEffectNoSchedule}})
})

It("should fail to register", func() {
statusCall := ctl.StubFunc(corev1.ConditionFalse, build.BuildReason(resources.BuildRunBuildFieldOverrideForbidden), "cannot use 'tolerations' override and 'buildSpec' simultaneously")
statusWriter.UpdateCalls(statusCall)

_, err := reconciler.Reconcile(context.TODO(), buildRunRequest)
Expect(err).To(BeNil())
Expect(statusWriter.UpdateCallCount()).To(Equal(1))
})
})

Context("when a buildrun has a buildSpec defined and overrides schedulerName", func() {
BeforeEach(func() {
buildRunSample = ctl.BuildRunWithSchedulerNameOverride(buildRunName, buildName, "testSchedulerName")
})

It("should fail to register", func() {
statusCall := ctl.StubFunc(corev1.ConditionFalse, build.BuildReason(resources.BuildRunBuildFieldOverrideForbidden), "cannot use 'schedulerName' override and 'buildSpec' simultaneously")
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))
})
})
})
})
69 changes: 69 additions & 0 deletions pkg/validate/nodeselector_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright The Shipwright Contributors
//
// SPDX-License-Identifier: Apache-2.0

package validate_test

import (
"context"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

corev1 "k8s.io/apimachinery/pkg/apis/meta/v1"

. "github.com/shipwright-io/build/pkg/apis/build/v1beta1"
"github.com/shipwright-io/build/pkg/validate"
)

var _ = Describe("ValidateNodeSelector", func() {
var ctx context.Context

BeforeEach(func() {
ctx = context.TODO()
})

var validate = func(build *Build) {
GinkgoHelper()

var validator = &validate.NodeSelectorRef{Build: build}
Expect(validator.ValidatePath(ctx)).To(Succeed())
}

var sampleBuild = func(key string, value string) *Build {
return &Build{
ObjectMeta: corev1.ObjectMeta{
Namespace: "foo",
Name: "bar",
},
Spec: BuildSpec{
NodeSelector: map[string]string{"Key": key, "Value": value},
},
}
}

Context("when node selector is specified", func() {
It("should pass an empty key and value", func() {
build := sampleBuild("", "")
validate(build)
Expect(build.Status.Reason).To(BeNil())
Expect(build.Status.Message).To(BeNil())
})

It("should pass an empty key and valid value", func() {
build := sampleBuild("", "validvalue")
validate(build)
Expect(build.Status.Reason).To(BeNil())
Expect(build.Status.Message).To(BeNil())
})

It("should fail an empty key and invalid value", func() {
build := sampleBuild("", "invalidvalue!")
validate(build)
Expect(build.Status.Reason).To(BeNil())
Expect(build.Status.Message).To(BeNil())
})

// TODO: add the rest of the combinations
})
})
62 changes: 62 additions & 0 deletions pkg/validate/scheduler_name_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright The Shipwright Contributors
//
// SPDX-License-Identifier: Apache-2.0

package validate_test

import (
"context"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

corev1 "k8s.io/apimachinery/pkg/apis/meta/v1"

. "github.com/shipwright-io/build/pkg/apis/build/v1beta1"
"github.com/shipwright-io/build/pkg/validate"
)

var _ = Describe("ValidateSchedulerName", func() {
var ctx context.Context

BeforeEach(func() {
ctx = context.TODO()
})

var validate = func(build *Build) {
GinkgoHelper()

var validator = &validate.SchedulerNameRef{Build: build}
Expect(validator.ValidatePath(ctx)).To(Succeed())
}

var sampleBuild = func(schedulerName string) *Build {
return &Build{
ObjectMeta: corev1.ObjectMeta{
Namespace: "foo",
Name: "bar",
},
Spec: BuildSpec{
SchedulerName: schedulerName,
},
}
}

Context("when schedulerName is specified", func() {
It("should pass an empty name", func() {
build := sampleBuild("")
validate(build)
Expect(build.Status.Reason).To(BeNil())
Expect(build.Status.Message).To(BeNil())
})

It("should fail an invalid name", func() {
build := sampleBuild("invalidname!")
validate(build)
Expect(build.Status.Reason).To(BeNil())
Expect(build.Status.Message).To(BeNil())
})

// TODO: add the rest of the combinations
})
})
56 changes: 56 additions & 0 deletions pkg/validate/tolerations_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright The Shipwright Contributors
//
// SPDX-License-Identifier: Apache-2.0

package validate_test

import (
"context"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

v1 "k8s.io/api/core/v1"
corev1 "k8s.io/apimachinery/pkg/apis/meta/v1"

. "github.com/shipwright-io/build/pkg/apis/build/v1beta1"
"github.com/shipwright-io/build/pkg/validate"
)

var _ = Describe("ValidateTolerations", func() {
var ctx context.Context

BeforeEach(func() {
ctx = context.TODO()
})

var validate = func(build *Build) {
GinkgoHelper()

var validator = &validate.TolerationsRef{Build: build}
Expect(validator.ValidatePath(ctx)).To(Succeed())
}

var sampleBuild = func(toleration v1.Toleration) *Build {
return &Build{
ObjectMeta: corev1.ObjectMeta{
Namespace: "foo",
Name: "bar",
},
Spec: BuildSpec{
Tolerations: []v1.Toleration{toleration},
},
}
}

Context("when node selector is specified", func() {
It("should pass an empty key and empty value", func() {
build := sampleBuild(v1.Toleration{Key: "", Operator: v1.TolerationOpEqual, Value: "", Effect: v1.TaintEffectNoSchedule})
validate(build)
Expect(build.Status.Reason).To(BeNil())
Expect(build.Status.Message).To(BeNil())
})

// TODO: add the rest of the combinations
})
})
5 changes: 2 additions & 3 deletions test/e2e/v1beta1/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ import (
v1 "github.com/google/go-containerregistry/pkg/v1"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

buildv1beta1 "github.com/shipwright-io/build/pkg/apis/build/v1beta1"
shpgit "github.com/shipwright-io/build/pkg/git"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

corev1 "k8s.io/api/core/v1"
)

var _ = Describe("For a Kubernetes cluster with Tekton and build installed", func() {
Expand Down
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))
})
})
})
})
Loading

0 comments on commit 7377fd4

Please sign in to comment.