Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/test/tests/workspace/gp_top_test.go
2496 views
1
// Copyright (c) 2020 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
"encoding/json"
10
"testing"
11
"time"
12
13
agent "github.com/gitpod-io/gitpod/test/pkg/agent/workspace/api"
14
"github.com/gitpod-io/gitpod/test/pkg/integration"
15
"sigs.k8s.io/e2e-framework/pkg/envconf"
16
"sigs.k8s.io/e2e-framework/pkg/features"
17
)
18
19
func TestGpTop(t *testing.T) {
20
f := features.New("gp top").
21
WithLabel("component", "workspace").
22
WithLabel("type", "gp top").
23
Assess("it can run gp top and retrieve cpu/memory usage", func(testCtx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
24
t.Parallel()
25
26
ctx, cancel := context.WithTimeout(testCtx, time.Duration(10*time.Minute))
27
defer cancel()
28
29
api := integration.NewComponentAPI(ctx, cfg.Namespace(), kubeconfig, cfg.Client())
30
t.Cleanup(func() {
31
api.Done(t)
32
})
33
34
nfo, stopWs, err := integration.LaunchWorkspaceDirectly(t, ctx, api)
35
if err != nil {
36
t.Fatal(err)
37
}
38
39
t.Cleanup(func() {
40
sctx, scancel := context.WithTimeout(context.Background(), 5*time.Minute)
41
defer scancel()
42
43
sapi := integration.NewComponentAPI(sctx, cfg.Namespace(), kubeconfig, cfg.Client())
44
defer sapi.Done(t)
45
46
if _, err = stopWs(true, sapi); err != nil {
47
t.Errorf("cannot stop workspace: %q", err)
48
}
49
})
50
51
rsa, closer, err := integration.Instrument(integration.ComponentWorkspace, "workspace", cfg.Namespace(), kubeconfig, cfg.Client(), integration.WithInstanceID(nfo.Req.Id))
52
integration.DeferCloser(t, closer)
53
if err != nil {
54
t.Fatalf("unexpected error instrumenting workspace: %v", err)
55
}
56
defer rsa.Close()
57
58
t.Logf("running gp top")
59
var res agent.ExecResponse
60
err = rsa.Call("WorkspaceAgent.Exec", &agent.ExecRequest{
61
Dir: "/workspace",
62
Command: "gp",
63
Env: []string{"SUPERVISOR_ADDR=10.0.5.2:22999"},
64
Args: []string{"top", "--json"},
65
}, &res)
66
if err != nil {
67
t.Fatal(err)
68
}
69
if res.ExitCode != 0 {
70
t.Fatalf("gp top failed (%d): %s", res.ExitCode, res.Stderr)
71
}
72
73
t.Logf("gp top: %s", res.Stdout)
74
75
type gpTop struct {
76
Resources struct {
77
CPU struct {
78
Used int `json:"used"`
79
Limit int `json:"limit"`
80
} `json:"cpu"`
81
Memory struct {
82
Used int `json:"used"`
83
Limit int `json:"limit"`
84
} `json:"memory"`
85
} `json:"resources"`
86
}
87
var top gpTop
88
err = json.Unmarshal([]byte(res.Stdout), &top)
89
if err != nil {
90
t.Fatalf("cannot unmarshal gp top output: %v", err)
91
}
92
93
t.Logf("verifying gp top output is not empty")
94
if top.Resources.CPU.Used == 0 {
95
t.Errorf("gp top reports 0 CPU usage")
96
}
97
if top.Resources.Memory.Used == 0 {
98
t.Errorf("gp top reports 0 memory usage")
99
}
100
if top.Resources.CPU.Limit == 0 {
101
t.Errorf("gp top reports 0 CPU limit")
102
}
103
if top.Resources.Memory.Limit == 0 {
104
t.Errorf("gp top reports 0 memory limit")
105
}
106
107
return testCtx
108
}).
109
Feature()
110
111
testEnv.Test(t, f)
112
}
113
114