|
| 1 | +package sqlds_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/grafana/grafana-plugin-sdk-go/backend" |
| 8 | + sqlds "github.com/grafana/sqlds/v4" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func getFakeConnector(t *testing.T, shouldFail bool) *sqlds.Connector { |
| 14 | + t.Helper() |
| 15 | + c, _ := sqlds.NewConnector(context.TODO(), &sqlds.SQLMock{ShouldFailToConnect: shouldFail}, backend.DataSourceInstanceSettings{}, false) |
| 16 | + return c |
| 17 | +} |
| 18 | + |
| 19 | +func TestHealthChecker_Check(t *testing.T) { |
| 20 | + tests := []struct { |
| 21 | + name string |
| 22 | + Connector *sqlds.Connector |
| 23 | + Metrics sqlds.Metrics |
| 24 | + PreCheckHealth func(ctx context.Context, req *backend.CheckHealthRequest) *backend.CheckHealthResult |
| 25 | + PostCheckHealth func(ctx context.Context, req *backend.CheckHealthRequest) *backend.CheckHealthResult |
| 26 | + ctx context.Context |
| 27 | + req *backend.CheckHealthRequest |
| 28 | + want *backend.CheckHealthResult |
| 29 | + wantErr error |
| 30 | + }{ |
| 31 | + { |
| 32 | + name: "default health check should return valid result", |
| 33 | + Connector: getFakeConnector(t, false), |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "should not error when pre check succeed", |
| 37 | + Connector: getFakeConnector(t, false), |
| 38 | + PreCheckHealth: func(ctx context.Context, req *backend.CheckHealthRequest) *backend.CheckHealthResult { |
| 39 | + return &backend.CheckHealthResult{Status: backend.HealthStatusOk} |
| 40 | + }, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "should error when pre check failed", |
| 44 | + Connector: getFakeConnector(t, false), |
| 45 | + PreCheckHealth: func(ctx context.Context, req *backend.CheckHealthRequest) *backend.CheckHealthResult { |
| 46 | + return &backend.CheckHealthResult{Status: backend.HealthStatusError, Message: "unknown error"} |
| 47 | + }, |
| 48 | + want: &backend.CheckHealthResult{Status: backend.HealthStatusError, Message: "unknown error"}, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "should return actual error when pre and post health check succeed but actual connect failed", |
| 52 | + Connector: getFakeConnector(t, true), |
| 53 | + PreCheckHealth: func(ctx context.Context, req *backend.CheckHealthRequest) *backend.CheckHealthResult { |
| 54 | + return &backend.CheckHealthResult{Status: backend.HealthStatusOk} |
| 55 | + }, |
| 56 | + PostCheckHealth: func(ctx context.Context, req *backend.CheckHealthRequest) *backend.CheckHealthResult { |
| 57 | + return &backend.CheckHealthResult{Status: backend.HealthStatusOk} |
| 58 | + }, |
| 59 | + want: &backend.CheckHealthResult{Status: backend.HealthStatusError, Message: "unable to get default db connection"}, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "should not error when post check succeed", |
| 63 | + Connector: getFakeConnector(t, false), |
| 64 | + PostCheckHealth: func(ctx context.Context, req *backend.CheckHealthRequest) *backend.CheckHealthResult { |
| 65 | + return &backend.CheckHealthResult{Status: backend.HealthStatusOk} |
| 66 | + }, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "should error when post check failed", |
| 70 | + Connector: getFakeConnector(t, false), |
| 71 | + PostCheckHealth: func(ctx context.Context, req *backend.CheckHealthRequest) *backend.CheckHealthResult { |
| 72 | + return &backend.CheckHealthResult{Status: backend.HealthStatusError, Message: "unknown error"} |
| 73 | + }, |
| 74 | + want: &backend.CheckHealthResult{Status: backend.HealthStatusError, Message: "unknown error"}, |
| 75 | + }, |
| 76 | + } |
| 77 | + for _, tt := range tests { |
| 78 | + t.Run(tt.name, func(t *testing.T) { |
| 79 | + connector := tt.Connector |
| 80 | + if connector == nil { |
| 81 | + connector = &sqlds.Connector{} |
| 82 | + } |
| 83 | + req := tt.req |
| 84 | + if req == nil { |
| 85 | + req = &backend.CheckHealthRequest{} |
| 86 | + } |
| 87 | + want := tt.want |
| 88 | + if want == nil { |
| 89 | + want = &backend.CheckHealthResult{Status: backend.HealthStatusOk, Message: "Data source is working"} |
| 90 | + } |
| 91 | + hc := &sqlds.HealthChecker{ |
| 92 | + Connector: connector, |
| 93 | + Metrics: tt.Metrics, |
| 94 | + PreCheckHealth: tt.PreCheckHealth, |
| 95 | + PostCheckHealth: tt.PostCheckHealth, |
| 96 | + } |
| 97 | + got, err := hc.Check(tt.ctx, req) |
| 98 | + if tt.wantErr != nil { |
| 99 | + require.NotNil(t, err) |
| 100 | + assert.Equal(t, tt.wantErr.Error(), err.Error()) |
| 101 | + return |
| 102 | + } |
| 103 | + require.Nil(t, err) |
| 104 | + require.NotNil(t, got) |
| 105 | + assert.Equal(t, want.Message, got.Message) |
| 106 | + assert.Equal(t, want.Status, got.Status) |
| 107 | + }) |
| 108 | + } |
| 109 | +} |
0 commit comments