idbeta commented on a change in pull request #3404:
URL: https://github.com/apache/apisix/pull/3404#discussion_r564992390



##########
File path: t/chaos/kill-etcd_test.go
##########
@@ -0,0 +1,173 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package chaos
+
+import (
+       "fmt"
+       "net/http"
+       "os/exec"
+       "strings"
+       "testing"
+       "time"
+
+       "github.com/gavv/httpexpect/v2"
+       . "github.com/onsi/gomega"
+)
+
+var (
+       token = "edd1c9f034335f136f87ad84b625c8f1"
+       host  = "http://127.0.0.1:9080";
+)
+
+type httpTestCase struct {
+       E            *httpexpect.Expect
+       Method       string
+       Path         string
+       Body         string
+       Headers      map[string]string
+       ExpectStatus int
+       ExpectBody   string
+}
+
+func caseCheck(tc httpTestCase) {
+       e := tc.E
+       var req *httpexpect.Request
+       switch tc.Method {
+       case http.MethodGet:
+               req = e.GET(tc.Path)
+       case http.MethodPut:
+               req = e.PUT(tc.Path)
+       default:
+       }
+
+       if req == nil {
+               fmt.Printf("%#v", tc)
+               panic("fail to init request")
+       }
+       for key, val := range tc.Headers {
+               req.WithHeader(key, val)
+       }
+       if tc.Body != "" {
+               req.WithText(tc.Body)
+       }
+
+       resp := req.Expect()
+       if tc.ExpectStatus != 0 {
+               resp.Status(tc.ExpectStatus)
+       }
+
+       if tc.ExpectBody != "" {
+               resp.Body().Contains(tc.ExpectBody)
+       }
+}
+
+func setRoute(e *httpexpect.Expect, expectStatus int) {
+       caseCheck(httpTestCase{
+               E:       e,
+               Method:  http.MethodPut,
+               Path:    "/apisix/admin/routes/1",
+               Headers: map[string]string{"X-API-KEY": token},
+               Body: `{
+                       "uri": "/hello",
+                       "host": "foo.com",
+                       "plugins": {
+                               "prometheus": {}
+                       },
+                       "upstream": {
+                               "nodes": {
+                                       "bar.org": 1
+                               },
+                               "type": "roundrobin"
+                       }
+               }`,
+               ExpectStatus: expectStatus,
+       })
+}
+
+func getRoute(e *httpexpect.Expect, expectStatus int) {
+       caseCheck(httpTestCase{
+               E:            e,
+               Method:       http.MethodGet,
+               Path:         "/hello",
+               Headers:      map[string]string{"Host": "foo.com"},
+               ExpectStatus: expectStatus,
+       })
+}
+
+func deleteRoute(e *httpexpect.Expect, expectStatus int) {
+       caseCheck(httpTestCase{
+               E:            e,
+               Method:       http.MethodDelete,
+               Path:         "/apisix/admin/routes/1",
+               Headers:      map[string]string{"X-API-KEY": token},
+               ExpectStatus: expectStatus,
+       })
+}
+
+func getPrometheusMetric(e *httpexpect.Expect, expectEtcd int) {
+       caseCheck(httpTestCase{
+               E:          e,
+               Method:     http.MethodGet,
+               Path:       "/apisix/prometheus/metrics",
+               ExpectBody: fmt.Sprintf("apisix_etcd_reachable %d", expectEtcd),
+       })
+}
+
+func runCommand(t *testing.T, cmd string) string {
+       out, err := exec.Command("bash", "-c", cmd).CombinedOutput()
+       if err != nil {
+               t.Fatalf("fail to run command %s: %s, %s", cmd, err.Error(), 
out)
+       }
+       return string(out)
+}
+
+func TestGetSuccessWhenEtcdKilled(t *testing.T) {
+       g := NewWithT(t)
+       e := httpexpect.New(t, host)
+
+       // check if everything works
+       setRoute(e, http.StatusCreated)
+       getRoute(e, http.StatusOK)
+       getPrometheusMetric(e, 1)
+
+       podName := runCommand(t, "kubectl get pod -l app=apisix-gw -o 
'jsonpath={..metadata.name}'")
+       t.Run("error log not contains etcd error", func(t *testing.T) {
+               errorLog := runCommand(t, fmt.Sprintf("kubectl exec -it %s -- 
cat logs/error.log", podName))
+               g.Expect(strings.Contains(errorLog, "failed to fetch data from 
etcd")).To(BeFalse())
+       })
+
+       // TODO: use client-go
+       // apply chaos to kill all etcd pods
+       t.Run("kill all etcd pods", func(t *testing.T) {
+               _ = runCommand(t, "kubectl apply -f kill-etcd.yaml")
+               time.Sleep(30)

Review comment:
       have to sleep so long?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to