summaryrefslogtreecommitdiff
path: root/internal/config/args_test.go
blob: 3e1f1a15b8f90a8bd26ba5f2b0449c44f61ed414 (plain)
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
package config

import (
	"encoding/base64"
	"strings"
	"testing"

	"github.com/mimecast/dtail/internal/lcontext"
	"github.com/mimecast/dtail/internal/omode"
	"github.com/mimecast/dtail/internal/testutil"
)

func TestArgs(t *testing.T) {
	t.Run("default values", func(t *testing.T) {
		args := Args{}
		
		// Test zero values
		testutil.AssertEqual(t, false, args.Quiet)
		testutil.AssertEqual(t, false, args.Plain)
		testutil.AssertEqual(t, false, args.Serverless)
		testutil.AssertEqual(t, false, args.NoColor)
		testutil.AssertEqual(t, false, args.RegexInvert)
		testutil.AssertEqual(t, "", args.SSHPrivateKeyFilePath)
		testutil.AssertEqual(t, false, args.TrustAllHosts)
		testutil.AssertEqual(t, 0, args.ConnectionsPerCPU)
		testutil.AssertEqual(t, "", args.ServersStr)
		testutil.AssertEqual(t, "", args.What)
		testutil.AssertEqual(t, "", args.QueryStr)
		testutil.AssertEqual(t, "", args.RegexStr)
		testutil.AssertEqual(t, 0, args.SSHPort)
		testutil.AssertEqual(t, omode.Mode(0), args.Mode)
	})

	t.Run("serialize options", func(t *testing.T) {
		args := Args{
			Quiet:      true,
			Plain:      true,
			Serverless: false,
			LContext: lcontext.LContext{
				MaxCount:      10,
				BeforeContext: 2,
				AfterContext:  3,
			},
		}
		
		// Serialize
		serialized := args.SerializeOptions()
		testutil.AssertContains(t, serialized, "quiet=true")
		testutil.AssertContains(t, serialized, "plain=true")
		testutil.AssertContains(t, serialized, "max=10")
		testutil.AssertContains(t, serialized, "before=2")
		testutil.AssertContains(t, serialized, "after=3")
		// serverless=false should not be included
		if strings.Contains(serialized, "serverless") {
			t.Error("serverless=false should not be serialized")
		}
	})

	t.Run("deserialize options", func(t *testing.T) {
		options := []string{
			"quiet=true",
			"plain=true",
			"before=5",
			"after=3",
			"max=100",
		}
		
		opts, ltx, err := DeserializeOptions(options)
		testutil.AssertNoError(t, err)
		
		// Check parsed options
		testutil.AssertEqual(t, "true", opts["quiet"])
		testutil.AssertEqual(t, "true", opts["plain"])
		
		// Check lcontext values
		testutil.AssertEqual(t, 5, ltx.BeforeContext)
		testutil.AssertEqual(t, 3, ltx.AfterContext)
		testutil.AssertEqual(t, 100, ltx.MaxCount)
	})

	t.Run("deserialize with base64", func(t *testing.T) {
		// Create a base64 encoded value
		testValue := "test pattern with spaces"
		encoded := "base64%" + base64.StdEncoding.EncodeToString([]byte(testValue))
		
		options := []string{
			"what=" + encoded,
			"quiet=true",
		}
		
		opts, _, err := DeserializeOptions(options)
		testutil.AssertNoError(t, err)
		
		testutil.AssertEqual(t, testValue, opts["what"])
		testutil.AssertEqual(t, "true", opts["quiet"])
	})

	t.Run("deserialize invalid format", func(t *testing.T) {
		options := []string{
			"invalidformat", // No equals sign
		}
		
		_, _, err := DeserializeOptions(options)
		testutil.AssertError(t, err, "Unable to parse options")
	})

	t.Run("deserialize invalid base64", func(t *testing.T) {
		options := []string{
			"what=base64%invalid!!!base64",
		}
		
		_, _, err := DeserializeOptions(options)
		testutil.AssertError(t, err, "")
	})

	t.Run("deserialize invalid numeric values", func(t *testing.T) {
		options := []string{
			"before=notanumber",
		}
		
		_, _, err := DeserializeOptions(options)
		testutil.AssertError(t, err, "")
	})

	t.Run("string representation", func(t *testing.T) {
		args := Args{
			Quiet:      true,
			Plain:      true,
			ServersStr: "server1,server2",
			What:       "error",
			UserName:   "testuser",
			SSHPort:    2222,
		}
		
		str := args.String()
		testutil.AssertContains(t, str, "Quiet:true")
		testutil.AssertContains(t, str, "Plain:true")
		testutil.AssertContains(t, str, "ServersStr:server1,server2")
		testutil.AssertContains(t, str, "What:error")
		testutil.AssertContains(t, str, "UserName:testuser")
		testutil.AssertContains(t, str, "SSHPort:2222")
	})
}