Skip to content

Commit

Permalink
Add tests for flags
Browse files Browse the repository at this point in the history
  • Loading branch information
tkuchiki committed Sep 24, 2023
1 parent 3960e66 commit 6052499
Show file tree
Hide file tree
Showing 8 changed files with 370 additions and 11 deletions.
8 changes: 4 additions & 4 deletions cmd/alp/cmd/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ import (
func TestCommonFlags(t *testing.T) {
tempDir := t.TempDir()

tempLog, err := testutil.CreateTempDirAndFile(tempDir, testutil.JsonLog(testutil.NewJsonLogKeys()))
tempLog, err := testutil.CreateTempDirAndFile(tempDir, "test_common_flags_temp_log", testutil.JsonLog(testutil.NewJsonLogKeys()))
if err != nil {
t.Fatal(err)
}

tempConfig, err := testutil.CreateTempDirAndFile(tempDir, testutil.ConfigFile())
tempConfig, err := testutil.CreateTempDirAndFile(tempDir, "test_common_flags_temp_config", testutil.ConfigFile())
if err != nil {
t.Fatal(err)
}

tempPos, err := testutil.CreateTempDirAndFile(tempDir, "")
tempPos, err := testutil.CreateTempDirAndFile(tempDir, "test_common_flags_temp_pos", "")
if err != nil {
t.Fatal(err)
}

tempDump, err := testutil.CreateTempDirAndFile(tempDir, "")
tempDump, err := testutil.CreateTempDirAndFile(tempDir, "test_common_flags_temp_dump", "")
if err != nil {
t.Fatal(err)
}
Expand Down
135 changes: 135 additions & 0 deletions cmd/alp/cmd/flags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package cmd

import (
"strings"
"testing"

"github.com/spf13/viper"

"github.com/google/go-cmp/cmp"
"github.com/tkuchiki/alp/internal/testutil"
"github.com/tkuchiki/alp/options"
)

func Test_createOptionsFromConfig(t *testing.T) {
viper.Reset()
rootCmd := NewRootCmd("test")
flags := newFlags()

flags.defineProfileOptions(rootCmd)
flags.defineJSONOptions(rootCmd)
flags.defineLTSVOptions(rootCmd)
flags.defineRegexpOptions(rootCmd)
flags.definePcapOptions(rootCmd)
flags.defineCountKeys(rootCmd)

tempDir := t.TempDir()
sort := "max"
dummyOpts := testutil.DummyOptions(sort)

var err error
flags.config, err = testutil.CreateTempDirAndFile(tempDir, "test_create_options_from_config_config", testutil.DummyConfigFile(sort, dummyOpts))
if err != nil {
t.Fatal(err)
}

var opts *options.Options
opts, err = flags.createOptionsFromConfig(rootCmd)
if err != nil {
t.Fatal(err)
}

if diff := cmp.Diff(dummyOpts, opts); diff != "" {
t.Errorf("%s", diff)
}
}

func Test_createOptionsFromConfig_overwrite(t *testing.T) {
rootCmd := NewRootCmd("test")
flags := newFlags()

flags.defineProfileOptions(rootCmd)
flags.defineJSONOptions(rootCmd)
flags.defineLTSVOptions(rootCmd)
flags.defineRegexpOptions(rootCmd)
flags.definePcapOptions(rootCmd)
flags.defineCountKeys(rootCmd)

tempDir := t.TempDir()
sort := "max"

overwrittenSort := "min"
overwrittenOpts := testutil.DummyOverwrittenOptions(overwrittenSort)

var err error
flags.config, err = testutil.CreateTempDirAndFile(tempDir, "test_create_options_from_config_overwrite_config", testutil.DummyConfigFile(sort, overwrittenOpts))
if err != nil {
t.Fatal(err)
}

viper.Set("file", overwrittenOpts.File)
viper.Set("dump", overwrittenOpts.Dump)
viper.Set("load", overwrittenOpts.Load)
viper.Set("sort", overwrittenSort)
viper.Set("reverse", overwrittenOpts.Reverse)
viper.Set("query_string", overwrittenOpts.QueryString)
viper.Set("query_string_ignore_values", overwrittenOpts.QueryStringIgnoreValues)
viper.Set("decode_uri", overwrittenOpts.DecodeUri)
viper.Set("format", overwrittenOpts.Format)
viper.Set("noheaders", overwrittenOpts.NoHeaders)
viper.Set("show_footers", overwrittenOpts.ShowFooters)
viper.Set("limit", overwrittenOpts.Limit)
viper.Set("matching_groups", strings.Join(overwrittenOpts.MatchingGroups, ","))
viper.Set("filters", overwrittenOpts.Filters)
viper.Set("pos_file", overwrittenOpts.PosFile)
viper.Set("nosave_pos", overwrittenOpts.NoSavePos)
viper.Set("location", overwrittenOpts.Location)
viper.Set("output", overwrittenOpts.Output)
viper.Set("percentiles", testutil.IntSliceToString(overwrittenOpts.Percentiles))
viper.Set("pagination_limit", overwrittenOpts.PaginationLimit)

// json
viper.Set("json.uri_key", overwrittenOpts.JSON.UriKey)
viper.Set("json.method_key", overwrittenOpts.JSON.MethodKey)
viper.Set("json.time_key", overwrittenOpts.JSON.TimeKey)
viper.Set("json.restime_key", overwrittenOpts.JSON.ResponseTimeKey)
viper.Set("json.reqtime_key", overwrittenOpts.JSON.RequestTimeKey)
viper.Set("json.body_bytes_key", overwrittenOpts.JSON.BodyBytesKey)
viper.Set("json.status_key", overwrittenOpts.JSON.StatusKey)

// ltsv
viper.Set("ltsv.uri_label", overwrittenOpts.LTSV.UriLabel)
viper.Set("ltsv.method_label", overwrittenOpts.LTSV.MethodLabel)
viper.Set("ltsv.time_label", overwrittenOpts.LTSV.TimeLabel)
viper.Set("ltsv.apptime_label", overwrittenOpts.LTSV.ApptimeLabel)
viper.Set("ltsv.reqtime_label", overwrittenOpts.LTSV.ReqtimeLabel)
viper.Set("ltsv.size_label", overwrittenOpts.LTSV.SizeLabel)
viper.Set("ltsv.status_label", overwrittenOpts.LTSV.StatusLabel)

// regexp
viper.Set("regexp.pattern", overwrittenOpts.Regexp.Pattern)
viper.Set("regexp.uri_subexp", overwrittenOpts.Regexp.UriSubexp)
viper.Set("regexp.method_subexp", overwrittenOpts.Regexp.MethodSubexp)
viper.Set("regexp.time_subexp", overwrittenOpts.Regexp.TimeSubexp)
viper.Set("regexp.restime_subexp", overwrittenOpts.Regexp.ResponseTimeSubexp)
viper.Set("regexp.reqtime_subexp", overwrittenOpts.Regexp.RequestTimeSubexp)
viper.Set("regexp.body_bytes_subexp", overwrittenOpts.Regexp.BodyBytesSubexp)
viper.Set("regexp.status_subexp", overwrittenOpts.Regexp.StatusSubexp)

// pcap
viper.Set("pcap.server_ips", strings.Join(overwrittenOpts.Pcap.ServerIPs, ","))
viper.Set("pcap.server_port", overwrittenOpts.Pcap.ServerPort)

// count
viper.Set("count.keys", overwrittenOpts.Count.Keys)

var opts *options.Options
opts, err = flags.createOptionsFromConfig(rootCmd)
if err != nil {
t.Fatal(err)
}

if diff := cmp.Diff(overwrittenOpts, opts); diff != "" {
t.Errorf("%s", diff)
}
}
2 changes: 1 addition & 1 deletion cmd/alp/cmd/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestJSONCmd(t *testing.T) {

jsonLog := testutil.JsonLog(keys)

tempFile, err := testutil.CreateTempDirAndFile(t.TempDir(), jsonLog)
tempFile, err := testutil.CreateTempDirAndFile(t.TempDir(), "test_json_cmd_temp_file", jsonLog)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/alp/cmd/ltsv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestLTSVCmd(t *testing.T) {

ltsvLog := testutil.LTSVLog(keys)

tempFile, err := testutil.CreateTempDirAndFile(t.TempDir(), ltsvLog)
tempFile, err := testutil.CreateTempDirAndFile(t.TempDir(), "test_ltsv_cmd_temp_file", ltsvLog)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/alp/cmd/regexp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestRegexpCmd(t *testing.T) {

regexpLog := testutil.RegexpLog()

tempFile, err := testutil.CreateTempDirAndFile(t.TempDir(), regexpLog)
tempFile, err := testutil.CreateTempDirAndFile(t.TempDir(), "test_regexp_cmd_temp_file", regexpLog)
if err != nil {
t.Fatal(err)
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/tkuchiki/alp
require (
github.com/Songmu/go-ltsv v0.0.0-20200903131950-a608c3f6a014
github.com/antonmedv/expr v1.8.9
github.com/google/go-cmp v0.5.9
github.com/google/gopacket v1.1.19
github.com/kylelemons/godebug v1.1.0
github.com/olekukonko/tablewriter v0.0.5
Expand Down
Loading

0 comments on commit 6052499

Please sign in to comment.