github.com/kardianos/nomad@v0.1.3-0.20151022182107-b13df73ee850/nomad/structs/funcs_test.go (about)

     1  package structs
     2  
     3  import (
     4  	"regexp"
     5  	"testing"
     6  )
     7  
     8  func TestRemoveAllocs(t *testing.T) {
     9  	l := []*Allocation{
    10  		&Allocation{ID: "foo"},
    11  		&Allocation{ID: "bar"},
    12  		&Allocation{ID: "baz"},
    13  		&Allocation{ID: "zip"},
    14  	}
    15  
    16  	out := RemoveAllocs(l, []*Allocation{l[1], l[3]})
    17  	if len(out) != 2 {
    18  		t.Fatalf("bad: %#v", out)
    19  	}
    20  	if out[0].ID != "foo" && out[1].ID != "baz" {
    21  		t.Fatalf("bad: %#v", out)
    22  	}
    23  }
    24  
    25  func TestFilterTerminalALlocs(t *testing.T) {
    26  	l := []*Allocation{
    27  		&Allocation{ID: "foo", DesiredStatus: AllocDesiredStatusRun},
    28  		&Allocation{ID: "bar", DesiredStatus: AllocDesiredStatusEvict},
    29  		&Allocation{ID: "baz", DesiredStatus: AllocDesiredStatusStop},
    30  		&Allocation{ID: "zip", DesiredStatus: AllocDesiredStatusRun},
    31  	}
    32  
    33  	out := FilterTerminalAllocs(l)
    34  	if len(out) != 2 {
    35  		t.Fatalf("bad: %#v", out)
    36  	}
    37  	if out[0].ID != "foo" && out[1].ID != "zip" {
    38  		t.Fatalf("bad: %#v", out)
    39  	}
    40  }
    41  
    42  func TestAllocsFit_PortsOvercommitted(t *testing.T) {
    43  	n := &Node{
    44  		Resources: &Resources{
    45  			Networks: []*NetworkResource{
    46  				&NetworkResource{
    47  					Device: "eth0",
    48  					CIDR:   "10.0.0.0/8",
    49  					MBits:  100,
    50  				},
    51  			},
    52  		},
    53  	}
    54  
    55  	a1 := &Allocation{
    56  		TaskResources: map[string]*Resources{
    57  			"web": &Resources{
    58  				Networks: []*NetworkResource{
    59  					&NetworkResource{
    60  						Device:        "eth0",
    61  						IP:            "10.0.0.1",
    62  						MBits:         50,
    63  						ReservedPorts: []int{8000},
    64  					},
    65  				},
    66  			},
    67  		},
    68  	}
    69  
    70  	// Should fit one allocation
    71  	fit, dim, _, err := AllocsFit(n, []*Allocation{a1}, nil)
    72  	if err != nil {
    73  		t.Fatalf("err: %v", err)
    74  	}
    75  	if !fit {
    76  		t.Fatalf("Bad: %s", dim)
    77  	}
    78  
    79  	// Should not fit second allocation
    80  	fit, _, _, err = AllocsFit(n, []*Allocation{a1, a1}, nil)
    81  	if err != nil {
    82  		t.Fatalf("err: %v", err)
    83  	}
    84  	if fit {
    85  		t.Fatalf("Bad")
    86  	}
    87  }
    88  
    89  func TestAllocsFit(t *testing.T) {
    90  	n := &Node{
    91  		Resources: &Resources{
    92  			CPU:      2000,
    93  			MemoryMB: 2048,
    94  			DiskMB:   10000,
    95  			IOPS:     100,
    96  			Networks: []*NetworkResource{
    97  				&NetworkResource{
    98  					Device: "eth0",
    99  					CIDR:   "10.0.0.0/8",
   100  					MBits:  100,
   101  				},
   102  			},
   103  		},
   104  		Reserved: &Resources{
   105  			CPU:      1000,
   106  			MemoryMB: 1024,
   107  			DiskMB:   5000,
   108  			IOPS:     50,
   109  			Networks: []*NetworkResource{
   110  				&NetworkResource{
   111  					Device:        "eth0",
   112  					IP:            "10.0.0.1",
   113  					MBits:         50,
   114  					ReservedPorts: []int{80},
   115  				},
   116  			},
   117  		},
   118  	}
   119  
   120  	a1 := &Allocation{
   121  		Resources: &Resources{
   122  			CPU:      1000,
   123  			MemoryMB: 1024,
   124  			DiskMB:   5000,
   125  			IOPS:     50,
   126  			Networks: []*NetworkResource{
   127  				&NetworkResource{
   128  					Device:        "eth0",
   129  					IP:            "10.0.0.1",
   130  					MBits:         50,
   131  					ReservedPorts: []int{8000},
   132  				},
   133  			},
   134  		},
   135  	}
   136  
   137  	// Should fit one allocation
   138  	fit, _, used, err := AllocsFit(n, []*Allocation{a1}, nil)
   139  	if err != nil {
   140  		t.Fatalf("err: %v", err)
   141  	}
   142  	if !fit {
   143  		t.Fatalf("Bad")
   144  	}
   145  
   146  	// Sanity check the used resources
   147  	if used.CPU != 2000 {
   148  		t.Fatalf("bad: %#v", used)
   149  	}
   150  	if used.MemoryMB != 2048 {
   151  		t.Fatalf("bad: %#v", used)
   152  	}
   153  
   154  	// Should not fit second allocation
   155  	fit, _, used, err = AllocsFit(n, []*Allocation{a1, a1}, nil)
   156  	if err != nil {
   157  		t.Fatalf("err: %v", err)
   158  	}
   159  	if fit {
   160  		t.Fatalf("Bad")
   161  	}
   162  
   163  	// Sanity check the used resources
   164  	if used.CPU != 3000 {
   165  		t.Fatalf("bad: %#v", used)
   166  	}
   167  	if used.MemoryMB != 3072 {
   168  		t.Fatalf("bad: %#v", used)
   169  	}
   170  
   171  }
   172  
   173  func TestScoreFit(t *testing.T) {
   174  	node := &Node{}
   175  	node.Resources = &Resources{
   176  		CPU:      4096,
   177  		MemoryMB: 8192,
   178  	}
   179  	node.Reserved = &Resources{
   180  		CPU:      2048,
   181  		MemoryMB: 4096,
   182  	}
   183  
   184  	// Test a perfect fit
   185  	util := &Resources{
   186  		CPU:      2048,
   187  		MemoryMB: 4096,
   188  	}
   189  	score := ScoreFit(node, util)
   190  	if score != 18.0 {
   191  		t.Fatalf("bad: %v", score)
   192  	}
   193  
   194  	// Test the worst fit
   195  	util = &Resources{
   196  		CPU:      0,
   197  		MemoryMB: 0,
   198  	}
   199  	score = ScoreFit(node, util)
   200  	if score != 0.0 {
   201  		t.Fatalf("bad: %v", score)
   202  	}
   203  
   204  	// Test a mid-case scenario
   205  	util = &Resources{
   206  		CPU:      1024,
   207  		MemoryMB: 2048,
   208  	}
   209  	score = ScoreFit(node, util)
   210  	if score < 10.0 || score > 16.0 {
   211  		t.Fatalf("bad: %v", score)
   212  	}
   213  }
   214  
   215  func TestGenerateUUID(t *testing.T) {
   216  	prev := GenerateUUID()
   217  	for i := 0; i < 100; i++ {
   218  		id := GenerateUUID()
   219  		if prev == id {
   220  			t.Fatalf("Should get a new ID!")
   221  		}
   222  
   223  		matched, err := regexp.MatchString(
   224  			"[\\da-f]{8}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{12}", id)
   225  		if !matched || err != nil {
   226  			t.Fatalf("expected match %s %v %s", id, matched, err)
   227  		}
   228  	}
   229  }