-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathports-config_test.go
140 lines (123 loc) · 3.35 KB
/
ports-config_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.
package ports
import (
"context"
"testing"
"github.com/golang/mock/gomock"
"github.com/google/go-cmp/cmp"
gitpod "github.com/gitpod-io/gitpod/gitpod-protocol"
)
func TestPortsConfig(t *testing.T) {
tests := []struct {
Desc string
GitpodConfig *gitpod.GitpodConfig
Expectation *PortConfigTestExpectations
}{
{
Desc: "no configs",
Expectation: &PortConfigTestExpectations{},
},
{
Desc: "instance port config",
GitpodConfig: &gitpod.GitpodConfig{
Ports: []*gitpod.PortsItems{
{
Port: 9229,
OnOpen: "ignore",
Visibility: "public",
Name: "Nice Port Name",
Description: "Nice Port Description",
},
},
},
Expectation: &PortConfigTestExpectations{
InstancePortConfigs: []*gitpod.PortConfig{
{
Port: 9229,
OnOpen: "ignore",
Visibility: "public",
Name: "Nice Port Name",
Description: "Nice Port Description",
},
},
},
},
{
Desc: "instance range config",
GitpodConfig: &gitpod.GitpodConfig{
Ports: []*gitpod.PortsItems{
{
Port: "9229-9339",
OnOpen: "ignore",
Visibility: "public",
Name: "Nice Port Name",
Description: "Nice Port Description",
},
},
},
Expectation: &PortConfigTestExpectations{
InstanceRangeConfigs: []*RangeConfig{
{
PortsItems: gitpod.PortsItems{
Port: "9229-9339",
OnOpen: "ignore",
Visibility: "public",
Description: "Nice Port Description",
Name: "Nice Port Name",
},
Start: 9229,
End: 9339,
},
},
},
},
}
for _, test := range tests {
t.Run(test.Desc, func(t *testing.T) {
configService := &testGitpodConfigService{
configs: make(chan *gitpod.GitpodConfig),
}
defer close(configService.configs)
context, cancel := context.WithCancel(context.Background())
defer cancel()
workspaceID := "test"
ctrl := gomock.NewController(t)
defer ctrl.Finish()
gitpodAPI := gitpod.NewMockAPIInterface(ctrl)
service := NewConfigService(workspaceID, configService, gitpodAPI)
updates, errors := service.Observe(context)
actual := &PortConfigTestExpectations{}
if test.GitpodConfig != nil {
go func() {
configService.configs <- test.GitpodConfig
}()
select {
case err := <-errors:
t.Fatal(err)
case change := <-updates:
actual.InstanceRangeConfigs = change.instanceRangeConfigs
for _, config := range change.instancePortConfigs {
actual.InstancePortConfigs = append(actual.InstancePortConfigs, &config.PortConfig)
}
}
}
if diff := cmp.Diff(test.Expectation, actual); diff != "" {
t.Errorf("unexpected output (-want +got):\n%s", diff)
}
})
}
}
type PortConfigTestExpectations struct {
InstancePortConfigs []*gitpod.PortConfig
InstanceRangeConfigs []*RangeConfig
}
type testGitpodConfigService struct {
configs chan *gitpod.GitpodConfig
}
func (service *testGitpodConfigService) Watch(ctx context.Context) {
}
func (service *testGitpodConfigService) Observe(ctx context.Context) <-chan *gitpod.GitpodConfig {
return service.configs
}