generic_scheduler_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package test
  15. import (
  16. "testing"
  17. "github.com/golang/mock/gomock"
  18. "github.com/stretchr/testify/assert"
  19. "yunion.io/x/onecloud/pkg/apis/compute"
  20. apisdu "yunion.io/x/onecloud/pkg/apis/scheduler"
  21. _ "yunion.io/x/onecloud/pkg/compute/guestdrivers"
  22. "yunion.io/x/onecloud/pkg/compute/models"
  23. "yunion.io/x/onecloud/pkg/scheduler/api"
  24. "yunion.io/x/onecloud/pkg/scheduler/core"
  25. )
  26. func TestGenericSchedulerSchedule(t *testing.T) {
  27. ctrl := gomock.NewController(t)
  28. defer ctrl.Finish()
  29. commonInfo := &api.SchedInfo{
  30. ScheduleInput: &apisdu.ScheduleInput{
  31. ServerConfig: apisdu.ServerConfig{
  32. ServerConfigs: &compute.ServerConfigs{
  33. PreferRegion: GlobalCloudregion.GetId(),
  34. PreferZone: GlobalZone.GetId(),
  35. PreferHost: "host01",
  36. Hypervisor: "hypervisor",
  37. ResourceType: "shared",
  38. InstanceType: "ecs.g1.c1m1",
  39. Sku: "ecs.g1.c1m1",
  40. Backup: false,
  41. Count: 1,
  42. Disks: []*compute.DiskConfig{
  43. {
  44. Backend: "local",
  45. DiskType: "sys",
  46. ImageId: "CentOS7.6",
  47. Index: 0,
  48. SizeMb: 30720,
  49. },
  50. {
  51. Backend: "local",
  52. DiskType: "data",
  53. Index: 1,
  54. SizeMb: 10240,
  55. },
  56. },
  57. Networks: []*compute.NetworkConfig{
  58. {
  59. Index: 0,
  60. Network: "network01",
  61. Domain: GlobalDoamin,
  62. },
  63. },
  64. BaremetalDiskConfigs: []*compute.BaremetalDiskConfig{
  65. {
  66. Type: "hybrid",
  67. Conf: "none",
  68. Count: 0,
  69. },
  70. },
  71. InstanceGroupIds: []string{
  72. "instancegroup01",
  73. },
  74. },
  75. Memory: 1024,
  76. Ncpu: 1,
  77. Project: GlobalProject,
  78. Domain: GlobalDoamin,
  79. },
  80. },
  81. PreferCandidates: []string{
  82. "host01",
  83. },
  84. RequiredCandidates: 1,
  85. InstanceGroupsDetail: map[string]*models.SGroup{
  86. "instancegroup01": buildInstanceGroup("instancegroup01", 1, true),
  87. },
  88. }
  89. t.Run("Base test", func(t *testing.T) {
  90. info := deepCopy(commonInfo)
  91. getterParam := sGetterParams{
  92. HostId: "host01",
  93. HostName: "host01name",
  94. Domain: "default",
  95. PublicScope: "system",
  96. Zone: buildZone("zone01", ""),
  97. CloudRegion: buildCloudregion("default", "", ""),
  98. HostType: api.HostHypervisorForKvm,
  99. Storages: []*api.CandidateStorage{buildStorage("storage01", "", 201330)},
  100. Networks: []*api.CandidateNetwork{buildNetwork("network01", "", "192.168.1.0/24")},
  101. TotalCPUCount: 8,
  102. FreeCPUCount: 8,
  103. TotalMemorySize: 10240,
  104. FreeMemorySize: 10240,
  105. FreeStorageSizeAnyType: 201330,
  106. FreeGroupCount: 1,
  107. Skus: []string{"ecs.g1.c1m1"},
  108. }
  109. netowrkNicCount := map[string]int{"nework01": 10}
  110. scheduler, err := core.NewGenericScheduler(buildScheduler(ctrl, netowrkNicCount, basePredicateNames...))
  111. if err != nil {
  112. t.Errorf("NewGenericScheduler: %s", err.Error())
  113. return
  114. }
  115. candidate := buildCandidate(ctrl, getterParam)
  116. _, err = scheduler.Schedule(preSchedule(info, []core.Candidater{candidate}, false))
  117. if err != nil {
  118. t.Errorf("genericScheduler.Schedule error: %s", err.Error())
  119. }
  120. })
  121. t.Run("Forcast schedule: no match specified network", func(t *testing.T) {
  122. info := deepCopy(commonInfo)
  123. info.PreferHost = ""
  124. info.PreferCandidates = []string{}
  125. info.InstanceGroupIds = []string{}
  126. info.InstanceGroupsDetail = make(map[string]*models.SGroup)
  127. info.Count = 2
  128. getterParam1 := sGetterParams{
  129. HostId: "host01",
  130. HostName: "host01name",
  131. Domain: "default",
  132. PublicScope: "system",
  133. Zone: buildZone("zone01", ""),
  134. CloudRegion: buildCloudregion("default", "", ""),
  135. HostType: api.HostHypervisorForKvm,
  136. Storages: []*api.CandidateStorage{buildStorage("storage01", "", 201330)},
  137. Networks: []*api.CandidateNetwork{
  138. buildNetwork("network01", "nework01name", "192.168.1.0/24"),
  139. buildNetwork("network02", "nework02name", "192.168.2.0/24"),
  140. },
  141. TotalCPUCount: 8,
  142. FreeCPUCount: 8,
  143. TotalMemorySize: 10240,
  144. FreeMemorySize: 10240,
  145. FreeStorageSizeAnyType: 201330,
  146. Skus: []string{"ecs.g1.c1m1"},
  147. }
  148. getterParam2 := getterParam1
  149. getterParam2.HostId = "host02"
  150. getterParam2.HostName = "host02name"
  151. getterParam2.Networks = []*api.CandidateNetwork{
  152. buildNetwork("network03", "nework03name", "192.168.3.0/24"),
  153. buildNetwork("network04", "nework04name", "192.168.4.0/24"),
  154. }
  155. candidates := []core.Candidater{
  156. buildCandidate(ctrl, getterParam1),
  157. buildCandidate(ctrl, getterParam2),
  158. }
  159. netowrkNicCount := map[string]int{
  160. "network01": 255,
  161. "network02": 256,
  162. "network03": 1,
  163. "network04": 1,
  164. }
  165. scheduler, err := core.NewGenericScheduler(buildScheduler(ctrl, netowrkNicCount, basePredicateNames...))
  166. if err != nil {
  167. t.Errorf("NewGenericScheduler: %s", err.Error())
  168. return
  169. }
  170. res, err := scheduler.Schedule(preSchedule(info, candidates, true))
  171. if err != nil {
  172. t.Errorf("genericScheduler.Schedule error: %s", err.Error())
  173. }
  174. forcastResult := &api.SchedForecastResult{
  175. CanCreate: false,
  176. AllowCount: 1,
  177. ReqCount: 2,
  178. NotAllowReasons: []string{"Out of resource"},
  179. FilteredCandidates: []api.FilteredCandidate{
  180. {
  181. FilterName: "host_network",
  182. ID: "host02",
  183. Name: "host02name",
  184. Reasons: []string{
  185. "nework03name(network03): id/name not matched",
  186. "nework04name(network04): id/name not matched",
  187. },
  188. },
  189. },
  190. }
  191. res.ForecastResult.Candidates = nil
  192. assert := assert.New(t)
  193. assert.Equal(forcastResult, res.ForecastResult, "ForecastResult should equal")
  194. })
  195. }