Skip to content

Commit

Permalink
Remove unused code, retry gateway connection, improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
nathancoleman committed Nov 14, 2024
1 parent 44d9361 commit 2d6e8a0
Showing 1 changed file with 11 additions and 63 deletions.
74 changes: 11 additions & 63 deletions acceptance/tests/openshift/basic_openshift_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,21 @@ import (
"context"
"crypto/tls"
"encoding/json"
"log"
"net/http"
"os/exec"
"testing"
"time"

"github.com/go-logr/logr"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/sdk/testutil/retry"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/types"
logf "sigs.k8s.io/controller-runtime/pkg/log"
gwv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"

"github.com/hashicorp/consul-k8s/acceptance/framework/helpers"
"github.com/hashicorp/consul-k8s/acceptance/framework/logger"

"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// Test that api gateway basic functionality works in a default installation and a secure installation.
Expand Down Expand Up @@ -71,13 +67,9 @@ func TestOpenshift_Basic(t *testing.T) {
logf.SetLogger(logr.New(nil))
logger.Log(t, "creating resources for OpenShift test")

kubectlCmd := exec.Command("kubectl", "apply", "-f", "../fixtures/cases/openshift/basic")

output, err = kubectlCmd.CombinedOutput()
if err != nil {
log.Fatal(string(output))
require.NoError(t, err)
}
cmd = exec.Command("kubectl", "apply", "-f", "../fixtures/cases/openshift/basic")
output, err = cmd.CombinedOutput()
require.NoErrorf(t, err, "failed to create resources: %s", string(output))

helpers.Cleanup(t, cfg.NoCleanupOnFailure, cfg.NoCleanup, func() {
cmd := exec.Command("kubectl", "delete", "-f", "../fixtures/cases/openshift/basic")
Expand Down Expand Up @@ -114,9 +106,13 @@ func TestOpenshift_Basic(t *testing.T) {
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}}

resp, err := client.Get("https://" + gatewayIP)
require.NoError(t, err)
assert.Equal(t, resp.StatusCode, http.StatusOK)
var resp *http.Response
counter = &retry.Counter{Count: 120, Wait: 2 * time.Second}
retry.RunWith(counter, t, func(r *retry.R) {
resp, err = client.Get("https://" + gatewayIP)
require.NoErrorf(t, err, "request to API gateway failed: %s", err)
assert.Equalf(t, resp.StatusCode, http.StatusOK, "request to API gateway returned failure code: %d", resp.StatusCode)
})

var body struct {
Body string `json:"body"`
Expand Down Expand Up @@ -144,51 +140,3 @@ func TestOpenshift_Basic(t *testing.T) {
assert.Equal(t, 200, backend.Code)
assert.Equal(t, "backend", backend.Name)
}

func checkStatusCondition(t require.TestingT, conditions []metav1.Condition, toCheck metav1.Condition) {
for _, c := range conditions {
if c.Type == toCheck.Type {
require.EqualValues(t, toCheck.Reason, c.Reason)
require.EqualValues(t, toCheck.Status, c.Status)
return
}
}

t.Errorf("expected condition not found: %s", toCheck.Type)
}

func trueCondition(conditionType, reason string) metav1.Condition {
return metav1.Condition{
Type: conditionType,
Reason: reason,
Status: metav1.ConditionTrue,
}
}

func falseCondition(conditionType, reason string) metav1.Condition {
return metav1.Condition{
Type: conditionType,
Reason: reason,
Status: metav1.ConditionFalse,
}
}

func checkConsulStatusCondition(t require.TestingT, conditions []api.Condition, toCheck api.Condition) {
for _, c := range conditions {
if c.Type == toCheck.Type {
require.EqualValues(t, toCheck.Reason, c.Reason)
require.EqualValues(t, toCheck.Status, c.Status)
return
}
}

t.Errorf("expected condition not found: %s", toCheck.Type)
}

func trueConsulCondition(conditionType, reason string) api.Condition {
return api.Condition{
Type: conditionType,
Reason: reason,
Status: "True",
}
}

0 comments on commit 2d6e8a0

Please sign in to comment.