@@ -2,6 +2,7 @@ package main
22
33import (
44 "bytes"
5+ "context"
56 "encoding/base64"
67 "encoding/json"
78 "errors"
@@ -13,6 +14,7 @@ import (
1314
1415 "github.com/doitintl/kube-no-trouble/pkg/collector"
1516 "github.com/doitintl/kube-no-trouble/pkg/config"
17+ ctxKey "github.com/doitintl/kube-no-trouble/pkg/context"
1618 "github.com/doitintl/kube-no-trouble/pkg/judge"
1719
1820 "github.com/rs/zerolog"
@@ -108,6 +110,7 @@ func TestMainExitCodes(t *testing.T) {
108110 defer os .RemoveAll (tmpDir )
109111
110112 expectedJsonOutput , _ := os .ReadFile (filepath .Join (FIXTURES_DIR , "expected-json-output.json" ))
113+ expectedJsonOutputLabels , _ := os .ReadFile (filepath .Join (FIXTURES_DIR , "expected-json-output-labels.json" ))
111114 helm3FlagDisabled := "--helm3=false"
112115 clusterFlagDisabled := "--cluster=false"
113116 testCases := []struct {
@@ -121,6 +124,7 @@ func TestMainExitCodes(t *testing.T) {
121124 {"success" , []string {clusterFlagDisabled , helm3FlagDisabled }, 0 , "" , "" , false },
122125 {"errorBadFlag" , []string {"-c=not-boolean" }, 2 , "" , "" , false },
123126 {"successFound" , []string {"-o=json" , clusterFlagDisabled , helm3FlagDisabled , "-f=" + filepath .Join (FIXTURES_DIR , "deployment-v1beta1.yaml" )}, 0 , string (expectedJsonOutput ), "" , false },
127+ {"successFoundWithLabels" , []string {"--labels=true" , "-o=json" , clusterFlagDisabled , helm3FlagDisabled , "-f=" + filepath .Join (FIXTURES_DIR , "deployment-v1beta1-labels.yaml" )}, 0 , string (expectedJsonOutputLabels ), "" , false },
124128 {"exitErrorFlagNone" , []string {clusterFlagDisabled , helm3FlagDisabled , "-e" }, 0 , "" , "" , false },
125129 {"exitErrorFlagFound" , []string {clusterFlagDisabled , helm3FlagDisabled , "-e" , "-f=" + filepath .Join (FIXTURES_DIR , "deployment-v1beta1.yaml" )}, 200 , "" , "" , false },
126130 {"version short flag set" , []string {"-v" }, 0 , "" , "" , false },
@@ -131,6 +135,7 @@ func TestMainExitCodes(t *testing.T) {
131135 {"json-file" , []string {"-o=json" , clusterFlagDisabled , helm3FlagDisabled , "-f=" + filepath .Join (FIXTURES_DIR , "deployment-v1beta1.yaml" )}, 0 , "" , filepath .Join (tmpDir , "json-file.out" ), false },
132136 {"text-file" , []string {"-o=text" , clusterFlagDisabled , helm3FlagDisabled , "-f=" + filepath .Join (FIXTURES_DIR , "deployment-v1beta1.yaml" )}, 0 , "" , filepath .Join (tmpDir , "text-file.out" ), false },
133137 {"json-stdout" , []string {"-o=json" , clusterFlagDisabled , helm3FlagDisabled , "-f=" + filepath .Join (FIXTURES_DIR , "deployment-v1beta1.yaml" )}, 0 , string (expectedJsonOutput ), "-" , false },
138+ {"json-stdout-with-labels" , []string {"--labels=true" , "-o=json" , clusterFlagDisabled , helm3FlagDisabled , "-f=" + filepath .Join (FIXTURES_DIR , "deployment-v1beta1-labels.yaml" )}, 0 , string (expectedJsonOutputLabels ), "-" , false },
134139 {"error-bad-file" , []string {clusterFlagDisabled , helm3FlagDisabled }, 1 , "" , "/this/dir/is/unlikely/to/exist" , false },
135140 {"no-3rdparty-output" , []string {clusterFlagDisabled , helm3FlagDisabled , "-l=disabled" }, 0 , "" , "" , true },
136141 }
@@ -286,9 +291,12 @@ func Test_outputResults(t *testing.T) {
286291 {"bad-new-printer-file" , args {testResults , "text" , "/unlikely/to/exist/dir" }, true },
287292 }
288293
294+ labelsFlag := false
295+ ctx := context .WithValue (context .Background (), ctxKey .LABELS_CTX_KEY , & labelsFlag )
296+
289297 for _ , tt := range tests {
290298 t .Run (tt .name , func (t * testing.T ) {
291- if err := outputResults (tt .args .results , tt .args .outputType , tt .args .outputFile ); (err != nil ) != tt .wantErr {
299+ if err := outputResults (tt .args .results , tt .args .outputType , tt .args .outputFile , ctx ); (err != nil ) != tt .wantErr {
292300 t .Errorf ("unexpected error - got: %v, wantErr: %v" , err , tt .wantErr )
293301 }
294302 })
0 commit comments