|
| 1 | +// Copyright The Shipwright Contributors |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +package buildrun_test |
| 6 | + |
| 7 | +import ( |
| 8 | + . "github.com/onsi/ginkgo" |
| 9 | + . "github.com/onsi/gomega" |
| 10 | + build "github.com/shipwright-io/build/pkg/apis/build/v1alpha1" |
| 11 | + "github.com/shipwright-io/build/test" |
| 12 | + corev1 "k8s.io/api/core/v1" |
| 13 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 14 | +) |
| 15 | + |
| 16 | +var _ = Describe("Conditions", func() { |
| 17 | + |
| 18 | + var ( |
| 19 | + ctl test.Catalog |
| 20 | + ) |
| 21 | + |
| 22 | + Context("Operating on Conditions", func() { |
| 23 | + |
| 24 | + It("should be able to retrieve an existing condition message", func() { |
| 25 | + // BuildRun sample with an embedded condition of the type Succeeded |
| 26 | + br := ctl.BuildRunWithSucceededCondition() |
| 27 | + |
| 28 | + // BuildRun implements StatusConditions, therefore it can operate on |
| 29 | + // an existing Condition |
| 30 | + msg := br.Status.GetCondition(build.Succeeded).GetMessage() |
| 31 | + Expect(msg).To(Equal("foo is not bar")) |
| 32 | + }) |
| 33 | + |
| 34 | + It("should be able to retrieve an existing condition reason", func() { |
| 35 | + // BuildRun sample with an embedded condition of the type Succeeded |
| 36 | + br := ctl.BuildRunWithSucceededCondition() |
| 37 | + |
| 38 | + reason := br.Status.GetCondition(build.Succeeded).GetReason() |
| 39 | + Expect(reason).To(Equal("foobar")) |
| 40 | + }) |
| 41 | + |
| 42 | + It("should be able to retrieve an existing condition status", func() { |
| 43 | + // BuildRun sample with an embedded condition of the type Succeeded |
| 44 | + br := ctl.BuildRunWithSucceededCondition() |
| 45 | + |
| 46 | + status := br.Status.GetCondition(build.Succeeded).GetStatus() |
| 47 | + Expect(status).To(Equal(corev1.ConditionUnknown)) |
| 48 | + }) |
| 49 | + |
| 50 | + It("should return nil if a condition is not available when operating on it", func() { |
| 51 | + br := ctl.DefaultBuildRun("foo", "bar") |
| 52 | + |
| 53 | + // when getting a condition that does not exists on the BuildRun, do not |
| 54 | + // panic but rather return a nil |
| 55 | + cond := br.Status.GetCondition(build.Succeeded) |
| 56 | + Expect(cond).To(BeNil()) |
| 57 | + }) |
| 58 | + |
| 59 | + It("should be able to set a condition based on a type", func() { |
| 60 | + br := ctl.DefaultBuildRun("foo", "bar") |
| 61 | + |
| 62 | + // generate a condition of the type Succeeded |
| 63 | + tmpCond := &build.Condition{ |
| 64 | + Type: build.Succeeded, |
| 65 | + Status: corev1.ConditionUnknown, |
| 66 | + Message: "foobar", |
| 67 | + Reason: "foo is bar", |
| 68 | + LastTransitionTime: metav1.Now(), |
| 69 | + } |
| 70 | + |
| 71 | + // set the condition on the BuildRun resource |
| 72 | + br.Status.SetCondition(tmpCond) |
| 73 | + |
| 74 | + condition := br.Status.GetCondition(build.Succeeded) |
| 75 | + Expect(condition).ToNot(BeNil()) |
| 76 | + Expect(condition.Type).To(Equal(build.Succeeded)) |
| 77 | + |
| 78 | + condMsg := br.Status.GetCondition(build.Succeeded).GetMessage() |
| 79 | + Expect(condMsg).To(Equal("foobar")) |
| 80 | + }) |
| 81 | + |
| 82 | + It("should be able to update an existing condition based on a type", func() { |
| 83 | + // BuildRun sample with an embedded condition of the type Succeeded |
| 84 | + br := ctl.BuildRunWithSucceededCondition() |
| 85 | + |
| 86 | + reason := br.Status.GetCondition(build.Succeeded).GetReason() |
| 87 | + Expect(reason).To(Equal("foobar")) |
| 88 | + |
| 89 | + // generate a condition in order to update the existing one |
| 90 | + tmpCond := &build.Condition{ |
| 91 | + Type: build.Succeeded, |
| 92 | + Status: corev1.ConditionUnknown, |
| 93 | + Message: "foobar was updated", |
| 94 | + Reason: "foo is bar", |
| 95 | + LastTransitionTime: metav1.Now(), |
| 96 | + } |
| 97 | + |
| 98 | + // update the condition on the BuildRun resource |
| 99 | + br.Status.SetCondition(tmpCond) |
| 100 | + |
| 101 | + condMsg := br.Status.GetCondition(build.Succeeded).GetMessage() |
| 102 | + Expect(condMsg).To(Equal("foobar was updated")) |
| 103 | + }) |
| 104 | + }) |
| 105 | +}) |
0 commit comments