|
| 1 | +package analyzer |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +func Test_textAnalyze(t *testing.T) { |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + analyzer troubleshootv1beta1.TextAnalyze |
| 16 | + expectResult AnalyzeResult |
| 17 | + files map[string][]byte |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "success case 1", |
| 21 | + analyzer: troubleshootv1beta1.TextAnalyze{ |
| 22 | + Outcomes: []*troubleshootv1beta1.Outcome{ |
| 23 | + { |
| 24 | + Pass: &troubleshootv1beta1.SingleOutcome{ |
| 25 | + Message: "pass", |
| 26 | + }, |
| 27 | + }, |
| 28 | + { |
| 29 | + Fail: &troubleshootv1beta1.SingleOutcome{ |
| 30 | + Message: "fail", |
| 31 | + }, |
| 32 | + }, |
| 33 | + }, |
| 34 | + CollectorName: "text-collector-1", |
| 35 | + FileName: "cfile-1.txt", |
| 36 | + RegexPattern: "succeeded", |
| 37 | + }, |
| 38 | + expectResult: AnalyzeResult{ |
| 39 | + IsPass: true, |
| 40 | + IsWarn: false, |
| 41 | + IsFail: false, |
| 42 | + Message: "pass", |
| 43 | + }, |
| 44 | + files: map[string][]byte{ |
| 45 | + "text-collector-1/cfile-1.txt": []byte("Yes it all succeeded"), |
| 46 | + }, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "failure case 1", |
| 50 | + analyzer: troubleshootv1beta1.TextAnalyze{ |
| 51 | + Outcomes: []*troubleshootv1beta1.Outcome{ |
| 52 | + { |
| 53 | + Pass: &troubleshootv1beta1.SingleOutcome{ |
| 54 | + Message: "success", |
| 55 | + }, |
| 56 | + }, |
| 57 | + { |
| 58 | + Fail: &troubleshootv1beta1.SingleOutcome{ |
| 59 | + Message: "fail", |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + CollectorName: "text-collector-2", |
| 64 | + FileName: "cfile-2.txt", |
| 65 | + RegexPattern: "succeeded", |
| 66 | + }, |
| 67 | + expectResult: AnalyzeResult{ |
| 68 | + IsPass: false, |
| 69 | + IsWarn: false, |
| 70 | + IsFail: true, |
| 71 | + Message: "fail", |
| 72 | + }, |
| 73 | + files: map[string][]byte{ |
| 74 | + "text-collector-2/cfile-2.txt": []byte(""), |
| 75 | + }, |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "success case 2", |
| 79 | + analyzer: troubleshootv1beta1.TextAnalyze{ |
| 80 | + Outcomes: []*troubleshootv1beta1.Outcome{ |
| 81 | + { |
| 82 | + Pass: &troubleshootv1beta1.SingleOutcome{ |
| 83 | + Message: "success", |
| 84 | + }, |
| 85 | + }, |
| 86 | + { |
| 87 | + Fail: &troubleshootv1beta1.SingleOutcome{ |
| 88 | + Message: "fail", |
| 89 | + }, |
| 90 | + }, |
| 91 | + }, |
| 92 | + CollectorName: "text-collector-3", |
| 93 | + FileName: "cfile-3.txt", |
| 94 | + RegexPattern: "", |
| 95 | + }, |
| 96 | + expectResult: AnalyzeResult{ |
| 97 | + IsPass: true, |
| 98 | + IsWarn: false, |
| 99 | + IsFail: false, |
| 100 | + Message: "success", |
| 101 | + }, |
| 102 | + files: map[string][]byte{ |
| 103 | + "text-collector-3/cfile-3.txt": []byte("Connection to service succeeded"), |
| 104 | + }, |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "success case 3", |
| 108 | + analyzer: troubleshootv1beta1.TextAnalyze{ |
| 109 | + Outcomes: []*troubleshootv1beta1.Outcome{ |
| 110 | + { |
| 111 | + Pass: &troubleshootv1beta1.SingleOutcome{ |
| 112 | + Message: "success", |
| 113 | + }, |
| 114 | + }, |
| 115 | + { |
| 116 | + Fail: &troubleshootv1beta1.SingleOutcome{ |
| 117 | + Message: "fail", |
| 118 | + }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + CollectorName: "text-collector-5", |
| 122 | + FileName: "cfile-5.txt", |
| 123 | + RegexPattern: "([a-zA-Z0-9\\-_:*\\s])*succe([a-zA-Z0-9\\-_:*\\s!])*", |
| 124 | + }, |
| 125 | + expectResult: AnalyzeResult{ |
| 126 | + IsPass: true, |
| 127 | + IsWarn: false, |
| 128 | + IsFail: false, |
| 129 | + Message: "success", |
| 130 | + }, |
| 131 | + files: map[string][]byte{ |
| 132 | + "text-collector-5/cfile-5.txt": []byte("Connection to service succeeded!"), |
| 133 | + }, |
| 134 | + }, |
| 135 | + { |
| 136 | + name: "failure case 3", |
| 137 | + analyzer: troubleshootv1beta1.TextAnalyze{ |
| 138 | + Outcomes: []*troubleshootv1beta1.Outcome{ |
| 139 | + { |
| 140 | + Pass: &troubleshootv1beta1.SingleOutcome{ |
| 141 | + Message: "success", |
| 142 | + }, |
| 143 | + }, |
| 144 | + { |
| 145 | + Fail: &troubleshootv1beta1.SingleOutcome{ |
| 146 | + Message: "fail", |
| 147 | + }, |
| 148 | + }, |
| 149 | + }, |
| 150 | + CollectorName: "text-collector-4", |
| 151 | + FileName: "cfile-4.txt", |
| 152 | + RegexPattern: "succeeded", |
| 153 | + }, |
| 154 | + expectResult: AnalyzeResult{ |
| 155 | + IsPass: false, |
| 156 | + IsWarn: false, |
| 157 | + IsFail: true, |
| 158 | + Message: "fail", |
| 159 | + }, |
| 160 | + files: map[string][]byte{ |
| 161 | + "text-collector-4/cfile-4.txt": []byte("A different message"), |
| 162 | + }, |
| 163 | + }, |
| 164 | + { |
| 165 | + name: "failure case 4", |
| 166 | + analyzer: troubleshootv1beta1.TextAnalyze{ |
| 167 | + Outcomes: []*troubleshootv1beta1.Outcome{ |
| 168 | + { |
| 169 | + Pass: &troubleshootv1beta1.SingleOutcome{ |
| 170 | + Message: "success", |
| 171 | + }, |
| 172 | + }, |
| 173 | + { |
| 174 | + Fail: &troubleshootv1beta1.SingleOutcome{ |
| 175 | + Message: "fail", |
| 176 | + }, |
| 177 | + }, |
| 178 | + }, |
| 179 | + CollectorName: "text-collector-6", |
| 180 | + FileName: "cfile-6.txt", |
| 181 | + RegexPattern: "([a-zA-Z0-9\\-_:*\\s])*succe([a-zA-Z0-9\\-_:*\\s!])*", |
| 182 | + }, |
| 183 | + expectResult: AnalyzeResult{ |
| 184 | + IsPass: false, |
| 185 | + IsWarn: false, |
| 186 | + IsFail: true, |
| 187 | + Message: "fail", |
| 188 | + }, |
| 189 | + files: map[string][]byte{ |
| 190 | + "text-collector-6/cfile-6.txt": []byte("A different message"), |
| 191 | + }, |
| 192 | + }, |
| 193 | + } |
| 194 | + |
| 195 | + for _, test := range tests { |
| 196 | + t.Run(test.name, func(t *testing.T) { |
| 197 | + req := require.New(t) |
| 198 | + |
| 199 | + getFiles := func(n string) ([]byte, error) { |
| 200 | + val, ok := test.files[n] |
| 201 | + if !ok { |
| 202 | + return nil, fmt.Errorf("File not found: %s", n) |
| 203 | + } |
| 204 | + return val, nil |
| 205 | + } |
| 206 | + |
| 207 | + actual, err := analyzeTextAnalyze(&test.analyzer, getFiles) |
| 208 | + req.NoError(err) |
| 209 | + assert.Equal(t, &test.expectResult, actual) |
| 210 | + }) |
| 211 | + } |
| 212 | +} |
0 commit comments