Skip to content

Commit

Permalink
test changes based on switch to error out for certain permutations
Browse files Browse the repository at this point in the history
  • Loading branch information
gabemontero committed Aug 31, 2021
1 parent b9c1ef0 commit f72a901
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 8 deletions.
65 changes: 64 additions & 1 deletion controllers/default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
crdv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -34,11 +35,73 @@ var _ = g.Describe("Reconcile default ShipwrightBuild installation", func() {
// build Build instance employed during testing
var build *v1alpha1.ShipwrightBuild

truePtr := true
g.BeforeEach(func() {
// setting up the namespaces, where Shipwright Controller will be deployed
createNamespace(namespace)
createNamespace(targetNamespace)

g.By("does tekton taskrun crd exist")
err := k8sClient.Get(ctx, types.NamespacedName{Name: "taskruns.tekton.dev"}, &crdv1.CustomResourceDefinition{})
if errors.IsNotFound(err) {
g.By("creating tekton taskrun crd")
taskRunCRD := &crdv1.CustomResourceDefinition{}
taskRunCRD.Name = "taskruns.tekton.dev"
taskRunCRD.Spec.Group = "tekton.dev"
taskRunCRD.Spec.Scope = crdv1.NamespaceScoped
taskRunCRD.Spec.Versions = []crdv1.CustomResourceDefinitionVersion{
{
Name: "v1beta1",
Storage: true,
Schema: &crdv1.CustomResourceValidation{
OpenAPIV3Schema: &crdv1.JSONSchemaProps{
Type: "object",
XPreserveUnknownFields: &truePtr,
},
},
},
}
taskRunCRD.Spec.Names.Plural = "taskruns"
taskRunCRD.Spec.Names.Singular = "taskrun"
taskRunCRD.Spec.Names.Kind = "TaskRun"
taskRunCRD.Spec.Names.ListKind = "TaskRunList"
taskRunCRD.Status.StoredVersions = []string{"v1beta1"}
err = k8sClient.Create(ctx, taskRunCRD, &client.CreateOptions{})
o.Expect(err).NotTo(o.HaveOccurred())

}
o.Expect(err).NotTo(o.HaveOccurred())

g.By("does tektonconfig crd exist")
err = k8sClient.Get(ctx, types.NamespacedName{Name: "tektonconfigs.operator.tekton.dev"}, &crdv1.CustomResourceDefinition{})
if errors.IsNotFound(err) {
tektonOpCRD := &crdv1.CustomResourceDefinition{}
tektonOpCRD.Name = "tektonconfigs.operator.tekton.dev"
tektonOpCRD.Labels = map[string]string{"version": "v0.49.0"}
tektonOpCRD.Spec.Group = "operator.tekton.dev"
tektonOpCRD.Spec.Scope = crdv1.ClusterScoped
tektonOpCRD.Spec.Versions = []crdv1.CustomResourceDefinitionVersion{
{
Name: "v1alpha1",
Storage: true,
Schema: &crdv1.CustomResourceValidation{
OpenAPIV3Schema: &crdv1.JSONSchemaProps{
Type: "object",
XPreserveUnknownFields: &truePtr,
},
},
},
}
tektonOpCRD.Spec.Names.Plural = "tektonconfigs"
tektonOpCRD.Spec.Names.Singular = "tektonconfig"
tektonOpCRD.Spec.Names.Kind = "TektonConfig"
tektonOpCRD.Spec.Names.ListKind = "TektonConfigList"
tektonOpCRD.Status.StoredVersions = []string{"v1alpha1"}
err = k8sClient.Create(ctx, tektonOpCRD, &client.CreateOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
}
o.Expect(err).NotTo(o.HaveOccurred())

g.By("creating a ShipwrightBuild instance")
build = &v1alpha1.ShipwrightBuild{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -49,7 +112,7 @@ var _ = g.Describe("Reconcile default ShipwrightBuild installation", func() {
TargetNamespace: targetNamespace,
},
}
err := k8sClient.Create(ctx, build, &client.CreateOptions{})
err = k8sClient.Create(ctx, build, &client.CreateOptions{})
o.Expect(err).NotTo(o.HaveOccurred())

// when the finalizer is in place, the deployment of manifest elements is done, and therefore
Expand Down
8 changes: 4 additions & 4 deletions controllers/shipwrightbuild_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,19 @@ func (r *ShipwrightBuildReconciler) Reconcile(ctx context.Context, req ctrl.Requ
case tektonAPIErr == nil && tektonOPErr != nil:
logger.Info("Tekton Pipelines has been installed without use of its associated operator.")
case errors.IsNotFound(tektonAPIErr) && errors.IsNotFound(tektonOPErr):
//TODO should we error out here or proceed and give the user the opportunity of install Tekton after Shipwright?
logger.Info("Evidence of neither required Tekton APIs nor the Tekton Operator being installed can be found. Will proceed with the Shipwright installation attempt, but Shiipwright will not be functional without Tekton")
logger.Info("Evidence of neither required Tekton APIs nor the Tekton Operator being installed can be found. Aborting the Shipwright installation attempt.")
return ctrl.Result{Requeue: false}, fmt.Errorf("neither Tekton Pipeliens nor Tekton Operator are available")
case tektonAPIErr != nil && tektonOPErr == nil:
if tektonOpCRD.Labels == nil {
retErr := fmt.Errorf("the CRD TektonConfig does not have labels set, inclding its version")
logger.Error(retErr, "Problem confirming Tekton Operator version")
return RequeueWithError(retErr)
return ctrl.Result{Requeue: false}, fmt.Errorf("the Tekton Operator is present, but no specification of its version exists on the CRD in question")
}
value, exists := tektonOpCRD.Labels["version"]
if !exists {
retErr := fmt.Errorf("the CRD TektonConfig does not have labels set, inclding its version")
logger.Error(retErr, "Problem confirming Tekton Operator version")
return RequeueWithError(retErr)
return ctrl.Result{Requeue: false}, fmt.Errorf("the Tekton Operator is present, but no specification of its version exists on the CRD in question")
}
// replace v0 with v1 to make k8s version check happy
if strings.HasPrefix(value, "v0") {
Expand Down
6 changes: 3 additions & 3 deletions controllers/shipwrightbuild_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func TestShipwrightBuildReconciler_ProvisionTekton(t *testing.T) {

res, err := r.Reconcile(ctx, req)
g.Expect(err).NotTo(o.BeNil())
g.Expect(res.Requeue).To(o.BeTrue())
g.Expect(res.Requeue).To(o.BeFalse())

_, err = r.TektonOperatorClient.TektonConfigs().Get(ctx, "config", metav1.GetOptions{})
g.Expect(err).NotTo(o.BeNil())
Expand Down Expand Up @@ -203,7 +203,7 @@ func TestShipwrightBuildReconciler_ProvisionTekton(t *testing.T) {

res, err := r.Reconcile(ctx, req)
g.Expect(err).NotTo(o.BeNil())
g.Expect(res.Requeue).To(o.BeTrue())
g.Expect(res.Requeue).To(o.BeFalse())

_, err = r.TektonOperatorClient.TektonConfigs().Get(ctx, "config", metav1.GetOptions{})
g.Expect(err).NotTo(o.BeNil())
Expand Down Expand Up @@ -296,7 +296,7 @@ func TestShipwrightBuildReconciler_ProvisionTekton(t *testing.T) {
_, _, _, r := bootstrapShipwrightBuildReconciler(t, b, nil, nil)

res, err := r.Reconcile(ctx, req)
g.Expect(err).To(o.BeNil())
g.Expect(err).NotTo(o.BeNil())
g.Expect(res.Requeue).To(o.BeFalse())
})

Expand Down

0 comments on commit f72a901

Please sign in to comment.