Skip to content

Commit

Permalink
Add test for cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
tkuchiki committed Sep 8, 2023
1 parent 673398a commit efb2ac8
Show file tree
Hide file tree
Showing 20 changed files with 864 additions and 24 deletions.
111 changes: 111 additions & 0 deletions cmd/alp/cmd/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package cmd

import (
"strings"
"testing"

"github.com/tkuchiki/alp/internal/testutil"
)

func TestCommonFlags(t *testing.T) {
tempDir := t.TempDir()

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

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

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

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

tests := []struct {
args []string
}{
{
args: []string{"json",
"--file", tempLog,
"--noheaders",
"--format", "tsv",
"--config", tempConfig,
},
},
{
args: []string{"json",
"--file", tempLog,
"--decode-uri",
"--filters", "Method == 'POST'",
"--format", "markdown",
"--limit", "5",
"--location", "America/Adak",
"--matching-groups", "/foo/bar/.+",
"--output", "count,uri",
"--page", "10",
"--percentiles", "99",
"--qs-ignore-values",
"--query-string",
"--reverse",
"--show-footers",
"--sort", "uri",
},
},
{
args: []string{"json",
"--file", tempLog,
"-f", "Method == 'POST'",
"-m", "/foo/bar/.+",
"-o", "count,uri",
"-q",
"-r",
},
},
{
args: []string{"json",
"--file", tempLog,
"--pos", tempPos,
},
},
{
args: []string{"json",
"--file", tempLog,
"--pos", tempPos,
"--nosave-pos",
},
},
// Do not change the order
{
args: []string{"json",
"--file", tempLog,
"--dump", tempDump,
},
},
{
args: []string{"json",
"--load", tempDump,
},
},
}

for _, tt := range tests {
t.Run(strings.Join(tt.args, " "), func(t *testing.T) {
rootCmd := NewRootCmd("test")
rootCmd.SetArgs(tt.args)

err := rootCmd.Execute()
if err != nil {
t.Fatal(err)
}
})
}
}
23 changes: 23 additions & 0 deletions cmd/alp/cmd/count_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cmd

import (
"testing"
)

func TestCountCmd(t *testing.T) {
file := "../../../example/logs/json_access.log"
args := []string{"count",
"--file", file,
"--format", "json",
"--reverse",
"--keys", "ua",
}

rootCmd := NewRootCmd("test")
rootCmd.SetArgs(args)

err := rootCmd.Execute()
if err != nil {
t.Fatal(err)
}
}
2 changes: 1 addition & 1 deletion cmd/alp/cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/tkuchiki/alp/stats"
)

