2020-09-07 11:10:56 +03:00
|
|
|
package home
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-11-02 16:18:02 +03:00
|
|
|
"net/netip"
|
2020-09-07 11:10:56 +03:00
|
|
|
"testing"
|
2021-03-11 17:32:58 +03:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-09-07 11:10:56 +03:00
|
|
|
)
|
|
|
|
|
2021-03-11 17:32:58 +03:00
|
|
|
func testParseOK(t *testing.T, ss ...string) options {
|
|
|
|
t.Helper()
|
|
|
|
|
2022-11-02 16:18:02 +03:00
|
|
|
o, _, err := parseCmdOpts("", ss)
|
2021-10-22 11:58:18 +03:00
|
|
|
require.NoError(t, err)
|
2021-03-11 17:32:58 +03:00
|
|
|
|
2020-09-07 11:10:56 +03:00
|
|
|
return o
|
|
|
|
}
|
|
|
|
|
|
|
|
func testParseErr(t *testing.T, descr string, ss ...string) {
|
2021-03-11 17:32:58 +03:00
|
|
|
t.Helper()
|
|
|
|
|
2022-11-02 16:18:02 +03:00
|
|
|
_, _, err := parseCmdOpts("", ss)
|
2021-10-22 11:58:18 +03:00
|
|
|
require.Error(t, err)
|
2020-09-07 11:10:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func testParseParamMissing(t *testing.T, param string) {
|
2021-03-11 17:32:58 +03:00
|
|
|
t.Helper()
|
|
|
|
|
2020-09-07 11:10:56 +03:00
|
|
|
testParseErr(t, fmt.Sprintf("%s parameter missing", param), param)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseVerbose(t *testing.T) {
|
2021-03-11 17:32:58 +03:00
|
|
|
assert.False(t, testParseOK(t).verbose, "empty is not verbose")
|
|
|
|
assert.True(t, testParseOK(t, "-v").verbose, "-v is verbose")
|
|
|
|
assert.True(t, testParseOK(t, "--verbose").verbose, "--verbose is verbose")
|
2020-09-07 11:10:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseConfigFilename(t *testing.T) {
|
2022-11-02 16:18:02 +03:00
|
|
|
assert.Equal(t, "", testParseOK(t).confFilename, "empty is no config filename")
|
|
|
|
assert.Equal(t, "path", testParseOK(t, "-c", "path").confFilename, "-c is config filename")
|
2020-09-07 11:10:56 +03:00
|
|
|
testParseParamMissing(t, "-c")
|
2021-03-11 17:32:58 +03:00
|
|
|
|
2022-11-02 16:18:02 +03:00
|
|
|
assert.Equal(t, "path", testParseOK(t, "--config", "path").confFilename, "--config is config filename")
|
2020-09-07 11:10:56 +03:00
|
|
|
testParseParamMissing(t, "--config")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseWorkDir(t *testing.T) {
|
2021-03-11 17:32:58 +03:00
|
|
|
assert.Equal(t, "", testParseOK(t).workDir, "empty is no work dir")
|
|
|
|
assert.Equal(t, "path", testParseOK(t, "-w", "path").workDir, "-w is work dir")
|
2020-09-07 11:10:56 +03:00
|
|
|
testParseParamMissing(t, "-w")
|
2021-03-11 17:32:58 +03:00
|
|
|
|
|
|
|
assert.Equal(t, "path", testParseOK(t, "--work-dir", "path").workDir, "--work-dir is work dir")
|
2020-09-07 11:10:56 +03:00
|
|
|
testParseParamMissing(t, "--work-dir")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseBindHost(t *testing.T) {
|
2022-11-02 16:18:02 +03:00
|
|
|
wantAddr := netip.MustParseAddr("1.2.3.4")
|
|
|
|
|
|
|
|
assert.Zero(t, testParseOK(t).bindHost, "empty is not host")
|
|
|
|
assert.Equal(t, wantAddr, testParseOK(t, "-h", "1.2.3.4").bindHost, "-h is host")
|
2020-09-07 11:10:56 +03:00
|
|
|
testParseParamMissing(t, "-h")
|
2021-03-11 17:32:58 +03:00
|
|
|
|
2022-11-02 16:18:02 +03:00
|
|
|
assert.Equal(t, wantAddr, testParseOK(t, "--host", "1.2.3.4").bindHost, "--host is host")
|
2020-09-07 11:10:56 +03:00
|
|
|
testParseParamMissing(t, "--host")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseBindPort(t *testing.T) {
|
2021-03-11 17:32:58 +03:00
|
|
|
assert.Equal(t, 0, testParseOK(t).bindPort, "empty is port 0")
|
|
|
|
assert.Equal(t, 65535, testParseOK(t, "-p", "65535").bindPort, "-p is port")
|
2020-09-07 11:10:56 +03:00
|
|
|
testParseParamMissing(t, "-p")
|
2021-03-11 17:32:58 +03:00
|
|
|
|
|
|
|
assert.Equal(t, 65535, testParseOK(t, "--port", "65535").bindPort, "--port is port")
|
2020-09-07 11:10:56 +03:00
|
|
|
testParseParamMissing(t, "--port")
|
|
|
|
|
|
|
|
testParseErr(t, "not an int", "-p", "x")
|
|
|
|
testParseErr(t, "hex not supported", "-p", "0x100")
|
|
|
|
testParseErr(t, "port negative", "-p", "-1")
|
|
|
|
testParseErr(t, "port too high", "-p", "65536")
|
|
|
|
testParseErr(t, "port too high", "-p", "4294967297") // 2^32 + 1
|
|
|
|
testParseErr(t, "port too high", "-p", "18446744073709551617") // 2^64 + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseLogfile(t *testing.T) {
|
2021-03-11 17:32:58 +03:00
|
|
|
assert.Equal(t, "", testParseOK(t).logFile, "empty is no log file")
|
|
|
|
assert.Equal(t, "path", testParseOK(t, "-l", "path").logFile, "-l is log file")
|
|
|
|
assert.Equal(t, "path", testParseOK(t, "--logfile", "path").logFile, "--logfile is log file")
|
2020-09-07 11:10:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParsePidfile(t *testing.T) {
|
2021-03-11 17:32:58 +03:00
|
|
|
assert.Equal(t, "", testParseOK(t).pidFile, "empty is no pid file")
|
|
|
|
assert.Equal(t, "path", testParseOK(t, "--pidfile", "path").pidFile, "--pidfile is pid file")
|
2020-09-07 11:10:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseCheckConfig(t *testing.T) {
|
2021-03-11 17:32:58 +03:00
|
|
|
assert.False(t, testParseOK(t).checkConfig, "empty is not check config")
|
|
|
|
assert.True(t, testParseOK(t, "--check-config").checkConfig, "--check-config is check config")
|
2020-09-07 11:10:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseDisableUpdate(t *testing.T) {
|
2021-03-11 17:32:58 +03:00
|
|
|
assert.False(t, testParseOK(t).disableUpdate, "empty is not disable update")
|
|
|
|
assert.True(t, testParseOK(t, "--no-check-update").disableUpdate, "--no-check-update is disable update")
|
2020-09-07 11:10:56 +03:00
|
|
|
}
|
|
|
|
|
2022-11-23 16:52:05 +03:00
|
|
|
func TestParsePerformUpdate(t *testing.T) {
|
|
|
|
assert.False(t, testParseOK(t).performUpdate, "empty is not perform update")
|
|
|
|
assert.True(t, testParseOK(t, "--update").performUpdate, "--update is perform update")
|
|
|
|
}
|
|
|
|
|
2022-04-06 19:56:39 +03:00
|
|
|
// TODO(e.burkov): Remove after v0.108.0.
|
2020-09-11 13:21:37 +03:00
|
|
|
func TestParseDisableMemoryOptimization(t *testing.T) {
|
2022-11-02 16:18:02 +03:00
|
|
|
o, eff, err := parseCmdOpts("", []string{"--no-mem-optimization"})
|
2022-04-06 19:56:39 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Nil(t, eff)
|
|
|
|
assert.Zero(t, o)
|
2020-09-11 13:21:37 +03:00
|
|
|
}
|
|
|
|
|
2020-09-07 11:10:56 +03:00
|
|
|
func TestParseService(t *testing.T) {
|
2021-03-11 17:32:58 +03:00
|
|
|
assert.Equal(t, "", testParseOK(t).serviceControlAction, "empty is not service cmd")
|
|
|
|
assert.Equal(t, "cmd", testParseOK(t, "-s", "cmd").serviceControlAction, "-s is service cmd")
|
|
|
|
assert.Equal(t, "cmd", testParseOK(t, "--service", "cmd").serviceControlAction, "--service is service cmd")
|
2020-09-07 11:10:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseGLInet(t *testing.T) {
|
2021-03-11 17:32:58 +03:00
|
|
|
assert.False(t, testParseOK(t).glinetMode, "empty is not GL-Inet mode")
|
|
|
|
assert.True(t, testParseOK(t, "--glinet").glinetMode, "--glinet is GL-Inet mode")
|
2020-09-07 11:10:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseUnknown(t *testing.T) {
|
|
|
|
testParseErr(t, "unknown word", "x")
|
|
|
|
testParseErr(t, "unknown short", "-x")
|
|
|
|
testParseErr(t, "unknown long", "--x")
|
|
|
|
testParseErr(t, "unknown triple", "---x")
|
|
|
|
testParseErr(t, "unknown plus", "+x")
|
|
|
|
testParseErr(t, "unknown dash", "-")
|
|
|
|
}
|
2020-09-07 11:53:16 +03:00
|
|
|
|
2022-11-02 16:18:02 +03:00
|
|
|
func TestOptsToArgs(t *testing.T) {
|
2021-03-11 17:32:58 +03:00
|
|
|
testCases := []struct {
|
|
|
|
name string
|
2022-11-02 16:18:02 +03:00
|
|
|
args []string
|
2021-03-11 17:32:58 +03:00
|
|
|
opts options
|
|
|
|
}{{
|
|
|
|
name: "empty",
|
2022-11-02 16:18:02 +03:00
|
|
|
args: []string{},
|
2021-03-11 17:32:58 +03:00
|
|
|
opts: options{},
|
|
|
|
}, {
|
|
|
|
name: "config_filename",
|
2022-11-02 16:18:02 +03:00
|
|
|
args: []string{"-c", "path"},
|
|
|
|
opts: options{confFilename: "path"},
|
2021-03-11 17:32:58 +03:00
|
|
|
}, {
|
|
|
|
name: "work_dir",
|
2022-11-02 16:18:02 +03:00
|
|
|
args: []string{"-w", "path"},
|
2021-03-11 17:32:58 +03:00
|
|
|
opts: options{workDir: "path"},
|
|
|
|
}, {
|
|
|
|
name: "bind_host",
|
2022-11-02 16:18:02 +03:00
|
|
|
opts: options{bindHost: netip.MustParseAddr("1.2.3.4")},
|
|
|
|
args: []string{"-h", "1.2.3.4"},
|
2021-03-11 17:32:58 +03:00
|
|
|
}, {
|
|
|
|
name: "bind_port",
|
2022-11-02 16:18:02 +03:00
|
|
|
args: []string{"-p", "666"},
|
2021-03-11 17:32:58 +03:00
|
|
|
opts: options{bindPort: 666},
|
|
|
|
}, {
|
|
|
|
name: "log_file",
|
2022-11-02 16:18:02 +03:00
|
|
|
args: []string{"-l", "path"},
|
2021-03-11 17:32:58 +03:00
|
|
|
opts: options{logFile: "path"},
|
|
|
|
}, {
|
|
|
|
name: "pid_file",
|
2022-11-02 16:18:02 +03:00
|
|
|
args: []string{"--pidfile", "path"},
|
2021-03-11 17:32:58 +03:00
|
|
|
opts: options{pidFile: "path"},
|
|
|
|
}, {
|
|
|
|
name: "disable_update",
|
2022-11-02 16:18:02 +03:00
|
|
|
args: []string{"--no-check-update"},
|
2021-03-11 17:32:58 +03:00
|
|
|
opts: options{disableUpdate: true},
|
2022-11-23 16:52:05 +03:00
|
|
|
}, {
|
|
|
|
name: "perform_update",
|
|
|
|
args: []string{"--update"},
|
|
|
|
opts: options{performUpdate: true},
|
2021-03-11 17:32:58 +03:00
|
|
|
}, {
|
|
|
|
name: "control_action",
|
2022-11-02 16:18:02 +03:00
|
|
|
args: []string{"-s", "run"},
|
2021-03-11 17:32:58 +03:00
|
|
|
opts: options{serviceControlAction: "run"},
|
|
|
|
}, {
|
|
|
|
name: "glinet_mode",
|
2022-11-02 16:18:02 +03:00
|
|
|
args: []string{"--glinet"},
|
2021-03-11 17:32:58 +03:00
|
|
|
opts: options{glinetMode: true},
|
|
|
|
}, {
|
|
|
|
name: "multiple",
|
2022-11-02 16:18:02 +03:00
|
|
|
args: []string{
|
2021-03-11 17:32:58 +03:00
|
|
|
"-c", "config",
|
|
|
|
"-w", "work",
|
|
|
|
"-s", "run",
|
|
|
|
"--pidfile", "pid",
|
|
|
|
"--no-check-update",
|
|
|
|
},
|
2022-11-02 16:18:02 +03:00
|
|
|
opts: options{
|
|
|
|
serviceControlAction: "run",
|
|
|
|
confFilename: "config",
|
|
|
|
workDir: "work",
|
|
|
|
pidFile: "pid",
|
|
|
|
disableUpdate: true,
|
|
|
|
},
|
2021-03-11 17:32:58 +03:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2022-11-02 16:18:02 +03:00
|
|
|
result := optsToArgs(tc.opts)
|
|
|
|
assert.ElementsMatch(t, tc.args, result)
|
2021-03-11 17:32:58 +03:00
|
|
|
})
|
2020-09-07 11:53:16 +03:00
|
|
|
}
|
|
|
|
}
|