Skip to content

Commit

Permalink
Adjust e2e tests for beta
Browse files Browse the repository at this point in the history
  • Loading branch information
qu1queee committed Jan 24, 2024
1 parent 53a5e85 commit 88df9b5
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 10 deletions.
56 changes: 54 additions & 2 deletions test/e2e/v1alpha1/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package e2e_test

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
Expand All @@ -16,15 +17,18 @@ import (

. "github.com/onsi/gomega"
knativeapis "knative.dev/pkg/apis"
"sigs.k8s.io/controller-runtime/pkg/client"

corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/rand"

buildv1alpha1 "github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
"github.com/shipwright-io/build/pkg/reconciler/buildrun/resources"
"github.com/shipwright-io/build/pkg/ctxlog"
resources "github.com/shipwright-io/build/pkg/reconciler/buildrun/resources"
utils "github.com/shipwright-io/build/test/utils/v1alpha1"
)

Expand Down Expand Up @@ -143,7 +147,7 @@ func retrieveBuildAndBuildRun(testBuild *utils.TestBuild, namespace string, buil
}

var build buildv1alpha1.Build
if err := resources.GetBuildObject(testBuild.Context, testBuild.ControllerRuntimeClient, buildRun, &build); err != nil {
if err := GetBuildObject(testBuild.Context, testBuild.ControllerRuntimeClient, buildRun, &build); err != nil {
Logf("Failed to get Build from BuildRun %s: %s", buildRunName, err)
return nil, buildRun, err
}
Expand Down Expand Up @@ -246,3 +250,51 @@ func printTestFailureDebugInfo(testBuild *utils.TestBuild, namespace string, bui
}
}
}

// GetBuildObject retrieves an existing Build based on a name and namespace
func GetBuildObject(ctx context.Context, client client.Client, buildRun *buildv1alpha1.BuildRun, build *buildv1alpha1.Build) error {
// Option #1: BuildRef is specified
// An actual Build resource is specified by name and needs to be looked up in the cluster.
if buildRun.Spec.BuildRef.Name != "" {
err := client.Get(ctx, types.NamespacedName{Name: buildRun.Spec.BuildName(), Namespace: buildRun.Namespace}, build)
if apierrors.IsNotFound(err) {
// stop reconciling and mark the BuildRun as Failed
// we only reconcile again if the status.Update call fails
if updateErr := UpdateConditionWithFalseStatus(ctx, client, buildRun, fmt.Sprintf("build.shipwright.io %q not found", buildRun.Spec.BuildName()), resources.ConditionBuildNotFound); updateErr != nil {
return resources.HandleError("build object not found", err, updateErr)
}
}

return err
}

// Option #2: BuildSpec is specified
// The build specification is embedded in the BuildRun itself, create a transient Build resource.
if buildRun.Spec.BuildSpec != nil {
build.Name = ""
build.Namespace = buildRun.Namespace
build.Status = buildv1alpha1.BuildStatus{}
buildRun.Spec.BuildSpec.DeepCopyInto(&build.Spec)
return nil
}

// Bail out hard in case of an invalid state
return fmt.Errorf("invalid BuildRun resource that neither has a BuildRef nor an embedded BuildSpec")
}

func UpdateConditionWithFalseStatus(ctx context.Context, client client.Client, buildRun *buildv1alpha1.BuildRun, errorMessage string, reason string) error {
now := metav1.Now()
buildRun.Status.CompletionTime = &now
buildRun.Status.SetCondition(&buildv1alpha1.Condition{
LastTransitionTime: now,
Type: buildv1alpha1.Succeeded,
Status: corev1.ConditionFalse,
Reason: reason,
Message: errorMessage,
})
ctxlog.Debug(ctx, "updating buildRun status", "namespace", buildRun.Namespace, "name", buildRun.Name, "reason", reason)
if err := client.Status().Update(ctx, buildRun); err != nil {
return err
}
return nil
}
5 changes: 5 additions & 0 deletions test/e2e/v1beta1/common_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ func (b *buildPrototype) SourceCredentials(name string) *buildPrototype {
return b
}

func (b *buildPrototype) SourceType(sourceType string) *buildPrototype {
b.build.Spec.Source.Type = buildv1beta1.BuildSourceType(sourceType)
return b
}

func (b *buildPrototype) SourceGit(repository string) *buildPrototype {
if b.build.Spec.Source.GitSource == nil {
b.build.Spec.Source.GitSource = &buildv1beta1.Git{}
Expand Down
11 changes: 3 additions & 8 deletions test/e2e/v1beta1/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/rand"

buildv1alpha1 "github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
buildv1beta1 "github.com/shipwright-io/build/pkg/apis/build/v1beta1"
"github.com/shipwright-io/build/pkg/reconciler/buildrun/resources"
utils "github.com/shipwright-io/build/test/utils/v1beta1"
Expand Down Expand Up @@ -138,13 +137,14 @@ func amendBuild(identifier string, b *buildv1beta1.Build) {

// retrieveBuildAndBuildRun will retrieve the build and buildRun
func retrieveBuildAndBuildRun(testBuild *utils.TestBuild, namespace string, buildRunName string) (*buildv1beta1.Build, *buildv1beta1.BuildRun, error) {
// TODO: double check the following function content.
buildRun, err := testBuild.LookupBuildRun(types.NamespacedName{Name: buildRunName, Namespace: namespace})
if err != nil {
Logf("Failed to get BuildRun %q: %s", buildRunName, err)
return nil, nil, err
}

var alphaBuild buildv1alpha1.Build
var alphaBuild buildv1beta1.Build
var obj unstructured.Unstructured

buildRun.ConvertTo(testBuild.Context, &obj)
Expand All @@ -153,19 +153,14 @@ func retrieveBuildAndBuildRun(testBuild *utils.TestBuild, namespace string, buil
Logf("Failed to convert the buildRun to v1alpha1: %s", err)
}

var alphaBuildRun buildv1alpha1.BuildRun
var alphaBuildRun buildv1beta1.BuildRun
json.Unmarshal(jsonData, &alphaBuildRun)

if err := resources.GetBuildObject(testBuild.Context, testBuild.ControllerRuntimeClient, &alphaBuildRun, &alphaBuild); err != nil {
Logf("Failed to get Build from BuildRun %s: %s", buildRunName, err)
return nil, buildRun, err
}

alphaBuild.ConvertTo(testBuild.Context, &obj)
jsonData, err = json.Marshal(obj.Object)
if err != nil {
Logf("Failed to convert the build to v1beta1: %s", err)
}
var betaBuild buildv1beta1.Build
json.Unmarshal(jsonData, &betaBuild)

Expand Down
2 changes: 2 additions & 0 deletions test/e2e/v1beta1/e2e_one_off_builds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ var _ = Describe("Using One-Off Builds", func() {
ClusterBuildStrategy("buildpacks-v3").
Namespace(testBuild.Namespace).
Name(testID).
SourceType("Git").
SourceGit("https://github.com/shipwright-io/sample-go.git").
SourceContextDir("source-build").
OutputImage(outputImage.String()).
Expand All @@ -88,6 +89,7 @@ var _ = Describe("Using One-Off Builds", func() {
ClusterBuildStrategy("buildah-shipwright-managed-push").
Namespace(testBuild.Namespace).
Name(testID).
SourceType("Git").
SourceGit("https://github.com/shipwright-io/sample-go.git").
SourceContextDir("docker-build").
Dockerfile("Dockerfile").
Expand Down

0 comments on commit 88df9b5

Please sign in to comment.