Skip to content

Adding testing phase to Skaffold run #5594

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Apr 1, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
11 changes: 11 additions & 0 deletions cmd/skaffold/app/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,18 @@ func doRun(ctx context.Context, out io.Writer) error {
return fmt.Errorf("failed to build: %w", err)
}

if !opts.SkipTests {
err = r.Test(ctx, out, bRes)
if err != nil {
return fmt.Errorf("failed to test: %w", err)
}
}

err = r.DeployAndLog(ctx, out, bRes)
if err != nil {
return fmt.Errorf("failed to deploy: %w", err)
}

if err == nil {
tips.PrintForRun(out, opts)
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/skaffold/app/cmd/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ func (r *mockRunRunner) Build(_ context.Context, _ io.Writer, artifacts []*lates
return result, nil
}

func (r *mockRunRunner) Test(context.Context, io.Writer, []build.Artifact) error {
return nil
}

func (r *mockRunRunner) DeployAndLog(context.Context, io.Writer, []build.Artifact) error {
return nil
}
Expand Down
93 changes: 93 additions & 0 deletions integration/test_run_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
Copyright 2021 The Skaffold Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package integration

import (
"io/ioutil"
"os"
"testing"
"time"

"k8s.io/apimachinery/pkg/util/wait"

"github.com/GoogleContainerTools/skaffold/integration/skaffold"
)

func TestRunTest(t *testing.T) {
MarkIntegrationTest(t, CanRunWithoutGcp)

tests := []struct {
description string
testDir string
testFile string
args []string
skipTests bool
expectedText string
}{
{
description: "Run test",
testDir: "testdata/run-test",
testFile: "testdata/run-test/test",
skipTests: false,
expectedText: "foo\n",
},
{
description: "Run test with skip test false",
testDir: "testdata/run-test",
testFile: "testdata/run-test/test",
args: []string{"--skip-tests=false"},
skipTests: false,
expectedText: "foo\n",
},
{
description: "Run test with skip test true",
testDir: "testdata/run-test",
testFile: "testdata/run-test/test",
args: []string{"--skip-tests=True"},
skipTests: true,
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
defer func() {
os.Remove(test.testFile)
}()

// Run skaffold build first to fail quickly on a build failure
skaffold.Build().InDir(test.testDir).RunOrFail(t)

ns, client := SetupNamespace(t)
skaffold.Run(test.args...).InDir(test.testDir).InNs(ns.Name).RunLive(t)

client.WaitForPodsReady("run-test")

err := wait.PollImmediate(time.Millisecond*500, 1*time.Minute, func() (bool, error) {
_, e := os.Stat(test.testFile)
if test.skipTests {
if !os.IsNotExist(e) {
t.Fatalf("Tests are not skipped.")
}
return true, nil
}
out, e := ioutil.ReadFile(test.testFile)
failNowIfError(t, e)
return string(out) == test.expectedText, nil
})
failNowIfError(t, err)
})
}
}
12 changes: 12 additions & 0 deletions integration/testdata/run-test/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM golang:1.15 as builder
COPY main.go .
# `skaffold debug` sets SKAFFOLD_GO_GCFLAGS to disable compiler optimizations
ARG SKAFFOLD_GO_GCFLAGS
RUN go build -gcflags="${SKAFFOLD_GO_GCFLAGS}" -o /app main.go

FROM alpine:3
# Define GOTRACEBACK to mark this container as using the Go language runtime
# for `skaffold debug` (https://skaffold.dev/docs/workflows/debug/).
ENV GOTRACEBACK=single
CMD ["./app"]
COPY --from=builder /app .
8 changes: 8 additions & 0 deletions integration/testdata/run-test/k8s-pod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: v1
kind: Pod
metadata:
name: run-test
spec:
containers:
- name: run-test
image: run-test
14 changes: 14 additions & 0 deletions integration/testdata/run-test/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"fmt"
"time"
)

func main() {
for {
fmt.Println("Hello world!")

time.Sleep(time.Second * 1)
}
}
13 changes: 13 additions & 0 deletions integration/testdata/run-test/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: skaffold/v2beta13
kind: Config
build:
artifacts:
- image: run-test
test:
- image: run-test
custom:
- command: echo "foo" > test
deploy:
kubectl:
manifests:
- k8s-*