-
Notifications
You must be signed in to change notification settings - Fork 721
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: added initial tests for json output
Signed-off-by: Thomas Schuetz <[email protected]>
- Loading branch information
Showing
3 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package analysis | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/k8sgpt-ai/k8sgpt/pkg/analyzer" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestAnalysis_NoProblemJsonOutput(t *testing.T) { | ||
|
||
analysis := Analysis{ | ||
Results: []analyzer.Result{}, | ||
Namespace: "default", | ||
} | ||
|
||
expected := JsonOutput{ | ||
Status: StateOK, | ||
Problems: 0, | ||
Results: []analyzer.Result{}, | ||
} | ||
|
||
gotJson, err := analysis.JsonOutput() | ||
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: []analyzer.Result{ | ||
{ | ||
"Deployment", | ||
"test-deployment", | ||
[]string{"test-problem"}, | ||
"test-solution", | ||
"parent-resource"}, | ||
}, | ||
Namespace: "default", | ||
} | ||
|
||
expected := JsonOutput{ | ||
Status: StateProblemDetected, | ||
Problems: 1, | ||
Results: []analyzer.Result{ | ||
{"Deployment", | ||
"test-deployment", | ||
[]string{"test-problem"}, | ||
"test-solution", | ||
"parent-resource"}, | ||
}, | ||
} | ||
|
||
gotJson, err := analysis.JsonOutput() | ||
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: []analyzer.Result{ | ||
{ | ||
"Deployment", | ||
"test-deployment", | ||
[]string{"test-problem", "another-test-problem"}, | ||
"test-solution", | ||
"parent-resource"}, | ||
}, | ||
Namespace: "default", | ||
} | ||
|
||
expected := JsonOutput{ | ||
Status: StateProblemDetected, | ||
Problems: 2, | ||
Results: []analyzer.Result{ | ||
{"Deployment", | ||
"test-deployment", | ||
[]string{"test-problem", "another-test-problem"}, | ||
"test-solution", | ||
"parent-resource"}, | ||
}, | ||
} | ||
|
||
gotJson, err := analysis.JsonOutput() | ||
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) | ||
} |