|
1 | 1 | package analyzer |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
| 5 | + "reflect" |
4 | 6 | "testing" |
5 | 7 |
|
| 8 | + troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" |
6 | 9 | "github.com/replicatedhq/troubleshoot/pkg/collect" |
7 | 10 | "github.com/stretchr/testify/assert" |
8 | 11 | "github.com/stretchr/testify/require" |
@@ -115,3 +118,109 @@ func Test_compareMssqlConditionalToActual(t *testing.T) { |
115 | 118 | }) |
116 | 119 | } |
117 | 120 | } |
| 121 | + |
| 122 | +func TestAnalyzeMssql_Analyze(t *testing.T) { |
| 123 | + tests := []struct { |
| 124 | + name string |
| 125 | + analyzer *troubleshootv1beta2.DatabaseAnalyze |
| 126 | + want []*AnalyzeResult |
| 127 | + data map[string]any |
| 128 | + wantErr bool |
| 129 | + }{ |
| 130 | + { |
| 131 | + name: "mssql analyze with passing condition", |
| 132 | + data: map[string]any{ |
| 133 | + "isConnected": true, |
| 134 | + "version": "15.0.2000.1565", |
| 135 | + }, |
| 136 | + analyzer: &troubleshootv1beta2.DatabaseAnalyze{ |
| 137 | + AnalyzeMeta: troubleshootv1beta2.AnalyzeMeta{ |
| 138 | + CheckName: "Must be SQLServer 15.x or later", |
| 139 | + }, |
| 140 | + Outcomes: []*troubleshootv1beta2.Outcome{ |
| 141 | + { |
| 142 | + Pass: &troubleshootv1beta2.SingleOutcome{ |
| 143 | + Message: "The SQLServer connection checks out", |
| 144 | + }, |
| 145 | + }, |
| 146 | + { |
| 147 | + Fail: &troubleshootv1beta2.SingleOutcome{ |
| 148 | + When: "connected == false", |
| 149 | + Message: "Cannot connect to SQLServer", |
| 150 | + }, |
| 151 | + }, |
| 152 | + }, |
| 153 | + }, |
| 154 | + want: []*AnalyzeResult{ |
| 155 | + { |
| 156 | + Title: "Must be SQLServer 15.x or later", |
| 157 | + Message: "The SQLServer connection checks out", |
| 158 | + IsPass: true, |
| 159 | + }, |
| 160 | + }, |
| 161 | + }, |
| 162 | + { |
| 163 | + name: "mssql analyze with failing condition", |
| 164 | + data: map[string]any{ |
| 165 | + "isConnected": true, |
| 166 | + "version": "14.0.2000.1565", |
| 167 | + }, |
| 168 | + analyzer: &troubleshootv1beta2.DatabaseAnalyze{ |
| 169 | + Outcomes: []*troubleshootv1beta2.Outcome{ |
| 170 | + { |
| 171 | + Fail: &troubleshootv1beta2.SingleOutcome{ |
| 172 | + When: "version < 15.x", |
| 173 | + Message: "The SQLServer must be at least version 15", |
| 174 | + }, |
| 175 | + }, |
| 176 | + { |
| 177 | + Pass: &troubleshootv1beta2.SingleOutcome{ |
| 178 | + Message: "The SQLServer connection checks out", |
| 179 | + }, |
| 180 | + }, |
| 181 | + }, |
| 182 | + }, |
| 183 | + want: []*AnalyzeResult{ |
| 184 | + { |
| 185 | + Title: "mssql", |
| 186 | + Message: "The SQLServer must be at least version 15", |
| 187 | + IsFail: true, |
| 188 | + }, |
| 189 | + }, |
| 190 | + }, |
| 191 | + } |
| 192 | + |
| 193 | + for _, tt := range tests { |
| 194 | + t.Run(tt.name, func(t *testing.T) { |
| 195 | + a := &AnalyzeMssql{ |
| 196 | + analyzer: tt.analyzer, |
| 197 | + } |
| 198 | + |
| 199 | + getFile := func(_ string) ([]byte, error) { |
| 200 | + return json.Marshal(tt.data) |
| 201 | + } |
| 202 | + |
| 203 | + got, err := a.Analyze(getFile, nil) |
| 204 | + assert.Equalf(t, tt.wantErr, err != nil, "got error = %v, wantErr %v", err, tt.wantErr) |
| 205 | + |
| 206 | + got2 := fromPointerSlice(got) |
| 207 | + want2 := fromPointerSlice(tt.want) |
| 208 | + if !reflect.DeepEqual(got2, want2) { |
| 209 | + t.Errorf("got = %v, want %v", toJSON(got2), toJSON(want2)) |
| 210 | + } |
| 211 | + }) |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +func fromPointerSlice(in []*AnalyzeResult) []AnalyzeResult { |
| 216 | + out := make([]AnalyzeResult, len(in)) |
| 217 | + for i := range in { |
| 218 | + out[i] = *in[i] |
| 219 | + } |
| 220 | + return out |
| 221 | +} |
| 222 | + |
| 223 | +func toJSON(in any) string { |
| 224 | + out, _ := json.MarshalIndent(in, "", " ") |
| 225 | + return string(out) |
| 226 | +} |
0 commit comments