|
| 1 | +package instancemgmt |
| 2 | + |
| 3 | +import ( |
| 4 | +"context" |
| 5 | +"testing" |
| 6 | +"time" |
| 7 | + |
| 8 | +"github.com/stretchr/testify/require" |
| 9 | + |
| 10 | +"github.com/grafana/grafana-plugin-sdk-go/backend" |
| 11 | +"github.com/grafana/grafana-plugin-sdk-go/experimental/featuretoggles" |
| 12 | +) |
| 13 | + |
| 14 | +func TestInstanceManagerWrapper(t *testing.T) { |
| 15 | +ctx := context.Background() |
| 16 | +tip := &testInstanceProvider{} |
| 17 | +im := NewInstanceManagerWrapper(tip) |
| 18 | + |
| 19 | +t.Run("Should use standard manager when feature toggle is disabled", func(t *testing.T) { |
| 20 | +pCtx := backend.PluginContext{ |
| 21 | +OrgID: 1, |
| 22 | +AppInstanceSettings: &backend.AppInstanceSettings{ |
| 23 | +Updated: time.Now(), |
| 24 | +}, |
| 25 | +GrafanaConfig: backend.NewGrafanaCfg(map[string]string{ |
| 26 | +featuretoggles.EnabledFeatures: "", |
| 27 | +}), |
| 28 | +} |
| 29 | + |
| 30 | +manager := im.(*instanceManagerWrapper).selectManager(ctx, pCtx) |
| 31 | +require.IsType(t, &instanceManager{}, manager) |
| 32 | +}) |
| 33 | + |
| 34 | +t.Run("Should use TTL manager when feature toggle is enabled", func(t *testing.T) { |
| 35 | +pCtx := backend.PluginContext{ |
| 36 | +OrgID: 1, |
| 37 | +AppInstanceSettings: &backend.AppInstanceSettings{ |
| 38 | +Updated: time.Now(), |
| 39 | +}, |
| 40 | +GrafanaConfig: backend.NewGrafanaCfg(map[string]string{ |
| 41 | +featuretoggles.EnabledFeatures: featuretoggles.TTLInstanceManager, |
| 42 | +}), |
| 43 | +} |
| 44 | + |
| 45 | +manager := im.(*instanceManagerWrapper).selectManager(ctx, pCtx) |
| 46 | +require.IsType(t, &instanceManagerWithTTL{}, manager) |
| 47 | +}) |
| 48 | + |
| 49 | +t.Run("Should use standard manager when GrafanaConfig is nil", func(t *testing.T) { |
| 50 | +pCtx := backend.PluginContext{ |
| 51 | +OrgID: 1, |
| 52 | +AppInstanceSettings: &backend.AppInstanceSettings{ |
| 53 | +Updated: time.Now(), |
| 54 | +}, |
| 55 | +GrafanaConfig: nil, |
| 56 | +} |
| 57 | + |
| 58 | +manager := im.(*instanceManagerWrapper).selectManager(ctx, pCtx) |
| 59 | +require.IsType(t, &instanceManager{}, manager) |
| 60 | +}) |
| 61 | + |
| 62 | +t.Run("Should use TTL manager when feature toggle is enabled with other flags", func(t *testing.T) { |
| 63 | +pCtx := backend.PluginContext{ |
| 64 | +OrgID: 1, |
| 65 | +AppInstanceSettings: &backend.AppInstanceSettings{ |
| 66 | +Updated: time.Now(), |
| 67 | +}, |
| 68 | +GrafanaConfig: backend.NewGrafanaCfg(map[string]string{ |
| 69 | +featuretoggles.EnabledFeatures: "someOtherFlag," + featuretoggles.TTLInstanceManager + ",anotherFlag", |
| 70 | +}), |
| 71 | +} |
| 72 | + |
| 73 | +manager := im.(*instanceManagerWrapper).selectManager(ctx, pCtx) |
| 74 | +require.IsType(t, &instanceManagerWithTTL{}, manager) |
| 75 | +}) |
| 76 | + |
| 77 | +t.Run("Should delegate Get calls correctly", func(t *testing.T) { |
| 78 | +// Test with TTL manager enabled |
| 79 | +pCtx := backend.PluginContext{ |
| 80 | +OrgID: 1, |
| 81 | +AppInstanceSettings: &backend.AppInstanceSettings{ |
| 82 | +Updated: time.Now(), |
| 83 | +}, |
| 84 | +GrafanaConfig: backend.NewGrafanaCfg(map[string]string{ |
| 85 | +featuretoggles.EnabledFeatures: featuretoggles.TTLInstanceManager, |
| 86 | +}), |
| 87 | +} |
| 88 | + |
| 89 | +instance, err := im.Get(ctx, pCtx) |
| 90 | +require.NoError(t, err) |
| 91 | +require.NotNil(t, instance) |
| 92 | +require.Equal(t, pCtx.OrgID, instance.(*testInstance).orgID) |
| 93 | +}) |
| 94 | + |
| 95 | +t.Run("Should delegate Do calls correctly", func(t *testing.T) { |
| 96 | +// Test with standard manager (no feature toggle) |
| 97 | +pCtx := backend.PluginContext{ |
| 98 | +OrgID: 2, |
| 99 | +AppInstanceSettings: &backend.AppInstanceSettings{ |
| 100 | +Updated: time.Now(), |
| 101 | +}, |
| 102 | +GrafanaConfig: backend.NewGrafanaCfg(map[string]string{ |
| 103 | +featuretoggles.EnabledFeatures: "", |
| 104 | +}), |
| 105 | +} |
| 106 | + |
| 107 | +var receivedInstance *testInstance |
| 108 | +err := im.Do(ctx, pCtx, func(instance Instance) { |
| 109 | +receivedInstance = instance.(*testInstance) |
| 110 | +}) |
| 111 | +require.NoError(t, err) |
| 112 | +require.NotNil(t, receivedInstance) |
| 113 | +require.Equal(t, pCtx.OrgID, receivedInstance.orgID) |
| 114 | +}) |
| 115 | +} |
0 commit comments