/
ctl_install.go
143 lines (123 loc) · 3.74 KB
/
ctl_install.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
/*
Copyright 2021 The cert-manager 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 ctl
import (
"bytes"
"context"
"fmt"
"regexp"
"strings"
"testing"
"time"
"github.com/sergi/go-diff/diffmatchpatch"
"github.com/jetstack/cert-manager/cmd/ctl/cmd"
"github.com/jetstack/cert-manager/test/integration/ctl/install_framework"
"github.com/jetstack/cert-manager/test/internal/util"
)
func TestCtlInstall(t *testing.T) {
tests := map[string]struct {
prerun bool
preInputArgs []string
preExpErr bool
preExpOutput string
inputArgs []string
expErr bool
expOutput string
}{
"install cert-manager": {
inputArgs: []string{},
expErr: false,
expOutput: `STATUS: deployed`,
},
"install cert-manager (already installed)": {
prerun: true,
preInputArgs: []string{},
preExpErr: false,
preExpOutput: `STATUS: deployed`,
inputArgs: []string{},
expErr: true,
expOutput: `^Found existing installed cert-manager CRDs! Cannot continue with installation.$`,
},
"install cert-manager (already installed, in other namespace)": {
prerun: true,
preInputArgs: []string{"--namespace=test"},
preExpErr: false,
preExpOutput: `STATUS: deployed`,
inputArgs: []string{},
expErr: true,
expOutput: `^Found existing installed cert-manager CRDs! Cannot continue with installation.$`,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
testApiServer, cleanup := install_framework.NewTestInstallApiServer(t)
defer cleanup()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*40)
defer cancel()
if test.prerun {
executeCommandAndCheckOutput(t, ctx, testApiServer.KubeConfig(), test.preInputArgs, test.preExpErr, test.preExpOutput)
}
executeCommandAndCheckOutput(t, ctx, testApiServer.KubeConfig(), test.inputArgs, test.expErr, test.expOutput)
})
}
}
func executeCommandAndCheckOutput(
t *testing.T,
ctx context.Context,
kubeConfig string,
inputArgs []string,
expErr bool,
expOutput string,
) {
// Options to run status command
stdin := bytes.NewBufferString("")
stdout := bytes.NewBufferString("")
chartPath := util.GetTestPath("deploy", "charts", "cert-manager", "cert-manager.tgz")
cmd := cmd.NewCertManagerCtlCommand(ctx, stdin, stdout, stdout)
cmd.SetArgs(append([]string{
fmt.Sprintf("--kubeconfig=%s", kubeConfig),
"--wait=false",
fmt.Sprintf("--chart-name=%s", chartPath),
"x",
"install",
}, inputArgs...))
err := cmd.Execute()
if err != nil {
fmt.Fprintf(stdout, "%s\n", err)
if !expErr {
t.Errorf("got unexpected error: %v", err)
} else {
t.Logf("got an error, which was expected, details: %v", err)
}
} else if expErr {
// expected error but error is nil
t.Errorf("expected but got no error")
}
match, err := regexp.MatchString(strings.TrimSpace(expOutput), strings.TrimSpace(stdout.String()))
if err != nil {
t.Error(err)
}
dmp := diffmatchpatch.New()
if !match {
diffs := dmp.DiffMain(strings.TrimSpace(expOutput), strings.TrimSpace(stdout.String()), false)
t.Errorf(
"got unexpected output, diff (ignoring line anchors ^ and $ and regex for creation time):\n"+
"diff: %s\n\n"+
" exp: %s\n\n"+
" got: %s",
dmp.DiffPrettyText(diffs),
expOutput,
stdout.String(),
)
}
}