scale_test.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package kubectl
  14. import (
  15. "fmt"
  16. "testing"
  17. "time"
  18. autoscalingv1 "k8s.io/api/autoscaling/v1"
  19. kerrors "k8s.io/apimachinery/pkg/api/errors"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. api "k8s.io/apimachinery/pkg/apis/testapigroup/v1"
  22. "k8s.io/apimachinery/pkg/runtime"
  23. "k8s.io/apimachinery/pkg/runtime/schema"
  24. "k8s.io/client-go/scale"
  25. fakescale "k8s.io/client-go/scale/fake"
  26. testcore "k8s.io/client-go/testing"
  27. )
  28. func TestReplicationControllerScaleRetry(t *testing.T) {
  29. verbsOnError := map[string]*kerrors.StatusError{
  30. "update": kerrors.NewConflict(api.Resource("Status"), "foo", nil),
  31. }
  32. scaleClientExpectedAction := []string{"get", "update", "get"}
  33. scaleClient := createFakeScaleClient("replicationcontrollers", "foo-v1", 2, verbsOnError)
  34. scaler := NewScaler(scaleClient)
  35. preconditions := ScalePrecondition{-1, ""}
  36. count := uint(3)
  37. name := "foo-v1"
  38. namespace := metav1.NamespaceDefault
  39. scaleFunc := ScaleCondition(scaler, &preconditions, namespace, name, count, nil, schema.GroupResource{Group: "", Resource: "replicationcontrollers"})
  40. pass, err := scaleFunc()
  41. if pass {
  42. t.Errorf("Expected an update failure to return pass = false, got pass = %v", pass)
  43. }
  44. if err != nil {
  45. t.Errorf("Did not expect an error on update conflict failure, got %v", err)
  46. }
  47. preconditions = ScalePrecondition{3, ""}
  48. scaleFunc = ScaleCondition(scaler, &preconditions, namespace, name, count, nil, schema.GroupResource{Group: "", Resource: "replicationcontrollers"})
  49. pass, err = scaleFunc()
  50. if err == nil {
  51. t.Errorf("Expected error on precondition failure")
  52. }
  53. actions := scaleClient.Actions()
  54. if len(actions) != len(scaleClientExpectedAction) {
  55. t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))
  56. }
  57. for i, verb := range scaleClientExpectedAction {
  58. if actions[i].GetVerb() != verb {
  59. t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)
  60. }
  61. }
  62. }
  63. func TestReplicationControllerScaleInvalid(t *testing.T) {
  64. verbsOnError := map[string]*kerrors.StatusError{
  65. "update": kerrors.NewInvalid(api.Kind("Status"), "foo", nil),
  66. }
  67. scaleClientExpectedAction := []string{"get", "update"}
  68. scaleClient := createFakeScaleClient("replicationcontrollers", "foo-v1", 1, verbsOnError)
  69. scaler := NewScaler(scaleClient)
  70. preconditions := ScalePrecondition{-1, ""}
  71. count := uint(3)
  72. name := "foo-v1"
  73. namespace := "default"
  74. scaleFunc := ScaleCondition(scaler, &preconditions, namespace, name, count, nil, schema.GroupResource{Group: "", Resource: "replicationcontrollers"})
  75. pass, err := scaleFunc()
  76. if pass {
  77. t.Errorf("Expected an update failure to return pass = false, got pass = %v", pass)
  78. }
  79. if err == nil {
  80. t.Errorf("Expected error on invalid update failure, got %v", err)
  81. }
  82. actions := scaleClient.Actions()
  83. if len(actions) != len(scaleClientExpectedAction) {
  84. t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))
  85. }
  86. for i, verb := range scaleClientExpectedAction {
  87. if actions[i].GetVerb() != verb {
  88. t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)
  89. }
  90. }
  91. }
  92. func TestReplicationControllerScale(t *testing.T) {
  93. scaleClientExpectedAction := []string{"get", "update"}
  94. scaleClient := createFakeScaleClient("replicationcontrollers", "foo-v1", 2, nil)
  95. scaler := NewScaler(scaleClient)
  96. preconditions := ScalePrecondition{-1, ""}
  97. count := uint(3)
  98. name := "foo-v1"
  99. err := scaler.Scale("default", name, count, &preconditions, nil, nil, schema.GroupResource{Group: "", Resource: "replicationcontrollers"})
  100. if err != nil {
  101. t.Fatalf("unexpected error occurred = %v while scaling the resource", err)
  102. }
  103. actions := scaleClient.Actions()
  104. if len(actions) != len(scaleClientExpectedAction) {
  105. t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))
  106. }
  107. for i, verb := range scaleClientExpectedAction {
  108. if actions[i].GetVerb() != verb {
  109. t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)
  110. }
  111. }
  112. }
  113. func TestReplicationControllerScaleFailsPreconditions(t *testing.T) {
  114. scaleClientExpectedAction := []string{"get"}
  115. scaleClient := createFakeScaleClient("replicationcontrollers", "foo", 10, nil)
  116. scaler := NewScaler(scaleClient)
  117. preconditions := ScalePrecondition{2, ""}
  118. count := uint(3)
  119. name := "foo"
  120. err := scaler.Scale("default", name, count, &preconditions, nil, nil, schema.GroupResource{Group: "", Resource: "replicationcontrollers"})
  121. if err == nil {
  122. t.Fatal("expected to get an error but none was returned")
  123. }
  124. actions := scaleClient.Actions()
  125. if len(actions) != len(scaleClientExpectedAction) {
  126. t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))
  127. }
  128. for i, verb := range scaleClientExpectedAction {
  129. if actions[i].GetVerb() != verb {
  130. t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)
  131. }
  132. }
  133. }
  134. func TestDeploymentScaleRetry(t *testing.T) {
  135. verbsOnError := map[string]*kerrors.StatusError{
  136. "update": kerrors.NewConflict(api.Resource("Status"), "foo", nil),
  137. }
  138. scaleClientExpectedAction := []string{"get", "update", "get"}
  139. scaleClient := createFakeScaleClient("deployments", "foo", 2, verbsOnError)
  140. scaler := NewScaler(scaleClient)
  141. preconditions := &ScalePrecondition{-1, ""}
  142. count := uint(3)
  143. name := "foo"
  144. namespace := "default"
  145. scaleFunc := ScaleCondition(scaler, preconditions, namespace, name, count, nil, schema.GroupResource{Group: "apps", Resource: "deployments"})
  146. pass, err := scaleFunc()
  147. if pass != false {
  148. t.Errorf("Expected an update failure to return pass = false, got pass = %v", pass)
  149. }
  150. if err != nil {
  151. t.Errorf("Did not expect an error on update failure, got %v", err)
  152. }
  153. preconditions = &ScalePrecondition{3, ""}
  154. scaleFunc = ScaleCondition(scaler, preconditions, namespace, name, count, nil, schema.GroupResource{Group: "apps", Resource: "deployments"})
  155. pass, err = scaleFunc()
  156. if err == nil {
  157. t.Error("Expected error on precondition failure")
  158. }
  159. actions := scaleClient.Actions()
  160. if len(actions) != len(scaleClientExpectedAction) {
  161. t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))
  162. }
  163. for i, verb := range scaleClientExpectedAction {
  164. if actions[i].GetVerb() != verb {
  165. t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)
  166. }
  167. }
  168. }
  169. func TestDeploymentScale(t *testing.T) {
  170. scaleClientExpectedAction := []string{"get", "update"}
  171. scaleClient := createFakeScaleClient("deployments", "foo", 2, nil)
  172. scaler := NewScaler(scaleClient)
  173. preconditions := ScalePrecondition{-1, ""}
  174. count := uint(3)
  175. name := "foo"
  176. err := scaler.Scale("default", name, count, &preconditions, nil, nil, schema.GroupResource{Group: "apps", Resource: "deployments"})
  177. if err != nil {
  178. t.Fatal(err)
  179. }
  180. actions := scaleClient.Actions()
  181. if len(actions) != len(scaleClientExpectedAction) {
  182. t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))
  183. }
  184. for i, verb := range scaleClientExpectedAction {
  185. if actions[i].GetVerb() != verb {
  186. t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)
  187. }
  188. }
  189. }
  190. func TestDeploymentScaleInvalid(t *testing.T) {
  191. scaleClientExpectedAction := []string{"get", "update"}
  192. verbsOnError := map[string]*kerrors.StatusError{
  193. "update": kerrors.NewInvalid(api.Kind("Status"), "foo", nil),
  194. }
  195. scaleClient := createFakeScaleClient("deployments", "foo", 2, verbsOnError)
  196. scaler := NewScaler(scaleClient)
  197. preconditions := ScalePrecondition{-1, ""}
  198. count := uint(3)
  199. name := "foo"
  200. namespace := "default"
  201. scaleFunc := ScaleCondition(scaler, &preconditions, namespace, name, count, nil, schema.GroupResource{Group: "apps", Resource: "deployments"})
  202. pass, err := scaleFunc()
  203. if pass {
  204. t.Errorf("Expected an update failure to return pass = false, got pass = %v", pass)
  205. }
  206. if err == nil {
  207. t.Errorf("Expected error on invalid update failure, got %v", err)
  208. }
  209. actions := scaleClient.Actions()
  210. if len(actions) != len(scaleClientExpectedAction) {
  211. t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))
  212. }
  213. for i, verb := range scaleClientExpectedAction {
  214. if actions[i].GetVerb() != verb {
  215. t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)
  216. }
  217. }
  218. }
  219. func TestDeploymentScaleFailsPreconditions(t *testing.T) {
  220. scaleClientExpectedAction := []string{"get"}
  221. scaleClient := createFakeScaleClient("deployments", "foo", 10, nil)
  222. scaler := NewScaler(scaleClient)
  223. preconditions := ScalePrecondition{2, ""}
  224. count := uint(3)
  225. name := "foo"
  226. err := scaler.Scale("default", name, count, &preconditions, nil, nil, schema.GroupResource{Group: "apps", Resource: "deployments"})
  227. if err == nil {
  228. t.Fatal("exptected to get an error but none was returned")
  229. }
  230. actions := scaleClient.Actions()
  231. if len(actions) != len(scaleClientExpectedAction) {
  232. t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))
  233. }
  234. for i, verb := range scaleClientExpectedAction {
  235. if actions[i].GetVerb() != verb {
  236. t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)
  237. }
  238. }
  239. }
  240. func TestStatefulSetScale(t *testing.T) {
  241. scaleClientExpectedAction := []string{"get", "update"}
  242. scaleClient := createFakeScaleClient("statefulsets", "foo", 2, nil)
  243. scaler := NewScaler(scaleClient)
  244. preconditions := ScalePrecondition{-1, ""}
  245. count := uint(3)
  246. name := "foo"
  247. err := scaler.Scale("default", name, count, &preconditions, nil, nil, schema.GroupResource{Group: "apps", Resource: "statefulset"})
  248. if err != nil {
  249. t.Fatal(err)
  250. }
  251. actions := scaleClient.Actions()
  252. if len(actions) != len(scaleClientExpectedAction) {
  253. t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))
  254. }
  255. for i, verb := range scaleClientExpectedAction {
  256. if actions[i].GetVerb() != verb {
  257. t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)
  258. }
  259. }
  260. }
  261. func TestStatefulSetScaleRetry(t *testing.T) {
  262. scaleClientExpectedAction := []string{"get", "update", "get"}
  263. verbsOnError := map[string]*kerrors.StatusError{
  264. "update": kerrors.NewConflict(api.Resource("Status"), "foo", nil),
  265. }
  266. scaleClient := createFakeScaleClient("statefulsets", "foo", 2, verbsOnError)
  267. scaler := NewScaler(scaleClient)
  268. preconditions := &ScalePrecondition{-1, ""}
  269. count := uint(3)
  270. name := "foo"
  271. namespace := "default"
  272. scaleFunc := ScaleCondition(scaler, preconditions, namespace, name, count, nil, schema.GroupResource{Group: "apps", Resource: "statefulsets"})
  273. pass, err := scaleFunc()
  274. if pass != false {
  275. t.Errorf("Expected an update failure to return pass = false, got pass = %v", pass)
  276. }
  277. if err != nil {
  278. t.Errorf("Did not expect an error on update failure, got %v", err)
  279. }
  280. preconditions = &ScalePrecondition{3, ""}
  281. scaleFunc = ScaleCondition(scaler, preconditions, namespace, name, count, nil, schema.GroupResource{Group: "apps", Resource: "statefulsets"})
  282. pass, err = scaleFunc()
  283. if err == nil {
  284. t.Error("Expected error on precondition failure")
  285. }
  286. actions := scaleClient.Actions()
  287. if len(actions) != len(scaleClientExpectedAction) {
  288. t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))
  289. }
  290. for i, verb := range scaleClientExpectedAction {
  291. if actions[i].GetVerb() != verb {
  292. t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)
  293. }
  294. }
  295. }
  296. func TestStatefulSetScaleInvalid(t *testing.T) {
  297. scaleClientExpectedAction := []string{"get", "update"}
  298. verbsOnError := map[string]*kerrors.StatusError{
  299. "update": kerrors.NewInvalid(api.Kind("Status"), "foo", nil),
  300. }
  301. scaleClient := createFakeScaleClient("statefulsets", "foo", 2, verbsOnError)
  302. scaler := NewScaler(scaleClient)
  303. preconditions := ScalePrecondition{-1, ""}
  304. count := uint(3)
  305. name := "foo"
  306. namespace := "default"
  307. scaleFunc := ScaleCondition(scaler, &preconditions, namespace, name, count, nil, schema.GroupResource{Group: "apps", Resource: "statefulsets"})
  308. pass, err := scaleFunc()
  309. if pass {
  310. t.Errorf("Expected an update failure to return pass = false, got pass = %v", pass)
  311. }
  312. if err == nil {
  313. t.Errorf("Expected error on invalid update failure, got %v", err)
  314. }
  315. actions := scaleClient.Actions()
  316. if len(actions) != len(scaleClientExpectedAction) {
  317. t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))
  318. }
  319. for i, verb := range scaleClientExpectedAction {
  320. if actions[i].GetVerb() != verb {
  321. t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)
  322. }
  323. }
  324. }
  325. func TestStatefulSetScaleFailsPreconditions(t *testing.T) {
  326. scaleClientExpectedAction := []string{"get"}
  327. scaleClient := createFakeScaleClient("statefulsets", "foo", 10, nil)
  328. scaler := NewScaler(scaleClient)
  329. preconditions := ScalePrecondition{2, ""}
  330. count := uint(3)
  331. name := "foo"
  332. err := scaler.Scale("default", name, count, &preconditions, nil, nil, schema.GroupResource{Group: "apps", Resource: "statefulsets"})
  333. if err == nil {
  334. t.Fatal("expected to get an error but none was returned")
  335. }
  336. actions := scaleClient.Actions()
  337. if len(actions) != len(scaleClientExpectedAction) {
  338. t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))
  339. }
  340. for i, verb := range scaleClientExpectedAction {
  341. if actions[i].GetVerb() != verb {
  342. t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)
  343. }
  344. }
  345. }
  346. func TestReplicaSetScale(t *testing.T) {
  347. scaleClientExpectedAction := []string{"get", "update"}
  348. scaleClient := createFakeScaleClient("replicasets", "foo", 10, nil)
  349. scaler := NewScaler(scaleClient)
  350. preconditions := ScalePrecondition{-1, ""}
  351. count := uint(3)
  352. name := "foo"
  353. err := scaler.Scale("default", name, count, &preconditions, nil, nil, schema.GroupResource{Group: "extensions", Resource: "replicasets"})
  354. if err != nil {
  355. t.Fatal(err)
  356. }
  357. actions := scaleClient.Actions()
  358. if len(actions) != len(scaleClientExpectedAction) {
  359. t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))
  360. }
  361. for i, verb := range scaleClientExpectedAction {
  362. if actions[i].GetVerb() != verb {
  363. t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)
  364. }
  365. }
  366. }
  367. func TestReplicaSetScaleRetry(t *testing.T) {
  368. verbsOnError := map[string]*kerrors.StatusError{
  369. "update": kerrors.NewConflict(api.Resource("Status"), "foo", nil),
  370. }
  371. scaleClientExpectedAction := []string{"get", "update", "get"}
  372. scaleClient := createFakeScaleClient("replicasets", "foo", 2, verbsOnError)
  373. scaler := NewScaler(scaleClient)
  374. preconditions := &ScalePrecondition{-1, ""}
  375. count := uint(3)
  376. name := "foo"
  377. namespace := "default"
  378. scaleFunc := ScaleCondition(scaler, preconditions, namespace, name, count, nil, schema.GroupResource{Group: "extensions", Resource: "replicasets"})
  379. pass, err := scaleFunc()
  380. if pass != false {
  381. t.Errorf("Expected an update failure to return pass = false, got pass = %v", pass)
  382. }
  383. if err != nil {
  384. t.Errorf("Did not expect an error on update failure, got %v", err)
  385. }
  386. preconditions = &ScalePrecondition{3, ""}
  387. scaleFunc = ScaleCondition(scaler, preconditions, namespace, name, count, nil, schema.GroupResource{Group: "extensions", Resource: "replicasets"})
  388. pass, err = scaleFunc()
  389. if err == nil {
  390. t.Error("Expected error on precondition failure")
  391. }
  392. actions := scaleClient.Actions()
  393. if len(actions) != len(scaleClientExpectedAction) {
  394. t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))
  395. }
  396. for i, verb := range scaleClientExpectedAction {
  397. if actions[i].GetVerb() != verb {
  398. t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)
  399. }
  400. }
  401. }
  402. func TestReplicaSetScaleInvalid(t *testing.T) {
  403. verbsOnError := map[string]*kerrors.StatusError{
  404. "update": kerrors.NewInvalid(api.Kind("Status"), "foo", nil),
  405. }
  406. scaleClientExpectedAction := []string{"get", "update"}
  407. scaleClient := createFakeScaleClient("replicasets", "foo", 2, verbsOnError)
  408. scaler := NewScaler(scaleClient)
  409. preconditions := ScalePrecondition{-1, ""}
  410. count := uint(3)
  411. name := "foo"
  412. namespace := "default"
  413. scaleFunc := ScaleCondition(scaler, &preconditions, namespace, name, count, nil, schema.GroupResource{Group: "extensions", Resource: "replicasets"})
  414. pass, err := scaleFunc()
  415. if pass {
  416. t.Errorf("Expected an update failure to return pass = false, got pass = %v", pass)
  417. }
  418. if err == nil {
  419. t.Errorf("Expected error on invalid update failure, got %v", err)
  420. }
  421. actions := scaleClient.Actions()
  422. if len(actions) != len(scaleClientExpectedAction) {
  423. t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))
  424. }
  425. for i, verb := range scaleClientExpectedAction {
  426. if actions[i].GetVerb() != verb {
  427. t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)
  428. }
  429. }
  430. }
  431. func TestReplicaSetsGetterFailsPreconditions(t *testing.T) {
  432. scaleClientExpectedAction := []string{"get"}
  433. scaleClient := createFakeScaleClient("replicasets", "foo", 10, nil)
  434. scaler := NewScaler(scaleClient)
  435. preconditions := ScalePrecondition{2, ""}
  436. count := uint(3)
  437. name := "foo"
  438. err := scaler.Scale("default", name, count, &preconditions, nil, nil, schema.GroupResource{Group: "extensions", Resource: "replicasets"})
  439. if err == nil {
  440. t.Fatal("expected to get an error but non was returned")
  441. }
  442. actions := scaleClient.Actions()
  443. if len(actions) != len(scaleClientExpectedAction) {
  444. t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))
  445. }
  446. for i, verb := range scaleClientExpectedAction {
  447. if actions[i].GetVerb() != verb {
  448. t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)
  449. }
  450. }
  451. }
  452. // TestGenericScaleSimple exercises GenericScaler.ScaleSimple method
  453. func TestGenericScaleSimple(t *testing.T) {
  454. // test data
  455. scaleClient := createFakeScaleClient("deployments", "abc", 10, nil)
  456. // test scenarios
  457. scenarios := []struct {
  458. name string
  459. precondition ScalePrecondition
  460. newSize int
  461. targetGR schema.GroupResource
  462. resName string
  463. scaleGetter scale.ScalesGetter
  464. expectError bool
  465. }{
  466. // scenario 1: scale up the "abc" deployment
  467. {
  468. name: "scale up the \"abc\" deployment",
  469. precondition: ScalePrecondition{10, ""},
  470. newSize: 20,
  471. targetGR: schema.GroupResource{Group: "apps", Resource: "deployments"},
  472. resName: "abc",
  473. scaleGetter: scaleClient,
  474. },
  475. // scenario 2: scale down the "abc" deployment
  476. {
  477. name: "scale down the \"abs\" deployment",
  478. precondition: ScalePrecondition{20, ""},
  479. newSize: 5,
  480. targetGR: schema.GroupResource{Group: "apps", Resource: "deployments"},
  481. resName: "abc",
  482. scaleGetter: scaleClient,
  483. },
  484. // scenario 3: precondition error, expected size is 1,
  485. // note that the previous scenario (2) set the size to 5
  486. {
  487. name: "precondition error, expected size is 1",
  488. precondition: ScalePrecondition{1, ""},
  489. newSize: 5,
  490. targetGR: schema.GroupResource{Group: "apps", Resource: "deployments"},
  491. resName: "abc",
  492. scaleGetter: scaleClient,
  493. expectError: true,
  494. },
  495. // scenario 4: precondition is not validated when the precondition size is set to -1
  496. {
  497. name: "precondition is not validated when the size is set to -1",
  498. precondition: ScalePrecondition{-1, ""},
  499. newSize: 5,
  500. targetGR: schema.GroupResource{Group: "apps", Resource: "deployments"},
  501. resName: "abc",
  502. scaleGetter: scaleClient,
  503. },
  504. // scenario 5: precondition error, resource version mismatch
  505. {
  506. name: "precondition error, resource version mismatch",
  507. precondition: ScalePrecondition{5, "v1"},
  508. newSize: 5,
  509. targetGR: schema.GroupResource{Group: "apps", Resource: "deployments"},
  510. resName: "abc",
  511. scaleGetter: scaleClient,
  512. expectError: true,
  513. },
  514. }
  515. // act
  516. for index, scenario := range scenarios {
  517. t.Run(fmt.Sprintf("running scenario %d: %s", index+1, scenario.name), func(t *testing.T) {
  518. target := NewScaler(scenario.scaleGetter)
  519. resVersion, err := target.ScaleSimple("default", scenario.resName, &scenario.precondition, uint(scenario.newSize), scenario.targetGR)
  520. if scenario.expectError && err == nil {
  521. t.Fatal("expected an error but was not returned")
  522. }
  523. if !scenario.expectError && err != nil {
  524. t.Fatalf("unexpected error: %v", err)
  525. }
  526. if resVersion != "" {
  527. t.Fatalf("unexpected resource version returned = %s, wanted = %s", resVersion, "")
  528. }
  529. })
  530. }
  531. }
  532. // TestGenericScale exercises GenericScaler.Scale method
  533. func TestGenericScale(t *testing.T) {
  534. // test data
  535. scaleClient := createFakeScaleClient("deployments", "abc", 10, nil)
  536. // test scenarios
  537. scenarios := []struct {
  538. name string
  539. precondition ScalePrecondition
  540. newSize int
  541. targetGR schema.GroupResource
  542. resName string
  543. scaleGetter scale.ScalesGetter
  544. waitForReplicas *RetryParams
  545. expectError bool
  546. }{
  547. // scenario 1: scale up the "abc" deployment
  548. {
  549. name: "scale up the \"abc\" deployment",
  550. precondition: ScalePrecondition{10, ""},
  551. newSize: 20,
  552. targetGR: schema.GroupResource{Group: "apps", Resource: "deployments"},
  553. resName: "abc",
  554. scaleGetter: scaleClient,
  555. },
  556. //scenario 2: a resource name cannot be empty
  557. {
  558. name: "a resource name cannot be empty",
  559. precondition: ScalePrecondition{10, ""},
  560. newSize: 20,
  561. targetGR: schema.GroupResource{Group: "apps", Resource: "deployments"},
  562. resName: "",
  563. scaleGetter: scaleClient,
  564. expectError: true,
  565. },
  566. // scenario 3: wait for replicas error due to status.Replicas != spec.Replicas
  567. {
  568. name: "wait for replicas error due to status.Replicas != spec.Replicas",
  569. precondition: ScalePrecondition{10, ""},
  570. newSize: 20,
  571. targetGR: schema.GroupResource{Group: "apps", Resource: "deployments"},
  572. resName: "abc",
  573. scaleGetter: scaleClient,
  574. waitForReplicas: &RetryParams{time.Duration(5 * time.Second), time.Duration(5 * time.Second)},
  575. expectError: true,
  576. },
  577. }
  578. // act
  579. for _, scenario := range scenarios {
  580. t.Run(scenario.name, func(t *testing.T) {
  581. target := NewScaler(scenario.scaleGetter)
  582. err := target.Scale("default", scenario.resName, uint(scenario.newSize), &scenario.precondition, nil, scenario.waitForReplicas, scenario.targetGR)
  583. if scenario.expectError && err == nil {
  584. t.Fatal("expected an error but was not returned")
  585. }
  586. if !scenario.expectError && err != nil {
  587. t.Fatalf("unexpected error: %v", err)
  588. }
  589. })
  590. }
  591. }
  592. func createFakeScaleClient(resource string, resourceName string, replicas int, errorsOnVerb map[string]*kerrors.StatusError) *fakescale.FakeScaleClient {
  593. shouldReturnAnError := func(verb string) (*kerrors.StatusError, bool) {
  594. if anError, anErrorExists := errorsOnVerb[verb]; anErrorExists {
  595. return anError, true
  596. }
  597. return &kerrors.StatusError{}, false
  598. }
  599. newReplicas := int32(replicas)
  600. scaleClient := &fakescale.FakeScaleClient{}
  601. scaleClient.AddReactor("get", resource, func(rawAction testcore.Action) (handled bool, ret runtime.Object, err error) {
  602. action := rawAction.(testcore.GetAction)
  603. if action.GetName() != resourceName {
  604. return true, nil, fmt.Errorf("expected = %s, got = %s", resourceName, action.GetName())
  605. }
  606. if anError, should := shouldReturnAnError("get"); should {
  607. return true, nil, anError
  608. }
  609. obj := &autoscalingv1.Scale{
  610. ObjectMeta: metav1.ObjectMeta{
  611. Name: action.GetName(),
  612. Namespace: action.GetNamespace(),
  613. },
  614. Spec: autoscalingv1.ScaleSpec{
  615. Replicas: newReplicas,
  616. },
  617. }
  618. return true, obj, nil
  619. })
  620. scaleClient.AddReactor("update", resource, func(rawAction testcore.Action) (handled bool, ret runtime.Object, err error) {
  621. action := rawAction.(testcore.UpdateAction)
  622. obj := action.GetObject().(*autoscalingv1.Scale)
  623. if obj.Name != resourceName {
  624. return true, nil, fmt.Errorf("expected = %s, got = %s", resourceName, obj.Name)
  625. }
  626. if anError, should := shouldReturnAnError("update"); should {
  627. return true, nil, anError
  628. }
  629. newReplicas = obj.Spec.Replicas
  630. return true, &autoscalingv1.Scale{
  631. ObjectMeta: metav1.ObjectMeta{
  632. Name: obj.Name,
  633. Namespace: action.GetNamespace(),
  634. },
  635. Spec: autoscalingv1.ScaleSpec{
  636. Replicas: newReplicas,
  637. },
  638. }, nil
  639. })
  640. return scaleClient
  641. }