2023-03-28 19:12:30 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2019-04-30 23:27:16 +00:00
|
|
|
package delete
|
|
|
|
|
|
|
|
import (
|
2021-11-01 16:42:01 +00:00
|
|
|
"strconv"
|
2019-04-30 23:27:16 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent"
|
|
|
|
"github.com/hashicorp/consul/api"
|
2022-07-11 14:13:40 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
2019-04-30 23:27:16 +00:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestConfigDelete_noTabs(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
require.NotContains(t, New(cli.NewMockUi()).Help(), "\t")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigDelete(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2019-04-30 23:27:16 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-03-31 19:59:56 +00:00
|
|
|
a := agent.NewTestAgent(t, ``)
|
2019-04-30 23:27:16 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
client := a.Client()
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
|
|
|
|
2022-07-11 14:13:40 +00:00
|
|
|
err := createEntry(client)
|
2020-02-03 09:14:30 +00:00
|
|
|
require.NoError(t, err)
|
2019-04-30 23:27:16 +00:00
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
|
|
|
"-kind=" + api.ServiceDefaults,
|
|
|
|
"-name=web",
|
|
|
|
}
|
|
|
|
|
|
|
|
code := c.Run(args)
|
|
|
|
require.Equal(t, 0, code)
|
2020-06-29 20:47:40 +00:00
|
|
|
require.Contains(t, ui.OutputWriter.String(),
|
|
|
|
"Config entry deleted: service-defaults/web")
|
2019-04-30 23:27:16 +00:00
|
|
|
require.Empty(t, ui.ErrorWriter.String())
|
|
|
|
|
|
|
|
entry, _, err := client.ConfigEntries().Get(api.ServiceDefaults, "web", nil)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, entry)
|
|
|
|
}
|
|
|
|
|
2022-07-11 14:13:40 +00:00
|
|
|
func createEntry(client *api.Client) error {
|
|
|
|
_, _, err := client.ConfigEntries().Set(&api.ServiceConfigEntry{
|
|
|
|
Kind: api.ServiceDefaults,
|
|
|
|
Name: "web",
|
|
|
|
Protocol: "tcp",
|
|
|
|
}, nil)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-01 16:42:01 +00:00
|
|
|
func TestConfigDelete_CAS(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
a := agent.NewTestAgent(t, ``)
|
|
|
|
defer a.Shutdown()
|
|
|
|
client := a.Client()
|
|
|
|
|
2022-07-11 14:13:40 +00:00
|
|
|
err := createEntry(client)
|
2021-11-01 16:42:01 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
entry, _, err := client.ConfigEntries().Get(api.ServiceDefaults, "web", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
t.Run("with an invalid modify index", func(t *testing.T) {
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
|
|
|
"-kind=" + api.ServiceDefaults,
|
|
|
|
"-name=web",
|
|
|
|
"-cas",
|
|
|
|
"-modify-index=" + strconv.FormatUint(entry.GetModifyIndex()-1, 10),
|
|
|
|
}
|
|
|
|
|
|
|
|
code := c.Run(args)
|
|
|
|
require.Equal(t, 1, code)
|
|
|
|
require.Contains(t, ui.ErrorWriter.String(),
|
|
|
|
"Config entry not deleted: service-defaults/web")
|
|
|
|
require.Empty(t, ui.OutputWriter.String())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("with a valid modify index", func(t *testing.T) {
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
|
|
|
"-kind=" + api.ServiceDefaults,
|
|
|
|
"-name=web",
|
|
|
|
"-cas",
|
|
|
|
"-modify-index=" + strconv.FormatUint(entry.GetModifyIndex(), 10),
|
|
|
|
}
|
|
|
|
|
|
|
|
code := c.Run(args)
|
|
|
|
require.Equal(t, 0, code)
|
|
|
|
require.Contains(t, ui.OutputWriter.String(),
|
|
|
|
"Config entry deleted: service-defaults/web")
|
|
|
|
require.Empty(t, ui.ErrorWriter.String())
|
2022-07-11 14:13:40 +00:00
|
|
|
entry, _, err := client.ConfigEntries().Get(api.ServiceDefaults, "web", nil)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, entry)
|
|
|
|
})
|
2021-11-01 16:42:01 +00:00
|
|
|
|
2022-07-11 14:13:40 +00:00
|
|
|
t.Run("delete from file with a valid modify index", func(t *testing.T) {
|
|
|
|
err := createEntry(client)
|
|
|
|
require.NoError(t, err)
|
2021-11-01 16:42:01 +00:00
|
|
|
entry, _, err := client.ConfigEntries().Get(api.ServiceDefaults, "web", nil)
|
2022-07-11 14:13:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
|
|
|
f := testutil.TempFile(t, "config-write-svc-web.hcl")
|
|
|
|
_, err = f.WriteString(`
|
|
|
|
Kind = "service-defaults"
|
|
|
|
Name = "web"
|
|
|
|
Protocol = "tcp"
|
|
|
|
`)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
|
|
|
"-filename=" + f.Name(),
|
|
|
|
"-cas",
|
|
|
|
"-modify-index=" + strconv.FormatUint(entry.GetModifyIndex(), 10),
|
|
|
|
}
|
|
|
|
|
|
|
|
code := c.Run(args)
|
|
|
|
require.Equal(t, 0, code)
|
|
|
|
require.Contains(t, ui.OutputWriter.String(),
|
|
|
|
"Config entry deleted: service-defaults/web")
|
|
|
|
require.Empty(t, ui.ErrorWriter.String())
|
|
|
|
|
|
|
|
entry, _, err = client.ConfigEntries().Get(api.ServiceDefaults, "web", nil)
|
2021-11-01 16:42:01 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, entry)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-30 23:27:16 +00:00
|
|
|
func TestConfigDelete_InvalidArgs(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2021-11-01 16:42:01 +00:00
|
|
|
cases := map[string]struct {
|
|
|
|
args []string
|
|
|
|
err string
|
|
|
|
}{
|
2022-07-11 14:13:40 +00:00
|
|
|
"no kind or filename": {
|
2021-11-01 16:42:01 +00:00
|
|
|
args: []string{},
|
2022-07-11 14:13:40 +00:00
|
|
|
err: "Must specify the -kind or -filename parameter",
|
|
|
|
},
|
|
|
|
"no kind": {
|
|
|
|
args: []string{"-name", "web"},
|
2021-11-01 16:42:01 +00:00
|
|
|
err: "Must specify the -kind parameter",
|
|
|
|
},
|
|
|
|
"no name": {
|
|
|
|
args: []string{"-kind", api.ServiceDefaults},
|
|
|
|
err: "Must specify the -name parameter",
|
|
|
|
},
|
|
|
|
"cas but no modify-index": {
|
|
|
|
args: []string{"-kind", api.ServiceDefaults, "-name", "web", "-cas"},
|
|
|
|
err: "Must specify a -modify-index greater than 0 with -cas",
|
|
|
|
},
|
|
|
|
"cas but no zero modify-index": {
|
|
|
|
args: []string{"-kind", api.ServiceDefaults, "-name", "web", "-cas", "-modify-index", "0"},
|
|
|
|
err: "Must specify a -modify-index greater than 0 with -cas",
|
|
|
|
},
|
|
|
|
"modify-index but no cas": {
|
|
|
|
args: []string{"-kind", api.ServiceDefaults, "-name", "web", "-modify-index", "1"},
|
|
|
|
err: "Cannot specify -modify-index without -cas",
|
|
|
|
},
|
2022-07-11 14:13:40 +00:00
|
|
|
"kind and filename": {
|
|
|
|
args: []string{"-kind", api.ServiceDefaults, "-filename", "config-file.hcl"},
|
|
|
|
err: "filename can't be used with kind or name",
|
|
|
|
},
|
|
|
|
"name and filename": {
|
|
|
|
args: []string{"-name", "db", "-filename", "config-file.hcl"},
|
|
|
|
err: "filename can't be used with kind or name",
|
|
|
|
},
|
|
|
|
"kind, name, and filename": {
|
|
|
|
args: []string{"-kind", api.ServiceDefaults, "-name", "db", "-filename", "config-file.hcl"},
|
|
|
|
err: "filename can't be used with kind or name",
|
|
|
|
},
|
2019-04-30 23:27:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, tcase := range cases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
|
|
|
|
2021-11-01 16:42:01 +00:00
|
|
|
require.NotEqual(t, 0, c.Run(tcase.args))
|
|
|
|
require.Contains(t, ui.ErrorWriter.String(), tcase.err)
|
2019-04-30 23:27:16 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|