Skip to content

Commit

Permalink
test: added missing tests for pkg/analysis package (#908)
Browse files Browse the repository at this point in the history
This commit bumps the code coverage of the `pkg/analysis` package to
60.8%

Signed-off-by: VaibhavMalik4187 <vaibhavmalik2018@gmail.com>
Co-authored-by: Alex Jones <alexsimonjones@gmail.com>
Co-authored-by: JuHyung Son <sonju0427@gmail.com>
  • Loading branch information
3 people committed May 5, 2024
1 parent e74fc08 commit c3a884f
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 2 deletions.
129 changes: 127 additions & 2 deletions pkg/analysis/analysis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ import (
"context"
"encoding/json"
"fmt"
"strings"
"testing"

"github.com/k8sgpt-ai/k8sgpt/pkg/ai"
"github.com/k8sgpt-ai/k8sgpt/pkg/cache"
"github.com/k8sgpt-ai/k8sgpt/pkg/common"
"github.com/k8sgpt-ai/k8sgpt/pkg/kubernetes"
"github.com/magiconair/properties/assert"
Expand All @@ -26,8 +31,6 @@ import (
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"
"strings"
"testing"
)

// sub-function
Expand Down Expand Up @@ -85,6 +88,7 @@ func analysis_RunAnalysisFilterTester(t *testing.T, filterFlag string) []common.
Client: &kubernetes.Client{
Client: clientset,
},
WithDoc: true,
}
if len(filterFlag) > 0 {
// `--filter` is explicitly given
Expand Down Expand Up @@ -133,6 +137,10 @@ func TestAnalysis_RunAnalysisActiveFilter(t *testing.T) {
viper.SetDefault("active_filters", []string{"Ingress", "Service", "Pod"})
results = analysis_RunAnalysisFilterTester(t, "")
assert.Equal(t, len(results), 3)

// Invalid filter
results = analysis_RunAnalysisFilterTester(t, "invalid")
assert.Equal(t, len(results), 0)
}

func TestAnalysis_NoProblemJsonOutput(t *testing.T) {
Expand Down Expand Up @@ -279,3 +287,120 @@ func TestAnalysis_MultipleProblemJsonOutput(t *testing.T) {

require.Equal(t, got, expected)
}

func TestNewAnalysis(t *testing.T) {
disabledCache := cache.New("disabled-cache")
disabledCache.DisableCache()
aiClient := &ai.NoOpAIClient{}
results := []common.Result{
{
Kind: "VulnerabilityReport",
Error: []common.Failure{
{
Text: "This is a custom failure",
KubernetesDoc: "test-kubernetes-doc",
Sensitive: []common.Sensitive{
{
Masked: "masked-error",
Unmasked: "unmasked-error",
},
},
},
},
},
}

tests := []struct {
name string
a Analysis
output string
anonymize bool
expectedErr string
}{
{
name: "Empty results",
a: Analysis{},
},
{
name: "cache disabled",
a: Analysis{
AIClient: aiClient,
Cache: disabledCache,
Results: results,
},
},
{
name: "output and anonymize both set",
a: Analysis{
AIClient: aiClient,
Cache: cache.New("test-cache"),
Results: results,
},
output: "test-output",
anonymize: true,
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
err := tt.a.GetAIResults(tt.output, tt.anonymize)
if tt.expectedErr == "" {
require.NoError(t, err)
} else {
require.ErrorContains(t, err, tt.expectedErr)
}
})
}
}

func TestGetAIResultForSanitizedFailures(t *testing.T) {
enabledCache := cache.New("enabled-cache")
disabledCache := cache.New("disabled-cache")
disabledCache.DisableCache()
aiClient := &ai.NoOpAIClient{}

tests := []struct {
name string
a Analysis
texts []string
promptTmpl string
expectedOutput string
expectedErr string
}{
{
name: "Cache enabled",
a: Analysis{
AIClient: aiClient,
Cache: enabledCache,
},
texts: []string{"some-data"},
expectedOutput: "I am a noop response to the prompt %!(EXTRA string=, string=some-data)",
},
{
name: "cache disabled",
a: Analysis{
AIClient: aiClient,
Cache: disabledCache,
Language: "English",
},
texts: []string{"test input"},
promptTmpl: "Response in %s: %s",
expectedOutput: "I am a noop response to the prompt Response in English: test input",
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
output, err := tt.a.getAIResultForSanitizedFailures(tt.texts, tt.promptTmpl)
if tt.expectedErr == "" {
require.NoError(t, err)
require.Equal(t, tt.expectedOutput, output)
} else {
require.ErrorContains(t, err, tt.expectedErr)
require.Empty(t, output)
}
})
}
}
64 changes: 64 additions & 0 deletions pkg/analysis/output_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Copyright 2024 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 analysis

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestPrintOutput(t *testing.T) {
require.NotEmpty(t, getOutputFormats())

tests := []struct {
name string
a *Analysis
format string
expectedOutput string
expectedErr string
}{
{
name: "json format",
a: &Analysis{},
format: "json",
expectedOutput: "{\n \"provider\": \"\",\n \"errors\": null,\n \"status\": \"OK\",\n \"problems\": 0,\n \"results\": null\n}",
},
{
name: "text format",
a: &Analysis{},
format: "text",
expectedOutput: "AI Provider: AI not used; --explain not set\n\nNo problems detected\n",
},
{
name: "unsupported format",
a: &Analysis{},
format: "unsupported",
expectedErr: "unsupported output format: unsupported. Available format",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
output, err := tt.a.PrintOutput(tt.format)
if tt.expectedErr == "" {
require.NoError(t, err)
require.Contains(t, string(output), tt.expectedOutput)
} else {
require.ErrorContains(t, err, tt.expectedErr)
require.Nil(t, output)
}
})
}
}

0 comments on commit c3a884f

Please sign in to comment.