Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/supervisor/pkg/ports/ports-config_test.go
2500 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 ports
6
7
import (
8
"context"
9
"testing"
10
11
"github.com/golang/mock/gomock"
12
"github.com/google/go-cmp/cmp"
13
14
gitpod "github.com/gitpod-io/gitpod/gitpod-protocol"
15
)
16
17
func TestPortsConfig(t *testing.T) {
18
tests := []struct {
19
Desc string
20
GitpodConfig *gitpod.GitpodConfig
21
Expectation *PortConfigTestExpectations
22
}{
23
{
24
Desc: "no configs",
25
Expectation: &PortConfigTestExpectations{},
26
},
27
{
28
Desc: "instance port config",
29
GitpodConfig: &gitpod.GitpodConfig{
30
Ports: []*gitpod.PortsItems{
31
{
32
Port: 9229,
33
OnOpen: "ignore",
34
Visibility: "public",
35
Name: "Nice Port Name",
36
Description: "Nice Port Description",
37
},
38
},
39
},
40
Expectation: &PortConfigTestExpectations{
41
InstancePortConfigs: []*gitpod.PortConfig{
42
{
43
Port: 9229,
44
OnOpen: "ignore",
45
Visibility: "public",
46
Name: "Nice Port Name",
47
Description: "Nice Port Description",
48
},
49
},
50
},
51
},
52
{
53
Desc: "instance range config",
54
GitpodConfig: &gitpod.GitpodConfig{
55
Ports: []*gitpod.PortsItems{
56
{
57
Port: "9229-9339",
58
OnOpen: "ignore",
59
Visibility: "public",
60
Name: "Nice Port Name",
61
Description: "Nice Port Description",
62
},
63
},
64
},
65
Expectation: &PortConfigTestExpectations{
66
InstanceRangeConfigs: []*RangeConfig{
67
{
68
PortsItems: gitpod.PortsItems{
69
Port: "9229-9339",
70
OnOpen: "ignore",
71
Visibility: "public",
72
Description: "Nice Port Description",
73
Name: "Nice Port Name",
74
},
75
Start: 9229,
76
End: 9339,
77
},
78
},
79
},
80
},
81
}
82
for _, test := range tests {
83
t.Run(test.Desc, func(t *testing.T) {
84
configService := &testGitpodConfigService{
85
configs: make(chan *gitpod.GitpodConfig),
86
changes: make(chan *struct{}),
87
}
88
defer close(configService.configs)
89
defer close(configService.changes)
90
91
context, cancel := context.WithCancel(context.Background())
92
defer cancel()
93
94
workspaceID := "test"
95
96
ctrl := gomock.NewController(t)
97
defer ctrl.Finish()
98
99
service := NewConfigService(workspaceID, configService)
100
updates, errors := service.Observe(context)
101
102
actual := &PortConfigTestExpectations{}
103
104
if test.GitpodConfig != nil {
105
go func() {
106
configService.configs <- test.GitpodConfig
107
}()
108
select {
109
case err := <-errors:
110
t.Fatal(err)
111
case change := <-updates:
112
actual.InstanceRangeConfigs = change.instanceRangeConfigs
113
for _, config := range change.instancePortConfigs {
114
actual.InstancePortConfigs = append(actual.InstancePortConfigs, &config.PortConfig)
115
}
116
}
117
}
118
119
if diff := cmp.Diff(test.Expectation, actual); diff != "" {
120
t.Errorf("unexpected output (-want +got):\n%s", diff)
121
}
122
})
123
}
124
}
125
126
type PortConfigTestExpectations struct {
127
InstancePortConfigs []*gitpod.PortConfig
128
InstanceRangeConfigs []*RangeConfig
129
}
130
131
type testGitpodConfigService struct {
132
configs chan *gitpod.GitpodConfig
133
changes chan *struct{}
134
}
135
136
func (service *testGitpodConfigService) Watch(ctx context.Context) {
137
}
138
139
func (service *testGitpodConfigService) Observe(ctx context.Context) <-chan *gitpod.GitpodConfig {
140
return service.configs
141
}
142
143
func (service *testGitpodConfigService) ObserveImageFile(ctx context.Context) <-chan *struct{} {
144
return service.changes
145
}
146
147