scale_test.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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. "errors"
  16. "testing"
  17. "k8s.io/kubernetes/pkg/api"
  18. kerrors "k8s.io/kubernetes/pkg/api/errors"
  19. "k8s.io/kubernetes/pkg/apis/batch"
  20. "k8s.io/kubernetes/pkg/apis/extensions"
  21. client "k8s.io/kubernetes/pkg/client/unversioned"
  22. "k8s.io/kubernetes/pkg/client/unversioned/testclient"
  23. )
  24. type ErrorReplicationControllers struct {
  25. testclient.FakeReplicationControllers
  26. conflict bool
  27. invalid bool
  28. }
  29. func (c *ErrorReplicationControllers) Update(controller *api.ReplicationController) (*api.ReplicationController, error) {
  30. switch {
  31. case c.invalid:
  32. return nil, kerrors.NewInvalid(api.Kind(controller.Kind), controller.Name, nil)
  33. case c.conflict:
  34. return nil, kerrors.NewConflict(api.Resource(controller.Kind), controller.Name, nil)
  35. }
  36. return nil, errors.New("Replication controller update failure")
  37. }
  38. type ErrorReplicationControllerClient struct {
  39. testclient.Fake
  40. conflict bool
  41. invalid bool
  42. }
  43. func (c *ErrorReplicationControllerClient) ReplicationControllers(namespace string) client.ReplicationControllerInterface {
  44. return &ErrorReplicationControllers{
  45. FakeReplicationControllers: testclient.FakeReplicationControllers{Fake: &c.Fake, Namespace: namespace},
  46. conflict: c.conflict,
  47. invalid: c.invalid,
  48. }
  49. }
  50. func TestReplicationControllerScaleRetry(t *testing.T) {
  51. fake := &ErrorReplicationControllerClient{Fake: testclient.Fake{}, conflict: true}
  52. scaler := ReplicationControllerScaler{fake}
  53. preconditions := ScalePrecondition{-1, ""}
  54. count := uint(3)
  55. name := "foo"
  56. namespace := "default"
  57. scaleFunc := ScaleCondition(&scaler, &preconditions, namespace, name, count, nil)
  58. pass, err := scaleFunc()
  59. if pass {
  60. t.Errorf("Expected an update failure to return pass = false, got pass = %v", pass)
  61. }
  62. if err != nil {
  63. t.Errorf("Did not expect an error on update conflict failure, got %v", err)
  64. }
  65. preconditions = ScalePrecondition{3, ""}
  66. scaleFunc = ScaleCondition(&scaler, &preconditions, namespace, name, count, nil)
  67. pass, err = scaleFunc()
  68. if err == nil {
  69. t.Errorf("Expected error on precondition failure")
  70. }
  71. }
  72. func TestReplicationControllerScaleInvalid(t *testing.T) {
  73. fake := &ErrorReplicationControllerClient{Fake: testclient.Fake{}, invalid: true}
  74. scaler := ReplicationControllerScaler{fake}
  75. preconditions := ScalePrecondition{-1, ""}
  76. count := uint(3)
  77. name := "foo"
  78. namespace := "default"
  79. scaleFunc := ScaleCondition(&scaler, &preconditions, namespace, name, count, nil)
  80. pass, err := scaleFunc()
  81. if pass {
  82. t.Errorf("Expected an update failure to return pass = false, got pass = %v", pass)
  83. }
  84. e, ok := err.(ScaleError)
  85. if err == nil || !ok || e.FailureType != ScaleUpdateFailure {
  86. t.Errorf("Expected error on invalid update failure, got %v", err)
  87. }
  88. }
  89. func TestReplicationControllerScale(t *testing.T) {
  90. fake := &testclient.Fake{}
  91. scaler := ReplicationControllerScaler{fake}
  92. preconditions := ScalePrecondition{-1, ""}
  93. count := uint(3)
  94. name := "foo"
  95. scaler.Scale("default", name, count, &preconditions, nil, nil)
  96. actions := fake.Actions()
  97. if len(actions) != 2 {
  98. t.Errorf("unexpected actions: %v, expected 2 actions (get, update)", actions)
  99. }
  100. if action, ok := actions[0].(testclient.GetAction); !ok || action.GetResource() != "replicationcontrollers" || action.GetName() != name {
  101. t.Errorf("unexpected action: %v, expected get-replicationController %s", actions[0], name)
  102. }
  103. if action, ok := actions[1].(testclient.UpdateAction); !ok || action.GetResource() != "replicationcontrollers" || action.GetObject().(*api.ReplicationController).Spec.Replicas != int32(count) {
  104. t.Errorf("unexpected action %v, expected update-replicationController with replicas = %d", actions[1], count)
  105. }
  106. }
  107. func TestReplicationControllerScaleFailsPreconditions(t *testing.T) {
  108. fake := testclient.NewSimpleFake(&api.ReplicationController{
  109. Spec: api.ReplicationControllerSpec{
  110. Replicas: 10,
  111. },
  112. })
  113. scaler := ReplicationControllerScaler{fake}
  114. preconditions := ScalePrecondition{2, ""}
  115. count := uint(3)
  116. name := "foo"
  117. scaler.Scale("default", name, count, &preconditions, nil, nil)
  118. actions := fake.Actions()
  119. if len(actions) != 1 {
  120. t.Errorf("unexpected actions: %v, expected 1 action (get)", actions)
  121. }
  122. if action, ok := actions[0].(testclient.GetAction); !ok || action.GetResource() != "replicationcontrollers" || action.GetName() != name {
  123. t.Errorf("unexpected action: %v, expected get-replicationController %s", actions[0], name)
  124. }
  125. }
  126. func TestValidateReplicationController(t *testing.T) {
  127. tests := []struct {
  128. preconditions ScalePrecondition
  129. controller api.ReplicationController
  130. expectError bool
  131. test string
  132. }{
  133. {
  134. preconditions: ScalePrecondition{-1, ""},
  135. expectError: false,
  136. test: "defaults",
  137. },
  138. {
  139. preconditions: ScalePrecondition{-1, ""},
  140. controller: api.ReplicationController{
  141. ObjectMeta: api.ObjectMeta{
  142. ResourceVersion: "foo",
  143. },
  144. Spec: api.ReplicationControllerSpec{
  145. Replicas: 10,
  146. },
  147. },
  148. expectError: false,
  149. test: "defaults 2",
  150. },
  151. {
  152. preconditions: ScalePrecondition{0, ""},
  153. controller: api.ReplicationController{
  154. ObjectMeta: api.ObjectMeta{
  155. ResourceVersion: "foo",
  156. },
  157. Spec: api.ReplicationControllerSpec{
  158. Replicas: 0,
  159. },
  160. },
  161. expectError: false,
  162. test: "size matches",
  163. },
  164. {
  165. preconditions: ScalePrecondition{-1, "foo"},
  166. controller: api.ReplicationController{
  167. ObjectMeta: api.ObjectMeta{
  168. ResourceVersion: "foo",
  169. },
  170. Spec: api.ReplicationControllerSpec{
  171. Replicas: 10,
  172. },
  173. },
  174. expectError: false,
  175. test: "resource version matches",
  176. },
  177. {
  178. preconditions: ScalePrecondition{10, "foo"},
  179. controller: api.ReplicationController{
  180. ObjectMeta: api.ObjectMeta{
  181. ResourceVersion: "foo",
  182. },
  183. Spec: api.ReplicationControllerSpec{
  184. Replicas: 10,
  185. },
  186. },
  187. expectError: false,
  188. test: "both match",
  189. },
  190. {
  191. preconditions: ScalePrecondition{10, "foo"},
  192. controller: api.ReplicationController{
  193. ObjectMeta: api.ObjectMeta{
  194. ResourceVersion: "foo",
  195. },
  196. Spec: api.ReplicationControllerSpec{
  197. Replicas: 20,
  198. },
  199. },
  200. expectError: true,
  201. test: "size different",
  202. },
  203. {
  204. preconditions: ScalePrecondition{10, "foo"},
  205. controller: api.ReplicationController{
  206. ObjectMeta: api.ObjectMeta{
  207. ResourceVersion: "bar",
  208. },
  209. Spec: api.ReplicationControllerSpec{
  210. Replicas: 10,
  211. },
  212. },
  213. expectError: true,
  214. test: "version different",
  215. },
  216. {
  217. preconditions: ScalePrecondition{10, "foo"},
  218. controller: api.ReplicationController{
  219. ObjectMeta: api.ObjectMeta{
  220. ResourceVersion: "bar",
  221. },
  222. Spec: api.ReplicationControllerSpec{
  223. Replicas: 20,
  224. },
  225. },
  226. expectError: true,
  227. test: "both different",
  228. },
  229. }
  230. for _, test := range tests {
  231. err := test.preconditions.ValidateReplicationController(&test.controller)
  232. if err != nil && !test.expectError {
  233. t.Errorf("unexpected error: %v (%s)", err, test.test)
  234. }
  235. if err == nil && test.expectError {
  236. t.Errorf("unexpected non-error: %v (%s)", err, test.test)
  237. }
  238. }
  239. }
  240. type ErrorJobs struct {
  241. testclient.FakeJobsV1
  242. conflict bool
  243. invalid bool
  244. }
  245. func (c *ErrorJobs) Update(job *batch.Job) (*batch.Job, error) {
  246. switch {
  247. case c.invalid:
  248. return nil, kerrors.NewInvalid(api.Kind(job.Kind), job.Name, nil)
  249. case c.conflict:
  250. return nil, kerrors.NewConflict(api.Resource(job.Kind), job.Name, nil)
  251. }
  252. return nil, errors.New("Job update failure")
  253. }
  254. func (c *ErrorJobs) Get(name string) (*batch.Job, error) {
  255. zero := int32(0)
  256. return &batch.Job{
  257. Spec: batch.JobSpec{
  258. Parallelism: &zero,
  259. },
  260. }, nil
  261. }
  262. type ErrorJobClient struct {
  263. testclient.FakeBatch
  264. conflict bool
  265. invalid bool
  266. }
  267. func (c *ErrorJobClient) Jobs(namespace string) client.JobInterface {
  268. return &ErrorJobs{
  269. FakeJobsV1: testclient.FakeJobsV1{Fake: &c.FakeBatch, Namespace: namespace},
  270. conflict: c.conflict,
  271. invalid: c.invalid,
  272. }
  273. }
  274. func TestJobScaleRetry(t *testing.T) {
  275. fake := &ErrorJobClient{FakeBatch: testclient.FakeBatch{}, conflict: true}
  276. scaler := JobScaler{fake}
  277. preconditions := ScalePrecondition{-1, ""}
  278. count := uint(3)
  279. name := "foo"
  280. namespace := "default"
  281. scaleFunc := ScaleCondition(&scaler, &preconditions, namespace, name, count, nil)
  282. pass, err := scaleFunc()
  283. if pass != false {
  284. t.Errorf("Expected an update failure to return pass = false, got pass = %v", pass)
  285. }
  286. if err != nil {
  287. t.Errorf("Did not expect an error on update failure, got %v", err)
  288. }
  289. preconditions = ScalePrecondition{3, ""}
  290. scaleFunc = ScaleCondition(&scaler, &preconditions, namespace, name, count, nil)
  291. pass, err = scaleFunc()
  292. if err == nil {
  293. t.Errorf("Expected error on precondition failure")
  294. }
  295. }
  296. func TestJobScale(t *testing.T) {
  297. fake := &testclient.FakeBatch{Fake: &testclient.Fake{}}
  298. scaler := JobScaler{fake}
  299. preconditions := ScalePrecondition{-1, ""}
  300. count := uint(3)
  301. name := "foo"
  302. scaler.Scale("default", name, count, &preconditions, nil, nil)
  303. actions := fake.Actions()
  304. if len(actions) != 2 {
  305. t.Errorf("unexpected actions: %v, expected 2 actions (get, update)", actions)
  306. }
  307. if action, ok := actions[0].(testclient.GetAction); !ok || action.GetResource() != "jobs" || action.GetName() != name {
  308. t.Errorf("unexpected action: %v, expected get-replicationController %s", actions[0], name)
  309. }
  310. if action, ok := actions[1].(testclient.UpdateAction); !ok || action.GetResource() != "jobs" || *action.GetObject().(*batch.Job).Spec.Parallelism != int32(count) {
  311. t.Errorf("unexpected action %v, expected update-job with parallelism = %d", actions[1], count)
  312. }
  313. }
  314. func TestJobScaleInvalid(t *testing.T) {
  315. fake := &ErrorJobClient{FakeBatch: testclient.FakeBatch{}, invalid: true}
  316. scaler := JobScaler{fake}
  317. preconditions := ScalePrecondition{-1, ""}
  318. count := uint(3)
  319. name := "foo"
  320. namespace := "default"
  321. scaleFunc := ScaleCondition(&scaler, &preconditions, namespace, name, count, nil)
  322. pass, err := scaleFunc()
  323. if pass {
  324. t.Errorf("Expected an update failure to return pass = false, got pass = %v", pass)
  325. }
  326. e, ok := err.(ScaleError)
  327. if err == nil || !ok || e.FailureType != ScaleUpdateFailure {
  328. t.Errorf("Expected error on invalid update failure, got %v", err)
  329. }
  330. }
  331. func TestJobScaleFailsPreconditions(t *testing.T) {
  332. ten := int32(10)
  333. fake := testclient.NewSimpleFake(&batch.Job{
  334. Spec: batch.JobSpec{
  335. Parallelism: &ten,
  336. },
  337. })
  338. scaler := JobScaler{&testclient.FakeBatch{Fake: fake}}
  339. preconditions := ScalePrecondition{2, ""}
  340. count := uint(3)
  341. name := "foo"
  342. scaler.Scale("default", name, count, &preconditions, nil, nil)
  343. actions := fake.Actions()
  344. if len(actions) != 1 {
  345. t.Errorf("unexpected actions: %v, expected 1 actions (get)", actions)
  346. }
  347. if action, ok := actions[0].(testclient.GetAction); !ok || action.GetResource() != "jobs" || action.GetName() != name {
  348. t.Errorf("unexpected action: %v, expected get-job %s", actions[0], name)
  349. }
  350. }
  351. func TestValidateJob(t *testing.T) {
  352. zero, ten, twenty := int32(0), int32(10), int32(20)
  353. tests := []struct {
  354. preconditions ScalePrecondition
  355. job batch.Job
  356. expectError bool
  357. test string
  358. }{
  359. {
  360. preconditions: ScalePrecondition{-1, ""},
  361. expectError: false,
  362. test: "defaults",
  363. },
  364. {
  365. preconditions: ScalePrecondition{-1, ""},
  366. job: batch.Job{
  367. ObjectMeta: api.ObjectMeta{
  368. ResourceVersion: "foo",
  369. },
  370. Spec: batch.JobSpec{
  371. Parallelism: &ten,
  372. },
  373. },
  374. expectError: false,
  375. test: "defaults 2",
  376. },
  377. {
  378. preconditions: ScalePrecondition{0, ""},
  379. job: batch.Job{
  380. ObjectMeta: api.ObjectMeta{
  381. ResourceVersion: "foo",
  382. },
  383. Spec: batch.JobSpec{
  384. Parallelism: &zero,
  385. },
  386. },
  387. expectError: false,
  388. test: "size matches",
  389. },
  390. {
  391. preconditions: ScalePrecondition{-1, "foo"},
  392. job: batch.Job{
  393. ObjectMeta: api.ObjectMeta{
  394. ResourceVersion: "foo",
  395. },
  396. Spec: batch.JobSpec{
  397. Parallelism: &ten,
  398. },
  399. },
  400. expectError: false,
  401. test: "resource version matches",
  402. },
  403. {
  404. preconditions: ScalePrecondition{10, "foo"},
  405. job: batch.Job{
  406. ObjectMeta: api.ObjectMeta{
  407. ResourceVersion: "foo",
  408. },
  409. Spec: batch.JobSpec{
  410. Parallelism: &ten,
  411. },
  412. },
  413. expectError: false,
  414. test: "both match",
  415. },
  416. {
  417. preconditions: ScalePrecondition{10, "foo"},
  418. job: batch.Job{
  419. ObjectMeta: api.ObjectMeta{
  420. ResourceVersion: "foo",
  421. },
  422. Spec: batch.JobSpec{
  423. Parallelism: &twenty,
  424. },
  425. },
  426. expectError: true,
  427. test: "size different",
  428. },
  429. {
  430. preconditions: ScalePrecondition{10, "foo"},
  431. job: batch.Job{
  432. ObjectMeta: api.ObjectMeta{
  433. ResourceVersion: "foo",
  434. },
  435. },
  436. expectError: true,
  437. test: "parallelism nil",
  438. },
  439. {
  440. preconditions: ScalePrecondition{10, "foo"},
  441. job: batch.Job{
  442. ObjectMeta: api.ObjectMeta{
  443. ResourceVersion: "bar",
  444. },
  445. Spec: batch.JobSpec{
  446. Parallelism: &ten,
  447. },
  448. },
  449. expectError: true,
  450. test: "version different",
  451. },
  452. {
  453. preconditions: ScalePrecondition{10, "foo"},
  454. job: batch.Job{
  455. ObjectMeta: api.ObjectMeta{
  456. ResourceVersion: "bar",
  457. },
  458. Spec: batch.JobSpec{
  459. Parallelism: &twenty,
  460. },
  461. },
  462. expectError: true,
  463. test: "both different",
  464. },
  465. }
  466. for _, test := range tests {
  467. err := test.preconditions.ValidateJob(&test.job)
  468. if err != nil && !test.expectError {
  469. t.Errorf("unexpected error: %v (%s)", err, test.test)
  470. }
  471. if err == nil && test.expectError {
  472. t.Errorf("unexpected non-error: %v (%s)", err, test.test)
  473. }
  474. }
  475. }
  476. type ErrorDeployments struct {
  477. testclient.FakeDeployments
  478. conflict bool
  479. invalid bool
  480. }
  481. func (c *ErrorDeployments) Update(deployment *extensions.Deployment) (*extensions.Deployment, error) {
  482. switch {
  483. case c.invalid:
  484. return nil, kerrors.NewInvalid(api.Kind(deployment.Kind), deployment.Name, nil)
  485. case c.conflict:
  486. return nil, kerrors.NewConflict(api.Resource(deployment.Kind), deployment.Name, nil)
  487. }
  488. return nil, errors.New("deployment update failure")
  489. }
  490. func (c *ErrorDeployments) Get(name string) (*extensions.Deployment, error) {
  491. return &extensions.Deployment{
  492. Spec: extensions.DeploymentSpec{
  493. Replicas: 0,
  494. },
  495. }, nil
  496. }
  497. type ErrorDeploymentClient struct {
  498. testclient.FakeExperimental
  499. conflict bool
  500. invalid bool
  501. }
  502. func (c *ErrorDeploymentClient) Deployments(namespace string) client.DeploymentInterface {
  503. return &ErrorDeployments{
  504. FakeDeployments: testclient.FakeDeployments{Fake: &c.FakeExperimental, Namespace: namespace},
  505. invalid: c.invalid,
  506. conflict: c.conflict,
  507. }
  508. }
  509. func TestDeploymentScaleRetry(t *testing.T) {
  510. fake := &ErrorDeploymentClient{FakeExperimental: testclient.FakeExperimental{}, conflict: true}
  511. scaler := &DeploymentScaler{fake}
  512. preconditions := &ScalePrecondition{-1, ""}
  513. count := uint(3)
  514. name := "foo"
  515. namespace := "default"
  516. scaleFunc := ScaleCondition(scaler, preconditions, namespace, name, count, nil)
  517. pass, err := scaleFunc()
  518. if pass != false {
  519. t.Errorf("Expected an update failure to return pass = false, got pass = %v", pass)
  520. }
  521. if err != nil {
  522. t.Errorf("Did not expect an error on update failure, got %v", err)
  523. }
  524. preconditions = &ScalePrecondition{3, ""}
  525. scaleFunc = ScaleCondition(scaler, preconditions, namespace, name, count, nil)
  526. pass, err = scaleFunc()
  527. if err == nil {
  528. t.Errorf("Expected error on precondition failure")
  529. }
  530. }
  531. func TestDeploymentScale(t *testing.T) {
  532. fake := &testclient.FakeExperimental{Fake: &testclient.Fake{}}
  533. scaler := DeploymentScaler{fake}
  534. preconditions := ScalePrecondition{-1, ""}
  535. count := uint(3)
  536. name := "foo"
  537. scaler.Scale("default", name, count, &preconditions, nil, nil)
  538. actions := fake.Actions()
  539. if len(actions) != 2 {
  540. t.Errorf("unexpected actions: %v, expected 2 actions (get, update)", actions)
  541. }
  542. if action, ok := actions[0].(testclient.GetAction); !ok || action.GetResource() != "deployments" || action.GetName() != name {
  543. t.Errorf("unexpected action: %v, expected get-replicationController %s", actions[0], name)
  544. }
  545. if action, ok := actions[1].(testclient.UpdateAction); !ok || action.GetResource() != "deployments" || action.GetObject().(*extensions.Deployment).Spec.Replicas != int32(count) {
  546. t.Errorf("unexpected action %v, expected update-deployment with replicas = %d", actions[1], count)
  547. }
  548. }
  549. func TestDeploymentScaleInvalid(t *testing.T) {
  550. fake := &ErrorDeploymentClient{FakeExperimental: testclient.FakeExperimental{}, invalid: true}
  551. scaler := DeploymentScaler{fake}
  552. preconditions := ScalePrecondition{-1, ""}
  553. count := uint(3)
  554. name := "foo"
  555. namespace := "default"
  556. scaleFunc := ScaleCondition(&scaler, &preconditions, namespace, name, count, nil)
  557. pass, err := scaleFunc()
  558. if pass {
  559. t.Errorf("Expected an update failure to return pass = false, got pass = %v", pass)
  560. }
  561. e, ok := err.(ScaleError)
  562. if err == nil || !ok || e.FailureType != ScaleUpdateFailure {
  563. t.Errorf("Expected error on invalid update failure, got %v", err)
  564. }
  565. }
  566. func TestDeploymentScaleFailsPreconditions(t *testing.T) {
  567. fake := testclient.NewSimpleFake(&extensions.Deployment{
  568. Spec: extensions.DeploymentSpec{
  569. Replicas: 10,
  570. },
  571. })
  572. scaler := DeploymentScaler{&testclient.FakeExperimental{Fake: fake}}
  573. preconditions := ScalePrecondition{2, ""}
  574. count := uint(3)
  575. name := "foo"
  576. scaler.Scale("default", name, count, &preconditions, nil, nil)
  577. actions := fake.Actions()
  578. if len(actions) != 1 {
  579. t.Errorf("unexpected actions: %v, expected 1 actions (get)", actions)
  580. }
  581. if action, ok := actions[0].(testclient.GetAction); !ok || action.GetResource() != "deployments" || action.GetName() != name {
  582. t.Errorf("unexpected action: %v, expected get-deployment %s", actions[0], name)
  583. }
  584. }
  585. func TestValidateDeployment(t *testing.T) {
  586. zero, ten, twenty := int32(0), int32(10), int32(20)
  587. tests := []struct {
  588. preconditions ScalePrecondition
  589. deployment extensions.Deployment
  590. expectError bool
  591. test string
  592. }{
  593. {
  594. preconditions: ScalePrecondition{-1, ""},
  595. expectError: false,
  596. test: "defaults",
  597. },
  598. {
  599. preconditions: ScalePrecondition{-1, ""},
  600. deployment: extensions.Deployment{
  601. ObjectMeta: api.ObjectMeta{
  602. ResourceVersion: "foo",
  603. },
  604. Spec: extensions.DeploymentSpec{
  605. Replicas: ten,
  606. },
  607. },
  608. expectError: false,
  609. test: "defaults 2",
  610. },
  611. {
  612. preconditions: ScalePrecondition{0, ""},
  613. deployment: extensions.Deployment{
  614. ObjectMeta: api.ObjectMeta{
  615. ResourceVersion: "foo",
  616. },
  617. Spec: extensions.DeploymentSpec{
  618. Replicas: zero,
  619. },
  620. },
  621. expectError: false,
  622. test: "size matches",
  623. },
  624. {
  625. preconditions: ScalePrecondition{-1, "foo"},
  626. deployment: extensions.Deployment{
  627. ObjectMeta: api.ObjectMeta{
  628. ResourceVersion: "foo",
  629. },
  630. Spec: extensions.DeploymentSpec{
  631. Replicas: ten,
  632. },
  633. },
  634. expectError: false,
  635. test: "resource version matches",
  636. },
  637. {
  638. preconditions: ScalePrecondition{10, "foo"},
  639. deployment: extensions.Deployment{
  640. ObjectMeta: api.ObjectMeta{
  641. ResourceVersion: "foo",
  642. },
  643. Spec: extensions.DeploymentSpec{
  644. Replicas: ten,
  645. },
  646. },
  647. expectError: false,
  648. test: "both match",
  649. },
  650. {
  651. preconditions: ScalePrecondition{10, "foo"},
  652. deployment: extensions.Deployment{
  653. ObjectMeta: api.ObjectMeta{
  654. ResourceVersion: "foo",
  655. },
  656. Spec: extensions.DeploymentSpec{
  657. Replicas: twenty,
  658. },
  659. },
  660. expectError: true,
  661. test: "size different",
  662. },
  663. {
  664. preconditions: ScalePrecondition{10, "foo"},
  665. deployment: extensions.Deployment{
  666. ObjectMeta: api.ObjectMeta{
  667. ResourceVersion: "foo",
  668. },
  669. },
  670. expectError: true,
  671. test: "no replicas",
  672. },
  673. {
  674. preconditions: ScalePrecondition{10, "foo"},
  675. deployment: extensions.Deployment{
  676. ObjectMeta: api.ObjectMeta{
  677. ResourceVersion: "bar",
  678. },
  679. Spec: extensions.DeploymentSpec{
  680. Replicas: ten,
  681. },
  682. },
  683. expectError: true,
  684. test: "version different",
  685. },
  686. {
  687. preconditions: ScalePrecondition{10, "foo"},
  688. deployment: extensions.Deployment{
  689. ObjectMeta: api.ObjectMeta{
  690. ResourceVersion: "bar",
  691. },
  692. Spec: extensions.DeploymentSpec{
  693. Replicas: twenty,
  694. },
  695. },
  696. expectError: true,
  697. test: "both different",
  698. },
  699. }
  700. for _, test := range tests {
  701. err := test.preconditions.ValidateDeployment(&test.deployment)
  702. if err != nil && !test.expectError {
  703. t.Errorf("unexpected error: %v (%s)", err, test.test)
  704. }
  705. if err == nil && test.expectError {
  706. t.Errorf("unexpected non-error: %v (%s)", err, test.test)
  707. }
  708. }
  709. }