tester.go 2.9 KB

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