github.com/hernad/nomad@v1.6.112/command/service_delete_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package command 5 6 import ( 7 "fmt" 8 "testing" 9 10 "github.com/hernad/nomad/api" 11 "github.com/hernad/nomad/ci" 12 "github.com/hernad/nomad/nomad/mock" 13 "github.com/hernad/nomad/nomad/structs" 14 "github.com/hernad/nomad/testutil" 15 "github.com/mitchellh/cli" 16 "github.com/stretchr/testify/assert" 17 "github.com/stretchr/testify/require" 18 ) 19 20 func TestServiceDeleteCommand_Run(t *testing.T) { 21 ci.Parallel(t) 22 23 srv, client, url := testServer(t, true, nil) 24 defer srv.Shutdown() 25 26 // Wait for server and client to start 27 testutil.WaitForLeader(t, srv.Agent.RPC) 28 clientID := srv.Agent.Client().NodeID() 29 testutil.WaitForClient(t, srv.Agent.Client().RPC, clientID, srv.Agent.Client().Region()) 30 31 // Wait until our test node is ready. 32 testutil.WaitForResult(func() (bool, error) { 33 nodes, _, err := client.Nodes().List(nil) 34 if err != nil { 35 return false, err 36 } 37 if len(nodes) == 0 { 38 return false, fmt.Errorf("missing node") 39 } 40 if _, ok := nodes[0].Drivers["mock_driver"]; !ok { 41 return false, fmt.Errorf("mock_driver not ready") 42 } 43 return true, nil 44 }, func(err error) { 45 require.NoError(t, err) 46 }) 47 48 ui := cli.NewMockUi() 49 cmd := &ServiceDeleteCommand{ 50 Meta: Meta{ 51 Ui: ui, 52 flagAddress: url, 53 }, 54 } 55 56 // Run the command without any arguments to ensure we are performing this 57 // check. 58 require.Equal(t, 1, cmd.Run([]string{"-address=" + url})) 59 require.Contains(t, ui.ErrorWriter.String(), 60 "This command takes two arguments: <service_name> and <service_id>") 61 ui.ErrorWriter.Reset() 62 63 // Create an upsert some service registrations. 64 serviceRegs := mock.ServiceRegistrations() 65 assert.NoError(t, 66 srv.Agent.Server().State().UpsertServiceRegistrations(structs.MsgTypeTestSetup, 10, serviceRegs)) 67 68 // Detail the service within the default namespace as we need the ID. 69 defaultNSService, _, err := client.Services().Get(serviceRegs[0].ServiceName, nil) 70 require.NoError(t, err) 71 require.Len(t, defaultNSService, 1) 72 73 // Attempt to manually delete the service registration within the default 74 // namespace. 75 code := cmd.Run([]string{"-address=" + url, "service-discovery-nomad-delete", defaultNSService[0].ID}) 76 require.Equal(t, 0, code) 77 require.Contains(t, ui.OutputWriter.String(), "Successfully deleted service registration") 78 79 ui.OutputWriter.Reset() 80 ui.ErrorWriter.Reset() 81 82 // Detail the service within the platform namespace as we need the ID. 83 platformNSService, _, err := client.Services().Get(serviceRegs[1].ServiceName, &api.QueryOptions{ 84 Namespace: serviceRegs[1].Namespace}, 85 ) 86 require.NoError(t, err) 87 require.Len(t, platformNSService, 1) 88 89 // Attempt to manually delete the service registration within the platform 90 // namespace. 91 code = cmd.Run([]string{"-address=" + url, "-namespace=" + platformNSService[0].Namespace, 92 "service-discovery-nomad-delete", platformNSService[0].ID}) 93 require.Equal(t, 0, code) 94 require.Contains(t, ui.OutputWriter.String(), "Successfully deleted service registration") 95 96 ui.OutputWriter.Reset() 97 ui.ErrorWriter.Reset() 98 }