Path: blob/main/test/tests/workspace/process_priority_test.go
2498 views
// Copyright (c) 2023 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 workspace56import (7"context"8"fmt"9"os"10"strconv"11"strings"12"testing"13"time"1415agent "github.com/gitpod-io/gitpod/test/pkg/agent/workspace/api"16"github.com/gitpod-io/gitpod/test/pkg/integration"17"sigs.k8s.io/e2e-framework/pkg/envconf"18"sigs.k8s.io/e2e-framework/pkg/features"19)2021func TestProcessPriority(t *testing.T) {22userToken, _ := os.LookupEnv("USER_TOKEN")23integration.SkipWithoutUsername(t, username)24integration.SkipWithoutUserToken(t, userToken)2526f := features.New("process priority").27WithLabel("component", "workspace").28WithLabel("type", "process priority").29Assess("it has set process priority", func(testCtx context.Context, t *testing.T, cfg *envconf.Config) context.Context {30t.Parallel()3132ctx, cancel := context.WithTimeout(testCtx, time.Duration(5*time.Minute))33defer cancel()3435api := integration.NewComponentAPI(ctx, cfg.Namespace(), kubeconfig, cfg.Client())36t.Cleanup(func() {37api.Done(t)38})3940_, err := api.CreateUser(username, userToken)41if err != nil {42t.Fatal(err)43}4445nfo, stopWs, err := integration.LaunchWorkspaceFromContextURL(t, ctx, "https://github.com/gitpod-io/empty", username, api, integration.WithGitpodUser(username))46if err != nil {47t.Fatal(err)48}4950t.Cleanup(func() {51sctx, scancel := context.WithTimeout(context.Background(), 5*time.Minute)52defer scancel()5354sapi := integration.NewComponentAPI(sctx, cfg.Namespace(), kubeconfig, cfg.Client())55defer sapi.Done(t)5657if _, err = stopWs(true, sapi); err != nil {58t.Errorf("cannot stop workspace: %v", err)59}60})6162rsa, closer, err := integration.Instrument(integration.ComponentWorkspace, "workspace", cfg.Namespace(), kubeconfig, cfg.Client(), integration.WithInstanceID(nfo.LatestInstance.ID))63integration.DeferCloser(t, closer)64if err != nil {65t.Fatalf("unexpected error instrumenting workspace: %v", err)66}67defer rsa.Close()6869t.Logf("waiting for the next ws-daemon tick, before running ps")70time.Sleep(15 * time.Second)7172var res agent.ExecResponse73err = rsa.Call("WorkspaceAgent.Exec", &agent.ExecRequest{74Dir: "/workspace",75Command: "ps",76Args: []string{"eax", "-o", "ni,cmd", "--no-headers"},77}, &res)78if err != nil {79t.Fatal(err)80}81if res.ExitCode != 0 {82t.Fatalf("ps failed (%d): %s", res.ExitCode, res.Stderr)83}8485checkProcessPriorities(t, res.Stdout)8687return testCtx88}).89Feature()9091testEnv.Test(t, f)92}9394func checkProcessPriorities(t *testing.T, output string) {95t.Helper()9697processes := strings.Split(output, "\n")98for _, p := range processes {99parts := strings.Fields(p)100if len(parts) >= 2 {101checkProcessPriority(t, parts[0], parts[1])102}103}104}105106func checkProcessPriority(t *testing.T, priority, process string) {107t.Helper()108109actualPrio, err := strconv.Atoi(priority)110if err != nil {111return112}113114expectedPrio, err := determinePriority(process)115if err != nil {116return117}118119if actualPrio != expectedPrio {120t.Fatalf("expected priority of %v for process %v, but was %v", expectedPrio, process, actualPrio)121}122}123124func determinePriority(process string) (int, error) {125if strings.HasSuffix(process, "supervisor") {126return -10, nil127}128129if strings.HasSuffix(process, "/bin/code-server") {130return -10, nil131}132133if strings.HasSuffix(process, "/ide/bin/gitpod-code") {134return -10, nil135}136137if strings.HasSuffix(process, "/ide/node") {138return -5, nil139}140141return 0, fmt.Errorf("unknown")142}143144145