github.com/diptanu/nomad@v0.5.7-0.20170516172507-d72e86cbe3d9/api/tasks_test.go (about)

     1  package api
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/nomad/helper"
     8  )
     9  
    10  func TestTaskGroup_NewTaskGroup(t *testing.T) {
    11  	grp := NewTaskGroup("grp1", 2)
    12  	expect := &TaskGroup{
    13  		Name:  helper.StringToPtr("grp1"),
    14  		Count: helper.IntToPtr(2),
    15  	}
    16  	if !reflect.DeepEqual(grp, expect) {
    17  		t.Fatalf("expect: %#v, got: %#v", expect, grp)
    18  	}
    19  }
    20  
    21  func TestTaskGroup_Constrain(t *testing.T) {
    22  	grp := NewTaskGroup("grp1", 1)
    23  
    24  	// Add a constraint to the group
    25  	out := grp.Constrain(NewConstraint("kernel.name", "=", "darwin"))
    26  	if n := len(grp.Constraints); n != 1 {
    27  		t.Fatalf("expected 1 constraint, got: %d", n)
    28  	}
    29  
    30  	// Check that the group was returned
    31  	if out != grp {
    32  		t.Fatalf("expected: %#v, got: %#v", grp, out)
    33  	}
    34  
    35  	// Add a second constraint
    36  	grp.Constrain(NewConstraint("memory.totalbytes", ">=", "128000000"))
    37  	expect := []*Constraint{
    38  		&Constraint{
    39  			LTarget: "kernel.name",
    40  			RTarget: "darwin",
    41  			Operand: "=",
    42  		},
    43  		&Constraint{
    44  			LTarget: "memory.totalbytes",
    45  			RTarget: "128000000",
    46  			Operand: ">=",
    47  		},
    48  	}
    49  	if !reflect.DeepEqual(grp.Constraints, expect) {
    50  		t.Fatalf("expect: %#v, got: %#v", expect, grp.Constraints)
    51  	}
    52  }
    53  
    54  func TestTaskGroup_SetMeta(t *testing.T) {
    55  	grp := NewTaskGroup("grp1", 1)
    56  
    57  	// Initializes an empty map
    58  	out := grp.SetMeta("foo", "bar")
    59  	if grp.Meta == nil {
    60  		t.Fatalf("should be initialized")
    61  	}
    62  
    63  	// Check that we returned the group
    64  	if out != grp {
    65  		t.Fatalf("expect: %#v, got: %#v", grp, out)
    66  	}
    67  
    68  	// Add a second meta k/v
    69  	grp.SetMeta("baz", "zip")
    70  	expect := map[string]string{"foo": "bar", "baz": "zip"}
    71  	if !reflect.DeepEqual(grp.Meta, expect) {
    72  		t.Fatalf("expect: %#v, got: %#v", expect, grp.Meta)
    73  	}
    74  }
    75  
    76  func TestTaskGroup_AddTask(t *testing.T) {
    77  	grp := NewTaskGroup("grp1", 1)
    78  
    79  	// Add the task to the task group
    80  	out := grp.AddTask(NewTask("task1", "java"))
    81  	if n := len(grp.Tasks); n != 1 {
    82  		t.Fatalf("expected 1 task, got: %d", n)
    83  	}
    84  
    85  	// Check that we returned the group
    86  	if out != grp {
    87  		t.Fatalf("expect: %#v, got: %#v", grp, out)
    88  	}
    89  
    90  	// Add a second task
    91  	grp.AddTask(NewTask("task2", "exec"))
    92  	expect := []*Task{
    93  		&Task{
    94  			Name:   "task1",
    95  			Driver: "java",
    96  		},
    97  		&Task{
    98  			Name:   "task2",
    99  			Driver: "exec",
   100  		},
   101  	}
   102  	if !reflect.DeepEqual(grp.Tasks, expect) {
   103  		t.Fatalf("expect: %#v, got: %#v", expect, grp.Tasks)
   104  	}
   105  }
   106  
   107  func TestTask_NewTask(t *testing.T) {
   108  	task := NewTask("task1", "exec")
   109  	expect := &Task{
   110  		Name:   "task1",
   111  		Driver: "exec",
   112  	}
   113  	if !reflect.DeepEqual(task, expect) {
   114  		t.Fatalf("expect: %#v, got: %#v", expect, task)
   115  	}
   116  }
   117  
   118  func TestTask_SetConfig(t *testing.T) {
   119  	task := NewTask("task1", "exec")
   120  
   121  	// Initializes an empty map
   122  	out := task.SetConfig("foo", "bar")
   123  	if task.Config == nil {
   124  		t.Fatalf("should be initialized")
   125  	}
   126  
   127  	// Check that we returned the task
   128  	if out != task {
   129  		t.Fatalf("expect: %#v, got: %#v", task, out)
   130  	}
   131  
   132  	// Set another config value
   133  	task.SetConfig("baz", "zip")
   134  	expect := map[string]interface{}{"foo": "bar", "baz": "zip"}
   135  	if !reflect.DeepEqual(task.Config, expect) {
   136  		t.Fatalf("expect: %#v, got: %#v", expect, task.Config)
   137  	}
   138  }
   139  
   140  func TestTask_SetMeta(t *testing.T) {
   141  	task := NewTask("task1", "exec")
   142  
   143  	// Initializes an empty map
   144  	out := task.SetMeta("foo", "bar")
   145  	if task.Meta == nil {
   146  		t.Fatalf("should be initialized")
   147  	}
   148  
   149  	// Check that we returned the task
   150  	if out != task {
   151  		t.Fatalf("expect: %#v, got: %#v", task, out)
   152  	}
   153  
   154  	// Set another meta k/v
   155  	task.SetMeta("baz", "zip")
   156  	expect := map[string]string{"foo": "bar", "baz": "zip"}
   157  	if !reflect.DeepEqual(task.Meta, expect) {
   158  		t.Fatalf("expect: %#v, got: %#v", expect, task.Meta)
   159  	}
   160  }
   161  
   162  func TestTask_Require(t *testing.T) {
   163  	task := NewTask("task1", "exec")
   164  
   165  	// Create some require resources
   166  	resources := &Resources{
   167  		CPU:      helper.IntToPtr(1250),
   168  		MemoryMB: helper.IntToPtr(128),
   169  		DiskMB:   helper.IntToPtr(2048),
   170  		IOPS:     helper.IntToPtr(500),
   171  		Networks: []*NetworkResource{
   172  			&NetworkResource{
   173  				CIDR:          "0.0.0.0/0",
   174  				MBits:         helper.IntToPtr(100),
   175  				ReservedPorts: []Port{{"", 80}, {"", 443}},
   176  			},
   177  		},
   178  	}
   179  	out := task.Require(resources)
   180  	if !reflect.DeepEqual(task.Resources, resources) {
   181  		t.Fatalf("expect: %#v, got: %#v", resources, task.Resources)
   182  	}
   183  
   184  	// Check that we returned the task
   185  	if out != task {
   186  		t.Fatalf("expect: %#v, got: %#v", task, out)
   187  	}
   188  }
   189  
   190  func TestTask_Constrain(t *testing.T) {
   191  	task := NewTask("task1", "exec")
   192  
   193  	// Add a constraint to the task
   194  	out := task.Constrain(NewConstraint("kernel.name", "=", "darwin"))
   195  	if n := len(task.Constraints); n != 1 {
   196  		t.Fatalf("expected 1 constraint, got: %d", n)
   197  	}
   198  
   199  	// Check that the task was returned
   200  	if out != task {
   201  		t.Fatalf("expected: %#v, got: %#v", task, out)
   202  	}
   203  
   204  	// Add a second constraint
   205  	task.Constrain(NewConstraint("memory.totalbytes", ">=", "128000000"))
   206  	expect := []*Constraint{
   207  		&Constraint{
   208  			LTarget: "kernel.name",
   209  			RTarget: "darwin",
   210  			Operand: "=",
   211  		},
   212  		&Constraint{
   213  			LTarget: "memory.totalbytes",
   214  			RTarget: "128000000",
   215  			Operand: ">=",
   216  		},
   217  	}
   218  	if !reflect.DeepEqual(task.Constraints, expect) {
   219  		t.Fatalf("expect: %#v, got: %#v", expect, task.Constraints)
   220  	}
   221  }