From 3982e2023b270d844678c8bd9f46e19fb74870f2 Mon Sep 17 00:00:00 2001 From: emosbaugh Date: Thu, 23 Jan 2020 11:05:54 -0800 Subject: [PATCH] Treat empty preflight when condition as true (#125) * Treat empty preflight when condition as true * add test * fix --- pkg/analyze/cluster_version.go | 16 ++++- pkg/analyze/cluster_version_test.go | 99 +++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 pkg/analyze/cluster_version_test.go diff --git a/pkg/analyze/cluster_version.go b/pkg/analyze/cluster_version.go index fbe6be277..62c87a223 100644 --- a/pkg/analyze/cluster_version.go +++ b/pkg/analyze/cluster_version.go @@ -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" } @@ -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") diff --git a/pkg/analyze/cluster_version_test.go b/pkg/analyze/cluster_version_test.go new file mode 100644 index 000000000..739548f03 --- /dev/null +++ b/pkg/analyze/cluster_version_test.go @@ -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) + } + }) + } +}