10
"github.com/gruntwork-io/terratest/modules/logger"
11
"github.com/gruntwork-io/terratest/modules/retry"
12
"github.com/gruntwork-io/terratest/modules/shell"
13
"github.com/gruntwork-io/terratest/modules/testing"
14
"github.com/pkg/errors"
16
core_model "github.com/kumahq/kuma/pkg/core/resources/model"
17
"github.com/kumahq/kuma/pkg/core/resources/model/rest"
20
type KumactlOptions struct {
32
func NewKumactlOptions(
40
timeout time.Duration,
42
return &KumactlOptions{
47
ConfigPath: configPath,
51
Env: map[string]string{},
55
func (k *KumactlOptions) RunKumactl(args ...string) error {
56
out, err := k.RunKumactlAndGetOutput(args...)
58
return errors.Wrapf(err, out)
63
func (k *KumactlOptions) RunKumactlAndGetOutput(args ...string) (string, error) {
64
return k.RunKumactlAndGetOutputV(k.Verbose, args...)
67
func (k *KumactlOptions) RunKumactlAndGetOutputV(verbose bool, args ...string) (string, error) {
69
if len(k.ExtraArgs) > 0 {
70
cmdArgs = append(cmdArgs, k.ExtraArgs...)
72
if k.ConfigPath != "" {
73
cmdArgs = append(cmdArgs, "--config-file", k.ConfigPath)
76
cmdArgs = append(cmdArgs, args...)
77
command := shell.Command{
84
command.Logger = logger.Discard
87
return shell.RunCommandAndGetStdOutE(k.t, command)
90
func (k *KumactlOptions) KumactlVersion() (string, error) {
91
return k.RunKumactlAndGetOutput("version")
94
func (k *KumactlOptions) KumactlDelete(kumatype, name, mesh string) error {
95
return k.RunKumactl("delete", kumatype, name, "--mesh", mesh)
98
func (k *KumactlOptions) KumactlList(kumatype, mesh string) ([]string, error) {
99
out, err := k.RunKumactlAndGetOutput("get", kumatype, "--mesh", mesh, "-o", "json")
105
Name string `json:"name"`
107
type resourceList struct {
108
Items []item `json:"items"`
111
list := &resourceList{}
112
if err := json.Unmarshal([]byte(out), list); err != nil {
117
for _, item := range list.Items {
118
items = append(items, item.Name)
123
func (k *KumactlOptions) KumactlApply(configPath string) error {
124
return k.RunKumactl("apply", "-f", configPath)
127
func (k *KumactlOptions) KumactlApplyFromString(configData string) error {
128
tmpfile, err := storeConfigToTempFile(k.t.Name(), configData)
133
defer os.Remove(tmpfile)
135
return k.KumactlApply(tmpfile)
138
func storeConfigToTempFile(name string, configData string) (string, error) {
139
escapedTestName := url.PathEscape(name)
141
tmpfile, err := os.CreateTemp("", escapedTestName)
145
defer tmpfile.Close()
147
_, err = tmpfile.WriteString(configData)
149
return tmpfile.Name(), err
152
func (k *KumactlOptions) KumactlInstallCP(args ...string) (string, error) {
154
"install", "control-plane",
157
cmd = append(cmd, args...)
159
return k.RunKumactlAndGetOutputV(
160
false, // silence the log output of Install
164
func (k *KumactlOptions) KumactlInstallObservability(namespace string, components []string) (string, error) {
165
args := []string{"install", "observability", "--namespace", namespace}
166
if len(components) != 0 {
167
args = append(args, "--components", strings.Join(components, ","))
169
return k.RunKumactlAndGetOutput(args...)
172
func (k *KumactlOptions) KumactlConfigControlPlanesAdd(name, address, token string, headers []string) error {
173
_, err := retry.DoWithRetryE(k.t, "kumactl config control-planes add", k.Retries, k.Timeout,
174
func() (string, error) {
176
"config", "control-planes", "add",
179
"--address", address,
181
if len(headers) > 0 {
182
args = append(args, "--headers", strings.Join(headers, ","))
186
"--auth-type", "tokens",
187
"--auth-conf", "token="+token,
190
err := k.RunKumactl(args...)
192
return "Unable to register Kuma CP. Try again.", err
201
func (k *KumactlOptions) KumactlConfigControlPlanesSwitch(name string) error {
202
return k.RunKumactl("config", "control-planes", "switch", "--name", name)
205
// KumactlUpdateObject fetches an object and updates it after the update function is applied to it.
206
func (k *KumactlOptions) KumactlUpdateObject(
209
update func(core_model.Resource) core_model.Resource,
211
out, err := k.RunKumactlAndGetOutput("get", typeName, objectName, "-o", "yaml")
213
return errors.Wrapf(err, "failed to get %q object %q", typeName, objectName)
216
resource, err := rest.YAML.UnmarshalCore([]byte(out))
218
return errors.Wrapf(err, "failed to unmarshal %q object %q: %q", typeName, objectName, out)
221
updated := rest.From.Resource(update(resource))
222
jsonRes, err := json.Marshal(updated)
224
return errors.Wrapf(err, "failed to marshal JSON for %q object %q", typeName, objectName)
227
return k.KumactlApplyFromString(string(jsonRes))