mirror of
https://github.com/k8sgpt-ai/k8sgpt.git
synced 2025-08-18 15:57:04 +00:00
* chore: change license to Apache-2 Signed-off-by: Thomas Schuetz <thomas.schuetz@t-sc.eu>
168 lines
3.3 KiB
Go
168 lines
3.3 KiB
Go
/*
|
|
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 analysis
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/k8sgpt-ai/k8sgpt/pkg/common"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestAnalysis_NoProblemJsonOutput(t *testing.T) {
|
|
|
|
analysis := Analysis{
|
|
Results: []common.Result{},
|
|
Namespace: "default",
|
|
}
|
|
|
|
expected := JsonOutput{
|
|
Status: StateOK,
|
|
Problems: 0,
|
|
Results: []common.Result{},
|
|
}
|
|
|
|
gotJson, err := analysis.PrintOutput("json")
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
got := JsonOutput{}
|
|
err = json.Unmarshal(gotJson, &got)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
fmt.Println(got)
|
|
fmt.Println(expected)
|
|
|
|
require.Equal(t, got, expected)
|
|
}
|
|
|
|
func TestAnalysis_ProblemJsonOutput(t *testing.T) {
|
|
analysis := Analysis{
|
|
Results: []common.Result{
|
|
{
|
|
Kind: "Deployment",
|
|
Name: "test-deployment",
|
|
Error: []common.Failure{
|
|
{
|
|
Text: "test-problem",
|
|
Sensitive: []common.Sensitive{},
|
|
},
|
|
},
|
|
Details: "test-solution",
|
|
ParentObject: "parent-resource"},
|
|
},
|
|
Namespace: "default",
|
|
}
|
|
|
|
expected := JsonOutput{
|
|
Status: StateProblemDetected,
|
|
Problems: 1,
|
|
Results: []common.Result{
|
|
{
|
|
Kind: "Deployment",
|
|
Name: "test-deployment",
|
|
Error: []common.Failure{
|
|
{
|
|
Text: "test-problem",
|
|
Sensitive: []common.Sensitive{},
|
|
},
|
|
},
|
|
Details: "test-solution",
|
|
ParentObject: "parent-resource"},
|
|
},
|
|
}
|
|
|
|
gotJson, err := analysis.PrintOutput("json")
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
got := JsonOutput{}
|
|
err = json.Unmarshal(gotJson, &got)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
fmt.Println(got)
|
|
fmt.Println(expected)
|
|
|
|
require.Equal(t, got, expected)
|
|
}
|
|
|
|
func TestAnalysis_MultipleProblemJsonOutput(t *testing.T) {
|
|
analysis := Analysis{
|
|
Results: []common.Result{
|
|
{
|
|
Kind: "Deployment",
|
|
Name: "test-deployment",
|
|
Error: []common.Failure{
|
|
{
|
|
Text: "test-problem",
|
|
Sensitive: []common.Sensitive{},
|
|
},
|
|
{
|
|
Text: "another-test-problem",
|
|
Sensitive: []common.Sensitive{},
|
|
},
|
|
},
|
|
Details: "test-solution",
|
|
ParentObject: "parent-resource"},
|
|
},
|
|
Namespace: "default",
|
|
}
|
|
|
|
expected := JsonOutput{
|
|
Status: StateProblemDetected,
|
|
Problems: 2,
|
|
Results: []common.Result{
|
|
{
|
|
Kind: "Deployment",
|
|
Name: "test-deployment",
|
|
Error: []common.Failure{
|
|
{
|
|
Text: "test-problem",
|
|
Sensitive: []common.Sensitive{},
|
|
},
|
|
{
|
|
Text: "another-test-problem",
|
|
Sensitive: []common.Sensitive{},
|
|
},
|
|
},
|
|
Details: "test-solution",
|
|
ParentObject: "parent-resource"},
|
|
},
|
|
}
|
|
|
|
gotJson, err := analysis.PrintOutput("json")
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
got := JsonOutput{}
|
|
err = json.Unmarshal(gotJson, &got)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
fmt.Println(got)
|
|
fmt.Println(expected)
|
|
|
|
require.Equal(t, got, expected)
|
|
}
|