/vendor/github.com/hashicorp/nomad/nomad/structs/funcs_test.go

https://github.com/underarmour/libra · Go · 271 lines · 237 code · 25 blank · 9 comment · 47 complexity · bb7787afc85a6182ebf949a4f29d55fc MD5 · raw file

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