Skip to content

Commit

Permalink
Treat empty preflight when condition as true (#125)
Browse files Browse the repository at this point in the history
* Treat empty preflight when condition as true

* add test

* fix
  • Loading branch information
emosbaugh authored Jan 23, 2020
1 parent 8eddab6 commit 3982e20
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 2 deletions.
16 changes: 14 additions & 2 deletions pkg/analyze/cluster_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@ func analyzeClusterVersion(analyzer *troubleshootv1beta1.ClusterVersion, getColl
return nil, errors.Wrap(err, "failed to parse semver from cluster_version.json")
}

return analyzeClusterVersionResult(k8sVersion, analyzer.Outcomes, analyzer.CheckName)
}

func analyzeClusterVersionResult(k8sVersion semver.Version, outcomes []*troubleshootv1beta1.Outcome, checkName string) (*AnalyzeResult, error) {
result := AnalyzeResult{}
for _, outcome := range analyzer.Outcomes {
for _, outcome := range outcomes {
when := ""
message := ""
uri := ""

title := analyzer.CheckName
title := checkName
if title == "" {
title = "Required Kubernetes Version"
}
Expand Down Expand Up @@ -60,6 +64,14 @@ func analyzeClusterVersion(analyzer *troubleshootv1beta1.ClusterVersion, getColl
return nil, errors.New("empty outcome")
}

// When is usually empty as the final case and should be treated as true
if when == "" {
result.Message = message
result.URI = uri

return &result, nil
}

whenRange, err := semver.ParseRange(when)
if err != nil {
return nil, errors.Wrap(err, "failed to parse semver range")
Expand Down
99 changes: 99 additions & 0 deletions pkg/analyze/cluster_version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package analyzer

import (
"reflect"
"testing"

"github.com/blang/semver"
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
)

func Test_analyzeClusterVersionResult(t *testing.T) {
outcomes := []*troubleshootv1beta1.Outcome{
{
Fail: &troubleshootv1beta1.SingleOutcome{
When: "< 1.13.0",
Message: "Sentry requires at Kubernetes 1.13.0 or later, and recommends 1.15.0.",
URI: "https://www.kubernetes.io",
},
},
{
Warn: &troubleshootv1beta1.SingleOutcome{
When: "< 1.15.0",
Message: "Your cluster meets the minimum version of Kubernetes, but we recommend you update to 1.15.0 or later.",
URI: "https://www.kubernetes.io",
},
},
{
Pass: &troubleshootv1beta1.SingleOutcome{
Message: "Your cluster meets the recommended and required versions of Kubernetes.",
},
},
}

type args struct {
k8sVersion semver.Version
outcomes []*troubleshootv1beta1.Outcome
checkName string
}
tests := []struct {
name string
args args
want *AnalyzeResult
wantErr bool
}{
{
name: "fail",
args: args{
k8sVersion: semver.MustParse("1.12.5"),
outcomes: outcomes,
checkName: "Check Fail",
},
want: &AnalyzeResult{
IsFail: true,
Title: "Check Fail",
Message: "Sentry requires at Kubernetes 1.13.0 or later, and recommends 1.15.0.",
URI: "https://www.kubernetes.io",
},
},
{
name: "warn",
args: args{
k8sVersion: semver.MustParse("1.14.3"),
outcomes: outcomes,
checkName: "Check Warn",
},
want: &AnalyzeResult{
IsWarn: true,
Title: "Check Warn",
Message: "Your cluster meets the minimum version of Kubernetes, but we recommend you update to 1.15.0 or later.",
URI: "https://www.kubernetes.io",
},
},
{
name: "fallthrough",
args: args{
k8sVersion: semver.MustParse("1.17.0"),
outcomes: outcomes,
checkName: "Check Pass",
},
want: &AnalyzeResult{
IsPass: true,
Title: "Check Pass",
Message: "Your cluster meets the recommended and required versions of Kubernetes.",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := analyzeClusterVersionResult(tt.args.k8sVersion, tt.args.outcomes, tt.args.checkName)
if (err != nil) != tt.wantErr {
t.Errorf("analyzeClusterVersionResult() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("analyzeClusterVersionResult() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 3982e20

Please sign in to comment.