Skip to content

Commit 7ef1c2c

Browse files
committed
Improve integrations tests.
Signed-off-by: David Gageot <[email protected]>
1 parent 713a370 commit 7ef1c2c

File tree

2 files changed

+51
-101
lines changed

2 files changed

+51
-101
lines changed

integration/run_test.go

+50-100
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ package integration
2121
import (
2222
"bytes"
2323
"flag"
24-
"fmt"
2524
"os"
2625
"os/exec"
2726
"testing"
@@ -34,18 +33,16 @@ import (
3433
"k8s.io/api/core/v1"
3534
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3635
"k8s.io/client-go/kubernetes"
37-
"k8s.io/client-go/tools/clientcmd"
38-
"k8s.io/client-go/tools/clientcmd/api"
3936
)
4037

41-
var gkeZone = flag.String("gke-zone", "us-central1-a", "gke zone")
42-
var gkeClusterName = flag.String("gke-cluster-name", "integration-tests", "name of the integration test cluster")
43-
var gcpProject = flag.String("gcp-project", "k8s-skaffold", "the gcp project where the integration test cluster lives")
44-
var remote = flag.Bool("remote", false, "if true, run tests on a remote GKE cluster")
38+
var (
39+
gkeZone = flag.String("gke-zone", "us-central1-a", "gke zone")
40+
gkeClusterName = flag.String("gke-cluster-name", "integration-tests", "name of the integration test cluster")
41+
gcpProject = flag.String("gcp-project", "k8s-skaffold", "the gcp project where the integration test cluster lives")
42+
remote = flag.Bool("remote", false, "if true, run tests on a remote GKE cluster")
4543

46-
var client kubernetes.Interface
47-
48-
var context *api.Context
44+
client kubernetes.Interface
45+
)
4946

5047
func TestMain(m *testing.M) {
5148
flag.Parse()
@@ -62,95 +59,57 @@ func TestMain(m *testing.M) {
6259
logrus.Fatalf("Test setup error: getting kubernetes client: %s", err)
6360
}
6461

65-
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
66-
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &clientcmd.ConfigOverrides{})
67-
68-
cfg, err := kubeConfig.RawConfig()
69-
if err != nil {
70-
logrus.Fatalf("loading kubeconfig: %s", err)
71-
}
72-
73-
context = cfg.Contexts[cfg.CurrentContext]
74-
7562
exitCode := m.Run()
7663

77-
// Reset default context and namespace
78-
if err := exec.Command("kubectl", "config", "set-context", context.Cluster, "--namespace", context.Namespace).Run(); err != nil {
79-
logrus.Warn(err)
80-
}
81-
8264
os.Exit(exitCode)
8365
}
8466

