Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/test/tests/workspace/process_priority_test.go
2498 views
1
// Copyright (c) 2023 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package workspace
6
7
import (
8
"context"
9
"fmt"
10
"os"
11
"strconv"
12
"strings"
13
"testing"
14
"time"
15
16
agent "github.com/gitpod-io/gitpod/test/pkg/agent/workspace/api"
17
"github.com/gitpod-io/gitpod/test/pkg/integration"
18
"sigs.k8s.io/e2e-framework/pkg/envconf"
19
"sigs.k8s.io/e2e-framework/pkg/features"
20
)
21
22
func TestProcessPriority(t *testing.T) {
23
userToken, _ := os.LookupEnv("USER_TOKEN")
24
integration.SkipWithoutUsername(t, username)
25
integration.SkipWithoutUserToken(t, userToken)
26
27
f := features.New("process priority").
28
WithLabel("component", "workspace").
29
WithLabel("type", "process priority").
30
Assess("it has set process priority", func(testCtx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
31
t.Parallel()
32
33
ctx, cancel := context.WithTimeout(testCtx, time.Duration(5*time.Minute))
34
defer cancel()
35
36
api := integration.NewComponentAPI(ctx, cfg.Namespace(), kubeconfig, cfg.Client())
37
t.Cleanup(func() {
38
api.Done(t)
39
})
40
41
_, err := api.CreateUser(username, userToken)
42
if err != nil {
43
t.Fatal(err)
44
}
45
46
nfo, stopWs, err := integration.LaunchWorkspaceFromContextURL(t, ctx, "https://github.com/gitpod-io/empty", username, api, integration.WithGitpodUser(username))
47
if err != nil {
48
t.Fatal(err)
49
}
50
51
t.Cleanup(func() {
52
sctx, scancel := context.WithTimeout(context.Background(), 5*time.Minute)
53
defer scancel()
54
55
sapi := integration.NewComponentAPI(sctx, cfg.Namespace(), kubeconfig, cfg.Client())
56
defer sapi.Done(t)
57
58
if _, err = stopWs(true, sapi); err != nil {
59
t.Errorf("cannot stop workspace: %v", err)
60
}
61
})
62
63
rsa, closer, err := integration.Instrument(integration.ComponentWorkspace, "workspace", cfg.Namespace(), kubeconfig, cfg.Client(), integration.WithInstanceID(nfo.LatestInstance.ID))
64
integration.DeferCloser(t, closer)
65
if err != nil {
66
t.Fatalf("unexpected error instrumenting workspace: %v", err)
67
}
68
defer rsa.Close()
69
70
t.Logf("waiting for the next ws-daemon tick, before running ps")
71
time.Sleep(15 * time.Second)
72
73
var res agent.ExecResponse
74
err = rsa.Call("WorkspaceAgent.Exec", &agent.ExecRequest{
75
Dir: "/workspace",
76
Command: "ps",
77
Args: []string{"eax", "-o", "ni,cmd", "--no-headers"},
78
}, &res)
79
if err != nil {
80
t.Fatal(err)
81
}
82
if res.ExitCode != 0 {
83
t.Fatalf("ps failed (%d): %s", res.ExitCode, res.Stderr)
84
}
85
86
checkProcessPriorities(t, res.Stdout)
87
88
return testCtx
89
}).
90
Feature()
91
92
testEnv.Test(t, f)
93
}
94
95
func checkProcessPriorities(t *testing.T, output string) {
96
t.Helper()
97
98
processes := strings.Split(output, "\n")
99
for _, p := range processes {
100
parts := strings.Fields(p)
101
if len(parts) >= 2 {
102
checkProcessPriority(t, parts[0], parts[1])
103
}
104
}
105
}
106
107
func checkProcessPriority(t *testing.T, priority, process string) {
108
t.Helper()
109
110
actualPrio, err := strconv.Atoi(priority)
111
if err != nil {
112
return
113
}
114
115
expectedPrio, err := determinePriority(process)
116
if err != nil {
117
return
118
}
119
120
if actualPrio != expectedPrio {
121
t.Fatalf("expected priority of %v for process %v, but was %v", expectedPrio, process, actualPrio)
122
}
123
}
124
125
func determinePriority(process string) (int, error) {
126
if strings.HasSuffix(process, "supervisor") {
127
return -10, nil
128
}
129
130
if strings.HasSuffix(process, "/bin/code-server") {
131
return -10, nil
132
}
133
134
if strings.HasSuffix(process, "/ide/bin/gitpod-code") {
135
return -10, nil
136
}
137
138
if strings.HasSuffix(process, "/ide/node") {
139
return -5, nil
140
}
141
142
return 0, fmt.Errorf("unknown")
143
}
144
145