mirror of
https://github.com/k8sgpt-ai/k8sgpt.git
synced 2025-09-17 15:52:50 +00:00
chore: add more filter releavent UT in analysis_test.go (#435)
Signed-off-by: Peter Pan <Peter.Pan@daocloud.io> Co-authored-by: Alex Jones <alexsimonjones@gmail.com>
This commit is contained in:
@@ -14,14 +14,127 @@ limitations under the License.
|
|||||||
package analysis
|
package analysis
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/k8sgpt-ai/k8sgpt/pkg/common"
|
"github.com/k8sgpt-ai/k8sgpt/pkg/common"
|
||||||
|
"github.com/k8sgpt-ai/k8sgpt/pkg/kubernetes"
|
||||||
|
"github.com/magiconair/properties/assert"
|
||||||
|
"github.com/spf13/viper"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
v1 "k8s.io/api/core/v1"
|
||||||
|
networkingv1 "k8s.io/api/networking/v1"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"k8s.io/client-go/kubernetes/fake"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// sub-function
|
||||||
|
func analysis_RunAnalysisFilterTester(t *testing.T, filterFlag string) []common.Result {
|
||||||
|
clientset := fake.NewSimpleClientset(
|
||||||
|
&v1.Pod{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "example",
|
||||||
|
Namespace: "default",
|
||||||
|
},
|
||||||
|
Status: v1.PodStatus{
|
||||||
|
Phase: v1.PodPending,
|
||||||
|
Conditions: []v1.PodCondition{
|
||||||
|
{
|
||||||
|
Type: v1.PodScheduled,
|
||||||
|
Reason: "Unschedulable",
|
||||||
|
Message: "0/1 nodes are available: 1 node(s) had taint {node-role.kubernetes.io/master: }, that the pod didn't tolerate.",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
&v1.Endpoints{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "example",
|
||||||
|
Namespace: "default",
|
||||||
|
Annotations: map[string]string{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
&v1.Service{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "example",
|
||||||
|
Namespace: "default",
|
||||||
|
Annotations: map[string]string{},
|
||||||
|
},
|
||||||
|
Spec: v1.ServiceSpec{
|
||||||
|
Selector: map[string]string{
|
||||||
|
"app": "example",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
&networkingv1.Ingress{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "example",
|
||||||
|
Namespace: "default",
|
||||||
|
Annotations: map[string]string{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
analysis := Analysis{
|
||||||
|
Context: context.Background(),
|
||||||
|
Results: []common.Result{},
|
||||||
|
Namespace: "default",
|
||||||
|
MaxConcurrency: 1,
|
||||||
|
Client: &kubernetes.Client{
|
||||||
|
Client: clientset,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
if len(filterFlag) > 0 {
|
||||||
|
// `--filter` is explicitly given
|
||||||
|
analysis.Filters = strings.Split(filterFlag, ",")
|
||||||
|
}
|
||||||
|
analysis.RunAnalysis()
|
||||||
|
return analysis.Results
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test: Filter logic with running different Analyzers
|
||||||
|
func TestAnalysis_RunAnalysisWithFilter(t *testing.T) {
|
||||||
|
var results []common.Result
|
||||||
|
var filterFlag string
|
||||||
|
|
||||||
|
//1. Neither --filter flag Nor active filter is specified, only the "core analyzers"
|
||||||
|
results = analysis_RunAnalysisFilterTester(t, "")
|
||||||
|
assert.Equal(t, len(results), 3) // all built-in resource will be analyzed
|
||||||
|
|
||||||
|
//2. When the --filter flag is specified
|
||||||
|
|
||||||
|
filterFlag = "Pod" // --filter=Pod
|
||||||
|
results = analysis_RunAnalysisFilterTester(t, filterFlag)
|
||||||
|
assert.Equal(t, len(results), 1)
|
||||||
|
assert.Equal(t, results[0].Kind, filterFlag)
|
||||||
|
|
||||||
|
filterFlag = "Ingress,Pod" // --filter=Ingress,Pod
|
||||||
|
results = analysis_RunAnalysisFilterTester(t, filterFlag)
|
||||||
|
assert.Equal(t, len(results), 2)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test: Filter logic with Active Filter
|
||||||
|
func TestAnalysis_RunAnalysisActiveFilter(t *testing.T) {
|
||||||
|
|
||||||
|
//When the --filter flag is not specified but has actived filter in config
|
||||||
|
var results []common.Result
|
||||||
|
|
||||||
|
viper.SetDefault("active_filters", "Ingress")
|
||||||
|
results = analysis_RunAnalysisFilterTester(t, "")
|
||||||
|
assert.Equal(t, len(results), 1)
|
||||||
|
|
||||||
|
viper.SetDefault("active_filters", []string{"Ingress", "Service"})
|
||||||
|
results = analysis_RunAnalysisFilterTester(t, "")
|
||||||
|
assert.Equal(t, len(results), 2)
|
||||||
|
|
||||||
|
viper.SetDefault("active_filters", []string{"Ingress", "Service", "Pod"})
|
||||||
|
results = analysis_RunAnalysisFilterTester(t, "")
|
||||||
|
assert.Equal(t, len(results), 3)
|
||||||
|
}
|
||||||
|
|
||||||
func TestAnalysis_NoProblemJsonOutput(t *testing.T) {
|
func TestAnalysis_NoProblemJsonOutput(t *testing.T) {
|
||||||
|
|
||||||
analysis := Analysis{
|
analysis := Analysis{
|
||||||
@@ -50,6 +163,7 @@ func TestAnalysis_NoProblemJsonOutput(t *testing.T) {
|
|||||||
fmt.Println(expected)
|
fmt.Println(expected)
|
||||||
|
|
||||||
require.Equal(t, got, expected)
|
require.Equal(t, got, expected)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAnalysis_ProblemJsonOutput(t *testing.T) {
|
func TestAnalysis_ProblemJsonOutput(t *testing.T) {
|
||||||
|
Reference in New Issue
Block a user