Skip to content

Commit f044446

Browse files
author
jmccormick2001
committed
refactor text output and list flag logic
1 parent 21142ea commit f044446

File tree

8 files changed

+122
-62
lines changed

8 files changed

+122
-62
lines changed

hack/tests/subcommand-scorecard.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ operator-sdk scorecard --version v1alpha2 --config "$CONFIG_PATH" |& grep '^.*er
2020

2121
# test to see if v1alpha2 is used from the command line
2222
commandoutput="$(operator-sdk scorecard --version v1alpha2 --config "$CONFIG_PATH_V1ALPHA2" 2>&1)"
23-
failCount=`echo $commandoutput | grep -o ": fail" | wc -l`
23+
failCount=`echo $commandoutput | grep -o "fail" | wc -l`
2424
expectedFailCount=3
2525
if [ $failCount -ne $expectedFailCount ]
2626
then

internal/scorecard/helpers/test_definitions.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,6 @@ func (ts *TestSuite) ApplySelector(selector labels.Selector) {
116116
}
117117
}
118118

119-
// List prints out tests and removes the test from being executed
120-
func (ts *TestSuite) List() {
121-
for i := 0; i < len(ts.Tests); i++ {
122-
fmt.Printf("%s\n", ts.Tests[i].GetName())
123-
}
124-
}
125-
126119
// Run runs all Tests in a TestSuite
127120
func (ts *TestSuite) Run(ctx context.Context) {
128121
for _, test := range ts.Tests {

internal/scorecard/interfaces.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
)
3131

3232
type Plugin interface {
33+
List() scapiv1alpha1.ScorecardOutput
3334
Run() scapiv1alpha1.ScorecardOutput
3435
}
3536

@@ -42,6 +43,10 @@ type basicOrOLMPlugin struct {
4243
config scplugins.BasicAndOLMPluginConfig
4344
}
4445

46+
func (p externalPlugin) List() scapiv1alpha1.ScorecardOutput {
47+
return scapiv1alpha1.ScorecardOutput{}
48+
}
49+
4550
func (p externalPlugin) Run() scapiv1alpha1.ScorecardOutput {
4651
cmd := exec.Command(p.config.Command, p.config.Args...)
4752
for _, env := range p.config.Env {
@@ -84,6 +89,15 @@ var olmTestsPlugin = basicOrOLMPlugin{
8489
pluginType: scplugins.OLMIntegration,
8590
}
8691

92+
func (p basicOrOLMPlugin) List() scapiv1alpha1.ScorecardOutput {
93+
res, err := scplugins.ListInternalPlugin(p.pluginType, p.config)
94+
if err != nil {
95+
log.Errorf("%v", err)
96+
return scapiv1alpha1.ScorecardOutput{}
97+
}
98+
return res
99+
}
100+
87101
func (p basicOrOLMPlugin) Run() scapiv1alpha1.ScorecardOutput {
88102
pluginLogs := &bytes.Buffer{}
89103
res, err := scplugins.RunInternalPlugin(p.pluginType, p.config, pluginLogs)

internal/scorecard/plugins/plugin_runner.go

Lines changed: 71 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -269,26 +269,24 @@ func RunInternalPlugin(pluginType PluginType, config BasicAndOLMPluginConfig, lo
269269
log.SetOutput(logReadWriter)
270270
log.Printf("Running for cr: %s", cr)
271271
var obj *unstructured.Unstructured
272-
if !config.ListOpt {
273-
if !config.OLMDeployed {
274-
if err := createFromYAMLFile(config.Namespace, config.GlobalManifest, config.ProxyImage, config.ProxyPullPolicy); err != nil {
275-
return scapiv1alpha1.ScorecardOutput{}, fmt.Errorf("failed to create global resources: %v", err)
276-
}
277-
if err := createFromYAMLFile(config.Namespace, config.NamespacedManifest, config.ProxyImage, config.ProxyPullPolicy); err != nil {
278-
return scapiv1alpha1.ScorecardOutput{}, fmt.Errorf("failed to create namespaced resources: %v", err)
279-
}
280-
}
281-
if err := createFromYAMLFile(config.Namespace, cr, config.ProxyImage, config.ProxyPullPolicy); err != nil {
282-
return scapiv1alpha1.ScorecardOutput{}, fmt.Errorf("failed to create cr resource: %v", err)
283-
}
284-
obj, err = yamlToUnstructured(config.Namespace, cr)
285-
if err != nil {
286-
return scapiv1alpha1.ScorecardOutput{}, fmt.Errorf("failed to decode custom resource manifest into object: %s", err)
272+
if !config.OLMDeployed {
273+
if err := createFromYAMLFile(config.Namespace, config.GlobalManifest, config.ProxyImage, config.ProxyPullPolicy); err != nil {
274+
return scapiv1alpha1.ScorecardOutput{}, fmt.Errorf("failed to create global resources: %v", err)
287275
}
288-
if err := waitUntilCRStatusExists(time.Second*time.Duration(config.InitTimeout), obj); err != nil {
289-
return scapiv1alpha1.ScorecardOutput{}, fmt.Errorf("failed waiting to check if CR status exists: %v", err)
276+
if err := createFromYAMLFile(config.Namespace, config.NamespacedManifest, config.ProxyImage, config.ProxyPullPolicy); err != nil {
277+
return scapiv1alpha1.ScorecardOutput{}, fmt.Errorf("failed to create namespaced resources: %v", err)
290278
}
291279
}
280+
if err := createFromYAMLFile(config.Namespace, cr, config.ProxyImage, config.ProxyPullPolicy); err != nil {
281+
return scapiv1alpha1.ScorecardOutput{}, fmt.Errorf("failed to create cr resource: %v", err)
282+
}
283+
obj, err = yamlToUnstructured(config.Namespace, cr)
284+
if err != nil {
285+
return scapiv1alpha1.ScorecardOutput{}, fmt.Errorf("failed to decode custom resource manifest into object: %s", err)
286+
}
287+
if err := waitUntilCRStatusExists(time.Second*time.Duration(config.InitTimeout), obj); err != nil {
288+
return scapiv1alpha1.ScorecardOutput{}, fmt.Errorf("failed waiting to check if CR status exists: %v", err)
289+
}
292290
if pluginType == BasicOperator {
293291
conf := BasicTestConfig{
294292
Client: runtimeClient,
@@ -299,18 +297,14 @@ func RunInternalPlugin(pluginType PluginType, config BasicAndOLMPluginConfig, lo
299297
if schelpers.IsV1alpha2(config.Version) {
300298
basicTests.ApplySelector(config.Selector)
301299
}
302-
if config.ListOpt {
303-
basicTests.List()
300+
basicTests.Run(context.TODO())
301+
logs, err := ioutil.ReadAll(logReadWriter)
302+
if err != nil {
303+
basicTests.Log = fmt.Sprintf("failed to read log buffer: %v", err)
304304
} else {
305-
basicTests.Run(context.TODO())
306-
logs, err := ioutil.ReadAll(logReadWriter)
307-
if err != nil {
308-
basicTests.Log = fmt.Sprintf("failed to read log buffer: %v", err)
309-
} else {
310-
basicTests.Log = string(logs)
311-
}
312-
suites = append(suites, *basicTests)
305+
basicTests.Log = string(logs)
313306
}
307+
suites = append(suites, *basicTests)
314308
}
315309
if pluginType == OLMIntegration {
316310
conf := OLMTestConfig{
@@ -324,18 +318,14 @@ func RunInternalPlugin(pluginType PluginType, config BasicAndOLMPluginConfig, lo
324318
if schelpers.IsV1alpha2(config.Version) {
325319
olmTests.ApplySelector(config.Selector)
326320
}
327-
if config.ListOpt {
328-
olmTests.List()
321+
olmTests.Run(context.TODO())
322+
logs, err := ioutil.ReadAll(logReadWriter)
323+
if err != nil {
324+
olmTests.Log = fmt.Sprintf("failed to read log buffer: %v", err)
329325
} else {
330-
olmTests.Run(context.TODO())
331-
logs, err := ioutil.ReadAll(logReadWriter)
332-
if err != nil {
333-
olmTests.Log = fmt.Sprintf("failed to read log buffer: %v", err)
334-
} else {
335-
olmTests.Log = string(logs)
336-
}
337-
suites = append(suites, *olmTests)
326+
olmTests.Log = string(logs)
338327
}
328+
suites = append(suites, *olmTests)
339329
}
340330
// change logging back to main log
341331
log.SetOutput(logFile)
@@ -358,3 +348,47 @@ func RunInternalPlugin(pluginType PluginType, config BasicAndOLMPluginConfig, lo
358348
}
359349
return output, nil
360350
}
351+
352+
func ListInternalPlugin(pluginType PluginType, config BasicAndOLMPluginConfig) (scapiv1alpha1.ScorecardOutput, error) {
353+
var suites []schelpers.TestSuite
354+
355+
if pluginType == BasicOperator {
356+
conf := BasicTestConfig{}
357+
basicTests := NewBasicTestSuite(conf)
358+
if schelpers.IsV1alpha2(config.Version) {
359+
basicTests.ApplySelector(config.Selector)
360+
}
361+
362+
basicTests.TestResults = make([]schelpers.TestResult, 0)
363+
for i := 0; i < len(basicTests.Tests); i++ {
364+
result := schelpers.TestResult{}
365+
result.Test = basicTests.Tests[i]
366+
result.Suggestions = make([]string, 0)
367+
result.Errors = make([]error, 0)
368+
basicTests.TestResults = append(basicTests.TestResults, result)
369+
}
370+
suites = append(suites, *basicTests)
371+
}
372+
if pluginType == OLMIntegration {
373+
conf := OLMTestConfig{}
374+
olmTests := NewOLMTestSuite(conf)
375+
if schelpers.IsV1alpha2(config.Version) {
376+
olmTests.ApplySelector(config.Selector)
377+
}
378+
olmTests.TestResults = make([]schelpers.TestResult, 0)
379+
for i := 0; i < len(olmTests.Tests); i++ {
380+
result := schelpers.TestResult{}
381+
result.Test = olmTests.Tests[i]
382+
result.Suggestions = make([]string, 0)
383+
result.Errors = make([]error, 0)
384+
olmTests.TestResults = append(olmTests.TestResults, result)
385+
}
386+
suites = append(suites, *olmTests)
387+
}
388+
suites, err := schelpers.MergeSuites(suites)
389+
if err != nil {
390+
return scapiv1alpha1.ScorecardOutput{}, fmt.Errorf("failed to merge test suite results: %v", err)
391+
}
392+
output := schelpers.TestSuitesToScorecardOutput(suites, "")
393+
return output, nil
394+
}

internal/scorecard/scorecard.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,6 @@ func ScorecardTests(cmd *cobra.Command, args []string) error {
118118
return err
119119
}
120120

121-
if scViper.GetBool(ListOpt) {
122-
fmt.Printf("\nTests that would be executed include:\n")
123-
}
124-
125121
cmd.SilenceUsage = true
126122
plugins, err := getPlugins(scViper.GetString(schelpers.VersionOpt), selector)
127123
if err != nil {
@@ -130,7 +126,11 @@ func ScorecardTests(cmd *cobra.Command, args []string) error {
130126

131127
var pluginOutputs []scapiv1alpha1.ScorecardOutput
132128
for _, plugin := range plugins {
133-
pluginOutputs = append(pluginOutputs, plugin.Run())
129+
if scViper.GetBool(ListOpt) {
130+
pluginOutputs = append(pluginOutputs, plugin.List())
131+
} else {
132+
pluginOutputs = append(pluginOutputs, plugin.Run())
133+
}
134134
}
135135

136136
// Update the state for the tests

internal/scorecard/scorecard_output.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
schelpers "github.com/operator-framework/operator-sdk/internal/scorecard/helpers"
2121
scapi "github.com/operator-framework/operator-sdk/pkg/apis/scorecard"
2222
scapiv1alpha1 "github.com/operator-framework/operator-sdk/pkg/apis/scorecard/v1alpha1"
23+
scapiv1alpha2 "github.com/operator-framework/operator-sdk/pkg/apis/scorecard/v1alpha2"
2324
"io/ioutil"
2425
)
2526

@@ -34,6 +35,12 @@ func printPluginOutputs(version string, pluginOutputs []scapiv1alpha1.ScorecardO
3435

3536
if schelpers.IsV1alpha2(version) {
3637
list = scapi.ConvertScorecardOutputV1ToV2(list.(scapiv1alpha1.ScorecardOutput))
38+
if scViper.GetBool(ListOpt) {
39+
scorecardOutput := list.(scapiv1alpha2.ScorecardOutput)
40+
for i := 0; i < len(scorecardOutput.Results); i++ {
41+
scorecardOutput.Results[i].State = scapiv1alpha2.NotRunState
42+
}
43+
}
3744
}
3845

3946
// produce text output

pkg/apis/scorecard/v1alpha2/formatter.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ const (
3030
func (s ScorecardOutput) MarshalText() (string, error) {
3131
var sb strings.Builder
3232

33-
failColor := "\033[1;" + redColor + "m%s\033[0m\n"
34-
passColor := "\033[1;" + greenColor + "m%s\033[0m\n"
33+
failColor := ": \033[1;" + redColor + "m%s\033[0m\n"
34+
passColor := ": \033[1;" + greenColor + "m%s\033[0m\n"
3535

3636
// turn off colorization if not in a terminal
3737
if !isatty.IsTerminal(os.Stdout.Fd()) &&
@@ -46,27 +46,37 @@ func (s ScorecardOutput) MarshalText() (string, error) {
4646
sb.WriteString(fmt.Sprintf("%s:\n", result.Labels["suite"]))
4747
currentSuite = result.Labels["suite"]
4848
}
49-
sb.WriteString(fmt.Sprintf("\t%-35s: ", result.Name))
49+
sb.WriteString(fmt.Sprintf("\t%-35s ", result.Name))
5050

5151
if result.State == PassState {
5252
sb.WriteString(fmt.Sprintf(passColor, PassState))
53-
} else {
53+
} else if result.State == FailState {
5454
sb.WriteString(fmt.Sprintf(failColor, FailState))
55+
} else {
56+
sb.WriteString(fmt.Sprintf("\n"))
5557
}
56-
}
5758

58-
for _, result := range s.Results {
59-
for _, suggestion := range result.Suggestions {
59+
sb.WriteString("\tLabels: \n")
60+
for labelKey, labelValue := range result.Labels {
61+
sb.WriteString(fmt.Sprintf("\t\t%s:%s\n", labelKey, labelValue))
62+
}
63+
64+
if len(result.Suggestions) > 0 {
6065
// 33 is yellow (specifically, the same shade of yellow that logrus uses for warnings)
61-
sb.WriteString(fmt.Sprintf("\x1b[%dmSUGGESTION:\x1b[0m %s\n", 33, suggestion))
66+
sb.WriteString(fmt.Sprintf("\t\x1b[%dmSuggestions:\x1b[0m \n", 33))
67+
}
68+
for _, suggestion := range result.Suggestions {
69+
sb.WriteString(fmt.Sprintf("\t\t%s\n", suggestion))
6270
}
63-
}
6471

65-
for _, result := range s.Results {
66-
for _, err := range result.Errors {
72+
if len(result.Errors) > 0 {
6773
// 31 is red (specifically, the same shade of red that logrus uses for errors)
68-
sb.WriteString(fmt.Sprintf("\x1b[%dmERROR:\x1b[0m %s\n", 31, err))
74+
sb.WriteString(fmt.Sprintf("\t\x1b[%dmErrors:\x1b[0m \n", 31))
75+
}
76+
for _, err := range result.Errors {
77+
sb.WriteString(fmt.Sprintf("\t\t%s\n", err))
6978
}
79+
sb.WriteString(fmt.Sprintf("\n"))
7080
}
7181

7282
return sb.String(), nil

pkg/apis/scorecard/v1alpha2/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
type State string
2323

2424
const (
25+
// NotRun occurs when a user specifies the --list flag
26+
NotRunState State = "not run"
2527
// PassState occurs when a Test's ExpectedPoints == MaximumPoints.
2628
PassState State = "pass"
2729
// FailState occurs when a Test's ExpectedPoints == 0.

0 commit comments

Comments
 (0)