github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/job_scaling_events_test.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/nomad/ci"
     9  	"github.com/hashicorp/nomad/helper/pointer"
    10  	"github.com/hashicorp/nomad/testutil"
    11  	"github.com/mitchellh/cli"
    12  )
    13  
    14  func TestJobScalingEventsCommand_Run(t *testing.T) {
    15  	ci.Parallel(t)
    16  	srv, client, url := testServer(t, true, nil)
    17  	defer srv.Shutdown()
    18  	testutil.WaitForResult(func() (bool, error) {
    19  		nodes, _, err := client.Nodes().List(nil)
    20  		if err != nil {
    21  			return false, err
    22  		}
    23  		if len(nodes) == 0 {
    24  			return false, fmt.Errorf("missing node")
    25  		}
    26  		if _, ok := nodes[0].Drivers["mock_driver"]; !ok {
    27  			return false, fmt.Errorf("mock_driver not ready")
    28  		}
    29  		return true, nil
    30  	}, func(err error) {
    31  		t.Fatalf("err: %s", err)
    32  	})
    33  
    34  	ui := cli.NewMockUi()
    35  	cmd := &JobScalingEventsCommand{Meta: Meta{Ui: ui}}
    36  
    37  	// Register a test job and ensure it is running before moving on.
    38  	resp, _, err := client.Jobs().Register(testJob("scale_events_test_job"), nil)
    39  	if err != nil {
    40  		t.Fatalf("err: %s", err)
    41  	}
    42  	if code := waitForSuccess(ui, client, fullId, t, resp.EvalID); code != 0 {
    43  		t.Fatalf("expected waitForSuccess exit code 0, got: %d", code)
    44  	}
    45  
    46  	// List events without passing the jobID which should result in an error.
    47  	if code := cmd.Run([]string{"-address=" + url}); code != 1 {
    48  		t.Fatalf("expected cmd run exit code 1, got: %d", code)
    49  	}
    50  	if out := ui.ErrorWriter.String(); !strings.Contains(out, "This command takes one argument: <job_id>") {
    51  		t.Fatalf("Expected argument error: %v", out)
    52  	}
    53  
    54  	// List events for the job, which should present zero.
    55  	if code := cmd.Run([]string{"-address=" + url, "scale_events_test_job"}); code != 0 {
    56  		t.Fatalf("expected cmd run exit code 0, got: %d", code)
    57  	}
    58  	if out := ui.OutputWriter.String(); !strings.Contains(out, "No events found") {
    59  		t.Fatalf("Expected no events output but got: %v", out)
    60  	}
    61  
    62  	// Perform a scaling action to generate an event.
    63  	_, _, err = client.Jobs().Scale(
    64  		"scale_events_test_job",
    65  		"group1", pointer.Of(2),
    66  		"searchable custom test message", false, nil, nil)
    67  	if err != nil {
    68  		t.Fatalf("err: %s", err)
    69  	}
    70  
    71  	// List the scaling events which should include an entry.
    72  	if code := cmd.Run([]string{"-address=" + url, "scale_events_test_job"}); code != 0 {
    73  		t.Fatalf("expected cmd run exit code 0, got: %d", code)
    74  	}
    75  	if out := ui.OutputWriter.String(); !strings.Contains(out, "Task Group  Count  PrevCount  Date") {
    76  		t.Fatalf("Expected table headers but got: %v", out)
    77  	}
    78  
    79  	// List the scaling events with verbose flag to search for our message as
    80  	// well as the verbose table headers.
    81  	if code := cmd.Run([]string{"-address=" + url, "-verbose", "scale_events_test_job"}); code != 0 {
    82  		t.Fatalf("expected cmd run exit code 0, got: %d", code)
    83  	}
    84  	out := ui.OutputWriter.String()
    85  	if !strings.Contains(out, "searchable custom test message") {
    86  		t.Fatalf("Expected to find scaling message but got: %v", out)
    87  	}
    88  	if !strings.Contains(out, "Eval ID") {
    89  		t.Fatalf("Expected to verbose table headers: %v", out)
    90  	}
    91  }