Skip to content

Commit

Permalink
Make sure log tailing works with pods and deployments
Browse files Browse the repository at this point in the history
Signed-off-by: David Gageot <david@gageot.net>
  • Loading branch information
dgageot committed May 7, 2020
1 parent de2ed45 commit 3b7160e
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 23 deletions.
24 changes: 1 addition & 23 deletions integration/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down
32 changes: 32 additions & 0 deletions integration/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
}
7 changes: 7 additions & 0 deletions integration/testdata/hello/Dockerfile
Original file line number Diff line number Diff line change
@@ -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 .
18 changes: 18 additions & 0 deletions integration/testdata/hello/k8s/deployment.yaml
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions integration/testdata/hello/k8s/pod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: v1
kind: Pod
metadata:
name: getting-started
spec:
containers:
- name: getting-started
image: skaffold-hello
14 changes: 14 additions & 0 deletions integration/testdata/hello/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
17 changes: 17 additions & 0 deletions integration/testdata/hello/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions integration/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ limitations under the License.
package integration

import (
"bufio"
"context"
"fmt"
"io"
"os"
"os/exec"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -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++
}
}
}
}

0 comments on commit 3b7160e

Please sign in to comment.