2022-03-21 10:59:03 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api"
|
2022-03-24 07:45:13 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2022-04-06 07:36:23 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2022-03-21 10:59:03 +00:00
|
|
|
"github.com/hashicorp/nomad/testutil"
|
|
|
|
"github.com/mitchellh/cli"
|
2022-04-06 07:36:23 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2022-03-21 10:59:03 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestServiceDeleteCommand_Run(t *testing.T) {
|
2022-03-24 07:45:13 +00:00
|
|
|
ci.Parallel(t)
|
2022-03-21 10:59:03 +00:00
|
|
|
|
|
|
|
srv, client, url := testServer(t, true, nil)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
|
2022-03-30 02:07:39 +00:00
|
|
|
// Wait for server and client to start
|
|
|
|
testutil.WaitForLeader(t, srv.Agent.RPC)
|
|
|
|
clientID := srv.Agent.Client().NodeID()
|
|
|
|
testutil.WaitForClient(t, srv.Agent.Client().RPC, clientID, srv.Agent.Client().Region())
|
|
|
|
|
2022-03-21 10:59:03 +00:00
|
|
|
// Wait until our test node is ready.
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
nodes, _, err := client.Nodes().List(nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if len(nodes) == 0 {
|
|
|
|
return false, fmt.Errorf("missing node")
|
|
|
|
}
|
|
|
|
if _, ok := nodes[0].Drivers["mock_driver"]; !ok {
|
|
|
|
return false, fmt.Errorf("mock_driver not ready")
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
cmd := &ServiceDeleteCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
Ui: ui,
|
|
|
|
flagAddress: url,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the command without any arguments to ensure we are performing this
|
|
|
|
// check.
|
|
|
|
require.Equal(t, 1, cmd.Run([]string{"-address=" + url}))
|
|
|
|
require.Contains(t, ui.ErrorWriter.String(),
|
|
|
|
"This command takes two arguments: <service_name> and <service_id>")
|
|
|
|
ui.ErrorWriter.Reset()
|
|
|
|
|
2022-04-06 07:36:23 +00:00
|
|
|
// Create an upsert some service registrations.
|
|
|
|
serviceRegs := mock.ServiceRegistrations()
|
|
|
|
assert.NoError(t,
|
|
|
|
srv.Agent.Server().State().UpsertServiceRegistrations(structs.MsgTypeTestSetup, 10, serviceRegs))
|
2022-03-21 10:59:03 +00:00
|
|
|
|
2022-04-06 07:36:23 +00:00
|
|
|
// Detail the service within the default namespace as we need the ID.
|
|
|
|
defaultNSService, _, err := client.Services().Get(serviceRegs[0].ServiceName, nil)
|
2022-03-21 10:59:03 +00:00
|
|
|
require.NoError(t, err)
|
2022-04-06 07:36:23 +00:00
|
|
|
require.Len(t, defaultNSService, 1)
|
2022-03-21 10:59:03 +00:00
|
|
|
|
2022-04-06 07:36:23 +00:00
|
|
|
// Attempt to manually delete the service registration within the default
|
|
|
|
// namespace.
|
|
|
|
code := cmd.Run([]string{"-address=" + url, "service-discovery-nomad-delete", defaultNSService[0].ID})
|
|
|
|
require.Equal(t, 0, code)
|
|
|
|
require.Contains(t, ui.OutputWriter.String(), "Successfully deleted service registration")
|
|
|
|
|
|
|
|
ui.OutputWriter.Reset()
|
|
|
|
ui.ErrorWriter.Reset()
|
|
|
|
|
|
|
|
// Detail the service within the platform namespace as we need the ID.
|
|
|
|
platformNSService, _, err := client.Services().Get(serviceRegs[1].ServiceName, &api.QueryOptions{
|
|
|
|
Namespace: serviceRegs[1].Namespace},
|
|
|
|
)
|
2022-03-21 10:59:03 +00:00
|
|
|
require.NoError(t, err)
|
2022-04-06 07:36:23 +00:00
|
|
|
require.Len(t, platformNSService, 1)
|
2022-03-21 10:59:03 +00:00
|
|
|
|
2022-04-06 07:36:23 +00:00
|
|
|
// Attempt to manually delete the service registration within the platform
|
|
|
|
// namespace.
|
|
|
|
code = cmd.Run([]string{"-address=" + url, "-namespace=" + platformNSService[0].Namespace,
|
|
|
|
"service-discovery-nomad-delete", platformNSService[0].ID})
|
2022-03-21 10:59:03 +00:00
|
|
|
require.Equal(t, 0, code)
|
|
|
|
require.Contains(t, ui.OutputWriter.String(), "Successfully deleted service registration")
|
|
|
|
|
|
|
|
ui.OutputWriter.Reset()
|
|
|
|
ui.ErrorWriter.Reset()
|
|
|
|
}
|