-
Notifications
You must be signed in to change notification settings - Fork 12
/
main_test.go
80 lines (68 loc) · 3.32 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
// Copyright 2024 SAP SE
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"errors"
"fmt"
"strings"
"testing"
"github.com/euank/go-kmsg-parser/kmsgparser"
"github.com/stretchr/testify/require"
)
func TestGetPodUIDFromLog(t *testing.T) {
klog, podUIDs, containerIDs := getTestData()
var extractedContainerIDs []string
var extractedPodUIDs []string
for _, msg := range klog {
parsedMsg, err := parseMessage(msg)
require.NoError(t, err, "There should be no error while parsing kernel log")
uid, cid := getContainerIDFromLog(parsedMsg.Message)
fmt.Println(uid)
extractedContainerIDs = append(extractedContainerIDs, cid)
extractedPodUIDs = append(extractedPodUIDs, uid)
}
require.Equal(t, containerIDs, extractedContainerIDs, "Extracted container ids do not match the expected result")
require.Equal(t, podUIDs, extractedPodUIDs, "Extracted container ids do not match the expected result")
}
func parseMessage(input string) (kmsgparser.Message, error) {
// Format:
// PRIORITY,SEQUENCE_NUM,TIMESTAMP,-;MESSAGE
parts := strings.SplitN(input, ";", 2)
if len(parts) != 2 {
return kmsgparser.Message{}, errors.New("invalid kmsg; must contain a ';'")
}
metadata, message := parts[0], parts[1]
metadataParts := strings.Split(metadata, ",")
if len(metadataParts) < 3 {
return kmsgparser.Message{}, errors.New("invalid kmsg: must contain at least 3 ',' separated pieces at the start")
}
return kmsgparser.Message{
Message: message,
}, nil
}
func getTestData() (klog, podUIDs, containerIDs []string) {
klog = []string{
"6,22743,6115623303887,-;oom-kill:constraint=CONSTRAINT_MEMCG,nodemask=(null),cpuset=9f02d9fa0049eb2655fc83c765f142362b2cb403b57b70ba3185071015ca3b64,mems_allowed=0-1,oom_memcg=/kubepods/burstable/podd11ab7b0-d6db-4a24-a7de-4a2faf1e6980/9f02d9fa0049eb2655fc83c765f142362b2cb403b57b70ba3185071015ca3b64,task_memcg=/kubepods/burstable/podd11ab7b0-d6db-4a24-a7de-4a2faf1e6980/9f02d9fa0049eb2655fc83c765f142362b2cb403b57b70ba3185071015ca3b64,task=prometheus-conf,pid=3401999,uid=0",
"6,23800,6780904484233,-;oom-kill:constraint=CONSTRAINT_MEMCG,nodemask=(null),cpuset=cri-containerd-2260b35b008a15bd118e629c0c5d74e7f3a1fe18c724fbac61a54862fea196dc.scope,mems_allowed=0,oom_memcg=/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-poddfc377c9_c533_4d51_af9e_6e0e0b3db83b.slice,task_memcg=/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-poddfc377c9_c533_4d51_af9e_6e0e0b3db83b.slice/cri-containerd-2260b35b008a15bd118e629c0c5d74e7f3a1fe18c724fbac61a54862fea196dc.scope,task=stress,pid=255629,uid=0",
}
podUIDs = []string{
"d11ab7b0-d6db-4a24-a7de-4a2faf1e6980",
"dfc377c9_c533_4d51_af9e_6e0e0b3db83b",
}
containerIDs = []string{
"9f02d9fa0049eb2655fc83c765f142362b2cb403b57b70ba3185071015ca3b64",
"2260b35b008a15bd118e629c0c5d74e7f3a1fe18c724fbac61a54862fea196dc",
}
return
}