github.com/hernad/nomad@v1.6.112/command/service_list_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  	"time"
    10  
    11  	"github.com/hernad/nomad/api"
    12  	"github.com/hernad/nomad/ci"
    13  	"github.com/hernad/nomad/testutil"
    14  	"github.com/mitchellh/cli"
    15  	"github.com/stretchr/testify/assert"
    16  	"github.com/stretchr/testify/require"
    17  )
    18  
    19  func TestServiceListCommand_Run(t *testing.T) {
    20  	ci.Parallel(t)
    21  
    22  	srv, client, url := testServer(t, true, nil)
    23  	defer srv.Shutdown()
    24  
    25  	// Wait until our test node is ready.
    26  	testutil.WaitForResult(func() (bool, error) {
    27  		nodes, _, err := client.Nodes().List(nil)
    28  		if err != nil {
    29  			return false, err
    30  		}
    31  		if len(nodes) == 0 {
    32  			return false, fmt.Errorf("missing node")
    33  		}
    34  		if _, ok := nodes[0].Drivers["mock_driver"]; !ok {
    35  			return false, fmt.Errorf("mock_driver not ready")
    36  		}
    37  		return true, nil
    38  	}, func(err error) {
    39  		require.NoError(t, err)
    40  	})
    41  
    42  	ui := cli.NewMockUi()
    43  	cmd := &ServiceListCommand{
    44  		Meta: Meta{
    45  			Ui:          ui,
    46  			flagAddress: url,
    47  		},
    48  	}
    49  
    50  	// Run the command with some random arguments to ensure we are performing
    51  	// this check.
    52  	require.Equal(t, 1, cmd.Run([]string{"-address=" + url, "pretty-please"}))
    53  	require.Contains(t, ui.ErrorWriter.String(), "This command takes no arguments")
    54  	ui.ErrorWriter.Reset()
    55  
    56  	// Create a test job with a Nomad service.
    57  	testJob := testJob("service-discovery-nomad-list")
    58  	testJob.TaskGroups[0].Tasks[0].Services = []*api.Service{
    59  		{Name: "service-discovery-nomad-list", Provider: "nomad", Tags: []string{"foo", "bar"}}}
    60  
    61  	// Register that job.
    62  	regResp, _, err := client.Jobs().Register(testJob, nil)
    63  	require.NoError(t, err)
    64  	registerCode := waitForSuccess(ui, client, fullId, t, regResp.EvalID)
    65  	require.Equal(t, 0, registerCode)
    66  
    67  	// Reset the output writer, otherwise we will have additional information here.
    68  	ui.OutputWriter.Reset()
    69  
    70  	// Job register doesn't assure the service registration has completed. It
    71  	// therefore needs this wrapper to account for eventual service
    72  	// registration. One this has completed, we can perform lookups without
    73  	// similar wraps.
    74  	require.Eventually(t, func() bool {
    75  
    76  		defer ui.OutputWriter.Reset()
    77  
    78  		// Perform a standard lookup.
    79  		if code := cmd.Run([]string{"-address=" + url}); code != 0 {
    80  			return false
    81  		}
    82  
    83  		// Test each header and data entry.
    84  		s := ui.OutputWriter.String()
    85  		if !assert.Contains(t, s, "Service Name") {
    86  			return false
    87  		}
    88  		if !assert.Contains(t, s, "Tags") {
    89  			return false
    90  		}
    91  		if !assert.Contains(t, s, "service-discovery-nomad-list") {
    92  			return false
    93  		}
    94  		if !assert.Contains(t, s, "[bar,foo]") {
    95  			return false
    96  		}
    97  		return true
    98  	}, 5*time.Second, 100*time.Millisecond)
    99  
   100  	// Perform a wildcard namespace lookup.
   101  	code := cmd.Run([]string{"-address=" + url, "-namespace", "*"})
   102  	require.Equal(t, 0, code)
   103  
   104  	// Test each header and data entry.
   105  	s := ui.OutputWriter.String()
   106  	require.Contains(t, s, "Service Name")
   107  	require.Contains(t, s, "Namespace")
   108  	require.Contains(t, s, "Tags")
   109  	require.Contains(t, s, "service-discovery-nomad-list")
   110  	require.Contains(t, s, "default")
   111  	require.Contains(t, s, "[bar,foo]")
   112  
   113  	ui.OutputWriter.Reset()
   114  	ui.ErrorWriter.Reset()
   115  }