1
0
mirror of https://github.com/k8sgpt-ai/k8sgpt.git synced 2025-05-12 01:59:45 +00:00

test: added tests for the log analyzer ()

* Added new tests for `LogAnalyzer` defined in the `pkg/analyzer`
  package. Increased the code coverage of the `log.go` file to >90%

Partially addresses: https://github.com/k8sgpt-ai/k8sgpt/issues/889

Signed-off-by: VaibhavMalik4187 <vaibhavmalik2018@gmail.com>
This commit is contained in:
Vaibhav Malik 2024-05-09 11:48:22 +05:30 committed by GitHub
parent 3c4823127c
commit 2c7c74472c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 122 additions and 2 deletions
pkg/analyzer

View File

@ -71,7 +71,7 @@ func (LogAnalyzer) Analyze(a common.Analyzer) ([]common.Result, error) {
rawlogs := string(podLogs)
if errorPattern.MatchString(strings.ToLower(rawlogs)) {
failures = append(failures, common.Failure{
Text: printErrorLines(pod.Name, pod.Namespace, rawlogs, errorPattern),
Text: printErrorLines(rawlogs, errorPattern),
Sensitive: []common.Sensitive{
{
Unmasked: pod.Name,
@ -105,7 +105,7 @@ func (LogAnalyzer) Analyze(a common.Analyzer) ([]common.Result, error) {
return a.Results, nil
}
func printErrorLines(podName, namespace, logs string, errorPattern *regexp.Regexp) string {
func printErrorLines(logs string, errorPattern *regexp.Regexp) string {
// Split the logs into lines
logLines := strings.Split(logs, "\n")

120
pkg/analyzer/log_test.go Normal file
View File

@ -0,0 +1,120 @@
/*
Copyright 2023 The K8sGPT Authors.
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 analyzer
import (
"context"
"regexp"
"sort"
"testing"
"github.com/k8sgpt-ai/k8sgpt/pkg/common"
"github.com/k8sgpt-ai/k8sgpt/pkg/kubernetes"
"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"
)
func TestLogAnalyzer(t *testing.T) {
oldPattern := errorPattern
errorPattern = regexp.MustCompile(`(fake logs)`)
t.Cleanup(func() {
errorPattern = oldPattern
})
config := common.Analyzer{
Client: &kubernetes.Client{
Client: fake.NewSimpleClientset(
&v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "Pod1",
Namespace: "default",
Labels: map[string]string{
"Name": "Pod1",
"Namespace": "default",
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "test-container1",
},
{
Name: "test-container2",
},
},
},
},
&v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "Pod2",
Namespace: "default",
Labels: map[string]string{
"Name": "Pod1",
"Namespace": "default",
},
},
},
&v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "Pod3",
Namespace: "test-namespace",
Labels: map[string]string{
"Name": "Pod1",
"Namespace": "test-namespace",
},
},
},
&v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "Pod4",
Namespace: "default",
Labels: map[string]string{
"Name": "Pod4",
"Namespace": "default",
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "test-container3",
},
},
},
},
),
},
Context: context.Background(),
Namespace: "default",
}
logAnalyzer := LogAnalyzer{}
results, err := logAnalyzer.Analyze(config)
require.NoError(t, err)
sort.Slice(results, func(i, j int) bool {
return results[i].Name < results[j].Name
})
expectations := []string{"default/Pod1/test-container1", "default/Pod1/test-container2", "default/Pod4/test-container3"}
for i, expectation := range expectations {
require.Equal(t, expectation, results[i].Name)
for _, failure := range results[i].Error {
require.Equal(t, "fake logs", failure.Text)
}
}
}