cassandra.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 upgrades
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "io/ioutil"
  18. "net/http"
  19. "path/filepath"
  20. "sync"
  21. "time"
  22. "github.com/onsi/ginkgo"
  23. "github.com/onsi/gomega"
  24. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  25. "k8s.io/apimachinery/pkg/util/version"
  26. "k8s.io/apimachinery/pkg/util/wait"
  27. "k8s.io/kubernetes/test/e2e/framework"
  28. e2elog "k8s.io/kubernetes/test/e2e/framework/log"
  29. "k8s.io/kubernetes/test/e2e/framework/testfiles"
  30. )
  31. const cassandraManifestPath = "test/e2e/testing-manifests/statefulset/cassandra"
  32. // CassandraUpgradeTest ups and verifies that a Cassandra StatefulSet behaves
  33. // well across upgrades.
  34. type CassandraUpgradeTest struct {
  35. ip string
  36. successfulWrites int
  37. ssTester *framework.StatefulSetTester
  38. }
  39. // Name returns the tracking name of the test.
  40. func (CassandraUpgradeTest) Name() string { return "cassandra-upgrade" }
  41. // Skip returns true when this test can be skipped.
  42. func (CassandraUpgradeTest) Skip(upgCtx UpgradeContext) bool {
  43. minVersion := version.MustParseSemantic("1.6.0")
  44. for _, vCtx := range upgCtx.Versions {
  45. if vCtx.Version.LessThan(minVersion) {
  46. return true
  47. }
  48. }
  49. return false
  50. }
  51. func cassandraKubectlCreate(ns, file string) {
  52. input := string(testfiles.ReadOrDie(filepath.Join(cassandraManifestPath, file), ginkgo.Fail))
  53. framework.RunKubectlOrDieInput(input, "create", "-f", "-", fmt.Sprintf("--namespace=%s", ns))
  54. }
  55. // Setup creates a Cassandra StatefulSet and a PDB. It also brings up a tester
  56. // ReplicaSet and associated service and PDB to guarantee availability during
  57. // the upgrade.
  58. // It waits for the system to stabilize before adding two users to verify
  59. // connectivity.
  60. func (t *CassandraUpgradeTest) Setup(f *framework.Framework) {
  61. ns := f.Namespace.Name
  62. statefulsetPoll := 30 * time.Second
  63. statefulsetTimeout := 10 * time.Minute
  64. t.ssTester = framework.NewStatefulSetTester(f.ClientSet)
  65. ginkgo.By("Creating a PDB")
  66. cassandraKubectlCreate(ns, "pdb.yaml")
  67. ginkgo.By("Creating a Cassandra StatefulSet")
  68. t.ssTester.CreateStatefulSet(cassandraManifestPath, ns)
  69. ginkgo.By("Creating a cassandra-test-server deployment")
  70. cassandraKubectlCreate(ns, "tester.yaml")
  71. ginkgo.By("Getting the ingress IPs from the services")
  72. err := wait.PollImmediate(statefulsetPoll, statefulsetTimeout, func() (bool, error) {
  73. if t.ip = t.getServiceIP(f, ns, "test-server"); t.ip == "" {
  74. return false, nil
  75. }
  76. if _, err := t.listUsers(); err != nil {
  77. e2elog.Logf("Service endpoint is up but isn't responding")
  78. return false, nil
  79. }
  80. return true, nil
  81. })
  82. framework.ExpectNoError(err)
  83. e2elog.Logf("Service endpoint is up")
  84. ginkgo.By("Adding 2 dummy users")
  85. err = t.addUser("Alice")
  86. framework.ExpectNoError(err)
  87. err = t.addUser("Bob")
  88. framework.ExpectNoError(err)
  89. t.successfulWrites = 2
  90. ginkgo.By("Verifying that the users exist")
  91. users, err := t.listUsers()
  92. framework.ExpectNoError(err)
  93. gomega.Expect(len(users)).To(gomega.Equal(2))
  94. }
  95. // listUsers gets a list of users from the db via the tester service.
  96. func (t *CassandraUpgradeTest) listUsers() ([]string, error) {
  97. r, err := http.Get(fmt.Sprintf("http://%s:8080/list", t.ip))
  98. if err != nil {
  99. return nil, err
  100. }
  101. defer r.Body.Close()
  102. if r.StatusCode != http.StatusOK {
  103. b, err := ioutil.ReadAll(r.Body)
  104. if err != nil {
  105. return nil, err
  106. }
  107. return nil, fmt.Errorf(string(b))
  108. }
  109. var names []string
  110. if err := json.NewDecoder(r.Body).Decode(&names); err != nil {
  111. return nil, err
  112. }
  113. return names, nil
  114. }
  115. // addUser adds a user to the db via the tester services.
  116. func (t *CassandraUpgradeTest) addUser(name string) error {
  117. val := map[string][]string{"name": {name}}
  118. r, err := http.PostForm(fmt.Sprintf("http://%s:8080/add", t.ip), val)
  119. if err != nil {
  120. return err
  121. }
  122. defer r.Body.Close()
  123. if r.StatusCode != http.StatusOK {
  124. b, err := ioutil.ReadAll(r.Body)
  125. if err != nil {
  126. return err
  127. }
  128. return fmt.Errorf(string(b))
  129. }
  130. return nil
  131. }
  132. // getServiceIP is a helper method to extract the Ingress IP from the service.
  133. func (t *CassandraUpgradeTest) getServiceIP(f *framework.Framework, ns, svcName string) string {
  134. svc, err := f.ClientSet.CoreV1().Services(ns).Get(svcName, metav1.GetOptions{})
  135. framework.ExpectNoError(err)
  136. ingress := svc.Status.LoadBalancer.Ingress
  137. if len(ingress) == 0 {
  138. return ""
  139. }
  140. return ingress[0].IP
  141. }
  142. // Test is called during the upgrade.
  143. // It launches two goroutines, one continuously writes to the db and one reads
  144. // from the db. Each attempt is tallied and at the end we verify if the success
  145. // ratio is over a certain threshold (0.75). We also verify that we get
  146. // at least the same number of rows back as we successfully wrote.
  147. func (t *CassandraUpgradeTest) Test(f *framework.Framework, done <-chan struct{}, upgrade UpgradeType) {
  148. ginkgo.By("Continuously polling the database during upgrade.")
  149. var (
  150. success, failures, writeAttempts, lastUserCount int
  151. mu sync.Mutex
  152. errors = map[string]int{}
  153. )
  154. // Write loop.
  155. go wait.Until(func() {
  156. writeAttempts++
  157. if err := t.addUser(fmt.Sprintf("user-%d", writeAttempts)); err != nil {
  158. e2elog.Logf("Unable to add user: %v", err)
  159. mu.Lock()
  160. errors[err.Error()]++
  161. mu.Unlock()
  162. return
  163. }
  164. t.successfulWrites++
  165. }, 10*time.Millisecond, done)
  166. // Read loop.
  167. wait.Until(func() {
  168. users, err := t.listUsers()
  169. if err != nil {
  170. e2elog.Logf("Could not retrieve users: %v", err)
  171. failures++
  172. mu.Lock()
  173. errors[err.Error()]++
  174. mu.Unlock()
  175. return
  176. }
  177. success++
  178. lastUserCount = len(users)
  179. }, 10*time.Millisecond, done)
  180. e2elog.Logf("got %d users; want >=%d", lastUserCount, t.successfulWrites)
  181. gomega.Expect(lastUserCount >= t.successfulWrites).To(gomega.BeTrue())
  182. ratio := float64(success) / float64(success+failures)
  183. e2elog.Logf("Successful gets %d/%d=%v", success, success+failures, ratio)
  184. ratio = float64(t.successfulWrites) / float64(writeAttempts)
  185. e2elog.Logf("Successful writes %d/%d=%v", t.successfulWrites, writeAttempts, ratio)
  186. e2elog.Logf("Errors: %v", errors)
  187. // TODO(maisem): tweak this value once we have a few test runs.
  188. gomega.Expect(ratio > 0.75).To(gomega.BeTrue())
  189. }
  190. // Teardown does one final check of the data's availability.
  191. func (t *CassandraUpgradeTest) Teardown(f *framework.Framework) {
  192. users, err := t.listUsers()
  193. framework.ExpectNoError(err)
  194. gomega.Expect(len(users) >= t.successfulWrites).To(gomega.BeTrue())
  195. }