tester.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2015 CoreOS, Inc.
  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 main
  15. import (
  16. "log"
  17. "sync"
  18. "time"
  19. )
  20. type tester struct {
  21. failures []failure
  22. cluster *cluster
  23. limit int
  24. status Status
  25. }
  26. func (tt *tester) runLoop() {
  27. tt.status.Since = time.Now()
  28. tt.status.RoundLimit = tt.limit
  29. for _, f := range tt.failures {
  30. tt.status.Failures = append(tt.status.Failures, f.Desc())
  31. }
  32. for i := 0; i < tt.limit; i++ {
  33. tt.status.setRound(i)
  34. for j, f := range tt.failures {
  35. tt.status.setCase(j)
  36. if err := tt.cluster.WaitHealth(); err != nil {
  37. log.Printf("etcd-tester: [round#%d case#%d] wait full health error: %v", i, j, err)
  38. if err := tt.cleanup(i, j); err != nil {
  39. log.Printf("etcd-tester: [round#%d case#%d] cleanup error: %v", i, j, err)
  40. return
  41. }
  42. continue
  43. }
  44. log.Printf("etcd-tester: [round#%d case#%d] start failure %s", i, j, f.Desc())
  45. log.Printf("etcd-tester: [round#%d case#%d] start injecting failure...", i, j)
  46. if err := f.Inject(tt.cluster, i); err != nil {
  47. log.Printf("etcd-tester: [round#%d case#%d] injection error: %v", i, j, err)
  48. if err := tt.cleanup(i, j); err != nil {
  49. log.Printf("etcd-tester: [round#%d case#%d] cleanup error: %v", i, j, err)
  50. return
  51. }
  52. continue
  53. }
  54. log.Printf("etcd-tester: [round#%d case#%d] start recovering failure...", i, j)
  55. if err := f.Recover(tt.cluster, i); err != nil {
  56. log.Printf("etcd-tester: [round#%d case#%d] recovery error: %v", i, j, err)
  57. if err := tt.cleanup(i, j); err != nil {
  58. log.Printf("etcd-tester: [round#%d case#%d] cleanup error: %v", i, j, err)
  59. return
  60. }
  61. continue
  62. }
  63. log.Printf("etcd-tester: [round#%d case#%d] succeed!", i, j)
  64. }
  65. }
  66. }
  67. func (tt *tester) cleanup(i, j int) error {
  68. log.Printf("etcd-tester: [round#%d case#%d] cleaning up...", i, j)
  69. if err := tt.cluster.Cleanup(); err != nil {
  70. return err
  71. }
  72. return tt.cluster.Bootstrap()
  73. }
  74. type Status struct {
  75. Since time.Time
  76. Failures []string
  77. RoundLimit int
  78. mu sync.Mutex // guards Round and Case
  79. // TODO: add agent status
  80. Round int
  81. Case int
  82. }
  83. // get gets a copy of status
  84. func (s *Status) get() Status {
  85. s.mu.Lock()
  86. defer s.mu.Unlock()
  87. return *s
  88. }
  89. func (s *Status) setRound(r int) {
  90. s.mu.Lock()
  91. defer s.mu.Unlock()
  92. s.Round = r
  93. }
  94. func (s *Status) setCase(c int) {
  95. s.mu.Lock()
  96. defer s.mu.Unlock()
  97. s.Case = c
  98. }