|
| 1 | +// Package transient runs a chainsaw built from the e2e sandbox's patched tree |
| 2 | +// against a fake apiserver that answers the asserted object's GET with |
| 3 | +// HTTP 500 and an empty reason, which is what kube-apiserver sends for an etcd |
| 4 | +// error such as `etcdserver: leader changed`. |
| 5 | +// Release v0.2.15 ends an assert on the first such answer; the image carries |
| 6 | +// a patch that keeps it polling until its own timeout. |
| 7 | +package transient |
| 8 | + |
| 9 | +import ( |
| 10 | + "encoding/json" |
| 11 | + "fmt" |
| 12 | + "net/http" |
| 13 | + "net/http/httptest" |
| 14 | + "os" |
| 15 | + "os/exec" |
| 16 | + "path/filepath" |
| 17 | + "sync/atomic" |
| 18 | + "testing" |
| 19 | + "time" |
| 20 | +) |
| 21 | + |
| 22 | +const assertTimeout = 10 * time.Second |
| 23 | + |
| 24 | +func writeJSON(w http.ResponseWriter, code int, v any) { |
| 25 | + w.Header().Set("Content-Type", "application/json") |
| 26 | + w.WriteHeader(code) |
| 27 | + _ = json.NewEncoder(w).Encode(v) |
| 28 | +} |
| 29 | + |
| 30 | +func status(code int, reason, message string) map[string]any { |
| 31 | + return map[string]any{ |
| 32 | + "kind": "Status", "apiVersion": "v1", "status": "Failure", |
| 33 | + "code": code, "reason": reason, "message": message, |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// fakeAPIServer serves just enough discovery for chainsaw to resolve |
| 38 | +// v1/ConfigMap, and answers GET default/probe with 500 for the first |
| 39 | +// `failures` requests (all of them when failures < 0), then with the object. |
| 40 | +func fakeAPIServer(t *testing.T, failures int64) (*httptest.Server, *atomic.Int64) { |
| 41 | + t.Helper() |
| 42 | + var gets atomic.Int64 |
| 43 | + mux := http.NewServeMux() |
| 44 | + mux.HandleFunc("/version", func(w http.ResponseWriter, _ *http.Request) { |
| 45 | + writeJSON(w, http.StatusOK, map[string]string{"major": "1", "minor": "33", "gitVersion": "v1.33.0"}) |
| 46 | + }) |
| 47 | + mux.HandleFunc("/api", func(w http.ResponseWriter, _ *http.Request) { |
| 48 | + writeJSON(w, http.StatusOK, map[string]any{ |
| 49 | + "kind": "APIVersions", "versions": []string{"v1"}, |
| 50 | + "serverAddressByClientCIDRs": []map[string]string{{"clientCIDR": "0.0.0.0/0", "serverAddress": "127.0.0.1"}}, |
| 51 | + }) |
| 52 | + }) |
| 53 | + mux.HandleFunc("/apis", func(w http.ResponseWriter, _ *http.Request) { |
| 54 | + writeJSON(w, http.StatusOK, map[string]any{"kind": "APIGroupList", "apiVersion": "v1", "groups": []any{}}) |
| 55 | + }) |
| 56 | + mux.HandleFunc("/api/v1", func(w http.ResponseWriter, _ *http.Request) { |
| 57 | + verbs := []string{"create", "delete", "get", "list", "patch", "update", "watch"} |
| 58 | + writeJSON(w, http.StatusOK, map[string]any{ |
| 59 | + "kind": "APIResourceList", "apiVersion": "v1", "groupVersion": "v1", |
| 60 | + "resources": []map[string]any{ |
| 61 | + {"name": "namespaces", "singularName": "namespace", "namespaced": false, "kind": "Namespace", "verbs": verbs}, |
| 62 | + {"name": "configmaps", "singularName": "configmap", "namespaced": true, "kind": "ConfigMap", "verbs": verbs}, |
| 63 | + }, |
| 64 | + }) |
| 65 | + }) |
| 66 | + mux.HandleFunc("/api/v1/namespaces/default", func(w http.ResponseWriter, _ *http.Request) { |
| 67 | + writeJSON(w, http.StatusOK, map[string]any{ |
| 68 | + "kind": "Namespace", "apiVersion": "v1", |
| 69 | + "metadata": map[string]any{"name": "default"}, |
| 70 | + "status": map[string]any{"phase": "Active"}, |
| 71 | + }) |
| 72 | + }) |
| 73 | + mux.HandleFunc("/api/v1/namespaces/default/configmaps/probe", func(w http.ResponseWriter, r *http.Request) { |
| 74 | + if r.Method != http.MethodGet { |
| 75 | + writeJSON(w, http.StatusMethodNotAllowed, status(http.StatusMethodNotAllowed, "MethodNotAllowed", r.Method)) |
| 76 | + return |
| 77 | + } |
| 78 | + n := gets.Add(1) |
| 79 | + if failures < 0 || n <= failures { |
| 80 | + writeJSON(w, http.StatusInternalServerError, status(http.StatusInternalServerError, "", "etcdserver: leader changed")) |
| 81 | + return |
| 82 | + } |
| 83 | + writeJSON(w, http.StatusOK, map[string]any{ |
| 84 | + "kind": "ConfigMap", "apiVersion": "v1", |
| 85 | + "metadata": map[string]any{"name": "probe", "namespace": "default"}, |
| 86 | + "data": map[string]string{"ready": "true"}, |
| 87 | + }) |
| 88 | + }) |
| 89 | + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 90 | + writeJSON(w, http.StatusNotFound, status(http.StatusNotFound, "NotFound", r.URL.Path)) |
| 91 | + }) |
| 92 | + srv := httptest.NewServer(mux) |
| 93 | + t.Cleanup(srv.Close) |
| 94 | + return srv, &gets |
| 95 | +} |
| 96 | + |
| 97 | +func runChainsaw(t *testing.T, server string) (time.Duration, string, error) { |
| 98 | + t.Helper() |
| 99 | + bin := os.Getenv("CHAINSAW") |
| 100 | + // The image build sets CHAINSAW; `make go-module-tests` reaches this module |
| 101 | + // too, with no patched binary to test. |
| 102 | + if bin == "" { |
| 103 | + t.Skip("CHAINSAW is not set to the chainsaw binary under test") |
| 104 | + } |
| 105 | + dir := t.TempDir() |
| 106 | + kubeconfig := filepath.Join(dir, "kubeconfig") |
| 107 | + if err := os.WriteFile(kubeconfig, []byte(fmt.Sprintf(`apiVersion: v1 |
| 108 | +kind: Config |
| 109 | +clusters: |
| 110 | +- name: fake |
| 111 | + cluster: |
| 112 | + server: %s |
| 113 | +contexts: |
| 114 | +- name: fake |
| 115 | + context: |
| 116 | + cluster: fake |
| 117 | + user: fake |
| 118 | +current-context: fake |
| 119 | +users: |
| 120 | +- name: fake |
| 121 | + user: {} |
| 122 | +`, server)), 0o600); err != nil { |
| 123 | + t.Fatal(err) |
| 124 | + } |
| 125 | + suite := filepath.Join(dir, "suite") |
| 126 | + if err := os.Mkdir(suite, 0o755); err != nil { |
| 127 | + t.Fatal(err) |
| 128 | + } |
| 129 | + if err := os.WriteFile(filepath.Join(suite, "chainsaw-test.yaml"), []byte(fmt.Sprintf(`apiVersion: chainsaw.kyverno.io/v1alpha1 |
| 130 | +kind: Test |
| 131 | +metadata: |
| 132 | + name: transient |
| 133 | +spec: |
| 134 | + namespace: default |
| 135 | + skipDelete: true |
| 136 | + steps: |
| 137 | + - try: |
| 138 | + - assert: |
| 139 | + timeout: %s |
| 140 | + resource: |
| 141 | + apiVersion: v1 |
| 142 | + kind: ConfigMap |
| 143 | + metadata: |
| 144 | + name: probe |
| 145 | + namespace: default |
| 146 | + data: |
| 147 | + ready: "true" |
| 148 | +`, assertTimeout)), 0o600); err != nil { |
| 149 | + t.Fatal(err) |
| 150 | + } |
| 151 | + cmd := exec.Command(bin, "test", "--test-dir", suite, "--no-color") |
| 152 | + cmd.Env = append(os.Environ(), "KUBECONFIG="+kubeconfig) |
| 153 | + start := time.Now() |
| 154 | + out, err := cmd.CombinedOutput() |
| 155 | + return time.Since(start), string(out), err |
| 156 | +} |
| 157 | + |
| 158 | +// A run of 500s shorter than the timeout must not fail the assert: the poll |
| 159 | +// carries on and passes once the object comes back. |
| 160 | +func TestAssertSurvivesTransientServerErrors(t *testing.T) { |
| 161 | + srv, gets := fakeAPIServer(t, 5) |
| 162 | + elapsed, out, err := runChainsaw(t, srv.URL) |
| 163 | + if err != nil { |
| 164 | + t.Fatalf("chainsaw failed after %s and %d GETs of the object, want a pass once the 500s stop:\n%s", elapsed, gets.Load(), out) |
| 165 | + } |
| 166 | + if gets.Load() <= 5 { |
| 167 | + t.Fatalf("chainsaw passed after only %d GETs of the object, so it never saw the 500s:\n%s", gets.Load(), out) |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +// When the 500s never stop, the assert still fails, and it fails by its own |
| 172 | +// timeout rather than on the first error. |
| 173 | +func TestAssertFailsAtItsTimeoutOnPersistentServerErrors(t *testing.T) { |
| 174 | + srv, gets := fakeAPIServer(t, -1) |
| 175 | + elapsed, out, err := runChainsaw(t, srv.URL) |
| 176 | + if err == nil { |
| 177 | + t.Fatalf("chainsaw passed against an apiserver that only answers 500:\n%s", out) |
| 178 | + } |
| 179 | + if elapsed < assertTimeout || gets.Load() < 2 { |
| 180 | + t.Fatalf("chainsaw failed after %s and %d GETs of the object, want the %s assert timeout and more than one GET:\n%s", elapsed, gets.Load(), assertTimeout, out) |
| 181 | + } |
| 182 | +} |
0 commit comments