Path: blob/main/test/tests/components/ws-daemon/network_limiting_test.go
2501 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package wsdaemon56import (7"context"8"os"9"testing"10"time"1112"github.com/gitpod-io/gitpod/common-go/kubernetes"13daemon "github.com/gitpod-io/gitpod/test/pkg/agent/daemon/api"14"github.com/gitpod-io/gitpod/test/pkg/integration"15corev1 "k8s.io/api/core/v1"16"sigs.k8s.io/e2e-framework/pkg/envconf"17"sigs.k8s.io/e2e-framework/pkg/features"18)1920func TestNetworkLimiting(t *testing.T) {21userToken, _ := os.LookupEnv("USER_TOKEN")22integration.SkipWithoutUsername(t, username)23integration.SkipWithoutUserToken(t, userToken)2425f := features.New("network limiting").26WithLabel("component", "ws-daemon").27Assess("verify if network limiting works fine", func(testCtx context.Context, t *testing.T, cfg *envconf.Config) context.Context {28t.Parallel()2930ctx, cancel := context.WithTimeout(testCtx, 5*time.Minute)31defer cancel()3233api := integration.NewComponentAPI(ctx, cfg.Namespace(), kubeconfig, cfg.Client())34t.Cleanup(func() {35api.Done(t)36})3738_, err := api.CreateUser(username, userToken)39if err != nil {40t.Fatal(err)41}4243ws, stopWs, err := integration.LaunchWorkspaceFromContextURL(t, ctx, "https://github.com/gitpod-io/empty", username, api, integration.WithGitpodUser(username))44if err != nil {45t.Fatal(err)46}47t.Cleanup(func() {48sctx, scancel := context.WithTimeout(context.Background(), 5*time.Minute)49defer scancel()5051sapi := integration.NewComponentAPI(sctx, cfg.Namespace(), kubeconfig, cfg.Client())52defer sapi.Done(t)5354_, err = stopWs(true, sapi)55if err != nil {56t.Fatal(err)57}58})5960daemonClient, daemonCloser, err := integration.Instrument(integration.ComponentWorkspaceDaemon, "daemon", cfg.Namespace(), kubeconfig, cfg.Client(),61integration.WithWorkspacekitLift(false),62integration.WithContainer("ws-daemon"),63)64if err != nil {65t.Fatalf("unexpected error instrumenting daemon: %v", err)66}67defer daemonClient.Close()68integration.DeferCloser(t, daemonCloser)6970t.Logf("checking if workspace pod has network limit annotation")71var pod corev1.Pod72if err := cfg.Client().Resources().Get(ctx, "ws-"+ws.LatestInstance.ID, cfg.Namespace(), &pod); err != nil {73t.Fatal(err)74}75annotation, ok := pod.Annotations[kubernetes.WorkspaceNetConnLimitAnnotation]76if !ok {77t.Fatalf("expected annotation %s to be present on workspace pod but wasn't", kubernetes.WorkspaceNetConnLimitAnnotation)78}79if annotation != "true" {80t.Fatalf("expected annotation %s to be true but was %s", kubernetes.WorkspaceNetConnLimitAnnotation, annotation)81}8283t.Logf("checking nftable rules for rate limiting")84containerId := getCalicoContainerId(&pod)85var resp daemon.VerifyRateLimitingRuleResponse86err = daemonClient.Call("DaemonAgent.VerifyRateLimitingRule", daemon.VerifyRateLimitingRuleRequest{87ContainerId: containerId,88}, &resp)89if err != nil {90t.Errorf("error verifying rate limiting rule for container %s: %v", containerId, err)91}9293t.Logf("verified rate limiting rule")9495return testCtx96}).Feature()9798testEnv.Test(t, f)99}100101func getCalicoContainerId(pod *corev1.Pod) string {102return pod.Annotations["cni.projectcalico.org/containerID"]103}104105106