Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/content-service/pkg/initializer/initializer_test.go
2499 views
1
// Copyright (c) 2022 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 initializer_test
6
7
import (
8
"context"
9
"fmt"
10
"testing"
11
12
csapi "github.com/gitpod-io/gitpod/content-service/api"
13
"github.com/gitpod-io/gitpod/content-service/pkg/archive"
14
"github.com/gitpod-io/gitpod/content-service/pkg/initializer"
15
)
16
17
type InitializerFunc func(ctx context.Context, mappings []archive.IDMapping) (csapi.WorkspaceInitSource, csapi.InitializerMetrics, error)
18
19
func (f InitializerFunc) Run(ctx context.Context, mappings []archive.IDMapping) (csapi.WorkspaceInitSource, csapi.InitializerMetrics, error) {
20
return f(ctx, mappings)
21
}
22
23
type RecordingInitializer struct {
24
CallCount int
25
}
26
27
func (f *RecordingInitializer) Run(ctx context.Context, mappings []archive.IDMapping) (csapi.WorkspaceInitSource, csapi.InitializerMetrics, error) {
28
f.CallCount++
29
return csapi.WorkspaceInitFromOther, nil, nil
30
}
31
32
func TestCompositeInitializer(t *testing.T) {
33
tests := []struct {
34
Name string
35
Children []initializer.Initializer
36
Eval func(t *testing.T, src csapi.WorkspaceInitSource, err error, children []initializer.Initializer)
37
}{
38
{
39
Name: "empty",
40
Eval: func(t *testing.T, src csapi.WorkspaceInitSource, err error, children []initializer.Initializer) {
41
if err != nil {
42
t.Fatalf("unexpected error: %v", err)
43
}
44
},
45
},
46
{
47
Name: "single",
48
Children: []initializer.Initializer{
49
&RecordingInitializer{},
50
},
51
Eval: func(t *testing.T, src csapi.WorkspaceInitSource, err error, children []initializer.Initializer) {
52
if cc := children[0].(*RecordingInitializer).CallCount; cc != 1 {
53
t.Errorf("unexpected call count: expected 1, got %d", cc)
54
}
55
},
56
},
57
{
58
Name: "multiple",
59
Children: []initializer.Initializer{
60
&RecordingInitializer{},
61
&RecordingInitializer{},
62
&RecordingInitializer{},
63
},
64
Eval: func(t *testing.T, src csapi.WorkspaceInitSource, err error, children []initializer.Initializer) {
65
for i := range children {
66
if cc := children[i].(*RecordingInitializer).CallCount; cc != 1 {
67
t.Errorf("unexpected call count on initializer %d: expected 1, got %d", i, cc)
68
}
69
}
70
},
71
},
72
{
73
Name: "error propagation",
74
Children: []initializer.Initializer{
75
&RecordingInitializer{},
76
InitializerFunc(func(ctx context.Context, mappings []archive.IDMapping) (csapi.WorkspaceInitSource, csapi.InitializerMetrics, error) {
77
return csapi.WorkspaceInitFromOther, nil, fmt.Errorf("error happened here")
78
}),
79
&RecordingInitializer{},
80
},
81
Eval: func(t *testing.T, src csapi.WorkspaceInitSource, err error, children []initializer.Initializer) {
82
if err == nil {
83
t.Errorf("expected error, got nothing")
84
}
85
if cc := children[0].(*RecordingInitializer).CallCount; cc != 1 {
86
t.Errorf("unexpected call count on first initializer: expected 1, got %d", cc)
87
}
88
if cc := children[2].(*RecordingInitializer).CallCount; cc != 0 {
89
t.Errorf("unexpected call count on last initializer: expected 0, got %d", cc)
90
}
91
},
92
},
93
}
94
for _, test := range tests {
95
t.Run(test.Name, func(t *testing.T) {
96
comp := initializer.CompositeInitializer(test.Children)
97
src, _, err := comp.Run(context.Background(), nil)
98
test.Eval(t, src, err, test.Children)
99
})
100
}
101
}
102
103