github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/command/job_scale_test.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/nomad/api"
     9  	"github.com/hashicorp/nomad/helper"
    10  	"github.com/hashicorp/nomad/testutil"
    11  	"github.com/mitchellh/cli"
    12  )
    13  
    14  func TestJobScaleCommand_SingleGroup(t *testing.T) {
    15  	t.Parallel()
    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 := &JobScaleCommand{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_cmd_single_group"), 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  	// Perform the scaling action.
    47  	if code := cmd.Run([]string{"-address=" + url, "-detach", "scale_cmd_single_group", "2"}); code != 0 {
    48  		t.Fatalf("expected cmd run exit code 0, got: %d", code)
    49  	}
    50  	if out := ui.OutputWriter.String(); !strings.Contains(out, "Evaluation ID:") {
    51  		t.Fatalf("Expected Evaluation ID within output: %v", out)
    52  	}
    53  }
    54  
    55  func TestJobScaleCommand_MultiGroup(t *testing.T) {
    56  	t.Parallel()
    57  	srv, client, url := testServer(t, true, nil)
    58  	defer srv.Shutdown()
    59  	testutil.WaitForResult(func() (bool, error) {
    60  		nodes, _, err := client.Nodes().List(nil)
    61  		if err != nil {
    62  			return false, err
    63  		}
    64  		if len(nodes) == 0 {
    65  			return false, fmt.Errorf("missing node")
    66  		}
    67  		if _, ok := nodes[0].Drivers["mock_driver"]; !ok {
    68  			return false, fmt.Errorf("mock_driver not ready")
    69  		}
    70  		return true, nil
    71  	}, func(err error) {
    72  		t.Fatalf("err: %s", err)
    73  	})
    74  
    75  	ui := cli.NewMockUi()
    76  	cmd := &JobScaleCommand{Meta: Meta{Ui: ui}}
    77  
    78  	// Create a job with two task groups.
    79  	job := testJob("scale_cmd_multi_group")
    80  	task := api.NewTask("task2", "mock_driver").
    81  		SetConfig("kill_after", "1s").
    82  		SetConfig("run_for", "5s").
    83  		SetConfig("exit_code", 0).
    84  		Require(&api.Resources{
    85  			MemoryMB: helper.IntToPtr(256),
    86  			CPU:      helper.IntToPtr(100),
    87  		}).
    88  		SetLogConfig(&api.LogConfig{
    89  			MaxFiles:      helper.IntToPtr(1),
    90  			MaxFileSizeMB: helper.IntToPtr(2),
    91  		})
    92  	group2 := api.NewTaskGroup("group2", 1).
    93  		AddTask(task).
    94  		RequireDisk(&api.EphemeralDisk{
    95  			SizeMB: helper.IntToPtr(20),
    96  		})
    97  	job.AddTaskGroup(group2)
    98  
    99  	// Register a test job and ensure it is running before moving on.
   100  	resp, _, err := client.Jobs().Register(job, nil)
   101  	if err != nil {
   102  		t.Fatalf("err: %s", err)
   103  	}
   104  	if code := waitForSuccess(ui, client, fullId, t, resp.EvalID); code != 0 {
   105  		t.Fatalf("expected waitForSuccess exit code 0, got: %d", code)
   106  	}
   107  
   108  	// Attempt to scale without specifying the task group which should fail.
   109  	if code := cmd.Run([]string{"-address=" + url, "-detach", "scale_cmd_multi_group", "2"}); code != 1 {
   110  		t.Fatalf("expected cmd run exit code 1, got: %d", code)
   111  	}
   112  	if out := ui.ErrorWriter.String(); !strings.Contains(out, "Group name required") {
   113  		t.Fatalf("unexpected error message: %v", out)
   114  	}
   115  
   116  	// Specify the target group which should be successful.
   117  	if code := cmd.Run([]string{"-address=" + url, "-detach", "scale_cmd_multi_group", "group1", "2"}); code != 0 {
   118  		t.Fatalf("expected cmd run exit code 0, got: %d", code)
   119  	}
   120  	if out := ui.OutputWriter.String(); !strings.Contains(out, "Evaluation ID:") {
   121  		t.Fatalf("Expected Evaluation ID within output: %v", out)
   122  	}
   123  }