Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/content-service/pkg/service/headless-log-service_test.go
2499 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 service
6
7
import (
8
"context"
9
"fmt"
10
"testing"
11
12
"github.com/golang/mock/gomock"
13
"github.com/google/go-cmp/cmp"
14
15
"github.com/gitpod-io/gitpod/content-service/api"
16
"github.com/gitpod-io/gitpod/content-service/api/config"
17
"github.com/gitpod-io/gitpod/content-service/pkg/storage"
18
storagemock "github.com/gitpod-io/gitpod/content-service/pkg/storage/mock"
19
)
20
21
func TestListLogs(t *testing.T) {
22
cfg := config.StorageConfig{
23
Stage: config.StageProduction,
24
Kind: config.GCloudStorage, // dummy, mocked away
25
BlobQuota: 1073741824, // 1Gi
26
}
27
28
OwnerId := "1234"
29
WorkspaceId := "amber-baboon-cij4wozf"
30
InstanceId := "958aff1c-849a-460f-8af4-5c5b1401a599"
31
logFile := func(name string) string {
32
return fmt.Sprintf("workspace/%s/instance/%s/logs/%s", WorkspaceId, InstanceId, name)
33
}
34
tests := []struct {
35
Name string
36
Files []string
37
ExpectedTaskIds []string
38
}{
39
{
40
Name: "one instance, three tasks",
41
Files: []string{
42
logFile("1"),
43
logFile("3"),
44
logFile("4"),
45
},
46
ExpectedTaskIds: []string{"1", "3", "4"},
47
},
48
}
49
50
for _, test := range tests {
51
t.Run(test.Name, func(t *testing.T) {
52
ctrl := gomock.NewController(t)
53
defer ctrl.Finish()
54
55
s := storagemock.NewMockPresignedAccess(ctrl)
56
da := storagemock.NewMockDirectAccess(ctrl)
57
daFactory := func(cfg *config.StorageConfig) (storage.DirectAccess, error) {
58
return da, nil
59
}
60
svc := HeadlessLogService{
61
cfg: cfg,
62
s: s,
63
daFactory: daFactory,
64
}
65
66
s.EXPECT().InstanceObject(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
67
Return("")
68
da.EXPECT().Init(gomock.Any(), gomock.Eq(OwnerId), gomock.Eq(WorkspaceId), gomock.Not(gomock.Eq(""))).
69
Times(1)
70
da.EXPECT().ListObjects(gomock.Any(), gomock.Any()).Return(test.Files, nil)
71
72
ctx, cancel := context.WithCancel(context.Background())
73
defer cancel()
74
75
req := &api.ListLogsRequest{
76
OwnerId: OwnerId,
77
WorkspaceId: WorkspaceId,
78
InstanceId: InstanceId,
79
}
80
resp, err := svc.ListLogs(ctx, req)
81
if err != nil {
82
t.Fatalf("ListLogs err: %v", err)
83
}
84
if diff := cmp.Diff(test.ExpectedTaskIds, resp.TaskId); diff != "" {
85
t.Errorf("unexpected TaskIds (-want +got):\n%s", diff)
86
}
87
})
88
}
89
}
90
91