|
| 1 | +package http_tunnel |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "regexp" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/suite" |
| 11 | + |
| 12 | + "github.com/solo-io/gloo/pkg/utils/kubeutils" |
| 13 | + "github.com/solo-io/gloo/pkg/utils/requestutils/curl" |
| 14 | + "github.com/solo-io/gloo/projects/gateway/pkg/defaults" |
| 15 | + "github.com/solo-io/gloo/test/gomega/matchers" |
| 16 | + "github.com/solo-io/gloo/test/kubernetes/e2e" |
| 17 | + testDefaults "github.com/solo-io/gloo/test/kubernetes/e2e/defaults" |
| 18 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 19 | + |
| 20 | + _ "embed" |
| 21 | +) |
| 22 | + |
| 23 | +const ( |
| 24 | + httpbinExampleCom = "httpbin.example.com" |
| 25 | +) |
| 26 | + |
| 27 | +var _ e2e.NewSuiteFunc = NewTestingSuite |
| 28 | + |
| 29 | +//go:embed testdata/squid.yaml |
| 30 | +var squidYaml []byte |
| 31 | + |
| 32 | +//go:embed testdata/proxy.yaml |
| 33 | +var proxyYaml []byte |
| 34 | + |
| 35 | +//go:embed testdata/edge.yaml |
| 36 | +var edgeYaml []byte |
| 37 | + |
| 38 | +//go:embed testdata/gateway.yaml |
| 39 | +var gatewayYaml []byte |
| 40 | + |
| 41 | +// testingSuite is the entire Suite of tests for the HTTP Tunnel feature |
| 42 | +type testingSuite struct { |
| 43 | + suite.Suite |
| 44 | + ctx context.Context |
| 45 | + testInstallation *e2e.TestInstallation |
| 46 | +} |
| 47 | + |
| 48 | +func NewTestingSuite( |
| 49 | + ctx context.Context, |
| 50 | + testInst *e2e.TestInstallation, |
| 51 | +) suite.TestingSuite { |
| 52 | + return &testingSuite{ |
| 53 | + ctx: ctx, |
| 54 | + testInstallation: testInst, |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func (s *testingSuite) SetupSuite() { |
| 59 | + err := s.testInstallation.Actions.Kubectl().Apply(s.ctx, squidYaml) |
| 60 | + s.Require().NoError(err) |
| 61 | + |
| 62 | + err = s.testInstallation.Actions.Kubectl().Apply(s.ctx, testDefaults.HttpbinYaml) |
| 63 | + s.Require().NoError(err) |
| 64 | + |
| 65 | + err = s.testInstallation.Actions.Kubectl().Apply(s.ctx, testDefaults.CurlPodYaml) |
| 66 | + s.Require().NoError(err) |
| 67 | + |
| 68 | + err = s.testInstallation.Actions.Kubectl().Apply(s.ctx, proxyYaml) |
| 69 | + s.Require().NoError(err) |
| 70 | + |
| 71 | + if s.testInstallation.Metadata.K8sGatewayEnabled { |
| 72 | + err = s.testInstallation.Actions.Kubectl().Apply(s.ctx, gatewayYaml) |
| 73 | + s.Require().NoError(err) |
| 74 | + } else { |
| 75 | + err = s.testInstallation.Actions.Kubectl().Apply(s.ctx, edgeYaml) |
| 76 | + s.Require().NoError(err) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func (s *testingSuite) TearDownSuite() { |
| 81 | + if s.testInstallation.Metadata.K8sGatewayEnabled { |
| 82 | + err := s.testInstallation.Actions.Kubectl().Delete(s.ctx, gatewayYaml) |
| 83 | + s.Require().NoError(err) |
| 84 | + } else { |
| 85 | + err := s.testInstallation.Actions.Kubectl().Delete(s.ctx, edgeYaml) |
| 86 | + s.Require().NoError(err) |
| 87 | + } |
| 88 | + |
| 89 | + err := s.testInstallation.Actions.Kubectl().Delete(s.ctx, proxyYaml) |
| 90 | + s.Require().NoError(err) |
| 91 | + |
| 92 | + err = s.testInstallation.Actions.Kubectl().Delete(s.ctx, testDefaults.CurlPodYaml) |
| 93 | + s.Require().NoError(err) |
| 94 | + |
| 95 | + err = s.testInstallation.Actions.Kubectl().Delete(s.ctx, testDefaults.HttpbinYaml) |
| 96 | + s.Require().NoError(err) |
| 97 | + |
| 98 | + err = s.testInstallation.Actions.Kubectl().Delete(s.ctx, squidYaml) |
| 99 | + s.Require().NoError(err) |
| 100 | +} |
| 101 | + |
| 102 | +func (s *testingSuite) AfterTest(suiteName, testName string) { |
| 103 | + if s.T().Failed() { |
| 104 | + s.testInstallation.PreFailHandler(s.ctx, e2e.PreFailHandlerOption{ |
| 105 | + TestName: testName, |
| 106 | + }) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func (s *testingSuite) TestHttpTunnel() { |
| 111 | + opts := []curl.Option{ |
| 112 | + curl.WithHostHeader(httpbinExampleCom), |
| 113 | + curl.WithPath("/headers"), |
| 114 | + } |
| 115 | + if s.testInstallation.Metadata.K8sGatewayEnabled { |
| 116 | + opts = append(opts, |
| 117 | + curl.WithHost(kubeutils.ServiceFQDN(metav1.ObjectMeta{ |
| 118 | + Name: "gloo-proxy-gw", |
| 119 | + Namespace: "default", |
| 120 | + })), |
| 121 | + ) |
| 122 | + } else { |
| 123 | + opts = append(opts, |
| 124 | + curl.WithHost(kubeutils.ServiceFQDN(metav1.ObjectMeta{ |
| 125 | + Name: defaults.GatewayProxyName, |
| 126 | + Namespace: s.testInstallation.Metadata.InstallNamespace, |
| 127 | + })), |
| 128 | + curl.WithPort(80), |
| 129 | + ) |
| 130 | + } |
| 131 | + |
| 132 | + // confirm that the httpbin service is reachable |
| 133 | + s.testInstallation.AssertionsT(s.T()).AssertEventualCurlResponse( |
| 134 | + s.ctx, |
| 135 | + testDefaults.CurlPodExecOpt, |
| 136 | + opts, |
| 137 | + &matchers.HttpResponse{ |
| 138 | + StatusCode: http.StatusOK, |
| 139 | + Body: matchers.JSONContains([]byte(`{"headers":{"Host":["httpbin.example.com"]}}`)), |
| 140 | + }, |
| 141 | + ) |
| 142 | + |
| 143 | + // confirm that the squid proxy connected to the httpbin service |
| 144 | + s.testInstallation.AssertionsT(s.T()).Assert.Eventually(func() bool { |
| 145 | + logs, err := s.testInstallation.Actions.Kubectl().GetContainerLogs(s.ctx, "default", "squid") |
| 146 | + if err != nil { |
| 147 | + fmt.Printf("Error getting squid logs: %v\n", err) |
| 148 | + return false |
| 149 | + } |
| 150 | + |
| 151 | + pattern := "TCP_TUNNEL/200 [0-9]+ CONNECT httpbin.httpbin.svc.cluster.local:8080" |
| 152 | + match, err := regexp.Match(pattern, []byte(logs)) |
| 153 | + if err != nil { |
| 154 | + fmt.Printf("Error matching squid logs: %v\n", err) |
| 155 | + return false |
| 156 | + } |
| 157 | + |
| 158 | + return match |
| 159 | + }, time.Second*30, time.Second*3, "squid logs should indicate a connection to the httpbin service") |
| 160 | +} |
0 commit comments