|
| 1 | +package backends |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "path/filepath" |
| 7 | + |
| 8 | + "github.com/onsi/gomega" |
| 9 | + "github.com/stretchr/testify/suite" |
| 10 | + appsv1 "k8s.io/api/apps/v1" |
| 11 | + corev1 "k8s.io/api/core/v1" |
| 12 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 13 | + |
| 14 | + "github.com/kgateway-dev/kgateway/v2/api/v1alpha1" |
| 15 | + "github.com/kgateway-dev/kgateway/v2/pkg/utils/fsutils" |
| 16 | + "github.com/kgateway-dev/kgateway/v2/pkg/utils/kubeutils" |
| 17 | + "github.com/kgateway-dev/kgateway/v2/pkg/utils/requestutils/curl" |
| 18 | + "github.com/kgateway-dev/kgateway/v2/test/gomega/matchers" |
| 19 | + "github.com/kgateway-dev/kgateway/v2/test/kubernetes/e2e" |
| 20 | + "github.com/kgateway-dev/kgateway/v2/test/kubernetes/e2e/defaults" |
| 21 | +) |
| 22 | + |
| 23 | +var _ e2e.NewSuiteFunc = NewTestingSuite |
| 24 | + |
| 25 | +var ( |
| 26 | + manifests = []string{ |
| 27 | + filepath.Join(fsutils.MustGetThisDir(), "inputs/base.yaml"), |
| 28 | + // backend in separate manifest to allow creation independently of routing config |
| 29 | + filepath.Join(fsutils.MustGetThisDir(), "inputs/backend.yaml"), |
| 30 | + defaults.CurlPodManifest, |
| 31 | + } |
| 32 | + |
| 33 | + proxyObjMeta = metav1.ObjectMeta{ |
| 34 | + Name: "gw", |
| 35 | + Namespace: "default", |
| 36 | + } |
| 37 | + proxyDeployment = &appsv1.Deployment{ObjectMeta: proxyObjMeta} |
| 38 | + proxyService = &corev1.Service{ObjectMeta: proxyObjMeta} |
| 39 | + |
| 40 | + backendMeta = metav1.ObjectMeta{ |
| 41 | + Name: "nginx-static", |
| 42 | + Namespace: "default", |
| 43 | + } |
| 44 | + backend = &v1alpha1.Backend{ |
| 45 | + ObjectMeta: backendMeta, |
| 46 | + } |
| 47 | + |
| 48 | + nginxMeta = metav1.ObjectMeta{ |
| 49 | + Name: "nginx", |
| 50 | + Namespace: "default", |
| 51 | + } |
| 52 | +) |
| 53 | + |
| 54 | +type testingSuite struct { |
| 55 | + suite.Suite |
| 56 | + ctx context.Context |
| 57 | + testInstallation *e2e.TestInstallation |
| 58 | +} |
| 59 | + |
| 60 | +func NewTestingSuite(ctx context.Context, testInst *e2e.TestInstallation) suite.TestingSuite { |
| 61 | + return &testingSuite{ |
| 62 | + ctx: ctx, |
| 63 | + testInstallation: testInst, |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func (s *testingSuite) TestConfigureBackingDestinationsWithUpstream() { |
| 68 | + s.T().Cleanup(func() { |
| 69 | + for _, manifest := range manifests { |
| 70 | + err := s.testInstallation.Actions.Kubectl().DeleteFileSafe(s.ctx, manifest) |
| 71 | + s.Require().NoError(err) |
| 72 | + } |
| 73 | + s.testInstallation.Assertions.EventuallyObjectsNotExist(s.ctx, proxyService, proxyDeployment, backend) |
| 74 | + }) |
| 75 | + |
| 76 | + for _, manifest := range manifests { |
| 77 | + err := s.testInstallation.Actions.Kubectl().ApplyFile(s.ctx, manifest) |
| 78 | + s.Require().NoError(err) |
| 79 | + } |
| 80 | + |
| 81 | + // assert the expected resources are created and running before attempting to send traffic |
| 82 | + s.testInstallation.Assertions.EventuallyObjectsExist(s.ctx, proxyService, proxyDeployment, backend) |
| 83 | + // TODO: make this a specific assertion to remove the need for c/p the label selector |
| 84 | + // e.g. EventuallyCurlPodRunning(...) etc. |
| 85 | + s.testInstallation.Assertions.EventuallyPodsRunning(s.ctx, defaults.CurlPod.GetNamespace(), metav1.ListOptions{ |
| 86 | + LabelSelector: "app.kubernetes.io/name=curl", |
| 87 | + }) |
| 88 | + s.testInstallation.Assertions.EventuallyPodsRunning(s.ctx, nginxMeta.GetNamespace(), metav1.ListOptions{ |
| 89 | + LabelSelector: "app.kubernetes.io/name=nginx", |
| 90 | + }) |
| 91 | + s.testInstallation.Assertions.EventuallyPodsRunning(s.ctx, proxyObjMeta.GetNamespace(), metav1.ListOptions{ |
| 92 | + LabelSelector: "app.kubernetes.io/name=gw", |
| 93 | + }) |
| 94 | + |
| 95 | + s.testInstallation.Assertions.AssertEventualCurlResponse( |
| 96 | + s.ctx, |
| 97 | + defaults.CurlPodExecOpt, |
| 98 | + []curl.Option{ |
| 99 | + curl.WithHost(kubeutils.ServiceFQDN(proxyService.ObjectMeta)), |
| 100 | + curl.WithHostHeader("example.com"), |
| 101 | + curl.WithPath("/"), |
| 102 | + }, |
| 103 | + &matchers.HttpResponse{ |
| 104 | + StatusCode: http.StatusOK, |
| 105 | + Body: gomega.ContainSubstring(defaults.NginxResponse), |
| 106 | + }) |
| 107 | +} |
0 commit comments