diff --git a/integration/deploy_test.go b/integration/deploy_test.go index 2a6c0b4f0f3..0f770aeb2f0 100644 --- a/integration/deploy_test.go +++ b/integration/deploy_test.go @@ -17,10 +17,8 @@ limitations under the License. package integration import ( - "bufio" "strings" "testing" - "time" "github.com/GoogleContainerTools/skaffold/cmd/skaffold/app/flags" "github.com/GoogleContainerTools/skaffold/integration/skaffold" @@ -97,27 +95,7 @@ func TestDeployTail(t *testing.T) { // `--default-repo=` is used to cancel the default repo that is set by default. out := skaffold.Deploy("--tail", "--images", "busybox:latest", "--default-repo=").InDir("testdata/deploy-hello-tail").InNs(ns.Name).RunBackground(t) - // Wait for the logs to print "Hello world!" - lines := make(chan string) - go func() { - scanner := bufio.NewScanner(out) - for scanner.Scan() { - lines <- scanner.Text() - } - }() - - timer := time.NewTimer(30 * time.Second) - defer timer.Stop() - for { - select { - case <-timer.C: - t.Fatal("timeout") - case line := <-lines: - if strings.Contains(line, "Hello world!") { - return - } - } - } + WaitForLogs(t, out, "Hello world!") } func TestDeployWithInCorrectConfig(t *testing.T) { diff --git a/integration/run_test.go b/integration/run_test.go index 09e75248a70..393dd2d5ec2 100644 --- a/integration/run_test.go +++ b/integration/run_test.go @@ -251,3 +251,35 @@ func TestRunUnstableNotChecked(t *testing.T) { skaffold.Run("--status-check=false").InDir("testdata/unstable-deployment").InNs(ns.Name).RunOrFail(t) } + +func TestRunTailPod(t *testing.T) { + if testing.Short() || RunOnGCP() { + t.Skip("skipping kind integration test") + } + + ns, _ := SetupNamespace(t) + + out := skaffold.Run("--tail", "-p", "pod").InDir("testdata/hello").InNs(ns.Name).RunBackground(t) + + WaitForLogs(t, out, + "Hello world! 0", + "Hello world! 1", + "Hello world! 2", + ) +} + +func TestRunTailDeployment(t *testing.T) { + if testing.Short() || RunOnGCP() { + t.Skip("skipping kind integration test") + } + + ns, _ := SetupNamespace(t) + + out := skaffold.Run("--tail", "-p", "deployment").InDir("testdata/hello").InNs(ns.Name).RunBackground(t) + + WaitForLogs(t, out, + "Hello world! 0", + "Hello world! 1", + "Hello world! 2", + ) +} diff --git a/integration/testdata/hello/Dockerfile b/integration/testdata/hello/Dockerfile new file mode 100644 index 00000000000..d978b541f36 --- /dev/null +++ b/integration/testdata/hello/Dockerfile @@ -0,0 +1,7 @@ +FROM golang:1.12.9-alpine3.10 as builder +COPY main.go . +RUN go build -o /app main.go + +FROM alpine:3.10 +CMD ["./app"] +COPY --from=builder /app . diff --git a/integration/testdata/hello/k8s/deployment.yaml b/integration/testdata/hello/k8s/deployment.yaml new file mode 100644 index 00000000000..f1c71fd8b85 --- /dev/null +++ b/integration/testdata/hello/k8s/deployment.yaml @@ -0,0 +1,18 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: getting-started + labels: + app: getting-started +spec: + selector: + matchLabels: + app: getting-started + template: + metadata: + labels: + app: getting-started + spec: + containers: + - name: getting-started + image: skaffold-hello diff --git a/integration/testdata/hello/k8s/pod.yaml b/integration/testdata/hello/k8s/pod.yaml new file mode 100644 index 00000000000..f80ddcdf618 --- /dev/null +++ b/integration/testdata/hello/k8s/pod.yaml @@ -0,0 +1,8 @@ +apiVersion: v1 +kind: Pod +metadata: + name: getting-started +spec: + containers: + - name: getting-started + image: skaffold-hello diff --git a/integration/testdata/hello/main.go b/integration/testdata/hello/main.go new file mode 100644 index 00000000000..8c3e45267d6 --- /dev/null +++ b/integration/testdata/hello/main.go @@ -0,0 +1,14 @@ +package main + +import ( + "fmt" + "time" +) + +func main() { + for counter := 0; ; counter++ { + fmt.Println("Hello world!", counter) + + time.Sleep(time.Second * 1) + } +} diff --git a/integration/testdata/hello/skaffold.yaml b/integration/testdata/hello/skaffold.yaml new file mode 100644 index 00000000000..32012ea7f63 --- /dev/null +++ b/integration/testdata/hello/skaffold.yaml @@ -0,0 +1,17 @@ +apiVersion: skaffold/v2beta3 +kind: Config +build: + artifacts: + - image: skaffold-hello + +profiles: + - name: pod + deploy: + kubectl: + manifests: + - k8s/pod.yaml + - name: deployment + deploy: + kubectl: + manifests: + - k8s/deployment.yaml \ No newline at end of file diff --git a/integration/util.go b/integration/util.go index e6fad43e738..7b009575022 100644 --- a/integration/util.go +++ b/integration/util.go @@ -17,10 +17,13 @@ limitations under the License. package integration import ( + "bufio" "context" "fmt" + "io" "os" "os/exec" + "strings" "testing" "time" @@ -269,3 +272,34 @@ func (k *NSKubernetesClient) ExternalIP(serviceName string) string { func isStable(dp *appsv1.Deployment) bool { return dp.Generation <= dp.Status.ObservedGeneration && *(dp.Spec.Replicas) == dp.Status.Replicas } + +func WaitForLogs(t *testing.T, out io.ReadCloser, firstMessage string, moreMessages ...string) { + lines := make(chan string) + go func() { + scanner := bufio.NewScanner(out) + for scanner.Scan() { + lines <- scanner.Text() + } + }() + + current := 0 + message := firstMessage + + timer := time.NewTimer(30 * time.Second) + defer timer.Stop() + for { + select { + case <-timer.C: + t.Fatal("timeout") + case line := <-lines: + if strings.Contains(line, message) { + if current >= len(moreMessages) { + return + } + + message = moreMessages[current] + current++ + } + } + } +}