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