Skip to content

Commit

Permalink
Adapt paths unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
urso committed Jul 18, 2016
1 parent e87a2c1 commit 82f9556
Showing 1 changed file with 39 additions and 107 deletions.
146 changes: 39 additions & 107 deletions libbeat/paths/paths_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,59 +10,39 @@ import (

func TestHomePath(t *testing.T) {
type io struct {
CLIHome *string // cli flag home setting
CfgHome string // config file home setting
Path string // requested path
Result string // expected result
ResultData string // expected data path
Home string // cli flag home setting
Path string // requested path
Result string // expected result
ResultData string // expected data path
}

binDir, err := filepath.Abs(filepath.Dir(os.Args[0]))
assert.NoError(t, err)
tmp := "/tmp/"

tests := []io{
{
CLIHome: nil,
CfgHome: "",
Home: binDir,
Path: "test",
Result: filepath.Join(binDir, "test"),
ResultData: filepath.Join(binDir, "data", "test"),
},
{
CLIHome: &tmp,
CfgHome: "",
Home: "/tmp",
Path: "test",
Result: "/tmp/test",
ResultData: "/tmp/data/test",
},
{
CLIHome: &tmp,
CfgHome: "/root/",
Path: "test",
Result: "/tmp/test",
ResultData: "/tmp/data/test",
},
{
CLIHome: nil,
CfgHome: "/root/",
Path: "test",
Result: "/root/test",
ResultData: "/root/data/test",
},
{
CLIHome: nil,
CfgHome: "/root/",
Path: "/home/test",
Result: "/home/test",
ResultData: "/home/test",
Home: "/home/",
Path: "/abc/test",
Result: "/abc/test",
ResultData: "/abc/test",
},
}

for _, test := range tests {
t.Log("Executing test", test)
homePath = test.CLIHome
cfg := Path{Home: test.CfgHome}
cfg := Path{Home: test.Home}
assert.NoError(t, Paths.initPaths(&cfg))

assert.Equal(t, test.Result, Resolve(Home, test.Path))
Expand All @@ -78,59 +58,39 @@ func TestHomePath(t *testing.T) {

func TestDataPath(t *testing.T) {
type io struct {
CLIHome *string // cli flag home setting
CfgHome string // config file home setting
CLIData *string // cli flag for data setting
CfgData string // config file data setting
Path string // requested path
ResultData string // expected data path
Home string // cli flag home setting
Data string // cli flag for data setting
Path string // requested path
ResultData string // expected data path
}

binDir, err := filepath.Abs(filepath.Dir(os.Args[0]))
assert.NoError(t, err)
tmp := "/tmp/"
root := "/root/"

tests := []io{
{
CLIHome: nil,
CfgHome: "",
CLIData: nil,
CfgData: "",
Home: binDir,
Data: "",
Path: "test",
ResultData: filepath.Join(binDir, "data", "test"),
},
{
CLIHome: nil,
CfgHome: "/tmp/",
CLIData: nil,
CfgData: "/root/",
Path: "test",
ResultData: "/root/test",
},
{
CLIHome: &tmp,
CfgHome: "",
CLIData: nil,
CfgData: "/root/",
Home: "/tmp/",
Data: "/root/",
Path: "test",
ResultData: "/root/test",
},
{
CLIHome: &tmp,
CfgHome: "",
CLIData: &root,
CfgData: "/root/data",
Path: "test",
ResultData: "/root/test",
Home: "/tmp/",
Data: "/root/",
Path: "/var/data",
ResultData: "/var/data",
},
}

for _, test := range tests {
t.Log("Executing test", test)
homePath = test.CLIHome
dataPath = test.CLIData
cfg := Path{Home: test.CfgHome, Data: test.CfgData}
cfg := Path{Home: test.Home, Data: test.Data}
assert.NoError(t, Paths.initPaths(&cfg))

assert.Equal(t, test.ResultData, Resolve(Data, test.Path))
Expand All @@ -140,67 +100,39 @@ func TestDataPath(t *testing.T) {

func TestLogsPath(t *testing.T) {
type io struct {
CLIHome *string // cli flag home setting
CfgHome string // config file home setting
CLILogs *string // cli flag for data setting
CfgLogs string // config file data setting
Path string // requested path
ResultLogs string // expected logs path
Home string // cli flag home setting
Logs string // cli flag for data setting
Path string // requested path
ResultLogs string // expected logs path
}

binDir, err := filepath.Abs(filepath.Dir(os.Args[0]))
assert.NoError(t, err)
tmp := "/tmp/"
root := "/root/"

tests := []io{
{
CLIHome: nil,
CfgHome: "",
CLILogs: nil,
CfgLogs: "",
Path: "",
ResultLogs: filepath.Join(binDir, "logs"),
},
{
CLIHome: nil,
CfgHome: "",
CLILogs: nil,
CfgLogs: "",
Home: binDir,
Logs: "",
Path: "test",
ResultLogs: filepath.Join(binDir, "logs", "test"),
},
{
CLIHome: nil,
CfgHome: "/tmp/",
CLILogs: nil,
CfgLogs: "/root/logs",
Path: "",
ResultLogs: "/root/logs",
},
{
CLIHome: &tmp,
CfgHome: "",
CLILogs: nil,
CfgLogs: "/root/logs",
Path: "",
ResultLogs: "/root/logs",
Home: "/tmp/",
Logs: "/var/",
Path: "log",
ResultLogs: "/var/log",
},
{
CLIHome: &tmp,
CfgHome: "",
CLILogs: &root,
CfgLogs: "/root/logs",
Path: "",
ResultLogs: "/root",
Home: "/tmp/",
Logs: "/root/",
Path: "/var/log",
ResultLogs: "/var/log",
},
}

for _, test := range tests {
t.Log("Executing test", test)
homePath = test.CLIHome
logsPath = test.CLILogs
cfg := Path{Home: test.CfgHome, Logs: test.CfgLogs}
cfg := Path{Home: test.Home, Logs: test.Logs}
assert.NoError(t, Paths.initPaths(&cfg))

assert.Equal(t, test.ResultLogs, Resolve(Logs, test.Path))
Expand Down

0 comments on commit 82f9556

Please sign in to comment.