forked from GoogleContainerTools/skaffold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug_test.go
267 lines (228 loc) · 8.42 KB
/
debug_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
Copyright 2019 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 (
"context"
"encoding/json"
"strings"
"testing"
"time"
dockertypes "github.com/docker/docker/api/types"
"github.com/GoogleContainerTools/skaffold/v2/integration/skaffold"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/debug/types"
"github.com/GoogleContainerTools/skaffold/v2/proto/v1"
"github.com/GoogleContainerTools/skaffold/v2/testutil"
)
func TestDebug(t *testing.T) {
tests := []struct {
description string
dir string
config string
args []string
deployments []string
pods []string
ignoreWorkdir bool
}{
{
description: "kubectl",
dir: "testdata/debug",
deployments: []string{"java"},
pods: []string{"nodejs", "npm" /*, "python3"*/, "go" /*, "netcore"*/},
},
{
description: "kustomize",
dir: "testdata/debug",
args: []string{"--profile", "kustomize"},
deployments: []string{"java"},
pods: []string{"nodejs", "npm" /*, "python3"*/, "go" /*, "netcore"*/},
},
// TODO(#8811): Enable this test when issue is solve.
// {
// description: "buildpacks",
// dir: "testdata/debug",
// args: []string{"--profile", "buildpacks"},
// deployments: []string{"java"},
// pods: []string{"nodejs", "npm" /*, "python3"*/, "go" /*, "netcore"*/},
// },
{
description: "helm",
dir: "examples/helm-deployment",
deployments: []string{"skaffold-helm"},
ignoreWorkdir: true, // dockerfile doesn't have a workdir
},
{
description: "modules",
dir: "examples/multi-config-microservices",
deployments: []string{"leeroy-web", "leeroy-app"},
ignoreWorkdir: true, // dockerfile doesn't have a workdir
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
MarkIntegrationTest(t, CanRunWithoutGcp)
// Run skaffold build first to fail quickly on a build failure
skaffold.Build(test.args...).InDir(test.dir).RunOrFail(t)
ns, client := SetupNamespace(t)
skaffold.Debug(test.args...).InDir(test.dir).InNs(ns.Name).RunBackground(t)
verifyDebugAnnotations := func(annotations map[string]string) {
var configs map[string]types.ContainerDebugConfiguration
if anno, found := annotations["debug.cloud.google.com/config"]; !found {
t.Errorf("deployment missing debug annotation: %v", annotations)
} else if err := json.Unmarshal([]byte(anno), &configs); err != nil {
t.Errorf("error unmarshalling debug annotation: %v: %v", anno, err)
} else {
for k, config := range configs {
if !test.ignoreWorkdir && config.WorkingDir == "" {
t.Errorf("debug config for %q missing WorkingDir: %v: %v", k, anno, config)
}
if config.Runtime == "" {
t.Errorf("debug config for %q missing Runtime: %v: %v", k, anno, config)
}
}
}
}
for _, podName := range test.pods {
pod := client.GetPod(podName)
annotations := pod.Annotations
verifyDebugAnnotations(annotations)
}
for _, depName := range test.deployments {
deploy := client.GetDeployment(depName)
annotations := deploy.Spec.Template.GetAnnotations()
verifyDebugAnnotations(annotations)
}
})
}
}
func TestDockerDebug(t *testing.T) {
t.Run("debug docker deployment", func(t *testing.T) {
MarkIntegrationTest(t, CanRunWithoutGcp)
skaffold.Build("-p", "docker").InDir("testdata/debug").RunOrFail(t)
skaffold.Debug("-p", "docker").InDir("testdata/debug").RunBackground(t)
defer skaffold.Delete("-p", "docker").InDir("testdata/debug").RunBackground(t)
// use docker client to verify container has been created properly
// check this container and verify entrypoint has been rewritten
client := SetupDockerClient(t)
var (
verifyEntrypointRewrite bool
verifySupportContainer bool
tries = 0
sleepTime = 2 * time.Second // retrieve containers every two seconds
maxTries = 15 // try for 30 seconds max
)
for {
containers, err := client.ContainerList(context.Background(), dockertypes.ContainerListOptions{All: true})
if err != nil {
t.Fail()
}
time.Sleep(sleepTime)
if len(containers) == 0 {
continue
}
checkEntrypointRewrite(containers, &verifyEntrypointRewrite)
checkSupportContainer(containers, &verifySupportContainer)
if verifyEntrypointRewrite && verifySupportContainer {
break
}
tries++
if tries == maxTries {
break
}
}
if !verifyEntrypointRewrite {
t.Error("couldn't verify rewritten container")
}
if !verifySupportContainer {
t.Error("couldn't verify support container was created")
}
})
}
func checkEntrypointRewrite(containers []dockertypes.Container, found *bool) {
if *found {
return
}
for _, c := range containers {
if strings.Contains(c.Command, "docker-entrypoint.sh") {
*found = true
}
}
}
func checkSupportContainer(containers []dockertypes.Container, found *bool) {
if *found {
return
}
for _, c := range containers {
if strings.Contains(c.Image, "gcr.io/k8s-skaffold/skaffold-debug-support") {
*found = true
}
}
}
func TestFilterWithDebugging(t *testing.T) {
MarkIntegrationTest(t, CanRunWithoutGcp)
// `filter` currently expects to receive a digested yaml
renderedOutput := skaffold.Render("--digest-source=local").InDir("examples/getting-started").RunOrFailOutput(t)
testutil.Run(t, "no --build-artifacts should transform all images", func(t *testutil.T) {
transformedOutput := skaffold.Filter("--debugging").InDir("examples/getting-started").WithStdin(renderedOutput).RunOrFailOutput(t.T)
transformedYaml := string(transformedOutput)
if !strings.Contains(transformedYaml, "/dbg/go/bin/dlv") {
t.Error("transformed yaml seems to be missing debugging details", transformedYaml)
}
})
testutil.Run(t, "--build-artifacts=file should result in specific transforms", func(t *testutil.T) {
buildFile := t.TempFile("build.txt", []byte(`{"builds":[{"imageName":"doesnotexist","tag":"doesnotexist:notag"}]}`))
transformedOutput := skaffold.Filter("--debugging", "--build-artifacts="+buildFile).InDir("examples/getting-started").WithStdin(renderedOutput).RunOrFailOutput(t.T)
transformedYaml := string(transformedOutput)
if strings.Contains(transformedYaml, "/dbg/go/bin/dlv") {
t.Error("transformed yaml should not include debugging details", transformedYaml)
}
})
}
func TestDebugEventsRPC_StatusCheck(t *testing.T) {
t.Skipf("Disable the test dues to flakyness. https://github.com/GoogleContainerTools/skaffold/issues/7405")
MarkIntegrationTest(t, CanRunWithoutGcp)
// Run skaffold build first to fail quickly on a build failure
skaffold.Build().InDir("testdata/jib").RunOrFail(t)
ns, client := SetupNamespace(t)
rpcAddr := randomPort()
skaffold.Debug("--rpc-port", rpcAddr).InDir("testdata/jib").InNs(ns.Name).RunBackground(t)
waitForDebugEvent(t, client, rpcAddr)
}
func TestDebugEventsRPC_NoStatusCheck(t *testing.T) {
t.Skipf("Disable the test dues to flakyness. https://github.com/GoogleContainerTools/skaffold/issues/7405")
MarkIntegrationTest(t, CanRunWithoutGcp)
// Run skaffold build first to fail quickly on a build failure
skaffold.Build().InDir("testdata/jib").RunOrFail(t)
ns, client := SetupNamespace(t)
rpcAddr := randomPort()
skaffold.Debug("--rpc-port", rpcAddr, "--status-check=false").InDir("testdata/jib").InNs(ns.Name).RunBackground(t)
waitForDebugEvent(t, client, rpcAddr)
}
// nolint add lint back after https://github.com/GoogleContainerTools/skaffold/issues/7405
func waitForDebugEvent(t *testing.T, client *NSKubernetesClient, rpcAddr string) {
client.WaitForPodsReady()
_, entries := apiEvents(t, rpcAddr)
timeout := time.After(1 * time.Minute)
for {
select {
case <-timeout:
t.Fatalf("timed out waiting for debugging event")
case entry := <-entries:
switch entry.Event.GetEventType().(type) {
case *proto.Event_DebuggingContainerEvent:
// success!
return
default:
}
}
}
}