mysql.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. "context"
  16. "encoding/json"
  17. "fmt"
  18. "io/ioutil"
  19. "net/http"
  20. "path/filepath"
  21. "strconv"
  22. "time"
  23. "github.com/onsi/ginkgo"
  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. e2esset "k8s.io/kubernetes/test/e2e/framework/statefulset"
  29. "k8s.io/kubernetes/test/e2e/framework/testfiles"
  30. )
  31. const mysqlManifestPath = "test/e2e/testing-manifests/statefulset/mysql-upgrade"
  32. // MySQLUpgradeTest implements an upgrade test harness that polls a replicated sql database.
  33. type MySQLUpgradeTest struct {
  34. ip string
  35. successfulWrites int
  36. nextWrite int
  37. }
  38. // Name returns the tracking name of the test.
  39. func (MySQLUpgradeTest) Name() string { return "mysql-upgrade" }
  40. // Skip returns true when this test can be skipped.
  41. func (MySQLUpgradeTest) Skip(upgCtx UpgradeContext) bool {
  42. minVersion := version.MustParseSemantic("1.5.0")
  43. for _, vCtx := range upgCtx.Versions {
  44. if vCtx.Version.LessThan(minVersion) {
  45. return true
  46. }
  47. }
  48. return false
  49. }
  50. func mysqlKubectlCreate(ns, file string) {
  51. input := string(testfiles.ReadOrDie(filepath.Join(mysqlManifestPath, file)))
  52. framework.RunKubectlOrDieInput(ns, input, "create", "-f", "-", fmt.Sprintf("--namespace=%s", ns))
  53. }
  54. func (t *MySQLUpgradeTest) getServiceIP(f *framework.Framework, ns, svcName string) string {
  55. svc, err := f.ClientSet.CoreV1().Services(ns).Get(context.TODO(), svcName, metav1.GetOptions{})
  56. framework.ExpectNoError(err)
  57. ingress := svc.Status.LoadBalancer.Ingress
  58. if len(ingress) == 0 {
  59. return ""
  60. }
  61. return ingress[0].IP
  62. }
  63. // Setup creates a StatefulSet, HeadlessService, a Service to write to the db, and a Service to read
  64. // from the db. It then connects to the db with the write Service and populates the db with a table
  65. // and a few entries. Finally, it connects to the db with the read Service, and confirms the data is
  66. // available. The db connections are left open to be used later in the test.
  67. func (t *MySQLUpgradeTest) Setup(f *framework.Framework) {
  68. ns := f.Namespace.Name
  69. statefulsetPoll := 30 * time.Second
  70. statefulsetTimeout := 10 * time.Minute
  71. ginkgo.By("Creating a configmap")
  72. mysqlKubectlCreate(ns, "configmap.yaml")
  73. ginkgo.By("Creating a mysql StatefulSet")
  74. e2esset.CreateStatefulSet(f.ClientSet, mysqlManifestPath, ns)
  75. ginkgo.By("Creating a mysql-test-server deployment")
  76. mysqlKubectlCreate(ns, "tester.yaml")
  77. ginkgo.By("Getting the ingress IPs from the test-service")
  78. err := wait.PollImmediate(statefulsetPoll, statefulsetTimeout, func() (bool, error) {
  79. if t.ip = t.getServiceIP(f, ns, "test-server"); t.ip == "" {
  80. return false, nil
  81. }
  82. if _, err := t.countNames(); err != nil {
  83. framework.Logf("Service endpoint is up but isn't responding")
  84. return false, nil
  85. }
  86. return true, nil
  87. })
  88. framework.ExpectNoError(err)
  89. framework.Logf("Service endpoint is up")
  90. ginkgo.By("Adding 2 names to the database")
  91. err = t.addName(strconv.Itoa(t.nextWrite))
  92. framework.ExpectNoError(err)
  93. err = t.addName(strconv.Itoa(t.nextWrite))
  94. framework.ExpectNoError(err)
  95. ginkgo.By("Verifying that the 2 names have been inserted")
  96. count, err := t.countNames()
  97. framework.ExpectNoError(err)
  98. framework.ExpectEqual(count, 2)
  99. }
  100. // Test continually polls the db using the read and write connections, inserting data, and checking
  101. // that all the data is readable.
  102. func (t *MySQLUpgradeTest) Test(f *framework.Framework, done <-chan struct{}, upgrade UpgradeType) {
  103. var writeSuccess, readSuccess, writeFailure, readFailure int
  104. ginkgo.By("Continuously polling the database during upgrade.")
  105. go wait.Until(func() {
  106. _, err := t.countNames()
  107. if err != nil {
  108. framework.Logf("Error while trying to read data: %v", err)
  109. readFailure++
  110. } else {
  111. readSuccess++
  112. }
  113. }, framework.Poll, done)
  114. wait.Until(func() {
  115. err := t.addName(strconv.Itoa(t.nextWrite))
  116. if err != nil {
  117. framework.Logf("Error while trying to write data: %v", err)
  118. writeFailure++
  119. } else {
  120. writeSuccess++
  121. }
  122. }, framework.Poll, done)
  123. t.successfulWrites = writeSuccess
  124. framework.Logf("Successful reads: %d", readSuccess)
  125. framework.Logf("Successful writes: %d", writeSuccess)
  126. framework.Logf("Failed reads: %d", readFailure)
  127. framework.Logf("Failed writes: %d", writeFailure)
  128. // TODO: Not sure what the ratio defining a successful test run should be. At time of writing the
  129. // test, failures only seem to happen when a race condition occurs (read/write starts, doesn't
  130. // finish before upgrade interferes).
  131. readRatio := float64(readSuccess) / float64(readSuccess+readFailure)
  132. writeRatio := float64(writeSuccess) / float64(writeSuccess+writeFailure)
  133. if readRatio < 0.75 {
  134. framework.Failf("Too many failures reading data. Success ratio: %f", readRatio)
  135. }
  136. if writeRatio < 0.75 {
  137. framework.Failf("Too many failures writing data. Success ratio: %f", writeRatio)
  138. }
  139. }
  140. // Teardown performs one final check of the data's availability.
  141. func (t *MySQLUpgradeTest) Teardown(f *framework.Framework) {
  142. count, err := t.countNames()
  143. framework.ExpectNoError(err)
  144. framework.ExpectEqual(count >= t.successfulWrites, true)
  145. }
  146. // addName adds a new value to the db.
  147. func (t *MySQLUpgradeTest) addName(name string) error {
  148. val := map[string][]string{"name": {name}}
  149. t.nextWrite++
  150. r, err := http.PostForm(fmt.Sprintf("http://%s:8080/addName", t.ip), val)
  151. if err != nil {
  152. return err
  153. }
  154. defer r.Body.Close()
  155. if r.StatusCode != http.StatusOK {
  156. b, err := ioutil.ReadAll(r.Body)
  157. if err != nil {
  158. return err
  159. }
  160. return fmt.Errorf(string(b))
  161. }
  162. return nil
  163. }
  164. // countNames checks to make sure the values in testing.users are available, and returns
  165. // the count of them.
  166. func (t *MySQLUpgradeTest) countNames() (int, error) {
  167. r, err := http.Get(fmt.Sprintf("http://%s:8080/countNames", t.ip))
  168. if err != nil {
  169. return 0, err
  170. }
  171. defer r.Body.Close()
  172. if r.StatusCode != http.StatusOK {
  173. b, err := ioutil.ReadAll(r.Body)
  174. if err != nil {
  175. return 0, err
  176. }
  177. return 0, fmt.Errorf(string(b))
  178. }
  179. var count int
  180. if err := json.NewDecoder(r.Body).Decode(&count); err != nil {
  181. return 0, err
  182. }
  183. return count, nil
  184. }