-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
165 lines (155 loc) · 5.17 KB
/
main_test.go
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"errors"
"fmt"
"testing"
"time"
"github.com/aws/smithy-go"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/xhit/go-str2duration/v2"
)
func TestErrorMapError(t *testing.T) {
t.Run("verify with ErrorMap filled", func(t *testing.T) {
err := ErrorMap{
"test": errors.New("something"),
"test2": errors.New("something2"),
}
assert.Error(t, err)
assert.Contains(t, err.Error(), "something")
assert.Contains(t, err.Error(), "something2")
})
t.Run("verify with empty ErrorMap", func(t *testing.T) {
err := ErrorMap{}
assert.Error(t, err)
assert.EqualError(t, err, "")
})
}
func TestCheckError(t *testing.T) {
t.Run("with standard error", func(t *testing.T) {
CheckError(errors.New("error"), func(format string, args ...interface{}) {
assert.Equal(t, "%s\n", format)
assert.Implements(t, (*error)(nil), args[0])
})
})
t.Run("with smithy.APIError", func(t *testing.T) {
err := &smithy.GenericAPIError{Code: "1234", Message: "test", Fault: smithy.FaultClient}
CheckError(err, func(format string, args ...interface{}) {
assert.Equal(t, "code: %s, message: %s, fault: %s", format)
assert.Equal(t, "1234", args[0])
assert.Equal(t, "test", args[1])
})
})
}
func TestValidateFlags(t *testing.T) {
t.Run("Everything fine", func(t *testing.T) {
viper.Set(loggroup, "testgroup")
viper.Set(starttime, "12345")
err := validateFlags()
assert.NoError(t, err)
viper.Reset()
})
t.Run("No loggroup", func(t *testing.T) {
viper.Set(starttime, "12345")
err := validateFlags()
assert.Error(t, err)
assert.EqualError(t, err, fmt.Sprintf("log-group:%s is a required flag\n", loggroup))
viper.Reset()
})
t.Run("Endtime and duration at the same time", func(t *testing.T) {
viper.Set(loggroup, "testgroup")
viper.Set(endtime, "12345")
viper.Set(duration, "1d")
err := validateFlags()
assert.Error(t, err)
assert.EqualError(t, err, fmt.Sprintf("duration:%s and %s must not provided together\n", endtime, duration))
viper.Reset()
})
t.Cleanup(viper.Reset)
}
func TestParseFlags(t *testing.T) {
flagDefaults := func() {
viper.SetDefault(loggroup, "unittest")
viper.SetDefault(limit, 10000)
}
flagDefaults()
t.Run("Nothing else set", func(t *testing.T) {
t.Cleanup(flagDefaults)
filterLogsInput, err := parseFlags()
assert.NoError(t, err)
assert.Equal(t, "unittest", *filterLogsInput.LogGroupName)
assert.Equal(t, int32(10000), *filterLogsInput.Limit)
viper.Reset()
})
t.Run("With filter", func(t *testing.T) {
t.Cleanup(flagDefaults)
viper.Set(filter, "xyz")
filterLogsInput, err := parseFlags()
assert.NoError(t, err)
assert.Equal(t, "xyz", *filterLogsInput.FilterPattern)
viper.Reset()
})
t.Run("With logstreamnames", func(t *testing.T) {
t.Cleanup(flagDefaults)
viper.Set(logstreamnames, []string{"something"})
filterLogsInput, err := parseFlags()
assert.NoError(t, err)
assert.Contains(t, filterLogsInput.LogStreamNames, "something")
viper.Reset()
})
t.Run("With logstreamprefix", func(t *testing.T) {
t.Cleanup(flagDefaults)
viper.Set(logstreamprefix, "abc")
filterLogsInput, err := parseFlags()
assert.NoError(t, err)
assert.Equal(t, "abc", *filterLogsInput.LogStreamNamePrefix)
viper.Reset()
})
t.Run("With duration", func(t *testing.T) {
t.Cleanup(flagDefaults)
viper.Set(duration, "1d")
filterLogsInput, err := parseFlags()
assert.NoError(t, err)
expectedDuration, err := str2duration.ParseDuration("1d")
assert.NoError(t, err)
// verify that EndTime is greater then 0 as othwise this will result in an API error.
assert.Greater(t, *filterLogsInput.EndTime, int64(0))
// Millisecond precision is sometimes to hard so we except a derivation of 5ms
assert.WithinDuration(t, time.Now().Add(expectedDuration*-1), time.UnixMilli(*filterLogsInput.StartTime), 5*time.Millisecond)
viper.Reset()
})
t.Run("With startdate", func(t *testing.T) {
t.Cleanup(flagDefaults)
viper.Set(starttime, "2022-01-02T15:04:05Z")
filterLogsInput, err := parseFlags()
assert.NoError(t, err)
expectedStart, err := time.Parse(time.RFC3339, viper.GetString(starttime))
assert.NoError(t, err)
assert.Equal(t, expectedStart.UnixMilli(), *filterLogsInput.StartTime)
viper.Reset()
})
t.Run("With startdate and duration", func(t *testing.T) {
t.Cleanup(flagDefaults)
viper.Set(starttime, "2022-01-02T15:04:05Z")
viper.Set(duration, "1d")
duration, _ := str2duration.ParseDuration(viper.GetString(duration))
expectedStart, _ := time.Parse(time.RFC3339, viper.GetString(starttime))
expectedEnd := expectedStart.Add(duration)
filterLogsInput, err := parseFlags()
assert.NoError(t, err)
assert.NoError(t, err)
assert.Equal(t, expectedStart.UnixMilli(), *filterLogsInput.StartTime)
assert.WithinDuration(t, expectedEnd, time.UnixMilli(*filterLogsInput.EndTime), 5*time.Millisecond)
viper.Reset()
})
t.Run("With enddate", func(t *testing.T) {
t.Cleanup(flagDefaults)
viper.Set(endtime, "2022-01-02T15:04:05Z")
filterLogsInput, err := parseFlags()
assert.NoError(t, err)
expectedEnd, err := time.Parse(time.RFC3339, viper.GetString(endtime))
assert.NoError(t, err)
assert.Equal(t, expectedEnd.UnixMilli(), *filterLogsInput.EndTime)
viper.Reset()
})
}