...

Source file src/github.com/linkerd/linkerd2/test/integration/deep/egress/egress_test.go

Documentation: github.com/linkerd/linkerd2/test/integration/deep/egress

     1  package egress
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/linkerd/linkerd2/testutil"
    10  )
    11  
    12  //////////////////////
    13  ///   TEST SETUP   ///
    14  //////////////////////
    15  
    16  var TestHelper *testutil.TestHelper
    17  
    18  func TestMain(m *testing.M) {
    19  	TestHelper = testutil.NewTestHelper()
    20  	// Block test execution until control plane is running
    21  	TestHelper.WaitUntilDeployReady(testutil.LinkerdDeployReplicasEdge)
    22  	os.Exit(m.Run())
    23  }
    24  
    25  //////////////////////
    26  /// TEST EXECUTION ///
    27  //////////////////////
    28  
    29  func TestEgressHttp(t *testing.T) {
    30  	ctx := context.Background()
    31  	out, err := TestHelper.LinkerdRun("inject", "testdata/proxy.yaml")
    32  	if err != nil {
    33  		testutil.AnnotatedFatal(t, "unexpected error", err)
    34  	}
    35  
    36  	TestHelper.WithDataPlaneNamespace(ctx, "egress-test", map[string]string{}, t, func(t *testing.T, ns string) {
    37  
    38  		out, err = TestHelper.KubectlApply(out, ns)
    39  		if err != nil {
    40  			testutil.AnnotatedFatalf(t, "unexpected error", "unexpected error: %v output:\n%s", err, out)
    41  		}
    42  
    43  		err = TestHelper.CheckPods(ctx, ns, "egress-test", 1)
    44  		if err != nil {
    45  			//nolint:errorlint
    46  			if rce, ok := err.(*testutil.RestartCountError); ok {
    47  				testutil.AnnotatedWarn(t, "CheckPods timed-out", rce)
    48  			} else {
    49  				testutil.AnnotatedError(t, "CheckPods timed-out", err)
    50  			}
    51  		}
    52  
    53  		testCase := func(url, methodToUse string) {
    54  			testName := fmt.Sprintf("Can use egress to send %s request to (%s)", methodToUse, url)
    55  			t.Run(testName, func(t *testing.T) {
    56  				cmd := []string{
    57  					"-n", ns, "exec", "deploy/egress-test", "-c", "http-egress",
    58  					"--", "curl", "-sko", "/dev/null", "-w", "%{http_code}", "-X", methodToUse, url,
    59  				}
    60  				out, err := TestHelper.Kubectl("", cmd...)
    61  				if err != nil {
    62  					testutil.AnnotatedFatalf(t, fmt.Sprintf("failed to exec %s", cmd), "failed to exec %s: %s (%s)", cmd, err, out)
    63  				}
    64  
    65  				var status int
    66  				_, err = fmt.Sscanf(out, "%d", &status)
    67  				if err != nil {
    68  					testutil.AnnotatedFatalf(t, "failed to parse status code", "failed to parse status code (%s): %s", out, err)
    69  				}
    70  
    71  				if status < 100 || status >= 500 {
    72  					testutil.Fatalf(t, "got HTTP error code: %d\n", status)
    73  				}
    74  			})
    75  		}
    76  
    77  		supportedProtocols := []string{"http", "https"}
    78  		methods := []string{"GET", "POST"}
    79  		for _, protocolToUse := range supportedProtocols {
    80  			for _, methodToUse := range methods {
    81  				serviceName := fmt.Sprintf("%s://www.linkerd.io", protocolToUse)
    82  				testCase(serviceName, methodToUse)
    83  			}
    84  		}
    85  
    86  		// Test egress for a domain with fewer than 3 segments.
    87  		testCase("http://linkerd.io", "GET")
    88  
    89  	})
    90  }
    91  

View as plain text