github.com/zhizhiboom/nomad@v0.8.5-0.20180907175415-f28fd3a1a056/api/compose_test.go (about)

     1  package api
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/nomad/helper"
     8  )
     9  
    10  func TestCompose(t *testing.T) {
    11  	t.Parallel()
    12  	// Compose a task
    13  	task := NewTask("task1", "exec").
    14  		SetConfig("foo", "bar").
    15  		SetMeta("foo", "bar").
    16  		Constrain(NewConstraint("kernel.name", "=", "linux")).
    17  		Require(&Resources{
    18  			CPU:      helper.IntToPtr(1250),
    19  			MemoryMB: helper.IntToPtr(1024),
    20  			DiskMB:   helper.IntToPtr(2048),
    21  			IOPS:     helper.IntToPtr(500),
    22  			Networks: []*NetworkResource{
    23  				{
    24  					CIDR:          "0.0.0.0/0",
    25  					MBits:         helper.IntToPtr(100),
    26  					ReservedPorts: []Port{{"", 80}, {"", 443}},
    27  				},
    28  			},
    29  		})
    30  
    31  	// Compose a task group
    32  
    33  	st1 := NewSpreadTarget("dc1", 80)
    34  	st2 := NewSpreadTarget("dc2", 20)
    35  	grp := NewTaskGroup("grp1", 2).
    36  		Constrain(NewConstraint("kernel.name", "=", "linux")).
    37  		AddAffinity(NewAffinity("${node.class}", "=", "large", 50)).
    38  		AddSpread(NewSpread("${node.datacenter}", 30, []*SpreadTarget{st1, st2})).
    39  		SetMeta("foo", "bar").
    40  		AddTask(task)
    41  
    42  	// Compose a job
    43  	job := NewServiceJob("job1", "myjob", "region1", 2).
    44  		SetMeta("foo", "bar").
    45  		AddDatacenter("dc1").
    46  		Constrain(NewConstraint("kernel.name", "=", "linux")).
    47  		AddTaskGroup(grp)
    48  
    49  	// Check that the composed result looks correct
    50  	expect := &Job{
    51  		Region:   helper.StringToPtr("region1"),
    52  		ID:       helper.StringToPtr("job1"),
    53  		Name:     helper.StringToPtr("myjob"),
    54  		Type:     helper.StringToPtr(JobTypeService),
    55  		Priority: helper.IntToPtr(2),
    56  		Datacenters: []string{
    57  			"dc1",
    58  		},
    59  		Meta: map[string]string{
    60  			"foo": "bar",
    61  		},
    62  		Constraints: []*Constraint{
    63  			{
    64  				LTarget: "kernel.name",
    65  				RTarget: "linux",
    66  				Operand: "=",
    67  			},
    68  		},
    69  		TaskGroups: []*TaskGroup{
    70  			{
    71  				Name:  helper.StringToPtr("grp1"),
    72  				Count: helper.IntToPtr(2),
    73  				Constraints: []*Constraint{
    74  					{
    75  						LTarget: "kernel.name",
    76  						RTarget: "linux",
    77  						Operand: "=",
    78  					},
    79  				},
    80  				Affinities: []*Affinity{
    81  					{
    82  						LTarget: "${node.class}",
    83  						RTarget: "large",
    84  						Operand: "=",
    85  						Weight:  50,
    86  					},
    87  				},
    88  				Spreads: []*Spread{
    89  					{
    90  						Attribute: "${node.datacenter}",
    91  						Weight:    30,
    92  						SpreadTarget: []*SpreadTarget{
    93  							{
    94  								Value:   "dc1",
    95  								Percent: 80,
    96  							},
    97  							{
    98  								Value:   "dc2",
    99  								Percent: 20,
   100  							},
   101  						},
   102  					},
   103  				},
   104  				Tasks: []*Task{
   105  					{
   106  						Name:   "task1",
   107  						Driver: "exec",
   108  						Resources: &Resources{
   109  							CPU:      helper.IntToPtr(1250),
   110  							MemoryMB: helper.IntToPtr(1024),
   111  							DiskMB:   helper.IntToPtr(2048),
   112  							IOPS:     helper.IntToPtr(500),
   113  							Networks: []*NetworkResource{
   114  								{
   115  									CIDR:  "0.0.0.0/0",
   116  									MBits: helper.IntToPtr(100),
   117  									ReservedPorts: []Port{
   118  										{"", 80},
   119  										{"", 443},
   120  									},
   121  								},
   122  							},
   123  						},
   124  						Constraints: []*Constraint{
   125  							{
   126  								LTarget: "kernel.name",
   127  								RTarget: "linux",
   128  								Operand: "=",
   129  							},
   130  						},
   131  						Config: map[string]interface{}{
   132  							"foo": "bar",
   133  						},
   134  						Meta: map[string]string{
   135  							"foo": "bar",
   136  						},
   137  					},
   138  				},
   139  				Meta: map[string]string{
   140  					"foo": "bar",
   141  				},
   142  			},
   143  		},
   144  	}
   145  	if !reflect.DeepEqual(job, expect) {
   146  		t.Fatalf("expect: %#v, got: %#v", expect, job)
   147  	}
   148  }