|
| 1 | +package kusto |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/Azure/azure-kusto-go/kusto" |
| 9 | + "github.com/Azure/azure-kusto-go/kusto/ingest" |
| 10 | + "github.com/Azure/kusto-ingest/internal/cli/testingcli" |
| 11 | + "github.com/Azure/kusto-ingest/internal/kusto/testingkusto" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | +) |
| 14 | + |
| 15 | +func Test_ManagementOptions_Run_Success(t *testing.T) { |
| 16 | + cli := testingcli.New() |
| 17 | + |
| 18 | + executed := false |
| 19 | + q := testingkusto.NewQueryClient(func(qc *testingkusto.QueryClient) { |
| 20 | + qc.MgmtFn = func(_ context.Context, db string, stmt kusto.Statement, _ ...kusto.QueryOption) (*kusto.RowIterator, error) { |
| 21 | + executed = true |
| 22 | + assert.Equal(t, "TestDatabase", db) |
| 23 | + return nil, nil |
| 24 | + } |
| 25 | + }) |
| 26 | + |
| 27 | + opts := ManagementOptions{ |
| 28 | + Source: []byte(".show tables"), |
| 29 | + Auth: newTestAuth(), |
| 30 | + KustoTarget: newTestKustoTarget(), |
| 31 | + ingestorBuildSettings: ingestorBuildSettings{ |
| 32 | + CreateQueryClient: func(target KustoTargetOptions, auth AuthOptions) (ingest.QueryClient, error) { |
| 33 | + return q, nil |
| 34 | + }, |
| 35 | + }, |
| 36 | + } |
| 37 | + |
| 38 | + err := opts.Run(cli) |
| 39 | + assert.NoError(t, err) |
| 40 | + assert.True(t, executed, "expected management command to execute") |
| 41 | +} |
| 42 | + |
| 43 | +func Test_ManagementOptions_Run_CreateClientError(t *testing.T) { |
| 44 | + cli := testingcli.New() |
| 45 | + |
| 46 | + opts := ManagementOptions{ |
| 47 | + Source: []byte(".show tables"), |
| 48 | + Auth: newTestAuth(), |
| 49 | + KustoTarget: newTestKustoTarget(), |
| 50 | + ingestorBuildSettings: ingestorBuildSettings{ |
| 51 | + CreateQueryClient: func(target KustoTargetOptions, auth AuthOptions) (ingest.QueryClient, error) { |
| 52 | + return nil, errors.New("boom") |
| 53 | + }, |
| 54 | + }, |
| 55 | + } |
| 56 | + |
| 57 | + err := opts.Run(cli) |
| 58 | + assert.Error(t, err) |
| 59 | + assert.Contains(t, err.Error(), "create Kusto query client") |
| 60 | +} |
| 61 | + |
| 62 | +func Test_ManagementOptions_Run_MgmtError(t *testing.T) { |
| 63 | + cli := testingcli.New() |
| 64 | + |
| 65 | + q := testingkusto.NewQueryClient(func(qc *testingkusto.QueryClient) { |
| 66 | + qc.MgmtFn = func(_ context.Context, db string, stmt kusto.Statement, _ ...kusto.QueryOption) (*kusto.RowIterator, error) { |
| 67 | + return nil, errors.New("mgmt failed") |
| 68 | + } |
| 69 | + }) |
| 70 | + |
| 71 | + opts := ManagementOptions{ |
| 72 | + Source: []byte(".show tables"), |
| 73 | + Auth: newTestAuth(), |
| 74 | + KustoTarget: newTestKustoTarget(), |
| 75 | + ingestorBuildSettings: ingestorBuildSettings{ |
| 76 | + CreateQueryClient: func(target KustoTargetOptions, auth AuthOptions) (ingest.QueryClient, error) { |
| 77 | + return q, nil |
| 78 | + }, |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + err := opts.Run(cli) |
| 83 | + assert.Error(t, err) |
| 84 | + assert.Contains(t, err.Error(), "execute management commands") |
| 85 | +} |
0 commit comments