Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/image-builder-mk3/pkg/orchestrator/monitor_test.go
2500 views
1
// Copyright (c) 2021 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 orchestrator
6
7
import (
8
"testing"
9
"time"
10
11
"github.com/google/go-cmp/cmp"
12
"github.com/google/go-cmp/cmp/cmpopts"
13
"google.golang.org/protobuf/types/known/timestamppb"
14
15
"github.com/gitpod-io/gitpod/image-builder/api"
16
wsmanapi "github.com/gitpod-io/gitpod/ws-manager/api"
17
)
18
19
func TestExtractBuildResponse(t *testing.T) {
20
const (
21
buildID = "build-id"
22
ref = "ref"
23
baseref = "base-ref"
24
startedAt int64 = 12345
25
url = "https://some-url.some-domain.com"
26
ownerToken = "super-secret-owner-token"
27
)
28
tests := []struct {
29
Name string
30
Mod func(*wsmanapi.WorkspaceStatus)
31
Expectation func(*api.BuildResponse)
32
}{
33
{
34
Name: "running",
35
Mod: func(ws *wsmanapi.WorkspaceStatus) {},
36
Expectation: func(br *api.BuildResponse) {},
37
},
38
{
39
Name: "done",
40
Mod: func(ws *wsmanapi.WorkspaceStatus) {
41
ws.Phase = wsmanapi.WorkspacePhase_STOPPING
42
},
43
Expectation: func(br *api.BuildResponse) {
44
br.Status = api.BuildStatus_done_success
45
br.Info.Status = br.Status
46
},
47
},
48
{
49
Name: "done stopped",
50
Mod: func(ws *wsmanapi.WorkspaceStatus) {
51
ws.Phase = wsmanapi.WorkspacePhase_STOPPED
52
},
53
Expectation: func(br *api.BuildResponse) {
54
br.Status = api.BuildStatus_done_success
55
br.Info.Status = br.Status
56
},
57
},
58
{
59
Name: "done task failed",
60
Mod: func(ws *wsmanapi.WorkspaceStatus) {
61
ws.Phase = wsmanapi.WorkspacePhase_STOPPING
62
ws.Conditions.HeadlessTaskFailed = "image build failed"
63
},
64
Expectation: func(br *api.BuildResponse) {
65
br.Status = api.BuildStatus_done_failure
66
br.Info.Status = br.Status
67
br.Message = "image build failed"
68
},
69
},
70
{
71
Name: "done workspace failed",
72
Mod: func(ws *wsmanapi.WorkspaceStatus) {
73
ws.Phase = wsmanapi.WorkspacePhase_STOPPING
74
ws.Conditions.Failed = "image build failed"
75
},
76
Expectation: func(br *api.BuildResponse) {
77
br.Status = api.BuildStatus_done_failure
78
br.Info.Status = br.Status
79
br.Message = "image build failed"
80
},
81
},
82
}
83
84
for _, test := range tests {
85
t.Run(test.Name, func(t *testing.T) {
86
status := &wsmanapi.WorkspaceStatus{
87
Id: buildID,
88
Metadata: &wsmanapi.WorkspaceMetadata{
89
MetaId: buildID,
90
Annotations: map[string]string{
91
annotationRef: ref,
92
annotationBaseRef: baseref,
93
},
94
StartedAt: timestamppb.New(time.Unix(startedAt, 0)),
95
},
96
Conditions: &wsmanapi.WorkspaceConditions{},
97
Phase: wsmanapi.WorkspacePhase_RUNNING,
98
Auth: &wsmanapi.WorkspaceAuthentication{
99
OwnerToken: ownerToken,
100
},
101
Spec: &wsmanapi.WorkspaceSpec{
102
Url: url,
103
},
104
}
105
test.Mod(status)
106
act := extractBuildResponse(status)
107
108
exp := &api.BuildResponse{
109
Ref: ref,
110
BaseRef: baseref,
111
Status: api.BuildStatus_running,
112
Info: &api.BuildInfo{
113
BuildId: buildID,
114
Ref: ref,
115
BaseRef: baseref,
116
Status: api.BuildStatus_running,
117
StartedAt: startedAt,
118
LogInfo: &api.LogInfo{
119
Url: url,
120
Headers: map[string]string{
121
"x-gitpod-owner-token": status.Auth.OwnerToken,
122
},
123
},
124
},
125
}
126
test.Expectation(exp)
127
128
if diff := cmp.Diff(exp, act, cmpopts.IgnoreUnexported(api.BuildResponse{}, api.BuildInfo{}, api.LogInfo{})); diff != "" {
129
t.Errorf("extractBuildResponse() mismatch (-want +got):\n%s", diff)
130
}
131
})
132
}
133
}
134
135