mirror of
https://github.com/status-im/consul.git
synced 2025-01-09 13:26:07 +00:00
5fb9df1640
* Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at <Blog URL>, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
214 lines
5.3 KiB
Go
214 lines
5.3 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package delete
|
|
|
|
import (
|
|
"strconv"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/consul/agent"
|
|
"github.com/hashicorp/consul/api"
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
|
"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) {
|
|
if testing.Short() {
|
|
t.Skip("too slow for testing.Short")
|
|
}
|
|
|
|
t.Parallel()
|
|
|
|
a := agent.NewTestAgent(t, ``)
|
|
defer a.Shutdown()
|
|
client := a.Client()
|
|
|
|
ui := cli.NewMockUi()
|
|
c := New(ui)
|
|
|
|
err := createEntry(client)
|
|
require.NoError(t, err)
|
|
|
|
args := []string{
|
|
"-http-addr=" + a.HTTPAddr(),
|
|
"-kind=" + api.ServiceDefaults,
|
|
"-name=web",
|
|
}
|
|
|
|
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)
|
|
require.Error(t, err)
|
|
require.Nil(t, entry)
|
|
}
|
|
|
|
func createEntry(client *api.Client) error {
|
|
_, _, err := client.ConfigEntries().Set(&api.ServiceConfigEntry{
|
|
Kind: api.ServiceDefaults,
|
|
Name: "web",
|
|
Protocol: "tcp",
|
|
}, nil)
|
|
return err
|
|
}
|
|
|
|
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()
|
|
|
|
err := createEntry(client)
|
|
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())
|
|
entry, _, err := client.ConfigEntries().Get(api.ServiceDefaults, "web", nil)
|
|
require.Error(t, err)
|
|
require.Nil(t, entry)
|
|
})
|
|
|
|
t.Run("delete from file with a valid modify index", func(t *testing.T) {
|
|
err := createEntry(client)
|
|
require.NoError(t, err)
|
|
entry, _, err := client.ConfigEntries().Get(api.ServiceDefaults, "web", nil)
|
|
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)
|
|
require.Error(t, err)
|
|
require.Nil(t, entry)
|
|
})
|
|
}
|
|
|
|
func TestConfigDelete_InvalidArgs(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
cases := map[string]struct {
|
|
args []string
|
|
err string
|
|
}{
|
|
"no kind or filename": {
|
|
args: []string{},
|
|
err: "Must specify the -kind or -filename parameter",
|
|
},
|
|
"no kind": {
|
|
args: []string{"-name", "web"},
|
|
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",
|
|
},
|
|
"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",
|
|
},
|
|
}
|
|
|
|
for name, tcase := range cases {
|
|
t.Run(name, func(t *testing.T) {
|
|
ui := cli.NewMockUi()
|
|
c := New(ui)
|
|
|
|
require.NotEqual(t, 0, c.Run(tcase.args))
|
|
require.Contains(t, ui.ErrorWriter.String(), tcase.err)
|
|
})
|
|
}
|
|
}
|