Skip to content

Commit f4fbefd

Browse files
authored
fix: add tests for using the gitea registry in the cluster (#543)
1 parent 8916e77 commit f4fbefd

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

tests/e2e/docker/docker_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ func testCreatePath(t *testing.T) {
9494
e2e.TestCoreEndpoints(ctx, t, argoBaseUrl, giteaBaseUrl)
9595

9696
e2e.TestGiteaRegistry(ctx, t, "docker", e2e.DefaultBaseDomain, e2e.DefaultPort)
97+
e2e.TestGiteaRegistryInCluster(ctx, t, "docker", e2e.DefaultBaseDomain, e2e.DefaultPort, kubeClient)
9798
}
9899

99100
func testCreatePort(t *testing.T) {

tests/e2e/e2e.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ import (
2020
argov1alpha1 "github.com/cnoe-io/argocd-api/api/argo/application/v1alpha1"
2121
"github.com/cnoe-io/idpbuilder/pkg/k8s"
2222
"github.com/cnoe-io/idpbuilder/pkg/printer/types"
23+
corev1 "k8s.io/api/core/v1"
24+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2325
"github.com/stretchr/testify/assert"
2426
"github.com/stretchr/testify/require"
2527
"k8s.io/client-go/tools/clientcmd"
2628
"k8s.io/client-go/util/homedir"
29+
"k8s.io/client-go/util/retry"
30+
"k8s.io/apimachinery/pkg/util/wait"
2731
"sigs.k8s.io/controller-runtime/pkg/client"
2832
)
2933

@@ -405,3 +409,75 @@ func TestGiteaRegistry(ctx context.Context, t *testing.T, cmd, giteaHost, giteaP
405409
pull, err := RunCommand(ctx, fmt.Sprintf("%s pull %s", cmd, tag), 10*time.Second)
406410
require.NoErrorf(t, err, "%s pull err: %s", cmd, pull)
407411
}
412+
413+
// login, build a test image, push, then pull.
414+
func TestGiteaRegistryInCluster(ctx context.Context, t *testing.T, cmd, giteaHost, giteaPort string, kubeClient client.Client) {
415+
t.Log("testing using gitea container registry in cluster")
416+
b, err := RunCommand(ctx, fmt.Sprintf("%s get secrets -o json -p gitea", IdpbuilderBinaryLocation), 10*time.Second)
417+
assert.NoError(t, err)
418+
419+
secs := make([]types.Secret, 1)
420+
err = json.Unmarshal(b, &secs)
421+
assert.NoError(t, err)
422+
423+
sec := secs[0]
424+
user := sec.Username
425+
pass := sec.Password
426+
427+
login, err := RunCommand(ctx, fmt.Sprintf("%s login %s:%s -u %s -p %s", cmd, giteaHost, giteaPort, user, pass), 10*time.Second)
428+
require.NoErrorf(t, err, "%s login err: %s", cmd, login)
429+
430+
upstreamImage := "nginx"
431+
tag := fmt.Sprintf("%s:%s/giteaadmin/nginx:latest", giteaHost, giteaPort)
432+
433+
pull, err := RunCommand(ctx, fmt.Sprintf("%s pull %s", cmd, upstreamImage), 10*time.Second)
434+
require.NoErrorf(t, err, "%s pull err: %s", cmd, pull)
435+
436+
retag, err := RunCommand(ctx, fmt.Sprintf("%s tag %s %s", cmd, upstreamImage, tag), 10*time.Second)
437+
require.NoErrorf(t, err, "%s tag err: %s", cmd, retag)
438+
439+
push, err := RunCommand(ctx, fmt.Sprintf("%s push %s", cmd, tag), 10*time.Second)
440+
require.NoErrorf(t, err, "%s push err: %s", cmd, push)
441+
442+
pod := &corev1.Pod{
443+
ObjectMeta: metav1.ObjectMeta{Name: "test-pod", Namespace: "default"},
444+
Spec: corev1.PodSpec{
445+
Containers: []corev1.Container{{
446+
Name: "nginx",
447+
Image: tag,
448+
}},
449+
},
450+
}
451+
452+
err = kubeClient.Create(ctx, pod)
453+
require.NoErrorf(t, err, "pod creation err")
454+
455+
// Retry for 30 seconds
456+
backoff := wait.Backoff{
457+
Steps: 3,
458+
Duration: 10 * time.Second,
459+
Jitter: 0.0,
460+
}
461+
462+
retriable := func(_ error) bool {
463+
// Retry all errors
464+
return true
465+
}
466+
467+
testfunc := func() error {
468+
foundPod := &corev1.Pod{}
469+
err = kubeClient.Get(ctx, client.ObjectKey{Name: pod.Name, Namespace: pod.Namespace}, foundPod)
470+
if err != nil {
471+
return err
472+
}
473+
474+
if foundPod.Status.Phase != "Running" {
475+
return fmt.Errorf("Pod phase not running: %s", foundPod.Status.Phase)
476+
}
477+
478+
return nil
479+
}
480+
481+
err = retry.OnError(backoff, retriable, testfunc)
482+
require.NoErrorf(t, err, "pod startup err")
483+
}

0 commit comments

Comments
 (0)