|
| 1 | +package analyzer |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/blang/semver" |
| 8 | + troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1" |
| 9 | +) |
| 10 | + |
| 11 | +func Test_analyzeClusterVersionResult(t *testing.T) { |
| 12 | + outcomes := []*troubleshootv1beta1.Outcome{ |
| 13 | + { |
| 14 | + Fail: &troubleshootv1beta1.SingleOutcome{ |
| 15 | + When: "< 1.13.0", |
| 16 | + Message: "Sentry requires at Kubernetes 1.13.0 or later, and recommends 1.15.0.", |
| 17 | + URI: "https://www.kubernetes.io", |
| 18 | + }, |
| 19 | + }, |
| 20 | + { |
| 21 | + Warn: &troubleshootv1beta1.SingleOutcome{ |
| 22 | + When: "< 1.15.0", |
| 23 | + Message: "Your cluster meets the minimum version of Kubernetes, but we recommend you update to 1.15.0 or later.", |
| 24 | + URI: "https://www.kubernetes.io", |
| 25 | + }, |
| 26 | + }, |
| 27 | + { |
| 28 | + Pass: &troubleshootv1beta1.SingleOutcome{ |
| 29 | + Message: "Your cluster meets the recommended and required versions of Kubernetes.", |
| 30 | + }, |
| 31 | + }, |
| 32 | + } |
| 33 | + |
| 34 | + type args struct { |
| 35 | + k8sVersion semver.Version |
| 36 | + outcomes []*troubleshootv1beta1.Outcome |
| 37 | + checkName string |
| 38 | + } |
| 39 | + tests := []struct { |
| 40 | + name string |
| 41 | + args args |
| 42 | + want *AnalyzeResult |
| 43 | + wantErr bool |
| 44 | + }{ |
| 45 | + { |
| 46 | + name: "fail", |
| 47 | + args: args{ |
| 48 | + k8sVersion: semver.MustParse("1.12.5"), |
| 49 | + outcomes: outcomes, |
| 50 | + checkName: "Check Fail", |
| 51 | + }, |
| 52 | + want: &AnalyzeResult{ |
| 53 | + IsFail: true, |
| 54 | + Title: "Check Fail", |
| 55 | + Message: "Sentry requires at Kubernetes 1.13.0 or later, and recommends 1.15.0.", |
| 56 | + URI: "https://www.kubernetes.io", |
| 57 | + }, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "warn", |
| 61 | + args: args{ |
| 62 | + k8sVersion: semver.MustParse("1.14.3"), |
| 63 | + outcomes: outcomes, |
| 64 | + checkName: "Check Warn", |
| 65 | + }, |
| 66 | + want: &AnalyzeResult{ |
| 67 | + IsWarn: true, |
| 68 | + Title: "Check Warn", |
| 69 | + Message: "Your cluster meets the minimum version of Kubernetes, but we recommend you update to 1.15.0 or later.", |
| 70 | + URI: "https://www.kubernetes.io", |
| 71 | + }, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "fallthrough", |
| 75 | + args: args{ |
| 76 | + k8sVersion: semver.MustParse("1.17.0"), |
| 77 | + outcomes: outcomes, |
| 78 | + checkName: "Check Pass", |
| 79 | + }, |
| 80 | + want: &AnalyzeResult{ |
| 81 | + IsPass: true, |
| 82 | + Title: "Check Pass", |
| 83 | + Message: "Your cluster meets the recommended and required versions of Kubernetes.", |
| 84 | + }, |
| 85 | + }, |
| 86 | + } |
| 87 | + for _, tt := range tests { |
| 88 | + t.Run(tt.name, func(t *testing.T) { |
| 89 | + got, err := analyzeClusterVersionResult(tt.args.k8sVersion, tt.args.outcomes, tt.args.checkName) |
| 90 | + if (err != nil) != tt.wantErr { |
| 91 | + t.Errorf("analyzeClusterVersionResult() error = %v, wantErr %v", err, tt.wantErr) |
| 92 | + return |
| 93 | + } |
| 94 | + if !reflect.DeepEqual(got, tt.want) { |
| 95 | + t.Errorf("analyzeClusterVersionResult() = %v, want %v", got, tt.want) |
| 96 | + } |
| 97 | + }) |
| 98 | + } |
| 99 | +} |
0 commit comments