prepull_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. Copyright 2017 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 upgrade
  14. import (
  15. "testing"
  16. "time"
  17. "github.com/pkg/errors"
  18. "k8s.io/kubernetes/cmd/kubeadm/app/constants"
  19. //"k8s.io/apimachinery/pkg/util/version"
  20. )
  21. // failedCreatePrepuller is a fake prepuller that errors for kube-controller-manager in the CreateFunc call
  22. type failedCreatePrepuller struct{}
  23. func NewFailedCreatePrepuller() Prepuller {
  24. return &failedCreatePrepuller{}
  25. }
  26. func (p *failedCreatePrepuller) CreateFunc(component string) error {
  27. if component == "kube-controller-manager" {
  28. return errors.New("boo")
  29. }
  30. return nil
  31. }
  32. func (p *failedCreatePrepuller) WaitFunc(component string) {}
  33. func (p *failedCreatePrepuller) DeleteFunc(component string) error {
  34. return nil
  35. }
  36. // foreverWaitPrepuller is a fake prepuller that basically waits "forever" (10 mins, but longer than the 10sec timeout)
  37. type foreverWaitPrepuller struct{}
  38. func NewForeverWaitPrepuller() Prepuller {
  39. return &foreverWaitPrepuller{}
  40. }
  41. func (p *foreverWaitPrepuller) CreateFunc(component string) error {
  42. return nil
  43. }
  44. func (p *foreverWaitPrepuller) WaitFunc(component string) {
  45. time.Sleep(10 * time.Minute)
  46. }
  47. func (p *foreverWaitPrepuller) DeleteFunc(component string) error {
  48. return nil
  49. }
  50. // failedDeletePrepuller is a fake prepuller that errors for kube-scheduler in the DeleteFunc call
  51. type failedDeletePrepuller struct{}
  52. func NewFailedDeletePrepuller() Prepuller {
  53. return &failedDeletePrepuller{}
  54. }
  55. func (p *failedDeletePrepuller) CreateFunc(component string) error {
  56. return nil
  57. }
  58. func (p *failedDeletePrepuller) WaitFunc(component string) {}
  59. func (p *failedDeletePrepuller) DeleteFunc(component string) error {
  60. if component == "kube-scheduler" {
  61. return errors.New("boo")
  62. }
  63. return nil
  64. }
  65. // goodPrepuller is a fake prepuller that works as expected
  66. type goodPrepuller struct{}
  67. func NewGoodPrepuller() Prepuller {
  68. return &goodPrepuller{}
  69. }
  70. func (p *goodPrepuller) CreateFunc(component string) error {
  71. time.Sleep(300 * time.Millisecond)
  72. return nil
  73. }
  74. func (p *goodPrepuller) WaitFunc(component string) {
  75. time.Sleep(300 * time.Millisecond)
  76. }
  77. func (p *goodPrepuller) DeleteFunc(component string) error {
  78. time.Sleep(300 * time.Millisecond)
  79. return nil
  80. }
  81. func TestPrepullImagesInParallel(t *testing.T) {
  82. tests := []struct {
  83. name string
  84. p Prepuller
  85. timeout time.Duration
  86. expectedErr bool
  87. }{
  88. {
  89. name: "should error out; create failed",
  90. p: NewFailedCreatePrepuller(),
  91. timeout: constants.PrepullImagesInParallelTimeout,
  92. expectedErr: true,
  93. },
  94. {
  95. name: "should error out; timeout exceeded",
  96. p: NewForeverWaitPrepuller(),
  97. timeout: constants.PrepullImagesInParallelTimeout,
  98. expectedErr: true,
  99. },
  100. {
  101. name: "should error out; delete failed",
  102. p: NewFailedDeletePrepuller(),
  103. timeout: constants.PrepullImagesInParallelTimeout,
  104. expectedErr: true,
  105. },
  106. {
  107. name: "should work just fine",
  108. p: NewGoodPrepuller(),
  109. timeout: constants.PrepullImagesInParallelTimeout,
  110. expectedErr: false,
  111. },
  112. }
  113. for _, rt := range tests {
  114. t.Run(rt.name, func(t *testing.T) {
  115. actualErr := PrepullImagesInParallel(rt.p, rt.timeout, append(constants.ControlPlaneComponents, constants.Etcd))
  116. if (actualErr != nil) != rt.expectedErr {
  117. t.Errorf(
  118. "failed TestPrepullImagesInParallel\n\texpected error: %t\n\tgot: %t",
  119. rt.expectedErr,
  120. (actualErr != nil),
  121. )
  122. }
  123. })
  124. }
  125. }