From f0bbbbb6fd1e3a445a9c4273f335f71f1fc95d73 Mon Sep 17 00:00:00 2001 From: Chris Thain Date: Fri, 30 Jun 2023 09:35:47 -0700 Subject: [PATCH] Cleanup test code --- .../test/envoy_extensions/ext_authz_test.go | 53 ++++-------------- .../testdata/policies/bundle.tar.gz | Bin 281 -> 0 bytes 2 files changed, 10 insertions(+), 43 deletions(-) delete mode 100644 test/integration/consul-container/test/envoy_extensions/testdata/policies/bundle.tar.gz diff --git a/test/integration/consul-container/test/envoy_extensions/ext_authz_test.go b/test/integration/consul-container/test/envoy_extensions/ext_authz_test.go index 30775d8af0f7..938981c60f51 100644 --- a/test/integration/consul-container/test/envoy_extensions/ext_authz_test.go +++ b/test/integration/consul-container/test/envoy_extensions/ext_authz_test.go @@ -15,12 +15,12 @@ import ( "github.com/testcontainers/testcontainers-go" "github.com/hashicorp/consul/api" + "github.com/hashicorp/consul/sdk/testutil/retry" libassert "github.com/hashicorp/consul/test/integration/consul-container/libs/assert" libcluster "github.com/hashicorp/consul/test/integration/consul-container/libs/cluster" libservice "github.com/hashicorp/consul/test/integration/consul-container/libs/service" "github.com/hashicorp/consul/test/integration/consul-container/libs/topology" "github.com/hashicorp/go-cleanhttp" - "github.com/hashicorp/go-multierror" ) // TestExtAuthzLocal Summary @@ -65,7 +65,7 @@ func TestExtAuthzLocal(t *testing.T) { libassert.AssertContainerState(t, clientService, "running") libassert.AssertFortioName(t, fmt.Sprintf("http://localhost:%d", port), "static-server", "") - // wire up the ext-authz envoy extension for the static-server + // Wire up the ext-authz envoy extension for the static-server consul := cluster.APIClient(0) defaults := api.ServiceConfigEntry{ Kind: api.ServiceDefaults, @@ -84,6 +84,8 @@ func TestExtAuthzLocal(t *testing.T) { } consul.ConfigEntries().Set(&defaults, nil) + // Make requests to the static-server. We expect that all requests are rejected with 403 Forbidden + // unless they are to the /allow path. baseURL := fmt.Sprintf("http://localhost:%d", port) doRequest(t, baseURL, http.StatusForbidden) doRequest(t, baseURL+"/allow", http.StatusOK) @@ -139,7 +141,7 @@ func createLocalAuthzService(t *testing.T, cluster *libcluster.Cluster) { "--set=decision_logs.console=true", "--set=status.console=true", "--ignore=.*", - "/testdata/policies/bundle.tar.gz", + "/testdata/policies/policy.rego", }, Mounts: []testcontainers.ContainerMount{{ Source: testcontainers.DockerBindMountSource{ @@ -160,44 +162,9 @@ func createLocalAuthzService(t *testing.T, cluster *libcluster.Cluster) { } func doRequest(t *testing.T, url string, expStatus int) { - var errs error - for i := 0; i < 5; i++ { - req, err := http.NewRequest(http.MethodGet, url, nil) - if err != nil { - errs = multierror.Append(errs, fmt.Errorf("failed to create HTTP request: %w", err)) - } - res, err := cleanhttp.DefaultClient().Do(req) - if err == nil { - res.Body.Close() - fmt.Printf("\n\n!!! GET %s: exp %d, obs %d\n\n", url, expStatus, res.StatusCode) - if res.StatusCode == expStatus { - return - } else { - errs = multierror.Append(errs, fmt.Errorf("unexpected status code: want: %d, have: %d", expStatus, res.StatusCode)) - } - } else { - errs = multierror.Append(errs, fmt.Errorf("unexpected error: %w", err)) - } - time.Sleep(time.Duration(i+1) * time.Second) - } - t.Fatalf("request failed:\n%s", errs.Error()) -} - -type MeshServiceRequest struct { - Agent libcluster.Agent - ServiceOpts *libservice.ServiceOpts - ContainerRequest testcontainers.ContainerRequest - MapPorts []string - DisableTestdataMount bool -} - -func Wait() { - for { - _, err := os.Stat("continue") - if err == nil { - _ = os.Remove("continue") - break - } - time.Sleep(time.Second) - } + retry.RunWith(&retry.Timer{Timeout: 5 * time.Second, Wait: time.Second}, t, func(r *retry.R) { + resp, err := cleanhttp.DefaultClient().Get(url) + require.NoError(r, err) + require.Equal(r, expStatus, resp.StatusCode) + }) } diff --git a/test/integration/consul-container/test/envoy_extensions/testdata/policies/bundle.tar.gz b/test/integration/consul-container/test/envoy_extensions/testdata/policies/bundle.tar.gz deleted file mode 100644 index a689dc734fd0e2d5f751d6f1a1e4fe81e0af143d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 281 zcmV+!0p|W6iwFP!00000|LoMyN;5GK0PwvwPch{5eNeK~R(lY76koubh)lcLKQ_CG z$;7f2-(9StwH|tqwFvoLCK(uJa+u@=F{%4tyKFFsh;zhU#P1OiXM1PNa5BO3Nd`FF zNS|V0@)`cP*Wu&-i0!jD^$5uc<<6RB)hn+nJ5-Nj`#+n`5BL9UHck8g8yv0HRB6!N zqwN(%uAhWxT4y~&8992b*1F$VUxbf*)49lsn0>R1tb_9UA?m=YW_CaUUgO<+QiwvA z6b%CzV>fWIfRaqmLcHCZ61HNrU!hvfwJl(=0C{zJEybuZcBxw8RsB!qyD~Y38=1@7 f@t0o85!e4k`qGjlNsfC700960fVRhk01N;Cy6B6|