@@ -11,31 +11,29 @@ import (
11
11
kubefake "k8s.io/client-go/kubernetes/fake"
12
12
)
13
13
14
- func TestGatherLogs (t * testing.T ) {
15
- const testPodNamespace = "pod-namespace "
14
+ func testGatherLogs (t * testing.T , regexSearch bool , stringToSearch string , shouldExist bool ) {
15
+ const testPodName = "test "
16
16
const testLogFileName = "errors"
17
- // there's no way to specify logs fake pod will have, so we can only search for a hardcoded string "fake logs"
18
- const stringToSearch = "fake logs"
19
17
20
18
coreClient := kubefake .NewSimpleClientset ().CoreV1 ()
21
19
ctx := context .Background ()
22
20
23
- _ , err := coreClient .Pods (testPodNamespace ).Create (
21
+ _ , err := coreClient .Pods (testPodName ).Create (
24
22
ctx ,
25
23
& corev1.Pod {
26
24
ObjectMeta : metav1.ObjectMeta {
27
- Name : testPodNamespace ,
28
- Namespace : testPodNamespace ,
25
+ Name : testPodName ,
26
+ Namespace : testPodName ,
29
27
},
30
28
Status : corev1.PodStatus {
31
29
Phase : corev1 .PodRunning ,
32
30
ContainerStatuses : []corev1.ContainerStatus {
33
- {Name : testPodNamespace },
31
+ {Name : testPodName },
34
32
},
35
33
},
36
34
Spec : corev1.PodSpec {
37
35
Containers : []corev1.Container {
38
- {Name : testPodNamespace },
36
+ {Name : testPodName },
39
37
},
40
38
},
41
39
},
@@ -48,24 +46,53 @@ func TestGatherLogs(t *testing.T) {
48
46
records , err := gatherLogsFromPodsInNamespace (
49
47
ctx ,
50
48
coreClient ,
51
- testPodNamespace ,
49
+ testPodName ,
52
50
[]string {
53
51
stringToSearch ,
54
52
},
55
53
86400 , // last day
56
54
1024 * 64 , // maximum 64 kb of logs
57
55
testLogFileName ,
58
56
"" ,
57
+ regexSearch ,
59
58
)
60
59
if err != nil {
61
60
t .Fatal (err )
62
61
}
63
62
63
+ if ! shouldExist {
64
+ assert .Len (t , records , 0 )
65
+ return
66
+ }
67
+
64
68
assert .Len (t , records , 1 )
65
69
assert .Equal (
66
70
t ,
67
- fmt .Sprintf ("config/pod/%s/logs/%s/%s.log" , testPodNamespace , testPodNamespace , testLogFileName ),
71
+ fmt .Sprintf ("config/pod/%s/logs/%s/%s.log" , testPodName , testPodName , testLogFileName ),
68
72
records [0 ].Name ,
69
73
)
70
- assert .Equal (t , Raw {stringToSearch + "\n " }, records [0 ].Item )
74
+ if regexSearch {
75
+ assert .Regexp (t , stringToSearch , records [0 ].Item )
76
+ } else {
77
+ assert .Equal (t , Raw {stringToSearch + "\n " }, records [0 ].Item )
78
+ }
79
+ }
80
+
81
+ func TestGatherLogs (t * testing.T ) {
82
+ t .Run ("SubstringSearch_ShouldExist" , func (t * testing.T ) {
83
+ testGatherLogs (t , false , "fake logs" , true )
84
+ })
85
+ t .Run ("SubstringSearch_ShouldNotExist" , func (t * testing.T ) {
86
+ testGatherLogs (t , false , "The quick brown fox jumps over the lazy dog" , false )
87
+ })
88
+ t .Run ("SubstringSearch_ShouldNotExist" , func (t * testing.T ) {
89
+ testGatherLogs (t , false , "f.*l" , false )
90
+ })
91
+
92
+ t .Run ("RegexSearch_ShouldExist" , func (t * testing.T ) {
93
+ testGatherLogs (t , true , "f.*l" , true )
94
+ })
95
+ t .Run ("RegexSearch_ShouldNotExist" , func (t * testing.T ) {
96
+ testGatherLogs (t , true , "[0-9]99" , false )
97
+ })
71
98
}
0 commit comments