github.com/huiliang/nomad@v0.2.1-0.20151124023127-7a8b664699ff/command/monitor_test.go (about)

     1  package command
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/hashicorp/nomad/api"
     9  	"github.com/hashicorp/nomad/nomad/structs"
    10  	"github.com/mitchellh/cli"
    11  )
    12  
    13  func TestMonitor_Update_Eval(t *testing.T) {
    14  	ui := new(cli.MockUi)
    15  	mon := newMonitor(ui, nil)
    16  
    17  	// Evals triggered by jobs log
    18  	state := &evalState{
    19  		status: structs.EvalStatusPending,
    20  		job:    "job1",
    21  	}
    22  	mon.update(state)
    23  
    24  	out := ui.OutputWriter.String()
    25  	if !strings.Contains(out, "job1") {
    26  		t.Fatalf("missing job\n\n%s", out)
    27  	}
    28  	ui.OutputWriter.Reset()
    29  
    30  	// Evals trigerred by nodes log
    31  	state = &evalState{
    32  		status: structs.EvalStatusPending,
    33  		node:   "node1",
    34  	}
    35  	mon.update(state)
    36  
    37  	out = ui.OutputWriter.String()
    38  	if !strings.Contains(out, "node1") {
    39  		t.Fatalf("missing node\n\n%s", out)
    40  	}
    41  
    42  	// Transition to pending should not be logged
    43  	if strings.Contains(out, structs.EvalStatusPending) {
    44  		t.Fatalf("should skip status\n\n%s", out)
    45  	}
    46  	ui.OutputWriter.Reset()
    47  
    48  	// No logs sent if no update
    49  	mon.update(state)
    50  	if out := ui.OutputWriter.String(); out != "" {
    51  		t.Fatalf("expected no output\n\n%s", out)
    52  	}
    53  
    54  	// Status change sends more logs
    55  	state = &evalState{
    56  		status: structs.EvalStatusComplete,
    57  		node:   "node1",
    58  	}
    59  	mon.update(state)
    60  	out = ui.OutputWriter.String()
    61  	if !strings.Contains(out, structs.EvalStatusComplete) {
    62  		t.Fatalf("missing status\n\n%s", out)
    63  	}
    64  }
    65  
    66  func TestMonitor_Update_Allocs(t *testing.T) {
    67  	ui := new(cli.MockUi)
    68  	mon := newMonitor(ui, nil)
    69  
    70  	// New allocations write new logs
    71  	state := &evalState{
    72  		allocs: map[string]*allocState{
    73  			"alloc1": &allocState{
    74  				id:      "alloc1",
    75  				group:   "group1",
    76  				node:    "node1",
    77  				desired: structs.AllocDesiredStatusRun,
    78  				client:  structs.AllocClientStatusPending,
    79  				index:   1,
    80  			},
    81  		},
    82  	}
    83  	mon.update(state)
    84  
    85  	// Logs were output
    86  	out := ui.OutputWriter.String()
    87  	if !strings.Contains(out, "alloc1") {
    88  		t.Fatalf("missing alloc\n\n%s", out)
    89  	}
    90  	if !strings.Contains(out, "group1") {
    91  		t.Fatalf("missing group\n\n%s", out)
    92  	}
    93  	if !strings.Contains(out, "node1") {
    94  		t.Fatalf("missing node\n\n%s", out)
    95  	}
    96  	if !strings.Contains(out, "created") {
    97  		t.Fatalf("missing created\n\n%s", out)
    98  	}
    99  	ui.OutputWriter.Reset()
   100  
   101  	// No change yields no logs
   102  	mon.update(state)
   103  	if out := ui.OutputWriter.String(); out != "" {
   104  		t.Fatalf("expected no output\n\n%s", out)
   105  	}
   106  	ui.OutputWriter.Reset()
   107  
   108  	// Alloc updates cause more log lines
   109  	state = &evalState{
   110  		allocs: map[string]*allocState{
   111  			"alloc1": &allocState{
   112  				id:      "alloc1",
   113  				group:   "group1",
   114  				node:    "node1",
   115  				desired: structs.AllocDesiredStatusRun,
   116  				client:  structs.AllocClientStatusRunning,
   117  				index:   2,
   118  			},
   119  		},
   120  	}
   121  	mon.update(state)
   122  
   123  	// Updates were logged
   124  	out = ui.OutputWriter.String()
   125  	if !strings.Contains(out, "alloc1") {
   126  		t.Fatalf("missing alloc\n\n%s", out)
   127  	}
   128  	if !strings.Contains(out, "pending") {
   129  		t.Fatalf("missing old status\n\n%s", out)
   130  	}
   131  	if !strings.Contains(out, "running") {
   132  		t.Fatalf("missing new status\n\n%s", out)
   133  	}
   134  }
   135  
   136  func TestMonitor_Update_SchedulingFailure(t *testing.T) {
   137  	ui := new(cli.MockUi)
   138  	mon := newMonitor(ui, nil)
   139  
   140  	// New allocs with desired status failed warns
   141  	state := &evalState{
   142  		allocs: map[string]*allocState{
   143  			"alloc2": &allocState{
   144  				id:          "alloc2",
   145  				group:       "group2",
   146  				desired:     structs.AllocDesiredStatusFailed,
   147  				desiredDesc: "something failed",
   148  				client:      structs.AllocClientStatusFailed,
   149  				clientDesc:  "client failed",
   150  				index:       1,
   151  
   152  				// Attach the full failed allocation
   153  				full: &api.Allocation{
   154  					ID:            "alloc2",
   155  					TaskGroup:     "group2",
   156  					ClientStatus:  structs.AllocClientStatusFailed,
   157  					DesiredStatus: structs.AllocDesiredStatusFailed,
   158  					Metrics: &api.AllocationMetric{
   159  						NodesEvaluated: 3,
   160  						NodesFiltered:  3,
   161  						ConstraintFiltered: map[string]int{
   162  							"$attr.kernel.name = linux": 3,
   163  						},
   164  					},
   165  				},
   166  			},
   167  		},
   168  	}
   169  	mon.update(state)
   170  
   171  	// Scheduling failure was logged
   172  	out := ui.OutputWriter.String()
   173  	if !strings.Contains(out, "group2") {
   174  		t.Fatalf("missing group\n\n%s", out)
   175  	}
   176  	if !strings.Contains(out, "Scheduling error") {
   177  		t.Fatalf("missing failure\n\n%s", out)
   178  	}
   179  	if !strings.Contains(out, "something failed") {
   180  		t.Fatalf("missing desired desc\n\n%s", out)
   181  	}
   182  	if !strings.Contains(out, "client failed") {
   183  		t.Fatalf("missing client desc\n\n%s", out)
   184  	}
   185  
   186  	// Check that the allocation details were dumped
   187  	if !strings.Contains(out, "3/3") {
   188  		t.Fatalf("missing filter stats\n\n%s", out)
   189  	}
   190  	if !strings.Contains(out, structs.AllocDesiredStatusFailed) {
   191  		t.Fatalf("missing alloc status\n\n%s", out)
   192  	}
   193  	if !strings.Contains(out, "$attr.kernel.name = linux") {
   194  		t.Fatalf("missing constraint\n\n%s", out)
   195  	}
   196  }
   197  
   198  func TestMonitor_Update_AllocModification(t *testing.T) {
   199  	ui := new(cli.MockUi)
   200  	mon := newMonitor(ui, nil)
   201  
   202  	// New allocs with a create index lower than the
   203  	// eval create index are logged as modifications
   204  	state := &evalState{
   205  		index: 2,
   206  		allocs: map[string]*allocState{
   207  			"alloc3": &allocState{
   208  				id:    "alloc3",
   209  				node:  "node1",
   210  				group: "group2",
   211  				index: 1,
   212  			},
   213  		},
   214  	}
   215  	mon.update(state)
   216  
   217  	// Modification was logged
   218  	out := ui.OutputWriter.String()
   219  	if !strings.Contains(out, "alloc3") {
   220  		t.Fatalf("missing alloc\n\n%s", out)
   221  	}
   222  	if !strings.Contains(out, "group2") {
   223  		t.Fatalf("missing group\n\n%s", out)
   224  	}
   225  	if !strings.Contains(out, "node1") {
   226  		t.Fatalf("missing node\n\n%s", out)
   227  	}
   228  	if !strings.Contains(out, "modified") {
   229  		t.Fatalf("missing modification\n\n%s", out)
   230  	}
   231  }
   232  
   233  func TestMonitor_Monitor(t *testing.T) {
   234  	srv, client, _ := testServer(t, nil)
   235  	defer srv.Stop()
   236  
   237  	// Create the monitor
   238  	ui := new(cli.MockUi)
   239  	mon := newMonitor(ui, client)
   240  
   241  	// Submit a job - this creates a new evaluation we can monitor
   242  	job := testJob("job1")
   243  	evalID, _, err := client.Jobs().Register(job, nil)
   244  	if err != nil {
   245  		t.Fatalf("err: %s", err)
   246  	}
   247  
   248  	// Start monitoring the eval
   249  	var code int
   250  	doneCh := make(chan struct{})
   251  	go func() {
   252  		defer close(doneCh)
   253  		code = mon.monitor(evalID)
   254  	}()
   255  
   256  	// Wait for completion
   257  	select {
   258  	case <-doneCh:
   259  	case <-time.After(5 * time.Second):
   260  		t.Fatalf("eval monitor took too long")
   261  	}
   262  
   263  	// Check the return code. We should get exit code 2 as there
   264  	// would be a scheduling problem on the test server (no clients).
   265  	if code != 2 {
   266  		t.Fatalf("expect exit 2, got: %d", code)
   267  	}
   268  
   269  	// Check the output
   270  	out := ui.OutputWriter.String()
   271  	if !strings.Contains(out, evalID) {
   272  		t.Fatalf("missing eval\n\n%s", out)
   273  	}
   274  	if !strings.Contains(out, "finished with status") {
   275  		t.Fatalf("missing final status\n\n%s", out)
   276  	}
   277  }
   278  
   279  func TestMonitor_DumpAllocStatus(t *testing.T) {
   280  	ui := new(cli.MockUi)
   281  
   282  	// Create an allocation and dump its status to the UI
   283  	alloc := &api.Allocation{
   284  		ID:           "alloc1",
   285  		TaskGroup:    "group1",
   286  		ClientStatus: structs.AllocClientStatusRunning,
   287  		Metrics: &api.AllocationMetric{
   288  			NodesEvaluated: 10,
   289  			NodesFiltered:  5,
   290  			NodesExhausted: 1,
   291  			DimensionExhausted: map[string]int{
   292  				"cpu": 1,
   293  			},
   294  			ConstraintFiltered: map[string]int{
   295  				"$attr.kernel.name = linux": 1,
   296  			},
   297  			ClassExhausted: map[string]int{
   298  				"web-large": 1,
   299  			},
   300  		},
   301  	}
   302  	dumpAllocStatus(ui, alloc)
   303  
   304  	// Check the output
   305  	out := ui.OutputWriter.String()
   306  	if !strings.Contains(out, "alloc1") {
   307  		t.Fatalf("missing alloc\n\n%s", out)
   308  	}
   309  	if !strings.Contains(out, structs.AllocClientStatusRunning) {
   310  		t.Fatalf("missing status\n\n%s", out)
   311  	}
   312  	if !strings.Contains(out, "5/10") {
   313  		t.Fatalf("missing filter stats\n\n%s", out)
   314  	}
   315  	if !strings.Contains(
   316  		out, `Constraint "$attr.kernel.name = linux" filtered 1 nodes`) {
   317  		t.Fatalf("missing constraint\n\n%s", out)
   318  	}
   319  	if !strings.Contains(out, "Resources exhausted on 1 nodes") {
   320  		t.Fatalf("missing resource exhaustion\n\n%s", out)
   321  	}
   322  	if !strings.Contains(out, `Class "web-large" exhausted on 1 nodes`) {
   323  		t.Fatalf("missing class exhaustion\n\n%s", out)
   324  	}
   325  	if !strings.Contains(out, `Dimension "cpu" exhausted on 1 nodes`) {
   326  		t.Fatalf("missing dimension exhaustion\n\n%s", out)
   327  	}
   328  	ui.OutputWriter.Reset()
   329  
   330  	// Dumping alloc status with no eligible nodes adds a warning
   331  	alloc.Metrics.NodesEvaluated = 0
   332  	dumpAllocStatus(ui, alloc)
   333  
   334  	// Check the output
   335  	out = ui.OutputWriter.String()
   336  	if !strings.Contains(out, "No nodes were eligible") {
   337  		t.Fatalf("missing eligibility warning\n\n%s", out)
   338  	}
   339  }