8567
func TestRun(t *testing.T) {
86-
type testObject struct {
87-
name string
88-
}
89-
9068
type testRunCase struct {
9169
description string
9270
dir string
71+
filename string
9372
args []string
94-
deployments []testObject
95-
pods []testObject
73+
deployments []string
74+
pods []string
9675
deploymentValidation func(t *testing.T, d *appsv1.Deployment)
97-
env map[string]string
76+
env []string
9877

9978
remoteOnly bool
100-
cleanup func(t *testing.T)
10179
}
10280

10381
var testCases = []testRunCase{
10482
{
10583
description: "getting-started example",
10684
args: []string{"run"},
107-
pods: []testObject{
108-
{
109-
name: "getting-started",
110-
},
111-
},
112-
dir: "../examples/getting-started",
85+
pods: []string{"getting-started"},
86+
dir: "../examples/getting-started",
11387
},
11488
{
11589
description: "annotated getting-started example",
116-
args: []string{"run", "-f", "annotated-skaffold.yaml"},
117-
pods: []testObject{
118-
{
119-
name: "getting-started",
120-
},
121-
},
122-
dir: "../examples",
90+
args: []string{"run"},
91+
filename: "annotated-skaffold.yaml",
92+
pods: []string{"getting-started"},
93+
dir: "../examples",
12394
},
12495
{
12596
description: "getting-started envTagger",
12697
args: []string{"run"},
127-
pods: []testObject{
128-
{
129-
name: "getting-started",
130-
},
131-
},
132-
dir: "../examples/tagging-with-environment-variables",
133-
env: map[string]string{"FOO": "foo"},
98+
pods: []string{"getting-started"},
99+
dir: "../examples/tagging-with-environment-variables",
100+
env: []string{"FOO=foo"},
134101
},
135102
{
136103
description: "gcb builder example",
137104
args: []string{"run", "-p", "gcb"},
138-
pods: []testObject{
139-
{
140-
name: "getting-started",
141-
},
142-
},
143-
dir: "../examples/getting-started",
144-
remoteOnly: true,
105+
pods: []string{"getting-started"},
106+
dir: "../examples/getting-started",
107+
remoteOnly: true,
145108
},
146109
{
147110
description: "deploy kustomize",
148111
args: []string{"deploy", "--images", "index.docker.io/library/busybox:1"},
149-
deployments: []testObject{
150-
{
151-
name: "kustomize-test",
152-
},
153-
},
112+
deployments: []string{"kustomize-test"},
154113
deploymentValidation: func(t *testing.T, d *appsv1.Deployment) {
155114
if d == nil {
156115
t.Fatalf("Could not find deployment")
@@ -164,31 +123,16 @@ func TestRun(t *testing.T) {
164123
{
165124
description: "kaniko example",
166125
args: []string{"run"},
167-
pods: []testObject{
168-
{
169-
name: "getting-started-kaniko",
170-
},
171-
},
172-
dir: "../examples/kaniko",
173-
remoteOnly: true,
126+
pods: []string{"getting-started-kaniko"},
127+
dir: "../examples/kaniko",
128+
remoteOnly: true,
174129
},
175130
{
176131
description: "helm example",
177132
args: []string{"run"},
178-
deployments: []testObject{
179-
{
180-
name: "skaffold-helm",
181-
},
182-
},
183-
dir: "../examples/helm-deployment",
184-
remoteOnly: true,
185-
cleanup: func(t *testing.T) {
186-
cmd := exec.Command("helm", "delete", "--purge", "skaffold-helm")
187-
output, err := util.RunCmdOut(cmd)
188-
if err != nil {
189-
t.Fatalf("skaffold: %s %v", output, err)
190-
}
191-
},
133+
deployments: []string{"skaffold-helm"},
134+
dir: "../examples/helm-deployment",
135+
remoteOnly: true,
192136
},
193137
}
194138

@@ -201,42 +145,48 @@ func TestRun(t *testing.T) {
201145
ns, deleteNs := setupNamespace(t)
202146
defer deleteNs()
203147

204-
args := []string{"--namespace", ns.Name}
148+
args := []string{}
205149
args = append(args, testCase.args...)
150+
args = append(args, "--namespace", ns.Name)
151+
if testCase.filename != "" {
152+
args = append(args, "-f", testCase.filename)
153+
}
206154

207155
cmd := exec.Command("skaffold", args...)
208-
env := os.Environ()
209-
for k, v := range testCase.env {
210-
env = append(env, fmt.Sprintf("%s=%s", k, v))
211-
}
212-
cmd.Env = env
156+
cmd.Env = append(os.Environ(), testCase.env...)
213157
cmd.Dir = testCase.dir
214-
output, err := util.RunCmdOut(cmd)
215-
if err != nil {
158+
if output, err := util.RunCmdOut(cmd); err != nil {
216159
t.Fatalf("skaffold: %s %v", output, err)
217160
}
218161

219162
for _, p := range testCase.pods {
220-
if err := kubernetesutil.WaitForPodReady(client.CoreV1().Pods(ns.Name), p.name); err != nil {
163+
if err := kubernetesutil.WaitForPodReady(client.CoreV1().Pods(ns.Name), p); err != nil {
221164
t.Fatalf("Timed out waiting for pod ready")
222165
}
223166
}
224167

225168
for _, d := range testCase.deployments {
226-
if err := kubernetesutil.WaitForDeploymentToStabilize(client, ns.Name, d.name, 10*time.Minute); err != nil {
169+
if err := kubernetesutil.WaitForDeploymentToStabilize(client, ns.Name, d, 10*time.Minute); err != nil {
227170
t.Fatalf("Timed out waiting for deployment to stabilize")
228171
}
229172
if testCase.deploymentValidation != nil {
230-
deployment, err := client.AppsV1().Deployments(ns.Name).Get(d.name, meta_v1.GetOptions{})
173+
deployment, err := client.AppsV1().Deployments(ns.Name).Get(d, meta_v1.GetOptions{})
231174
if err != nil {
232175
t.Fatalf("Could not find deployment: %s %s", ns.Name, d)
233176
}
234177
testCase.deploymentValidation(t, deployment)
235178
}
179+
}
236180

237-
if testCase.cleanup != nil {
238-
testCase.cleanup(t)
239-
}
181+
// Cleanup
182+
args = []string{"delete", "--namespace", ns.Name}
183+
if testCase.filename != "" {
184+
args = append(args, "-f", testCase.filename)
185+
}
186+
cmd = exec.Command("skaffold", args...)
187+
cmd.Dir = testCase.dir
188+
if output, err := util.RunCmdOut(cmd); err != nil {
189+
t.Fatalf("skaffold delete: %s %v", output, err)
240190
}
241191
})
242192
}

pkg/skaffold/kubernetes/wait.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import (
3939

4040
func WaitForPodReady(pods corev1.PodInterface, podName string) error {
4141
logrus.Infof("Waiting for %s to be scheduled", podName)
42-
err := wait.PollImmediate(time.Millisecond*500, time.Second*10, func() (bool, error) {
42+
err := wait.PollImmediate(time.Millisecond*500, time.Second*30, func() (bool, error) {
4343
_, err := pods.Get(podName, meta_v1.GetOptions{
4444
IncludeUninitialized: true,
4545
})

0 commit comments

Comments
 (0)