pull/16278/head
Sarah Alsmiller 2 years ago
parent b8a4c8319d
commit 184b6bbcb0

@ -132,7 +132,7 @@ func BasicPeeringTwoClustersSetup(
_, adminPort := clientSidecarService.GetAdminAddr()
libassert.AssertUpstreamEndpointStatus(t, adminPort, fmt.Sprintf("static-server.default.%s.external", DialingPeerName), "HEALTHY", 1)
_, port := clientSidecarService.GetAddr()
libassert.HTTPServiceEchoes(t, "localhost", port, "")
libassert.HTTPServiceEchoes(t, "localhost", port, "", nil)
libassert.AssertFortioName(t, fmt.Sprintf("http://localhost:%d", port), "static-server")
return &BuiltCluster{

@ -33,7 +33,7 @@ func TestBasicConnectService(t *testing.T) {
libassert.GetEnvoyListenerTCPFilters(t, adminPort)
libassert.AssertContainerState(t, clientService, "running")
libassert.HTTPServiceEchoes(t, "localhost", port, "")
libassert.HTTPServiceEchoes(t, "localhost", port, "", nil)
libassert.AssertFortioName(t, fmt.Sprintf("http://localhost:%d", port), "static-server")
}

@ -2,6 +2,8 @@ package gateways
import (
"context"
"crypto/tls"
"fmt"
"github.com/hashicorp/consul/api"
libassert "github.com/hashicorp/consul/test/integration/consul-container/libs/assert"
libcluster "github.com/hashicorp/consul/test/integration/consul-container/libs/cluster"
@ -9,14 +11,21 @@ import (
"github.com/hashicorp/consul/test/integration/consul-container/libs/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io"
"net/http"
"strings"
"testing"
"time"
)
func TestGateway(t *testing.T) {
}
var (
checkTimeout = 1 * time.Minute
checkInterval = 1 * time.Second
)
// Creates a gateway service and tests to see if it is routable
func TestAPIGatewayCreate(t *testing.T) {
t.Skip()
if testing.Short() {
t.Skip("too slow for testing.Short")
}
@ -65,23 +74,6 @@ func TestAPIGatewayCreate(t *testing.T) {
// Create a client proxy instance with the server as an upstream
_, gatewayService := createServices(t, cluster, listenerPortOne)
//how to exec into the consul CLI
//agentUrl, err := cluster.Agents[0].GetPod().PortEndpoint(context.Background(), "8500", "http")
//cmdStr := "consul connect envoy -gateway api -register -service api-gateway -proxy-id api-gateway -http-addr " + agentUrl
//
//c := strings.Split(cmdStr, " ")
//t.Log("------------\n\n\n")
//cmd := exec.Command(c[0], c[1:]...)
//out := bytes.NewBufferString("")
//stdErr := bytes.NewBufferString("")
//cmd.Stdout = out
//cmd.Stderr = stdErr
//err = cmd.Run()
//t.Log(out)
//t.Log(stdErr)
//t.Log("------------\n\n\n")
//assert.NoError(t, err)
//TODO this can and should be broken up more effectively, this is just proof of concept
//check statuses
gatewayReady := false
@ -100,7 +92,7 @@ func TestAPIGatewayCreate(t *testing.T) {
routeReady = isBound(routeEntry.Status.Conditions)
}
libassert.HTTPServiceEchoes(t, "localhost", gatewayService.GetPort(listenerPortOne), "")
libassert.HTTPServiceEchoes(t, "localhost", gatewayService.GetPort(listenerPortOne), "", nil)
}
func isAccepted(conditions []api.Condition) bool {
@ -193,3 +185,46 @@ func createServices(t *testing.T, cluster *libcluster.Cluster, ports ...int) (li
return clientConnectProxy, gatewayService
}
func checkRoute(t *testing.T, port int, path string, expectedStatusCode int, expectedBody string, headers map[string]string, message string) {
t.Helper()
require.Eventually(t, func() bool {
client := &http.Client{Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}}
req, err := http.NewRequest("GET", fmt.Sprintf("https://localhost:%d%s", port, path), nil)
if err != nil {
return false
}
for k, v := range headers {
req.Header.Set(k, v)
if k == "Host" {
req.Host = v
}
}
resp, err := client.Do(req)
if err != nil {
t.Log(err)
return false
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
t.Log(err)
return false
}
t.Log(string(data))
if resp.StatusCode != expectedStatusCode {
t.Log("status code", resp.StatusCode)
return false
}
return strings.HasPrefix(string(data), expectedBody)
}, checkTimeout, checkInterval, message)
}

@ -14,42 +14,6 @@ import (
"time"
)
// import (
//
// "context"
// "crypto/tls"
// "encoding/json"
// "fmt"
// "io"
// "net"
// "net/http"
// "os"
// "strings"
// "testing"
// "time"
//
// "github.com/hashicorp/consul/api"
// "github.com/stretchr/testify/assert"
// "github.com/stretchr/testify/require"
// "golang.org/x/exp/slices"
// apps "k8s.io/api/apps/v1"
// core "k8s.io/api/core/v1"
// meta "k8s.io/apimachinery/pkg/apis/meta/v1"
// "sigs.k8s.io/e2e-framework/klient/k8s/resources"
// "sigs.k8s.io/e2e-framework/pkg/env"
// "sigs.k8s.io/e2e-framework/pkg/envconf"
// "sigs.k8s.io/e2e-framework/pkg/features"
// gwv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
// gwv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
//
// "github.com/hashicorp/consul-api-gateway/internal/k8s"
// rstatus "github.com/hashicorp/consul-api-gateway/internal/k8s/reconciler/status"
// "github.com/hashicorp/consul-api-gateway/internal/testing/e2e"
// apigwv1alpha1 "github.com/hashicorp/consul-api-gateway/pkg/apis/v1alpha1"
// "math/rand"
//
// )
// )
func getNamespace() string {
return ""
}
@ -101,6 +65,18 @@ func TestHTTPRouteFlattening(t *testing.T) {
path2 := "/v2"
//write config entries
proxyDefaults := &api.ProxyConfigEntry{
Kind: api.ProxyDefaults,
Name: api.ProxyConfigGlobal,
Namespace: namespace,
Config: map[string]interface{}{
"protocol": "http",
},
}
_, _, err := client.ConfigEntries().Set(proxyDefaults, nil)
assert.NoError(t, err)
apiGateway := &api.APIGatewayConfigEntry{
Kind: "api-gateway",
Name: gatewayName,
@ -118,8 +94,9 @@ func TestHTTPRouteFlattening(t *testing.T) {
Name: routeOneName,
Parents: []api.ResourceReference{
{
Kind: api.HTTPRoute,
Name: gatewayName,
Kind: api.APIGateway,
Name: gatewayName,
Namespace: namespace,
},
},
Hostnames: []string{
@ -152,13 +129,13 @@ func TestHTTPRouteFlattening(t *testing.T) {
Name: routeTwoName,
Parents: []api.ResourceReference{
{
Kind: api.HTTPRoute,
Name: gatewayName,
Kind: api.APIGateway,
Name: gatewayName,
Namespace: namespace,
},
},
Hostnames: []string{
"test.foo",
"test.example",
},
Namespace: namespace,
Rules: []api.HTTPRouteRule{
@ -181,7 +158,7 @@ func TestHTTPRouteFlattening(t *testing.T) {
},
}
_, _, err := client.ConfigEntries().Set(apiGateway, nil)
_, _, err = client.ConfigEntries().Set(apiGateway, nil)
assert.NoError(t, err)
_, _, err = client.ConfigEntries().Set(routeOne, nil)
assert.NoError(t, err)
@ -201,6 +178,7 @@ func TestHTTPRouteFlattening(t *testing.T) {
return false
}
apiEntry := entry.(*api.APIGatewayConfigEntry)
t.Log(entry)
return isAccepted(apiEntry.Status.Conditions)
}, time.Second*10, time.Second*1)
@ -228,8 +206,27 @@ func TestHTTPRouteFlattening(t *testing.T) {
}, time.Second*10, time.Second*1)
//gateway resolves
//libassert.HTTPServiceEchoes(t, "localhost", gatewayService.GetPort(listenerPort), path1)
//libassert.HTTPServiceEchoes(t, "localhost", gatewayService.GetPort(listenerPort), path2)
libassert.HTTPServiceEchoes(t, "localhost", gatewayService.GetPort(listenerPort), "", map[string]string{
"Host": "test.foo",
"x-v2": "v2",
})
for {
}
//libassert.HTTPServiceEchoes(t, "localhost", gatewayService.GetPort(listenerPort), "v2")
//checkRoute(t,
// gatewayService.GetPort(listenerPort),
// "/v2/test",
// http.StatusOK,
// "service2",
// "POST",
// map[string]string{
// "Host": "test.foo",
// "x-v2": "v2",
// },
// "service two not routable in allotted time",
//)
fmt.Println(gatewayService)
//

Loading…
Cancel
Save