-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathserver_test.go
More file actions
195 lines (171 loc) · 5.18 KB
/
server_test.go
File metadata and controls
195 lines (171 loc) · 5.18 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
// SPDX-FileCopyrightText: 2025 Canonical Ltd
//
// SPDX-License-Identifier: Apache-2.0
package main
import (
"context"
"errors"
"fmt"
"strings"
"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"
)
type mockWebUI struct {
started bool
}
func (m *mockWebUI) Start(ctx context.Context, syncChan chan<- struct{}) {
m.started = true
}
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 := &mockWebUI{}
nf := &mockNFConfigSuccess{}
err := runWebUIAndNFConfig(webui, nf)
if err != nil {
t.Errorf("expected no error, got %v", err)
}
if !webui.started {
t.Errorf("webui.Start was not called in time")
}
}
func TestRunWebUIAndNFConfig_GivenFailureInNfConfigServiceExpectError(t *testing.T) {
webui := &mockWebUI{}
nf := &mockNFConfigFail{}
err := runWebUIAndNFConfig(webui, nf)
if err == nil || !strings.Contains(err.Error(), "NFConfig start failed") {
t.Errorf("expected NFConfig failure, got %v", err)
}
}
func TestMainValidateCLIFlags(t *testing.T) {
tests := []struct {
name string
args []string
expectError bool
}{
{
name: "missing required flag",
args: []string{"webconsole"},
expectError: true,
},
{
name: "valid config flag",
args: []string{"webconsole", "-cfg", "test.conf"},
expectError: false,
},
{
name: "empty config value",
args: []string{"webconsole", "-cfg", ""},
expectError: true,
},
{
name: "invalid flag",
args: []string{"webconsole", "-invalid", "test.conf"},
expectError: true,
},
{
name: "multiple flags with valid config",
args: []string{"webconsole", "-cfg", "test.conf", "-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
originalNewNF := newNFConfigServer
originalRun := runServer
defer func() {
initMongoDB = originalInit
newNFConfigServer = originalNewNF
runServer = originalRun
}()
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("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)
}
})
}