func NewDiffCmd(rootCmd *cobra.Command) *cobra.Command {
func NewDiffCmd() *cobra.Command {
diffCmd := &cobra.Command{
Use: "diff <from> <to>",
Args: cobra.ExactArgs(2),
Expand Down
19 changes: 19 additions & 0 deletions cmd/alp/cmd/diff_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cmd

import "testing"

func TestDiffCmd(t *testing.T) {
from := "../../../example/logs/dump1.yaml"
to := "../../../example/logs/dump2.yaml"
args := []string{"diff",
from, to,
}

rootCmd := NewRootCmd("test")
rootCmd.SetArgs(args)

err := rootCmd.Execute()
if err != nil {
t.Fatal(err)
}
}
2 changes: 1 addition & 1 deletion cmd/alp/cmd/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/tkuchiki/alp/stats"
)

func NewJSONCmd(rootCmd *cobra.Command) *cobra.Command {
func NewJSONCmd() *cobra.Command {
var jsonCmd = &cobra.Command{
Use: "json",
Short: "Profile the logs for JSON",
Expand Down
45 changes: 45 additions & 0 deletions cmd/alp/cmd/json_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cmd

import (
"testing"

"github.com/tkuchiki/alp/internal/testutil"
)

func TestJSONCmd(t *testing.T) {
keys := testutil.LogKeys{
Uri: "u",
Method: "m",
Time: "t",
ResponseTime: "r",
RequestTime: "r2",
BodyBytes: "b",
Status: "s",
}

jsonLog := testutil.JsonLog(keys)

tempFile, err := testutil.CreateTempDirAndFile(t.TempDir(), jsonLog)
if err != nil {
t.Fatal(err)
}

args := []string{"json",
"--file", tempFile,
"--uri-key", keys.Uri,
"--method-key", keys.Method,
"--time-key", keys.Time,
"--restime-key", keys.ResponseTime,
"--reqtime-key", keys.RequestTime,
"--body-bytes-key", keys.BodyBytes,
"--status-key", keys.Status,
}

rootCmd := NewRootCmd("test")
rootCmd.SetArgs(args)

err = rootCmd.Execute()
if err != nil {
t.Fatal(err)
}
}
2 changes: 1 addition & 1 deletion cmd/alp/cmd/ltsv.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/tkuchiki/alp/stats"
)

func NewLTSVCmd(rootCmd *cobra.Command) *cobra.Command {
func NewLTSVCmd() *cobra.Command {
var ltsvCmd = &cobra.Command{
Use: "ltsv",
Short: "Profile the logs for LTSV",
Expand Down
45 changes: 45 additions & 0 deletions cmd/alp/cmd/ltsv_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cmd

import (
"testing"

"github.com/tkuchiki/alp/internal/testutil"
)

func TestLTSVCmd(t *testing.T) {
keys := testutil.LogKeys{
Uri: "u",
Method: "m",
Time: "t",
ResponseTime: "r",
RequestTime: "r2",
BodyBytes: "b",
Status: "s",
}

ltsvLog := testutil.LTSVLog(keys)

tempFile, err := testutil.CreateTempDirAndFile(t.TempDir(), ltsvLog)
if err != nil {
t.Fatal(err)
}

args := []string{"ltsv",
"--file", tempFile,
"--uri-label", keys.Uri,
"--method-label", keys.Method,
"--time-label", keys.Time,
"--apptime-label", keys.ResponseTime,
"--reqtime-label", keys.RequestTime,
"--size-label", keys.BodyBytes,
"--status-label", keys.Status,
}

rootCmd := NewRootCmd("test")
rootCmd.SetArgs(args)

err = rootCmd.Execute()
if err != nil {
t.Fatal(err)
}
}
1 change: 1 addition & 0 deletions cmd/alp/cmd/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ func createCommonOptionsFromFlags(cmd *cobra.Command, sortOptions *stats.SortOpt
func createOptionsFromConfig(cmd *cobra.Command, sortOptions *stats.SortOptions, config string) (*options.Options, error) {
opts := options.NewOptions()
viper.SetConfigFile(config)
viper.SetConfigType("yaml")

if err := viper.ReadInConfig(); err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion cmd/alp/cmd/pcap.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/tkuchiki/alp/stats"
)

func NewPcapCmd(rootCmd *cobra.Command) *cobra.Command {
func NewPcapCmd() *cobra.Command {
var pcapCmd = &cobra.Command{
Use: "pcap",
Short: "Profile the HTTP requests for captured packets",
Expand Down
26 changes: 26 additions & 0 deletions cmd/alp/cmd/pcap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cmd

import (
"testing"

"github.com/tkuchiki/alp/options"
)

func TestPcapCmd(t *testing.T) {
pcapFile := "../../../example/logs/http.cap"
pcapServerPort := "18080"

args := []string{"pcap",
"--file", pcapFile,
"--pcap-server-ip", options.DefaultPcapServerIPsOption[0],
"--pcap-server-port", pcapServerPort,
}

rootCmd := NewRootCmd("test")
rootCmd.SetArgs(args)

err := rootCmd.Execute()
if err != nil {
t.Fatal(err)
}
}
2 changes: 1 addition & 1 deletion cmd/alp/cmd/regexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/tkuchiki/alp/stats"
)

func NewRegexpCmd(rootCmd *cobra.Command) *cobra.Command {
func NewRegexpCmd() *cobra.Command {
var regexpCmd = &cobra.Command{
Use: "regexp",
Short: "Profile the logs that match a regular expression",
Expand Down
46 changes: 46 additions & 0 deletions cmd/alp/cmd/regexp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cmd

import (
"testing"

"github.com/tkuchiki/alp/internal/testutil"
)

func TestRegexpCmd(t *testing.T) {
keys := testutil.LogKeys{
Uri: "u",
Method: "m",
Time: "t",
ResponseTime: "r",
RequestTime: "r2",
BodyBytes: "b",
Status: "s",
}

regexpLog := testutil.RegexpLog()

tempFile, err := testutil.CreateTempDirAndFile(t.TempDir(), regexpLog)
if err != nil {
t.Fatal(err)
}

args := []string{"regexp",
"--file", tempFile,
"--pattern", testutil.RegexpPattern(keys),
"--uri-subexp", keys.Uri,
"--method-subexp", keys.Method,
"--time-subexp", keys.Time,
"--restime-subexp", keys.ResponseTime,
"--reqtime-subexp", keys.RequestTime,
"--body-bytes-subexp", keys.BodyBytes,
"--status-subexp", keys.Status,
}

rootCmd := NewRootCmd("test")
rootCmd.SetArgs(args)

err = rootCmd.Execute()
if err != nil {
t.Fatal(err)
}
}
10 changes: 5 additions & 5 deletions cmd/alp/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ func NewRootCmd(version string) *cobra.Command {
},
}

rootCmd.AddCommand(NewLTSVCmd(rootCmd))
rootCmd.AddCommand(NewJSONCmd(rootCmd))
rootCmd.AddCommand(NewRegexpCmd(rootCmd))
rootCmd.AddCommand(NewPcapCmd(rootCmd))
rootCmd.AddCommand(NewDiffCmd(rootCmd))
rootCmd.AddCommand(NewLTSVCmd())
rootCmd.AddCommand(NewJSONCmd())
rootCmd.AddCommand(NewRegexpCmd())
rootCmd.AddCommand(NewPcapCmd())
rootCmd.AddCommand(NewDiffCmd())
rootCmd.AddCommand(NewCountCmd())
rootCmd.SetVersionTemplate(fmt.Sprintln(version))

Expand Down
Loading

0 comments on commit efb2ac8

Please sign in to comment.