databricks-cli/integration/libs/telemetry/telemetry_test.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

91 lines
2.3 KiB
Go
Raw Normal View History

2024-12-20 06:42:22 +00:00
package telemetry_test
2024-11-29 00:57:48 +00:00
import (
"context"
"net/http"
2024-12-27 08:21:55 +00:00
"reflect"
2024-11-29 00:57:48 +00:00
"testing"
"time"
2024-12-20 06:08:17 +00:00
"github.com/databricks/cli/integration/internal/acc"
2024-11-29 00:57:48 +00:00
"github.com/databricks/cli/libs/telemetry"
2024-12-20 05:54:30 +00:00
"github.com/databricks/cli/libs/telemetry/events"
2024-11-29 00:57:48 +00:00
"github.com/databricks/databricks-sdk-go/client"
2024-12-27 08:21:55 +00:00
"github.com/google/uuid"
2024-11-29 00:57:48 +00:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Wrapper to capture the response from the API client since that's not directly
// accessible from the logger.
type apiClientWrapper struct {
response *telemetry.ResponseBody
apiClient *client.DatabricksClient
}
func (wrapper *apiClientWrapper) Do(ctx context.Context, method, path string,
headers map[string]string, request, response any,
2024-12-20 06:08:17 +00:00
visitors ...func(*http.Request) error,
) error {
2024-11-29 00:57:48 +00:00
err := wrapper.apiClient.Do(ctx, method, path, headers, request, response, visitors...)
wrapper.response = response.(*telemetry.ResponseBody)
return err
}
2024-12-20 06:08:17 +00:00
func TestTelemetryLogger(t *testing.T) {
2024-12-30 06:50:14 +00:00
events := []telemetry.DatabricksCliLog{
2024-12-27 08:21:55 +00:00
{
2024-12-30 06:50:14 +00:00
CliTestEvent: &events.CliTestEvent{
Name: events.DummyCliEnumValue1,
2024-12-27 08:21:55 +00:00
},
},
{
2024-12-30 06:50:14 +00:00
BundleInitEvent: &events.BundleInitEvent{
Uuid: uuid.New().String(),
TemplateName: "abc",
2025-01-02 09:04:28 +00:00
TemplateEnumArgs: []events.BundleInitTemplateEnumArg{
{
Key: "a",
Value: "b",
},
{
Key: "c",
Value: "d",
},
2024-12-27 08:21:55 +00:00
},
},
},
}
2025-01-02 19:16:07 +00:00
assert.Len(t, reflect.TypeOf(telemetry.DatabricksCliLog{}).NumField(), len(events),
2024-12-27 08:21:55 +00:00
"Number of events should match the number of fields in DatabricksCliLog. Please add a new event to this test.")
2024-11-29 00:57:48 +00:00
ctx, w := acc.WorkspaceTest(t)
2024-12-27 07:57:24 +00:00
ctx = telemetry.ContextWithLogger(ctx)
2024-11-29 00:57:48 +00:00
// Extend the maximum wait time for the telemetry flush just for this test.
telemetry.MaxAdditionalWaitTime = 1 * time.Hour
t.Cleanup(func() {
2024-11-29 01:04:42 +00:00
telemetry.MaxAdditionalWaitTime = 2 * time.Second
2024-11-29 00:57:48 +00:00
})
2024-12-27 08:21:55 +00:00
for _, event := range events {
2024-12-30 06:59:18 +00:00
telemetry.Log(ctx, event)
2024-12-27 08:21:55 +00:00
}
2024-11-29 00:57:48 +00:00
apiClient, err := client.New(w.W.Config)
require.NoError(t, err)
// Flush the events.
wrapper := &apiClientWrapper{
apiClient: apiClient,
}
telemetry.Flush(ctx, wrapper)
// Assert that the events were logged.
assert.Equal(t, telemetry.ResponseBody{
2024-12-27 08:21:55 +00:00
NumProtoSuccess: int64(len(events)),
2024-11-29 00:57:48 +00:00
Errors: []telemetry.LogError{},
}, *wrapper.response)
}