mysql_machine_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. package dao
  2. import (
  3. "testing"
  4. "time"
  5. "go-common/app/admin/ep/merlin/model"
  6. "go-common/library/log"
  7. . "github.com/smartystreets/goconvey/convey"
  8. )
  9. var tMachine *model.Machine
  10. func machineFunc(f func()) func() {
  11. return func() {
  12. beforeAllMT()
  13. defer afterAllMT()
  14. f()
  15. }
  16. }
  17. func beforeAllMT() {
  18. var err error
  19. tMachine = &model.Machine{
  20. Name: "test-machine-1",
  21. PodName: "test-machine-0-0",
  22. Status: 100,
  23. Username: "seanan",
  24. BusinessUnit: "test",
  25. Project: "ep",
  26. App: "merlin",
  27. ClusterID: 3,
  28. NetworkID: 5,
  29. Comment: "Unit test for the dao of merlin.",
  30. DelayStatus: 0,
  31. }
  32. if err = d.db.Create(tMachine).Error; err != nil {
  33. log.Error("Failed to init machine(%v) for test, err(%v)", tMachine, err)
  34. return
  35. }
  36. if err = d.db.Create(&model.MachineNode{MachineID: tMachine.ID, BusinessUnit: tMachine.BusinessUnit, Project: tMachine.Project, App: tMachine.App, TreeID: 1234}).Error; err != nil {
  37. log.Error("Failed to init machine node for test, err(%v)", tMachine, err)
  38. return
  39. }
  40. }
  41. func afterAllMT() {
  42. var err error
  43. if err = d.db.Where("name = ?", tMachine.Name).Delete(&model.Machine{}).Error; err != nil {
  44. log.Error("Failed to delete machine(%v) for test, err(%v)", tMachine, err)
  45. return
  46. }
  47. if err = d.db.Where("machine_id = ?", tMachine.ID).Delete(&model.MachineNode{}).Error; err != nil {
  48. log.Error("Failed to delete machine node for test, err(%v)", err)
  49. return
  50. }
  51. }
  52. func TestFindExpiredMachineByDay(t *testing.T) {
  53. Convey("The actualMachines must contain tMachine when deadline day is 0", t, machineFunc(func() {
  54. var err error
  55. if err = d.db.Model(&model.Machine{}).Where("id = ?", tMachine.ID).Update("end_time", time.Now()).Error; err != nil {
  56. log.Error("Cannot update the end_time of tMachine")
  57. return
  58. }
  59. actualMachines, err := d.FindExpiredMachineByDay(0)
  60. So(err, ShouldBeNil)
  61. for _, m := range actualMachines {
  62. if m.Name == tMachine.Name {
  63. return
  64. }
  65. }
  66. So(true, ShouldBeFalse)
  67. }))
  68. Convey("The actualMachines must not contain tMachine when deadline day is not 0", t, machineFunc(func() {
  69. var err error
  70. if err = d.db.Model(&model.Machine{}).Where("id = ?", tMachine.ID).Update("end_time", time.Now()).Error; err != nil {
  71. log.Error("Cannot update the end_time of tMachine")
  72. return
  73. }
  74. actualMachines, err := d.FindExpiredMachineByDay(1)
  75. So(err, ShouldBeNil)
  76. for _, m := range actualMachines {
  77. if m.Name == tMachine.Name {
  78. So(true, ShouldBeFalse)
  79. return
  80. }
  81. }
  82. }))
  83. }
  84. func TestFindExpiredMachine(t *testing.T) {
  85. Convey("The actualMachines must contain tMachine", t, machineFunc(func() {
  86. var err error
  87. if err = d.db.Model(&model.Machine{}).Where("id = ?", tMachine.ID).Update("end_time", time.Now().AddDate(0, 0, 1)).Error; err != nil {
  88. log.Error("Cannot update the end_time of tMachine")
  89. return
  90. }
  91. actualMachines, err := d.FindExpiredMachine()
  92. So(err, ShouldBeNil)
  93. for _, m := range actualMachines {
  94. if m.Name == tMachine.Name {
  95. return
  96. }
  97. }
  98. So(true, ShouldBeFalse)
  99. }))
  100. Convey("The actualMachines must not contain tMachine", t, machineFunc(func() {
  101. actualMachines, err := d.FindExpiredMachine()
  102. So(err, ShouldBeNil)
  103. for _, m := range actualMachines {
  104. if m.Name == tMachine.Name {
  105. So(true, ShouldBeFalse)
  106. return
  107. }
  108. }
  109. }))
  110. }
  111. func TestQueryMachine(t *testing.T) {
  112. Convey("The actualMachines must be tMachine when id is the id of tMachine", t, machineFunc(func() {
  113. actualMachines, err := d.QueryMachine(tMachine.ID)
  114. So(err, ShouldBeNil)
  115. So(actualMachines.ID, ShouldEqual, tMachine.ID)
  116. }))
  117. Convey("The actualMachines must not be tMachine when id is not the id of tMachine", t, machineFunc(func() {
  118. _, err := d.QueryMachine(0)
  119. So(err, ShouldNotBeNil)
  120. }))
  121. }
  122. // TestDelMachine this test only need positive test because negative case is filtered by service
  123. func TestDelMachine(t *testing.T) {
  124. Convey("The tMachine must be delete", t, machineFunc(func() {
  125. var (
  126. actual = &model.Machine{}
  127. updateBy = "seanan"
  128. )
  129. err := d.DelMachine(tMachine.ID, updateBy)
  130. So(err, ShouldBeNil)
  131. d.db.Where("id = ?", tMachine.ID).Find(actual)
  132. So(actual.Status, ShouldEqual, model.RemovedMachineInMerlin)
  133. So(actual.UpdateBy, ShouldEqual, updateBy)
  134. }))
  135. }
  136. // TestHasMachine HasMachine is fuzzy matching name which is end with -number
  137. func TestHasMachine(t *testing.T) {
  138. Convey("return true when machine existed", t, machineFunc(func() {
  139. b, err := d.HasMachine("test-machine")
  140. So(err, ShouldBeNil)
  141. So(b, ShouldBeTrue)
  142. }))
  143. Convey("return false when machine does not exist", t, machineFunc(func() {
  144. b, err := d.HasMachine("no-test-machine")
  145. So(err, ShouldBeNil)
  146. So(b, ShouldBeFalse)
  147. }))
  148. }
  149. func TestUpdateMachineStatus(t *testing.T) {
  150. Convey("The machine status must be boot", t, machineFunc(func() {
  151. var (
  152. err error
  153. actualMachine = &model.Machine{}
  154. )
  155. if err = d.db.Model(&model.Machine{}).Where("id = ?", tMachine.ID).Update("status", model.CreatingMachineInMerlin).Error; err != nil {
  156. return
  157. }
  158. err = d.UpdateMachineStatus(tMachine.ID, model.BootMachineInMerlin)
  159. So(err, ShouldBeNil)
  160. if err = d.db.Where("id = ?", tMachine.ID).First(actualMachine).Error; err != nil {
  161. log.Error("find machine err(%v)", err)
  162. return
  163. }
  164. So(actualMachine.Status, ShouldEqual, model.BootMachineInMerlin)
  165. }))
  166. }
  167. func TestQueryMachines(t *testing.T) {
  168. Convey("Find the boot machines by names", t, machineFunc(func() {
  169. var (
  170. err error
  171. qmr = &model.QueryMachineRequest{
  172. Pagination: model.Pagination{
  173. PageSize: 5,
  174. PageNum: 1,
  175. },
  176. }
  177. names = []string{"test-machine-0-0"}
  178. actualMachines []*model.Machine
  179. )
  180. _, actualMachines, err = d.QueryMachines(names, qmr)
  181. So(err, ShouldBeNil)
  182. for _, m := range actualMachines {
  183. if m.Name == tMachine.Name {
  184. return
  185. }
  186. }
  187. So(true, ShouldBeFalse)
  188. }))
  189. Convey("Find the creating machines by requester", t, machineFunc(func() {
  190. var (
  191. err error
  192. qmr = &model.QueryMachineRequest{
  193. Pagination: model.Pagination{
  194. PageSize: 5,
  195. PageNum: 1,
  196. },
  197. Requester: "seanan",
  198. }
  199. actualMachines []*model.Machine
  200. )
  201. if err = d.db.Model(&model.Machine{}).Where("id = ?", tMachine.ID).Update("status", model.CreatingMachineInMerlin).Error; err != nil {
  202. return
  203. }
  204. _, actualMachines, err = d.QueryMachines(nil, qmr)
  205. So(err, ShouldBeNil)
  206. for _, m := range actualMachines {
  207. if m.Name == tMachine.Name {
  208. return
  209. }
  210. }
  211. So(true, ShouldBeFalse)
  212. }))
  213. Convey("Cannot find the creating machines by other requester", t, machineFunc(func() {
  214. var (
  215. err error
  216. qmr = &model.QueryMachineRequest{
  217. Pagination: model.Pagination{
  218. PageSize: 5,
  219. PageNum: 1,
  220. },
  221. Requester: "other",
  222. }
  223. actualMachines []*model.Machine
  224. )
  225. if err = d.db.Model(&model.Machine{}).Where("id = ?", tMachine.ID).Update("status", model.CreatingMachineInMerlin).Error; err != nil {
  226. return
  227. }
  228. _, actualMachines, err = d.QueryMachines(nil, qmr)
  229. So(err, ShouldBeNil)
  230. for _, m := range actualMachines {
  231. if m.Name == tMachine.Name {
  232. So(true, ShouldBeFalse)
  233. return
  234. }
  235. }
  236. }))
  237. Convey("Find the machines filter by username", t, machineFunc(func() {
  238. var (
  239. err error
  240. qmr = &model.QueryMachineRequest{
  241. Pagination: model.Pagination{
  242. PageSize: 5,
  243. PageNum: 1,
  244. },
  245. Username: "sean",
  246. }
  247. names = []string{"test-machine-0-0"}
  248. actualMachines []*model.Machine
  249. )
  250. _, actualMachines, err = d.QueryMachines(names, qmr)
  251. So(err, ShouldBeNil)
  252. for _, m := range actualMachines {
  253. if m.Name == tMachine.Name {
  254. return
  255. }
  256. }
  257. So(true, ShouldBeFalse)
  258. }))
  259. Convey("Find the machines filter by machine name", t, machineFunc(func() {
  260. var (
  261. err error
  262. qmr = &model.QueryMachineRequest{
  263. Pagination: model.Pagination{
  264. PageSize: 5,
  265. PageNum: 1,
  266. },
  267. MachineName: "test-machine",
  268. }
  269. names = []string{"test-machine-0-0"}
  270. actualMachines []*model.Machine
  271. )
  272. _, actualMachines, err = d.QueryMachines(names, qmr)
  273. So(err, ShouldBeNil)
  274. for _, m := range actualMachines {
  275. if m.Name == tMachine.Name {
  276. return
  277. }
  278. }
  279. So(true, ShouldBeFalse)
  280. }))
  281. Convey("Find the machines filter by BusinessUnit", t, machineFunc(func() {
  282. var (
  283. err error
  284. qmr = &model.QueryMachineRequest{
  285. Pagination: model.Pagination{
  286. PageSize: 5,
  287. PageNum: 1,
  288. },
  289. TreeNode: model.TreeNode{
  290. BusinessUnit: "test",
  291. },
  292. }
  293. names = []string{"test-machine-0-0"}
  294. actualMachines []*model.Machine
  295. )
  296. _, actualMachines, err = d.QueryMachines(names, qmr)
  297. So(err, ShouldBeNil)
  298. for _, m := range actualMachines {
  299. if m.Name == tMachine.Name {
  300. return
  301. }
  302. }
  303. So(true, ShouldBeFalse)
  304. }))
  305. Convey("Find the machines filter by BusinessUnit and Project", t, machineFunc(func() {
  306. var (
  307. err error
  308. qmr = &model.QueryMachineRequest{
  309. Pagination: model.Pagination{
  310. PageSize: 5,
  311. PageNum: 1,
  312. },
  313. TreeNode: model.TreeNode{
  314. BusinessUnit: "test",
  315. Project: "ep",
  316. },
  317. }
  318. names = []string{"test-machine-0-0"}
  319. actualMachines []*model.Machine
  320. )
  321. _, actualMachines, err = d.QueryMachines(names, qmr)
  322. So(err, ShouldBeNil)
  323. for _, m := range actualMachines {
  324. if m.Name == tMachine.Name {
  325. return
  326. }
  327. }
  328. So(true, ShouldBeFalse)
  329. }))
  330. Convey("Find the machines filter by BusinessUnit, Project and App", t, machineFunc(func() {
  331. var (
  332. err error
  333. qmr = &model.QueryMachineRequest{
  334. Pagination: model.Pagination{
  335. PageSize: 5,
  336. PageNum: 1,
  337. },
  338. TreeNode: model.TreeNode{
  339. BusinessUnit: "test",
  340. Project: "ep",
  341. App: "merlin",
  342. },
  343. }
  344. names = []string{"test-machine-0-0"}
  345. actualMachines []*model.Machine
  346. )
  347. _, actualMachines, err = d.QueryMachines(names, qmr)
  348. So(err, ShouldBeNil)
  349. for _, m := range actualMachines {
  350. if m.Name == tMachine.Name {
  351. return
  352. }
  353. }
  354. So(true, ShouldBeFalse)
  355. }))
  356. }
  357. func TestUpdateMachineEndTime(t *testing.T) {
  358. Convey("The EndTime and DelayStatus should be updated", t, machineFunc(func() {
  359. var (
  360. actualMachine = &model.Machine{}
  361. expectTime = time.Now().AddDate(0, 1, 0)
  362. expectDelayStatus = 1
  363. err error
  364. )
  365. err = d.UpdateMachineEndTime(tMachine.ID, expectDelayStatus, expectTime)
  366. So(err, ShouldBeNil)
  367. if err = d.db.Where("id = ?", tMachine.ID).First(actualMachine).Error; err != nil {
  368. log.Error("find machine err(%v)", err)
  369. return
  370. }
  371. So(actualMachine.EndTime.Format("2006-01-02 15:04:05"), ShouldEqual, expectTime.Format("2006-01-02 15:04:05"))
  372. So(actualMachine.DelayStatus, ShouldEqual, expectDelayStatus)
  373. }))
  374. }
  375. func TestUpdateMachineDelayStatus(t *testing.T) {
  376. Convey("The DelayStatus should be updated", t, machineFunc(func() {
  377. var (
  378. actualMachine = &model.Machine{}
  379. expectDelayStatus = 1
  380. err error
  381. )
  382. err = d.UpdateMachineDelayStatus(tMachine.ID, expectDelayStatus)
  383. So(err, ShouldBeNil)
  384. if err = d.db.Where("id = ?", tMachine.ID).First(actualMachine).Error; err != nil {
  385. log.Error("find machine err(%v)", err)
  386. return
  387. }
  388. So(actualMachine.DelayStatus, ShouldEqual, expectDelayStatus)
  389. }))
  390. }
  391. func TestQueryPathAndPodNamesMapping(t *testing.T) {
  392. Convey("Find path and podNames mapping when the machine exceeds 20 minutes", t, machineFunc(func() {
  393. var (
  394. pathAndPodNames map[string][]string
  395. err error
  396. )
  397. if err = d.db.Model(&model.Machine{}).Where("id = ?", tMachine.ID).Update("status", model.CreatingMachineInMerlin).Update("ctime", time.Now().AddDate(0, 0, -1)).Error; err != nil {
  398. return
  399. }
  400. pathAndPodNames, err = d.QueryPathAndPodNamesMapping()
  401. So(err, ShouldBeNil)
  402. expectPath := tMachine.ToTreeNode().TreePath()
  403. for _, pn := range pathAndPodNames[expectPath] {
  404. if pn == tMachine.PodName {
  405. return
  406. }
  407. }
  408. So(true, ShouldBeFalse)
  409. }))
  410. Convey("cannot find path and podNames mapping when the machine does not exceed 20 minutes", t, machineFunc(func() {
  411. var (
  412. pathAndPodNames map[string][]string
  413. err error
  414. )
  415. if err = d.db.Model(&model.Machine{}).Where("id = ?", tMachine.ID).Update("status", model.CreatingMachineInMerlin).Error; err != nil {
  416. return
  417. }
  418. pathAndPodNames, err = d.QueryPathAndPodNamesMapping()
  419. So(err, ShouldBeNil)
  420. expectPath := tMachine.ToTreeNode().TreePath()
  421. for _, pn := range pathAndPodNames[expectPath] {
  422. if pn == tMachine.PodName {
  423. So(true, ShouldBeFalse)
  424. return
  425. }
  426. }
  427. }))
  428. }
  429. func TestUpdateMachineStatusByPodNames(t *testing.T) {
  430. Convey("cannot find path and podNames mapping when the machine does not exceed 20 minutes", t, machineFunc(func() {
  431. var (
  432. err error
  433. actualMachine *model.Machine
  434. expectStatus = model.BootMachineInMerlin
  435. )
  436. if err = d.db.Model(&model.Machine{}).Where("id = ?", tMachine.ID).Update("status", model.CreatingMachineInMerlin).Error; err != nil {
  437. return
  438. }
  439. err = d.UpdateMachineStatusByPodNames([]string{tMachine.PodName}, expectStatus)
  440. So(err, ShouldBeNil)
  441. if err = d.db.Where("id = ?", tMachine.ID).First(actualMachine).Error; err != nil {
  442. log.Error("find machine err(%v)", err)
  443. return
  444. }
  445. So(actualMachine.Status, ShouldEqual, expectStatus)
  446. }))
  447. }
  448. func TestQueryMachinesByPodNames(t *testing.T) {
  449. Convey("cannot find path and podNames mapping when the machine does not exceed 20 minutes", t, machineFunc(func() {
  450. var (
  451. err error
  452. actualMachines []*model.Machine
  453. )
  454. actualMachines, err = d.QueryMachinesByPodNames([]string{tMachine.PodName})
  455. So(err, ShouldBeNil)
  456. for _, m := range actualMachines {
  457. if m.ID == tMachine.ID {
  458. return
  459. }
  460. }
  461. So(true, ShouldBeFalse)
  462. }))
  463. }
  464. func TestInsertMachines(t *testing.T) {
  465. Convey("Insert 2 machines", t, machineFunc(func() {
  466. var (
  467. testMachine2 = "test-machine-2"
  468. testMachine3 = "test-machine-3"
  469. ins = []*model.CreateInstance{
  470. {
  471. Instance: model.Instance{
  472. InstanceName: testMachine2,
  473. },
  474. InstanceCreateStatus: model.CreatingMachineInPass,
  475. }, {
  476. Instance: model.Instance{
  477. InstanceName: testMachine3,
  478. },
  479. InstanceCreateStatus: model.CreateFailedMachineInPaas,
  480. },
  481. }
  482. u = "seanan"
  483. gmr = &model.GenMachinesRequest{
  484. Env: model.Env{
  485. ClusterID: 1,
  486. NetworkID: 1,
  487. },
  488. PaasMachine: model.PaasMachine{
  489. Name: "test-machine",
  490. Image: "test-Image",
  491. CPURequest: 1000,
  492. MemoryRequest: 1024,
  493. DiskRequest: 20,
  494. VolumnMount: "/data",
  495. },
  496. Nodes: []*model.Node{
  497. {
  498. BusinessUnit: "test",
  499. Project: "ep",
  500. App: "merlin",
  501. TreeID: 1234,
  502. },
  503. },
  504. Comment: "test",
  505. Amount: 1,
  506. }
  507. err error
  508. actualMachine []*model.Machine
  509. flag = 0
  510. )
  511. err = d.InsertMachines(u, gmr, ins)
  512. defer func() {
  513. if err = d.db.Where("name IN (?)", []string{testMachine2, testMachine3}).Delete(&model.Machine{}).Error; err != nil {
  514. log.Error("Failed to delete machine(%v) for test, err(%v)", tMachine, err)
  515. return
  516. }
  517. }()
  518. So(err, ShouldBeNil)
  519. if err = d.db.Where("name IN (?)", []string{testMachine2, testMachine3}).Find(&actualMachine).Error; err != nil {
  520. log.Error("Find machiens err(%v)", err)
  521. return
  522. }
  523. for _, m := range actualMachine {
  524. if m.Name == testMachine2 && m.Status == model.CreatingMachineInMerlin {
  525. flag++
  526. }
  527. if m.Name == testMachine3 && m.Status == model.ImmediatelyFailedMachineInMerlin {
  528. flag += 2
  529. }
  530. }
  531. So(flag, ShouldEqual, 3)
  532. }))
  533. }