chaosmonkey.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. Copyright 2016 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 chaosmonkey
  14. import "github.com/onsi/ginkgo"
  15. // Disruption is the type to construct a Chaosmonkey with; see Do for more information.
  16. type Disruption func()
  17. // Test is the type to register with a Chaosmonkey. A test will run asynchronously across the
  18. // Chaosmonkey's Disruption. A Test takes a Semaphore as an argument. It should call sem.Ready()
  19. // once it's ready for the disruption to start and should then wait until sem.StopCh (which is a
  20. // <-chan struct{}) is closed, which signals that the disruption is over. It should then clean up
  21. // and return. See Do and Semaphore for more information.
  22. type Test func(sem *Semaphore)
  23. // Interface can be implemented if you prefer to define tests without dealing with a Semaphore. You
  24. // may define a struct that implements Interface's three methods (Setup, Test, and Teardown) and
  25. // RegisterInterface. See RegisterInterface for more information.
  26. type Interface interface {
  27. Setup()
  28. Test(stopCh <-chan struct{})
  29. Teardown()
  30. }
  31. // Chaosmonkey is the type that holds the necessary content for chaosmonkey test
  32. type Chaosmonkey struct {
  33. disruption Disruption
  34. tests []Test
  35. }
  36. // New creates and returns a Chaosmonkey, with which the caller should register Tests and call Do.
  37. // See Do for more information.
  38. func New(disruption Disruption) *Chaosmonkey {
  39. return &Chaosmonkey{
  40. disruption,
  41. []Test{},
  42. }
  43. }
  44. // Register registers the given Test with the Chaosmonkey, so that the test will run over the
  45. // Disruption.
  46. func (cm *Chaosmonkey) Register(test Test) {
  47. cm.tests = append(cm.tests, test)
  48. }
  49. // RegisterInterface registers the given Interface with the Chaosmonkey, so the Chaosmonkey will
  50. // call Setup, Test, and Teardown properly. Test can tell that the Disruption is finished when
  51. // stopCh is closed.
  52. func (cm *Chaosmonkey) RegisterInterface(in Interface) {
  53. cm.Register(func(sem *Semaphore) {
  54. in.Setup()
  55. sem.Ready()
  56. in.Test(sem.StopCh)
  57. in.Teardown()
  58. })
  59. }
  60. // Do performs the Disruption while testing the registered Tests. Once the caller has registered
  61. // all Tests with the Chaosmonkey, they call Do. Do starts each registered test asynchronously and
  62. // waits for each test to signal that it is ready by calling sem.Ready(). Do will then do the
  63. // Disruption, and when it's complete, close sem.StopCh to signal to the registered Tests that the
  64. // Disruption is over, and wait for all Tests to return.
  65. func (cm *Chaosmonkey) Do() {
  66. sems := []*Semaphore{}
  67. // All semaphores have the same StopCh.
  68. stopCh := make(chan struct{})
  69. for _, test := range cm.tests {
  70. test := test
  71. sem := newSemaphore(stopCh)
  72. sems = append(sems, sem)
  73. go func() {
  74. defer ginkgo.GinkgoRecover()
  75. defer sem.done()
  76. test(sem)
  77. }()
  78. }
  79. ginkgo.By("Waiting for all async tests to be ready")
  80. for _, sem := range sems {
  81. // Wait for test to be ready. We have to wait for ready *or done* because a test
  82. // may panic before signaling that its ready, and we shouldn't block. Since we
  83. // defered sem.done() above, if a test panics, it's marked as done.
  84. sem.waitForReadyOrDone()
  85. }
  86. defer func() {
  87. close(stopCh)
  88. ginkgo.By("Waiting for async validations to complete")
  89. for _, sem := range sems {
  90. sem.waitForDone()
  91. }
  92. }()
  93. ginkgo.By("Starting disruption")
  94. cm.disruption()
  95. ginkgo.By("Disruption complete; stopping async validations")
  96. }
  97. // Semaphore is taken by a Test and provides: Ready(), for the Test to call when it's ready for the
  98. // disruption to start; and StopCh, the closure of which signals to the Test that the disruption is
  99. // finished.
  100. type Semaphore struct {
  101. readyCh chan struct{}
  102. StopCh <-chan struct{}
  103. doneCh chan struct{}
  104. }
  105. func newSemaphore(stopCh <-chan struct{}) *Semaphore {
  106. // We don't want to block on Ready() or done()
  107. return &Semaphore{
  108. make(chan struct{}, 1),
  109. stopCh,
  110. make(chan struct{}, 1),
  111. }
  112. }
  113. // Ready is called by the Test to signal that the Test is ready for the disruption to start.
  114. func (sem *Semaphore) Ready() {
  115. close(sem.readyCh)
  116. }
  117. // done is an internal method for Go to defer, both to wait for all tests to return, but also to
  118. // sense if a test panicked before calling Ready. See waitForReadyOrDone.
  119. func (sem *Semaphore) done() {
  120. close(sem.doneCh)
  121. }
  122. // We would like to just check if all tests are ready, but if they fail (which Ginkgo implements as
  123. // a panic), they may not have called Ready(). We check done as well to see if the function has
  124. // already returned; if it has, we don't care if it's ready, and just continue.
  125. func (sem *Semaphore) waitForReadyOrDone() {
  126. select {
  127. case <-sem.readyCh:
  128. case <-sem.doneCh:
  129. }
  130. }
  131. // waitForDone is an internal method for Go to wait on all Tests returning.
  132. func (sem *Semaphore) waitForDone() {
  133. <-sem.doneCh
  134. }