Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions e2e/tests/up/fixture_command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package up

import (
"context"
"encoding/json"
"os"
"os/exec"
"time"

"github.com/devsy-org/devsy/pkg/docker"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
)

// runFixtureCommand runs argv (argv[0] is the program), bounded by timeout. env entries
// are appended to the current environment.
func runFixtureCommand(
ctx context.Context,
timeout time.Duration,
env []string,
argv ...string,
) (string, error) {
commandCtx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
// #nosec G204 -- executable and arguments are controlled by this test fixture.
cmd := exec.CommandContext(commandCtx, argv[0], argv[1:]...)
docker.PrepareForGroupCancellation(cmd)
cmd.WaitDelay = 5 * time.Second
cmd.Env = append(os.Environ(), env...)
out, err := cmd.CombinedOutput()
return string(out), err
}

// fixtureCommand is runFixtureCommand that fails the current spec on error.
func fixtureCommand(
ctx context.Context,
timeout time.Duration,
env []string,
argv ...string,
) string {
out, err := runFixtureCommand(ctx, timeout, env, argv...)
gomega.Expect(err).
NotTo(gomega.HaveOccurred(), "fixture command %v: %s", argv, out)
return out
}

// fixtureWorkspacePod waits for exactly one ready workspace pod matching selector.
func fixtureWorkspacePod(ctx context.Context, namespace, selector string) corev1.Pod {
var pod corev1.Pod
gomega.Eventually(func(g gomega.Gomega) {
pollCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
out, err := runFixtureCommand(
pollCtx,
10*time.Second,
nil,
"kubectl",
"get",
"pods",
"-n",
namespace,
"-l",
selector,
"-o",
"json",
)
g.Expect(err).NotTo(gomega.HaveOccurred(), "workspace pod discovery: %s", out)
var pods corev1.PodList
g.Expect(json.Unmarshal([]byte(out), &pods)).To(gomega.Succeed())
g.Expect(pods.Items).To(gomega.HaveLen(1), "workspace-specific selector %s", selector)
pod = pods.Items[0]
g.Expect(pod.UID).NotTo(gomega.BeEmpty())
ready := false
for _, condition := range pod.Status.Conditions {
if condition.Type == corev1.PodReady && condition.Status == corev1.ConditionTrue {
ready = true
}
}
g.Expect(ready).To(gomega.BeTrue(), "workspace pod %s must be ready", pod.Name)
}).WithTimeout(30 * time.Second).WithPolling(time.Second).Should(gomega.Succeed())
ginkgo.GinkgoWriter.Printf("workspace pod %s UID=%s\n", pod.Name, pod.UID)
return pod
}
73 changes: 16 additions & 57 deletions e2e/tests/up/provider_kubernetes_dockerless_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"time"

"github.com/devsy-org/devsy/e2e/framework"
"github.com/devsy-org/devsy/pkg/docker"
"github.com/devsy-org/devsy/pkg/driver/kubernetes"
"github.com/devsy-org/devsy/pkg/flags/names"
v1 "github.com/google/go-containerregistry/pkg/v1"
Expand Down Expand Up @@ -94,7 +92,7 @@ func newDockerlessPathFixture(ctx context.Context, appendRemotePath bool) *docke
fixture.expectedPath += ":" + dockerlessPathRoot + "/remote-bin"
addDockerlessRemotePath(fixtureDir)
}
dockerlessPathKubectl(ctx, "create", "namespace", fixture.id)
fixtureCommand(ctx, time.Minute, nil, "kubectl", "create", "namespace", fixture.id)
ginkgo.DeferCleanup(fixture.deleteNamespace)
// Keep the built-in provider's Dockerless credentials enabled.
// Debug output records the helper created by the real callback.
Expand All @@ -111,7 +109,16 @@ func newDockerlessPathFixture(ctx context.Context, appendRemotePath bool) *docke
func (fixture *dockerlessPathFixture) deleteNamespace(specCtx ginkgo.SpecContext) {
cleanupCtx, cancel := context.WithTimeout(context.WithoutCancel(specCtx), time.Minute)
defer cancel()
dockerlessPathKubectl(cleanupCtx, "delete", "namespace", fixture.id, "--wait=false")
fixtureCommand(
cleanupCtx,
time.Minute,
nil,
"kubectl",
"delete",
"namespace",
fixture.id,
"--wait=false",
)
}

