Repository navigation
Expand file tree
/
Copy pathserver_test.go
More file actions
284 lines (249 loc) · 7.72 KB
/
Copy pathserver_test.go
File metadata and controls
284 lines (249 loc) · 7.72 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// SPDX-FileCopyrightText: 2025 Canonical Ltd
//
// SPDX-License-Identifier: Apache-2.0
package main
import (
"context"
"errors"
"fmt"
"os"
"strings"
"sync"
"sync/atomic"
"syscall"
"testing"
"time"
"github.com/omec-project/webconsole/backend/factory"
"github.com/omec-project/webconsole/backend/nfconfig"
"github.com/omec-project/webconsole/backend/webui_service"
"github.com/urfave/cli/v3"
)
const (
cliAppName = "webconsole"
cfgFlag = "-cfg"
testConfigFile = "test.conf"
)
type mockWebUI struct {
started atomic.Bool
startedCh chan struct{}
startedOnce sync.Once
}
func newMockWebUI() *mockWebUI {
return &mockWebUI{startedCh: make(chan struct{})}
}
func (m *mockWebUI) Start(ctx context.Context, syncChan chan<- struct{}) {
m.started.Store(true)
m.startedOnce.Do(func() {
close(m.startedCh)
})
}
type mockNFConfigSuccess struct{}
func (m *mockNFConfigSuccess) Start(ctx context.Context, syncChan <-chan struct{}) error {
time.Sleep(50 * time.Millisecond)
return nil
}
type mockNFConfigFail struct{}
func (m *mockNFConfigFail) Start(ctx context.Context, syncChan <-chan struct{}) error {
return errors.New("NFConfig start failed")
}
type mockNFConfig struct{}
func (m *mockNFConfig) Start(ctx context.Context, syncChan <-chan struct{}) error {
return nil
}
func TestRunWebUIAndNFConfig_Success_ExpectNoError(t *testing.T) {
webui := newMockWebUI()
nf := &mockNFConfigSuccess{}
err := runWebUIAndNFConfig(webui, nf)
if err != nil {
t.Errorf("expected no error, got %v", err)
}
select {
case <-webui.startedCh:
case <-time.After(time.Second):
t.Fatal("webui.Start was not called in time")
}
if !webui.started.Load() {
t.Errorf("webui.Start was not called in time")
}
}
func TestRunWebUIAndNFConfig_GivenFailureInNfConfigServiceExpectError(t *testing.T) {
webui := newMockWebUI()
nf := &mockNFConfigFail{}
err := runWebUIAndNFConfig(webui, nf)
if err == nil || !strings.Contains(err.Error(), "NFConfig start failed") {
t.Errorf("expected NFConfig failure, got %v", err)
}
}
// mockBlockingWebUI and mockBlockingNFConfig run until the context is
// cancelled, as the real servers do.
type mockBlockingWebUI struct {
startedCh chan struct{}
stopped atomic.Bool
}
func (m *mockBlockingWebUI) Start(ctx context.Context, syncChan chan<- struct{}) {
close(m.startedCh)
<-ctx.Done()
m.stopped.Store(true)
}
type mockBlockingNFConfig struct{}
func (m *mockBlockingNFConfig) Start(ctx context.Context, syncChan <-chan struct{}) error {
<-ctx.Done()
return nil
}
func TestRunWebUIAndNFConfig_GivenSIGTERM_ExpectBothStoppedAndNoError(t *testing.T) {
webui := &mockBlockingWebUI{startedCh: make(chan struct{})}
errCh := make(chan error, 1)
go func() { errCh <- runWebUIAndNFConfig(webui, &mockBlockingNFConfig{}) }()
select {
case <-webui.startedCh:
case <-time.After(time.Second):
t.Fatal("webui.Start was not called in time")
}
if err := syscall.Kill(os.Getpid(), syscall.SIGTERM); err != nil {
t.Fatalf("sending SIGTERM: %v", err)
}
select {
case err := <-errCh:
if err != nil {
t.Errorf("expected no error after SIGTERM, got %v", err)
}
case <-time.After(5 * time.Second):
t.Fatal("runWebUIAndNFConfig did not return after SIGTERM")
}
if !webui.stopped.Load() {
t.Error("expected the WebUI to have stopped before runWebUIAndNFConfig returned")
}
}
func TestMainValidateCLIFlags(t *testing.T) {
tests := []struct {
name string
args []string
expectError bool
}{
{
name: "missing required flag",
args: []string{cliAppName},
expectError: true,
},
{
name: "valid config flag",
args: []string{cliAppName, cfgFlag, testConfigFile},
expectError: false,
},
{
name: "empty config value",
args: []string{cliAppName, cfgFlag, ""},
expectError: true,
},
{
name: "invalid flag",
args: []string{cliAppName, "-invalid", testConfigFile},
expectError: true,
},
{
name: "multiple flags with valid config",
args: []string{cliAppName, cfgFlag, testConfigFile, "-verbose"},
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
app := &cli.Command{}
app.Name = "webui"
app.Usage = "Web UI"
app.UsageText = "webconsole -cfg <webui_config_file.yaml>"
app.Flags = factory.GetCliFlags()
app.Action = func(ctx context.Context, c *cli.Command) error {
cfg := c.String("cfg")
if cfg == "" {
return fmt.Errorf("required flag cfg not set")
}
return nil
}
err := app.Run(context.Background(), tt.args)
if tt.expectError && err == nil {
t.Error("expected error but got none")
}
if !tt.expectError && err != nil {
t.Errorf("unexpected error: %v", err)
}
})
}
}
func TestStartApplication(t *testing.T) {
originalInit := initMongoDB
originalEnsure := ensureSubscriberIndexes
originalNewNF := newNFConfigServer
originalRun := runServer
defer func() {
initMongoDB = originalInit
ensureSubscriberIndexes = originalEnsure
newNFConfigServer = originalNewNF
runServer = originalRun
}()
ensureSubscriberIndexes = func() error { return nil }
t.Run("nil config", func(t *testing.T) {
err := startApplication(nil)
if err == nil || !strings.Contains(err.Error(), "nil") {
t.Errorf("expected error for nil config, got: %v", err)
}
})
t.Run("mongo init failure", func(t *testing.T) {
initMongoDB = func() error {
return fmt.Errorf("mongo failed")
}
err := startApplication(&factory.Config{Configuration: &factory.Configuration{}})
if err == nil || !strings.Contains(err.Error(), "mongo failed") {
t.Errorf("expected mongo init error, got: %v", err)
}
})
t.Run("index ensure failure", func(t *testing.T) {
initMongoDB = func() error { return nil }
ensureSubscriberIndexes = func() error { return fmt.Errorf("index failed") }
defer func() { ensureSubscriberIndexes = func() error { return nil } }()
newNFConfigServer = func(config *factory.Config) (nfconfig.NFConfigInterface, error) {
t.Error("NF config server created although the indexes failed")
return nil, fmt.Errorf("unreachable")
}
err := startApplication(&factory.Config{Configuration: &factory.Configuration{}})
if err == nil || !strings.Contains(err.Error(), "index failed") {
t.Errorf("expected index ensure error, got: %v", err)
}
})
t.Run("nfconfig init failure", func(t *testing.T) {
initMongoDB = func() error { return nil }
newNFConfigServer = func(config *factory.Config) (nfconfig.NFConfigInterface, error) {
return nil, fmt.Errorf("nfconfig init fail")
}
err := startApplication(&factory.Config{Configuration: &factory.Configuration{}})
if err == nil || !strings.Contains(err.Error(), "nfconfig init fail") {
t.Errorf("expected NF config init failure, got: %v", err)
}
})
t.Run("run failure", func(t *testing.T) {
initMongoDB = func() error { return nil }
newNFConfigServer = func(config *factory.Config) (nfconfig.NFConfigInterface, error) {
return &mockNFConfig{}, nil
}
runServer = func(webui webui_service.WebUIInterface, nf nfconfig.NFConfigInterface) error {
return fmt.Errorf("run fail")
}
err := startApplication(&factory.Config{Configuration: &factory.Configuration{}})
if err == nil || !strings.Contains(err.Error(), "run fail") {
t.Errorf("expected run error, got: %v", err)
}
})
t.Run("success", func(t *testing.T) {
initMongoDB = func() error { return nil }
newNFConfigServer = func(config *factory.Config) (nfconfig.NFConfigInterface, error) {
return &mockNFConfig{}, nil
}
runServer = func(webui webui_service.WebUIInterface, nf nfconfig.NFConfigInterface) error {
return nil
}
err := startApplication(&factory.Config{Configuration: &factory.Configuration{}})
if err != nil {
t.Errorf("expected no error, got: %v", err)
}
})
}