Skip to content

Commit

Permalink
Cleanup test code
Browse files Browse the repository at this point in the history
  • Loading branch information
cthain committed Jun 30, 2023
1 parent d2203de commit f0bbbbb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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{
Expand All @@ -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)
})
}
Binary file not shown.

0 comments on commit f0bbbbb

Please sign in to comment.