Skip to content

Commit

Permalink
Make e2e tests work with local porch-server, but everything else in kind
Browse files Browse the repository at this point in the history
  • Loading branch information
kispaljr committed May 24, 2024
1 parent 2710e2a commit e862875
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 67 deletions.
10 changes: 5 additions & 5 deletions test/e2e/cli/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
)

func GetGitServerImageName(t *testing.T) string {
cmd := exec.Command("kubectl", "get", "pods", "--selector=app=porch-server", "--namespace=porch-system",
cmd := exec.Command("kubectl", "get", "pods", "--selector=app=function-runner", "--namespace=porch-system",
"--output=jsonpath={.items[*].spec.containers[*].image}")

var stderr bytes.Buffer
Expand Down Expand Up @@ -204,10 +204,10 @@ func RemovePackagerevFinalizers(t *testing.T, namespace string) {
}

func realySplit(s, sep string) []string {
if len(s) == 0 {
return []string{}
}
return strings.Split(s, sep)
if len(s) == 0 {
return []string{}
}
return strings.Split(s, sep)
}

func RegisterRepository(t *testing.T, repoURL, namespace, name string) {
Expand Down
8 changes: 6 additions & 2 deletions test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"strings"
"testing"
"time"

"github.com/google/go-cmp/cmp"
porchapi "github.com/nephio-project/porch/api/porch/v1alpha1"
configapi "github.com/nephio-project/porch/api/porchconfig/v1alpha1"
Expand Down Expand Up @@ -1018,15 +1018,18 @@ func (t *PorchSuite) TestDeleteFinal(ctx context.Context) {
t.mustExist(ctx, client.ObjectKey{Namespace: t.namespace, Name: created.Name}, &pkg)

// Propose the package revision to be finalized
t.Log("Proposing package")
pkg.Spec.Lifecycle = porchapi.PackageRevisionLifecycleProposed
t.UpdateF(ctx, &pkg)

t.Log("Approving package")
pkg.Spec.Lifecycle = porchapi.PackageRevisionLifecyclePublished
t.UpdateApprovalF(ctx, &pkg, metav1.UpdateOptions{})

t.mustExist(ctx, client.ObjectKey{Namespace: t.namespace, Name: created.Name}, &pkg)

// Try to delete the package. This should fail because it hasn't been proposed for deletion.
t.Log("Trying to delete package (should fail)")
t.DeleteL(ctx, &porchapi.PackageRevision{
ObjectMeta: metav1.ObjectMeta{
Namespace: t.namespace,
Expand All @@ -1036,9 +1039,11 @@ func (t *PorchSuite) TestDeleteFinal(ctx context.Context) {
t.mustExist(ctx, client.ObjectKey{Namespace: t.namespace, Name: created.Name}, &pkg)

// Propose deletion and then delete the package
t.Log("Proposing deletion of package")
pkg.Spec.Lifecycle = porchapi.PackageRevisionLifecycleDeletionProposed
t.UpdateApprovalF(ctx, &pkg, metav1.UpdateOptions{})

t.Log("Deleting package")
t.DeleteE(ctx, &porchapi.PackageRevision{
ObjectMeta: metav1.ObjectMeta{
Namespace: t.namespace,
Expand Down Expand Up @@ -2380,4 +2385,3 @@ func (t *PorchSuite) TestPackageRevisionFinalizers(ctx context.Context) {
Namespace: pr.Namespace,
}, 10*time.Second)
}

4 changes: 3 additions & 1 deletion test/e2e/e2e_test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ package e2e
import (
"context"
"fmt"
"time"
"strings"
"time"

"github.com/google/go-cmp/cmp"
porchapi "github.com/nephio-project/porch/api/porch/v1alpha1"
configapi "github.com/nephio-project/porch/api/porchconfig/v1alpha1"
Expand Down Expand Up @@ -188,6 +189,7 @@ func (t *TestSuite) createPackageDraftF(ctx context.Context, repository, name, w
}

func (t *TestSuite) mustExist(ctx context.Context, key client.ObjectKey, obj client.Object) {
t.Logf("Checking existence of %q...", key)
t.GetF(ctx, key, obj)
if got, want := obj.GetName(), key.Name; got != want {
t.Errorf("%T.Name: got %q, want %q", obj, got, want)
Expand Down
121 changes: 62 additions & 59 deletions test/e2e/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ import (
coreapi "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
Expand Down Expand Up @@ -167,32 +166,26 @@ func (t *TestSuite) IsUsingDevPorch() bool {
Name: porch.Spec.Service.Name,
}, &service)

return service.Spec.Type == coreapi.ServiceTypeExternalName && service.Spec.ExternalName == "host.docker.internal"
return len(service.Spec.Selector) == 0
}

func (t *TestSuite) CreateGitRepo() GitConfig {
if t.IsUsingDevPorch() {
// Create Git server on the local machine.
t.Logf("using dev porch; creating local git server")
return createLocalGitServer(t.T)
} else {
// Deploy Git server via k8s client.
t.Logf("not using dev porch; creating git server in cluster")
return t.createInClusterGitServer(context.TODO())
}
// Deploy Git server via k8s client.
t.Logf("creating git server in cluster")
return t.createInClusterGitServer(context.TODO(), t.IsUsingDevPorch())
}

type ErrorHandler func(format string, args ...interface{})

func (t *TestSuite) get(ctx context.Context, key client.ObjectKey, obj client.Object, eh ErrorHandler) {
if err := t.client.Get(ctx, key, obj); err != nil {
eh("failed to get resource %s %s/%s: %v", obj.GetObjectKind().GroupVersionKind(), key.Name, key.Namespace, err)
eh("failed to get resource %T %s: %v", obj, key, err)
}
}

func (c *TestSuite) list(ctx context.Context, list client.ObjectList, opts []client.ListOption, eh ErrorHandler) {
if err := c.client.List(ctx, list, opts...); err != nil {
eh("failed to list resources %s %+v: %v", list.GetObjectKind().GroupVersionKind(), list, err)
eh("failed to list resources %T %+v: %v", list, list, err)
}
}

Expand Down Expand Up @@ -251,6 +244,7 @@ func (t *TestSuite) patch(ctx context.Context, obj client.Object, patch client.P
}

func (t *TestSuite) updateApproval(ctx context.Context, obj *porchapi.PackageRevision, opts metav1.UpdateOptions, eh ErrorHandler) *porchapi.PackageRevision {
t.Logf("updating approval of %v", DebugFormat(obj))
if res, err := t.clientset.PorchV1alpha1().PackageRevisions(obj.Namespace).UpdateApproval(ctx, obj.Name, obj, opts); err != nil {
eh("failed to update approval of %s/%s: %v", obj.Namespace, obj.Name, err)
return nil
Expand Down Expand Up @@ -439,41 +433,42 @@ func createInitialCommit(t *testing.T, repo *gogit.Repository) {
}
}

func (t *TestSuite) createInClusterGitServer(ctx context.Context) GitConfig {
func (t *TestSuite) createInClusterGitServer(ctx context.Context, exposeByLoadBalancer bool) GitConfig {
// Determine git-server image name. Use the same container registry and tag as the Porch server,
// replacing base image name with `git-server`. TODO: Make configurable?

var porch appsv1.Deployment
t.GetF(ctx, client.ObjectKey{
Namespace: "porch-system",
Name: "porch-server",
Name: "function-runner",
}, &porch)

gitImage := porchtest.InferGitServerImage(porch.Spec.Template.Spec.Containers[0].Image)

var deploymentKey = client.ObjectKey{
Namespace: t.namespace,
Name: "git-server",
}
var replicas int32 = 1
var selector = strings.ReplaceAll(t.Name(), "/", "_")

var selector = map[string]string{
"git-server": strings.ReplaceAll(t.Name(), "/", "_"),
}
t.CreateF(ctx, &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "git-server",
Namespace: t.namespace,
Name: deploymentKey.Name,
Namespace: deploymentKey.Namespace,
Annotations: map[string]string{
"kpt.dev/porch-test": t.Name(),
},
},
Spec: appsv1.DeploymentSpec{
Replicas: &replicas,
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"git-server": selector,
},
MatchLabels: selector,
},
Template: coreapi.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"git-server": selector,
},
Labels: selector,
},
Spec: coreapi.PodSpec{
Containers: []coreapi.Container{
Expand All @@ -498,23 +493,24 @@ func (t *TestSuite) createInClusterGitServer(ctx context.Context) GitConfig {
t.Cleanup(func() {
t.DeleteE(ctx, &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "git-server",
Namespace: t.namespace,
Name: deploymentKey.Name,
Namespace: deploymentKey.Namespace,
},
})
})

t.Cleanup(func() {
t.DumpLogsForDeployment(ctx, types.NamespacedName{
Name: "git-server",
Namespace: t.namespace,
})
t.DumpLogsForDeployment(ctx, deploymentKey)
})

t.CreateF(ctx, &coreapi.Service{
serviceKey := client.ObjectKey{
Namespace: t.namespace,
Name: "git-server-service",
}
service := coreapi.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "git-server-service",
Namespace: t.namespace,
Name: serviceKey.Name,
Namespace: serviceKey.Namespace,
Annotations: map[string]string{
"kpt.dev/porch-test": t.Name(),
},
Expand All @@ -530,17 +526,19 @@ func (t *TestSuite) createInClusterGitServer(ctx context.Context) GitConfig {
},
},
},
Selector: map[string]string{
"git-server": selector,
},
Selector: selector,
},
})
}
if exposeByLoadBalancer {
service.Spec.Type = coreapi.ServiceTypeLoadBalancer
}
t.CreateF(ctx, &service)

t.Cleanup(func() {
t.DeleteE(ctx, &coreapi.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "git-server-service",
Namespace: t.namespace,
Name: serviceKey.Name,
Namespace: serviceKey.Namespace,
},
})
})
Expand All @@ -554,10 +552,7 @@ func (t *TestSuite) createInClusterGitServer(ctx context.Context) GitConfig {
time.Sleep(5 * time.Second)

var deployment appsv1.Deployment
t.GetF(ctx, client.ObjectKey{
Namespace: t.namespace,
Name: "git-server",
}, &deployment)
t.GetF(ctx, deploymentKey, &deployment)

ready := true
if ready && deployment.Generation != deployment.Status.ObservedGeneration {
Expand Down Expand Up @@ -600,31 +595,39 @@ func (t *TestSuite) createInClusterGitServer(ctx context.Context) GitConfig {

t.Logf("Waiting for git-server-service to be ready ...")

// Check the Endpoint resource for readiness
// Check the Endpoint and Service resources for readiness
gitUrl := fmt.Sprintf("http://%s.%s.svc.cluster.local:8080", serviceKey.Name, serviceKey.Namespace)
giveUp = time.Now().Add(time.Minute)

for {
time.Sleep(5 * time.Second)
if time.Now().After(giveUp) {
t.Fatalf("git-server-service not ready on time")
return GitConfig{}
}

var endpoint coreapi.Endpoints
err := t.client.Get(ctx, client.ObjectKey{
Namespace: t.namespace,
Name: "git-server-service",
}, &endpoint)

if err == nil && endpointIsReady(&endpoint) {
t.Logf("git-server-service is ready")
break
err := t.client.Get(ctx, serviceKey, &endpoint)
if err != nil || !endpointIsReady(&endpoint) {
t.Logf("waiting for Endpoint to be ready: %+v", endpoint)
continue
}

if time.Now().After(giveUp) {
t.Fatalf("git-server-service not ready on time: %s", &endpoint)
return GitConfig{}
if exposeByLoadBalancer {
var svc coreapi.Service
t.client.Get(ctx, serviceKey, &svc)
if len(svc.Status.LoadBalancer.Ingress) == 0 {
t.Logf("waiting for LoadBalancer to be assigned: %+v", svc)
continue
}
t.Logf("LoadBalancer IP was assigned to git-server-service: %s", svc.Status.LoadBalancer.Ingress[0].IP)
gitUrl = fmt.Sprintf("http://%s:8080", svc.Status.LoadBalancer.Ingress[0].IP)
}

t.Log("git-server-service is ready")
break
}

return GitConfig{
Repo: fmt.Sprintf("http://git-server-service.%s.svc.cluster.local:8080", t.namespace),
Repo: gitUrl,
Branch: "main",
Directory: "/",
}
Expand Down

0 comments on commit e862875

Please sign in to comment.