paas_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package dao
  2. import (
  3. "testing"
  4. "go-common/app/admin/ep/merlin/model"
  5. . "github.com/smartystreets/goconvey/convey"
  6. )
  7. var (
  8. gmRequest = model.PaasGenMachineRequest{
  9. Env: model.Env{
  10. ClusterID: 3,
  11. NetworkID: 7,
  12. },
  13. Machines: []model.PaasMachine{
  14. {
  15. Name: "vm1",
  16. Image: "docker-reg.bilibili.co/zccdebian:1.0",
  17. CPURequest: 1000,
  18. MemoryRequest: 500,
  19. DiskRequest: 20,
  20. VolumnMount: "",
  21. },
  22. {
  23. Name: "vm2",
  24. Image: "docker-reg.bilibili.co/debian:latest",
  25. CPURequest: 1000,
  26. MemoryRequest: 500,
  27. DiskRequest: 20,
  28. VolumnMount: "",
  29. },
  30. },
  31. }
  32. qdmRequest = model.PaasQueryAndDelMachineRequest{
  33. BusinessUnit: "ops",
  34. Project: "zcc",
  35. App: "zcc",
  36. ClusterID: 3,
  37. Name: "vm1",
  38. }
  39. )
  40. func Test_GenPaasMachines(t *testing.T) {
  41. Convey("create two machines in paas", t, WithPaasToken(func() {
  42. data := `{"status": 200,"message":"success","data":[{"instance_name": "vm1","instance_create_status": 2},{"instance_name": "vm2","instance_create_status": 1}]}`
  43. gmURL := d.c.Paas.Host + _genPaasMachines
  44. httpMock("POST", gmURL).Reply(200).JSON(data)
  45. res, err := d.GenPaasMachines(ctx(), &gmRequest)
  46. So(err, ShouldBeNil)
  47. So(res, ShouldResemble, []*model.CreateInstance{
  48. {
  49. Instance: model.Instance{InstanceName: "vm1"},
  50. InstanceCreateStatus: 2,
  51. },
  52. {
  53. Instance: model.Instance{InstanceName: "vm2"},
  54. InstanceCreateStatus: 1,
  55. },
  56. })
  57. }))
  58. }
  59. func Test_DelPaasMachine(t *testing.T) {
  60. Convey("Delete machine from paas", t, WithPaasToken(func() {
  61. data := `{"status": 200,"message": "success","data": {"instance_name": "vm1","instance_release_status": 1}}`
  62. dmURL := d.c.Paas.Host + _delPaasMachine
  63. httpMock("POST", dmURL).Reply(200).JSON(data)
  64. res, err := d.DelPaasMachine(ctx(), &qdmRequest)
  65. So(err, ShouldBeNil)
  66. So(res, ShouldResemble, &model.ReleaseInstance{
  67. Instance: model.Instance{InstanceName: "vm1"},
  68. InstanceReleaseStatus: 1,
  69. })
  70. }))
  71. }
  72. func Test_QueryPaasMachineStatus(t *testing.T) {
  73. Convey("query machine status", t, WithPaasToken(func() {
  74. data := `{"status": 200,"message": "success","data": {"condition": "Running","message": "","detail_conditions": {"Initialized": "True","PodScheduled": "True","Ready": "True"},"instance_ip": "172.16.62.84","restart_count": 0}}`
  75. qmsURL := d.c.Paas.Host + _queryPaasMachineStatus
  76. httpMock("POST", qmsURL).Reply(200).JSON(data)
  77. res, err := d.QueryPaasMachineStatus(ctx(), &qdmRequest)
  78. So(err, ShouldBeNil)
  79. So(res, ShouldResemble, &model.MachineStatus{
  80. Condition: "Running",
  81. Message: "",
  82. DetailCondition: model.DetailCondition{
  83. Initialized: "True",
  84. PodScheduled: "True",
  85. Ready: "True",
  86. },
  87. InstanceIP: "172.16.62.84",
  88. RestartCount: 0,
  89. })
  90. }))
  91. }
  92. func Test_QueryPaasMachine(t *testing.T) {
  93. Convey("query machine details", t, WithPaasToken(func() {
  94. data := `{"status": 200,"message": "success","data": {"condition": "Running","name": "vm1","image": "docker-reg.bilibili.co/zccdebian:1.0","cpu_request": 1000,"memory_request": 512,"disk_request": 0,"volumn_mount": "","cluster_name": "shyp-fat-k8s","env": "fat1","ip": "172.16.62.84"}}`
  95. qmdURL := d.c.Paas.Host + _queryPaasMachine
  96. httpMock("POST", qmdURL).Reply(200).JSON(data)
  97. res, err := d.QueryPaasMachine(ctx(), &qdmRequest)
  98. So(err, ShouldBeNil)
  99. So(res, ShouldResemble, &model.PaasMachineDetail{
  100. Condition: "Running",
  101. Name: "vm1",
  102. Image: "docker-reg.bilibili.co/zccdebian:1.0",
  103. CPURequest: 1000,
  104. MemoryRequest: 512,
  105. DiskRequest: 0,
  106. VolumnMount: "",
  107. ClusterName: "shyp-fat-k8s",
  108. Env: "fat1",
  109. IP: "172.16.62.84",
  110. })
  111. }))
  112. }
  113. func Test_QueryClusters(t *testing.T) {
  114. Convey("query clusters from paas", t, WithPaasToken(func() {
  115. data := `{"status": 200,"message": "success","count": 4,"data": {"items": [{"id": 1,"name": "shylf-uat-k8s","desc": "上海云立集成环境k8s集群","idc": "shylf","networks": [{"id": 16,"name": "shylf_uat_vlan1035","subnet": "172.22.35.0/24","capacity": 0.3346774193548387}],"resources": {"cpu_usage": 0.5977777777777777,"mem_usage": 0.19947637451689315,"pod_total": 439,"pod_capacity": 990,"nodes_num": 9}}]}}`
  116. qcURL := d.c.Paas.Host + _queryPaasClusters
  117. httpMock("GET", qcURL).Reply(200).JSON(data)
  118. res, err := d.QueryClusters(ctx())
  119. So(err, ShouldBeNil)
  120. So(res, ShouldResemble, []*model.Cluster{
  121. {
  122. ID: 1,
  123. Name: "shylf-uat-k8s",
  124. Desc: "上海云立集成环境k8s集群",
  125. IDc: "shylf",
  126. Networks: []model.Network{
  127. {
  128. ID: 16,
  129. Name: "shylf_uat_vlan1035",
  130. Subnet: "172.22.35.0/24",
  131. Capacity: 0.3346774193548387,
  132. },
  133. },
  134. Resources: model.Resource{
  135. CPUUsage: 0.5977777777777777,
  136. MemUsage: 0.19947637451689315,
  137. PodTotal: 439,
  138. PodCapacity: 990,
  139. NodesNum: 9,
  140. },
  141. },
  142. })
  143. }))
  144. }