Skip to content

Commit

Permalink
Refactor printing analysis summary
Browse files Browse the repository at this point in the history
- Print a message in case no issues were found
- Test using Go values, no decoding from JSON
  • Loading branch information
rhcarvalho committed Sep 16, 2016
1 parent 146b6e4 commit 657726d
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 129 deletions.
27 changes: 27 additions & 0 deletions analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"path/filepath"
"strings"

"github.com/feedhenry/fh-system-dump-tool/openshift/api/types"
)
Expand Down Expand Up @@ -177,3 +179,28 @@ func CheckDeploymentConfigs(deploymentConfigs types.DeploymentConfigList) CheckR
}
return result
}

// PrintAnalysisReport writes a summary of problems found in analysisResult to
// out.
func PrintAnalysisReport(analysisResult AnalysisResult, out io.Writer) {
ok := true
// TODO: handle analysisResult.Platform.
for _, projectResult := range analysisResult.Projects {
for _, checkResult := range projectResult.Results {
if !checkResult.Ok {
ok = false
fmt.Fprintf(out, "Potential issue in project %s: %s\n", projectResult.Project, checkResult.CheckName)
fmt.Fprintf(out, " Details:\n")
for _, info := range checkResult.Info {
fmt.Fprintf(out, " %s\n", strings.Replace(strings.TrimSpace(info.Message), "\n", "\n ", -1))
}
for _, event := range checkResult.Events {
fmt.Fprintf(out, " %s\n", strings.Replace(strings.TrimSpace(event.Message), "\n", "\n ", -1))
}
}
}
}
if ok {
fmt.Fprintln(out, "No issues found")
}
}
101 changes: 101 additions & 0 deletions analysis_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"strings"
"testing"

"github.com/feedhenry/fh-system-dump-tool/openshift/api/types"
Expand Down Expand Up @@ -304,3 +306,102 @@ func (l fakeDefinitionLoader) Err() error {
}

var _ DefinitionLoader = (*fakeDefinitionLoader)(nil)

func TestPrintAnalysisReport(t *testing.T) {
tests := []struct {
description string
analysisResult AnalysisResult
// set want for exact matches or contains/notContains for
// inexact matches.
want string
contains []string
notContains []string
}{
{
description: "empty analysis",
analysisResult: AnalysisResult{},
want: "No issues found\n",
},
{
description: "no errors",
analysisResult: AnalysisResult{
Projects: []ProjectResult{
{
Project: "dev",
Results: []CheckResult{
{
CheckName: "check event log for errors",
Ok: true,
Message: "this issue was not detected",
},
{
CheckName: "check number of replicas in deployment configs",
Ok: true,
Message: "this issue was not detected",
},
{
CheckName: "check pods for containers in waiting state",
Ok: true,
Message: "this issue was not detected",
},
},
},
},
},
want: "No issues found\n",
},
{
description: "errors found",
analysisResult: AnalysisResult{
Projects: []ProjectResult{
{
Project: "rhmap-core",
Results: []CheckResult{
{
CheckName: "check event log for errors",
Ok: false,
Message: "errors detected in event log",
Events: []types.Event{
{
TypeMeta: types.TypeMeta{Kind: "Event"},
InvolvedObject: types.ObjectReference{
Namespace: "rhmap-core",
Name: "fh-ngui",
},
Reason: "FailedUpdate",
Message: "Cannot update deployment rhmap-core/fh-ngui-3 status to Pending: replicationcontrollers \"fh-ngui-3\" cannot be updated: the object has been modified; please apply your changes to the latest version and try again",
Count: 1,
Type: "Warning",
},
},
},
},
},
},
},
contains: []string{"rhmap-core", "fh-ngui", "Cannot update deployment"},
notContains: []string{"No issues found"},
},
}
for _, tt := range tests {
var out bytes.Buffer
PrintAnalysisReport(tt.analysisResult, &out)
got := out.String()
if len(tt.contains) > 0 || len(tt.notContains) > 0 {
for _, want := range tt.contains {
if !strings.Contains(got, want) {
t.Errorf("%s: got %q, want to contain %q", tt.description, got, want)
}
}
for _, notWant := range tt.notContains {
if strings.Contains(got, notWant) {
t.Errorf("%s: got %q, want not to contain %q", tt.description, got, notWant)
}
}
} else {
if got != tt.want {
t.Errorf("%s: got %q, want %q", tt.description, got, tt.want)
}
}
}
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,5 +186,5 @@ func main() {
log.Printf("Finished in %v", delta)
}

RunOutputTask(os.Stdout, os.Stderr, analysisResults)
PrintAnalysisReport(analysisResults, os.Stdout)
}
27 changes: 0 additions & 27 deletions output_results.go

This file was deleted.

101 changes: 0 additions & 101 deletions output_results_test.go

This file was deleted.

0 comments on commit 657726d

Please sign in to comment.