-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommandline_test.go
193 lines (178 loc) · 5.1 KB
/
commandline_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
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
package main
import (
"bytes"
"errors"
"flag"
"github.com/mbretter/go-mmcli-svr/backend"
"github.com/stretchr/testify/assert"
"log/slog"
"os"
"testing"
)
func TestCommandLine_New(t *testing.T) {
var buff bytes.Buffer
logger := slog.New(slog.NewTextHandler(&buff, nil))
backendMock := backend.NewBackendMock(t)
cmd := NewCommandLine(logger, backendMock)
assert.Equal(t, defaultListen, cmd.Listen)
assert.Equal(t, defaultGpsRefresh, cmd.GpsRefresh)
assert.Empty(t, cmd.LocationGatherings)
assert.Same(t, logger, cmd.log)
assert.Same(t, backendMock, cmd.backend)
}
func TestCommandLine_Parse(t *testing.T) {
tests := []struct {
name string
args []string
settings *CommandLine
error string
}{
{
name: "No args",
args: []string{},
settings: &CommandLine{
Listen: defaultListen,
},
},
{
name: "Listen",
args: []string{"-listen=192.168.1.22:12345"},
settings: &CommandLine{
Listen: "192.168.1.22:12345",
},
},
{
name: "GPS refresh",
args: []string{"-gps-refresh=5"},
settings: &CommandLine{
Listen: defaultListen,
GpsRefresh: 5,
},
},
{
name: "Location enable agps-msa",
args: []string{"-location-enable=agps-msa"},
settings: &CommandLine{
Listen: defaultListen,
LocationGatherings: []string{"agps-msa", "gps-nmea"}, // assistet gps needs gps-nmea
},
},
{
name: "Location enable gps-nmea",
args: []string{"-location-enable=gps-nmea"},
settings: &CommandLine{
Listen: defaultListen,
LocationGatherings: []string{"gps-nmea"}, // assistet gps needs gps-nmea
},
},
{
name: "Location enable agps-msa",
args: []string{"-location-enable=agps-msb"},
settings: &CommandLine{
Listen: defaultListen,
LocationGatherings: []string{"agps-msb", "gps-nmea"}, // assistet gps needs gps-nmea
},
},
{
name: "Location enable multi",
args: []string{"-location-enable=agps-msa,gps-nmea,gps-raw,3gpp"},
settings: &CommandLine{
Listen: defaultListen,
LocationGatherings: []string{"3gpp", "agps-msa", "gps-nmea", "gps-raw"},
},
},
{
name: "Unknown location",
args: []string{"-location-enable=xxx"},
settings: &CommandLine{
Listen: defaultListen,
},
error: "unknown location type: xxx",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
os.Args = append([]string{"cmd"}, tt.args...)
// reset flag, otherwise redefinition errors might be thrown
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError) //flags are now reset
cmd := &CommandLine{}
err := cmd.Parse()
assert.Equal(t, tt.settings, cmd)
if len(tt.error) > 0 {
assert.Equal(t, tt.error, err.Error())
} else {
assert.Nil(t, err)
}
})
}
}
func TestCommandLine_Activate(t *testing.T) {
tests := []struct {
name string
execArgs []string
execError error
logs []string
settings *CommandLine
}{
{
name: "GPS refresh",
execArgs: []string{"--location-set-gps-refresh-rate=10"},
logs: []string{`level=INFO msg="Set gps-refresh" refresh=10`},
settings: &CommandLine{
GpsRefresh: 10,
},
},
{
name: "GPS refresh error",
execArgs: []string{"--location-set-gps-refresh-rate=10"},
execError: errors.New("failed"),
logs: []string{`level=INFO msg="Set gps-refresh" refresh=10`, `level=ERROR msg="Failed to execute command"`},
settings: &CommandLine{
GpsRefresh: 10,
},
},
{
name: "Location 3gpp enable",
execArgs: []string{"--location-enable-3gpp"},
logs: []string{`level=INFO msg="Enable location gatherings" location=3gpp`, `level=INFO msg=mmcli message=success`},
settings: &CommandLine{
LocationGatherings: []string{"3gpp"},
},
},
{
name: "Location enable agps",
execArgs: []string{"--location-enable-agps-msa", "--location-enable-gps-nmea"},
logs: []string{`level=INFO msg="Enable location gatherings" location=agps-msa,gps-nmea`, `level=INFO msg=mmcli message=success`},
settings: &CommandLine{
LocationGatherings: []string{"agps-msa", "gps-nmea"},
},
},
{
name: "Location enable agps error",
execArgs: []string{"--location-enable-agps-msa", "--location-enable-gps-nmea"},
execError: errors.New("failed"),
logs: []string{`level=INFO msg="Enable location gatherings" location=agps-msa,gps-nmea`, `level=ERROR msg="mmcli failed" command="mmcli --location-enable-agps-msa --location-enable-gps-nmea" error=failed`},
settings: &CommandLine{
LocationGatherings: []string{"agps-msa", "gps-nmea"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buff bytes.Buffer
logger := slog.New(slog.NewTextHandler(&buff, nil))
backendMock := backend.NewBackendMock(t)
args := make([]any, len(tt.execArgs))
for i, arg := range tt.execArgs {
args[i] = arg
}
backendMock.EXPECT().ExecModem("", args...).Return([]byte("success"), tt.execError)
tt.settings.backend = backendMock
tt.settings.log = logger
tt.settings.Activate()
for _, log := range tt.logs {
assert.Contains(t, buff.String(), log)
}
})
}
}