-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
126 lines (113 loc) · 2.81 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
package main
import (
"fmt"
"os"
"reflect"
"testing"
"fortio.org/log"
)
func TestValidateErrorsAndLogger(t *testing.T) {
req := SlackPostMessageRequest{}
err := validate(req)
if err == nil {
t.Errorf("Expected error on empty request validation, got nil")
}
// for running go test -v and seeing the log:
log.S(log.Error, "Testing logging - err", log.Any("err", err))
kv := log.Any("err", err)
kvStr := kv.StringValue()
expected := `"Channel is not set and Neither attachments, blocks, nor text is set"`
if kvStr != expected {
t.Errorf("Expected %s, got %s", expected, kvStr)
}
}
func TestGetSlackTokens(t *testing.T) {
tests := []struct {
name string
envValue string
expected []string
}{
{
name: "Multiple tokens",
envValue: "token1,token2, token3",
expected: []string{"token1", "token2", "token3"},
},
{
name: "Single token",
envValue: "token1",
expected: []string{"token1"},
},
{
name: "No tokens",
envValue: "",
expected: []string{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Set up the environment variable for the test
t.Setenv("SLACK_TOKENS", tt.envValue)
// Call the function
tokens := getSlackTokens()
// Check if the result matches the expected output
if !reflect.DeepEqual(tokens, tt.expected) {
t.Errorf("Expected %v, got %v", tt.expected, tokens)
}
// Clean up the environment variable
os.Unsetenv("SLACK_TOKENS")
})
}
}
func TestPodIndex(t *testing.T) {
tests := []struct {
name string
podName string
expected int
expectErr error
}{
{
name: "Valid pod name",
podName: "pod-3",
expected: 3,
expectErr: nil,
},
{
name: "Invalid pod name, no index",
podName: "pod",
expected: 0,
expectErr: fmt.Errorf("invalid pod name %s. Expected <name>-<index>", "pod"),
},
{
name: "Invalid pod name, dash at the end",
podName: "pod-",
expected: 0,
expectErr: fmt.Errorf("invalid pod name %s. Expected <name>-<index>", "pod-"),
},
{
name: "Invalid pod index",
podName: "pod-abcde",
expected: 0,
expectErr: fmt.Errorf("invalid pod name format. Expected <name>-<index>, got %s", "pod-abcde"),
},
{
name: "No pod name",
podName: "",
expected: 0,
expectErr: fmt.Errorf("invalid pod name %s. Expected <name>-<index>", ""),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Call the function
index, err := podIndex(tt.podName)
// Check if an error was expected
if tt.expectErr != nil && err.Error() != tt.expectErr.Error() {
t.Errorf("Expected error %v, but got %v", tt.expectErr, err)
}
// Check if the result matches the expected output
if index != tt.expected {
t.Errorf("Expected %v, got %v", tt.expected, index)
}
})
}
}