func (fixture *dockerlessPathFixture) deleteProvider(specCtx ginkgo.SpecContext) {
Expand Down Expand Up @@ -163,7 +170,7 @@ func (fixture *dockerlessPathFixture) up(

func (fixture *dockerlessPathFixture) verify(ctx context.Context, phase string) corev1.Pod {
selector := "devsy.sh/created=true,issue-1446=" + fixture.id
pod := dockerlessPathWorkspacePod(ctx, fixture.id, selector)
pod := fixtureWorkspacePod(ctx, fixture.id, selector)
fixture.containerName = ""
for _, container := range pod.Spec.Containers {
if container.Name == kubernetes.DevContainerName {
Expand Down Expand Up @@ -215,8 +222,10 @@ func (fixture *dockerlessPathFixture) verifyEnvironment(
// Raw kubectl exec reads files without treating the pod's original builder
// environment as the post-setup agent/SSH environment.
execInPod := func(args ...string) string {
baseArgs := []string{"exec", "-n", fixture.id, podName, "-c", fixture.containerName, "--"}
return dockerlessPathKubectl(commandCtx, append(baseArgs, args...)...)
baseArgs := []string{
"kubectl", "exec", "-n", fixture.id, podName, "-c", fixture.containerName, "--",
}
return fixtureCommand(commandCtx, time.Minute, nil, append(baseArgs, args...)...)
}
readFile := func(name string) string {
return execInPod("/bin/cat", name)
Expand All @@ -242,53 +251,3 @@ func (fixture *dockerlessPathFixture) verifyEnvironment(
execInPod("/bin/sh", "-c",
`for dir in /.dockerless/.docker/docker-credentials-*; do test ! -e "$dir" || exit 1; done`)
}

func dockerlessPathWorkspacePod(ctx context.Context, namespace, selector string) corev1.Pod {
var pod corev1.Pod
gomega.Eventually(func(g gomega.Gomega) {
pollCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
out, err := runDockerlessPathKubectl(
pollCtx,
"get",
"pods",
"-n",
namespace,
"-l",
selector,
"-o",
"json",
)
g.Expect(err).NotTo(gomega.HaveOccurred(), "workspace pod discovery: %s", out)
var pods corev1.PodList
g.Expect(json.Unmarshal([]byte(out), &pods)).To(gomega.Succeed())
g.Expect(pods.Items).To(gomega.HaveLen(1))
pod = pods.Items[0]
g.Expect(pod.UID).NotTo(gomega.BeEmpty())
ready := false
for _, condition := range pod.Status.Conditions {
if condition.Type == corev1.PodReady && condition.Status == corev1.ConditionTrue {
ready = true
}
}
g.Expect(ready).To(gomega.BeTrue(), "workspace pod %s must be ready", pod.Name)
}).WithTimeout(30 * time.Second).WithPolling(time.Second).Should(gomega.Succeed())
return pod
}

func runDockerlessPathKubectl(ctx context.Context, args ...string) (string, error) {
commandCtx, cancel := context.WithTimeout(ctx, time.Minute)
defer cancel()
// #nosec G204 -- kubectl arguments are controlled by this test fixture.
cmd := exec.CommandContext(commandCtx, "kubectl", args...)
docker.PrepareForGroupCancellation(cmd)
cmd.WaitDelay = 5 * time.Second
out, err := cmd.CombinedOutput()
return string(out), err
}

func dockerlessPathKubectl(ctx context.Context, args ...string) string {
out, err := runDockerlessPathKubectl(ctx, args...)
gomega.Expect(err).NotTo(gomega.HaveOccurred(), "kubectl %v: %s", args, out)
return out
}
Loading
Loading