github.com/mattyr/nomad@v0.3.3-0.20160919021406-3485a065154a/api/tasks_test.go (about)

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