-
Notifications
You must be signed in to change notification settings - Fork 288
/
down_test.go
161 lines (137 loc) · 4.33 KB
/
down_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
package cli
import (
"context"
"fmt"
"testing"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tilt-dev/tilt/internal/analytics"
"github.com/tilt-dev/tilt/internal/dockercompose"
"github.com/tilt-dev/tilt/internal/k8s"
"github.com/tilt-dev/tilt/internal/k8s/testyaml"
"github.com/tilt-dev/tilt/internal/testutils"
"github.com/tilt-dev/tilt/internal/tiltfile"
"github.com/tilt-dev/tilt/pkg/model"
)
func TestDown(t *testing.T) {
f := newDownFixture(t)
defer f.TearDown()
f.tfl.Result = tiltfile.TiltfileLoadResult{Manifests: newK8sManifest()}
err := f.cmd.down(f.ctx, f.deps, nil)
assert.NoError(t, err)
assert.Contains(t, f.kCli.DeletedYaml, "sancho")
}
func TestDownPreservesNamespacesByDefault(t *testing.T) {
f := newDownFixture(t)
defer f.TearDown()
manifests := append([]model.Manifest{}, newK8sManifest()...)
manifests = append(manifests, newK8sNamespaceManifest("foo"), newK8sNamespaceManifest("bar"))
f.tfl.Result = tiltfile.TiltfileLoadResult{Manifests: manifests}
err := f.cmd.down(f.ctx, f.deps, nil)
require.NoError(t, err)
require.Contains(t, f.kCli.DeletedYaml, "sancho")
for _, ns := range []string{"foo", "bar"} {
require.NotContains(t, f.kCli.DeletedYaml, ns)
}
}
func TestDownDeletesNamespacesIfSpecified(t *testing.T) {
f := newDownFixture(t)
defer f.TearDown()
manifests := append([]model.Manifest{}, newK8sManifest()...)
manifests = append(manifests, newK8sNamespaceManifest("foo"), newK8sNamespaceManifest("bar"))
f.tfl.Result = tiltfile.TiltfileLoadResult{Manifests: manifests}
f.cmd.deleteNamespaces = true
err := f.cmd.down(f.ctx, f.deps, nil)
require.NoError(t, err)
for _, ns := range []string{"sancho", "foo", "bar"} {
require.Contains(t, f.kCli.DeletedYaml, ns)
}
}
func TestDownK8sFails(t *testing.T) {
f := newDownFixture(t)
defer f.TearDown()
f.tfl.Result = tiltfile.TiltfileLoadResult{Manifests: newK8sManifest()}
f.kCli.DeleteError = fmt.Errorf("GARBLEGARBLE")
err := f.cmd.down(f.ctx, f.deps, nil)
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "GARBLEGARBLE")
}
}
func TestDownDCFails(t *testing.T) {
f := newDownFixture(t)
defer f.TearDown()
f.tfl.Result = tiltfile.TiltfileLoadResult{Manifests: newDCManifest()}
f.dcc.DownError = fmt.Errorf("GARBLEGARBLE")
err := f.cmd.down(f.ctx, f.deps, nil)
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "GARBLEGARBLE")
}
}
func TestDownArgs(t *testing.T) {
f := newDownFixture(t)
defer f.TearDown()
cmd := f.cmd.register()
cmd.SetArgs([]string{"foo", "bar"})
cmd.Run = func(cmd *cobra.Command, args []string) {
ctx, _, _ := testutils.CtxAndAnalyticsForTest()
err := f.cmd.run(ctx, args)
require.NoError(t, err)
}
err := cmd.Execute()
require.NoError(t, err)
require.Equal(t, []string{"foo", "bar"}, f.tfl.PassedUserConfigState().Args)
}
func newK8sManifest() []model.Manifest {
return []model.Manifest{model.Manifest{Name: "fe"}.WithDeployTarget(k8s.MustTarget("fe", testyaml.SanchoYAML))}
}
func newDCManifest() []model.Manifest {
return []model.Manifest{model.Manifest{Name: "fe"}.WithDeployTarget(model.DockerComposeTarget{
Name: "fe",
ConfigPaths: []string{"dc.yaml"},
})}
}
func newK8sNamespaceManifest(name string) model.Manifest {
yaml := fmt.Sprintf(`
apiVersion: v1
kind: Namespace
metadata:
name: %s
spec: {}
status: {}`, name)
return model.Manifest{Name: model.ManifestName(name)}.WithDeployTarget(model.K8sTarget{YAML: yaml})
}
type downFixture struct {
t *testing.T
ctx context.Context
cancel func()
cmd *downCmd
deps DownDeps
tfl *tiltfile.FakeTiltfileLoader
dcc *dockercompose.FakeDCClient
kCli *k8s.FakeK8sClient
}
func newDownFixture(t *testing.T) downFixture {
ctx, _, _ := testutils.CtxAndAnalyticsForTest()
ctx, cancel := context.WithCancel(ctx)
tfl := tiltfile.NewFakeTiltfileLoader()
dcc := dockercompose.NewFakeDockerComposeClient(t, ctx)
kCli := k8s.NewFakeK8sClient()
downDeps := DownDeps{tfl, dcc, kCli}
cmd := &downCmd{downDepsProvider: func(ctx context.Context, tiltAnalytics *analytics.TiltAnalytics, subcommand model.TiltSubcommand) (deps DownDeps, err error) {
return downDeps, nil
}}
return downFixture{
t: t,
ctx: ctx,
cancel: cancel,
cmd: cmd,
deps: downDeps,
tfl: tfl,
dcc: dcc,
kCli: kCli,
}
}
func (f *downFixture) TearDown() {
f.cancel()
}