diff --git a/acceptance/libs/telemetry/upload/script b/acceptance/libs/telemetry/upload/script index 0c65da29a..595a5303b 100644 --- a/acceptance/libs/telemetry/upload/script +++ b/acceptance/libs/telemetry/upload/script @@ -1 +1,3 @@ -envsubst < ./stdin | trace $CLI telemetry upload +# This command / test cannot be run in inprocess / debug mode. This is because +# it does not go through the [root.Execute] function. +trace $CLI telemetry upload < stdin diff --git a/cmd/root/root.go b/cmd/root/root.go index deffcb516..980478165 100644 --- a/cmd/root/root.go +++ b/cmd/root/root.go @@ -181,7 +181,6 @@ func uploadTelemetry(ctx context.Context, cmdStr string, start, end time.Time, e log.Debugf(ctx, "failed to create stdin pipe for telemetry worker: %s", err) } - // TODO: Change this back to Run() once we have a way to test this. err = telemetryCmd.Start() if err != nil { log.Debugf(ctx, "failed to start telemetry worker: %s", err) diff --git a/cmd/telemetry/telemetry.go b/cmd/telemetry/telemetry.go index caa960e5a..888aa318f 100644 --- a/cmd/telemetry/telemetry.go +++ b/cmd/telemetry/telemetry.go @@ -7,7 +7,7 @@ import ( func New() *cobra.Command { cmd := &cobra.Command{ Use: "telemetry", - Short: "CLI commands to publish telemetry to the Databricks backend.", + Short: "", Hidden: true, } diff --git a/libs/telemetry/upload_test.go b/libs/telemetry/upload_test.go new file mode 100644 index 000000000..9db975fe6 --- /dev/null +++ b/libs/telemetry/upload_test.go @@ -0,0 +1,86 @@ +package telemetry + +import ( + "encoding/json" + "net/http" + "os" + "path/filepath" + "testing" + + "github.com/databricks/cli/internal/testutil" + "github.com/databricks/cli/libs/telemetry/protos" + "github.com/databricks/cli/libs/testserver" + "github.com/databricks/databricks-sdk-go/apierr" + "github.com/google/uuid" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestTelemetryUpload(t *testing.T) { + server := testserver.New(t) + t.Cleanup(server.Close) + + count := 0 + server.Handle("POST /telemetry-ext", func(req *http.Request) (resp any, err error) { + count++ + if count == 1 { + return ResponseBody{ + NumProtoSuccess: 1, + }, nil + } + if count == 2 { + return ResponseBody{ + NumProtoSuccess: 2, + }, nil + } + return nil, apierr.NotFound("not found") + }) + + t.Setenv("DATABRICKS_HOST", server.URL) + t.Setenv("DATABRICKS_TOKEN", "token") + + logs := []protos.FrontendLog{ + { + FrontendLogEventID: uuid.New().String(), + Entry: protos.FrontendLogEntry{ + DatabricksCliLog: protos.DatabricksCliLog{ + CliTestEvent: &protos.CliTestEvent{Name: protos.DummyCliEnumValue1}, + }, + }, + }, + { + FrontendLogEventID: uuid.New().String(), + Entry: protos.FrontendLogEntry{ + DatabricksCliLog: protos.DatabricksCliLog{ + CliTestEvent: &protos.CliTestEvent{Name: protos.DummyCliEnumValue2}, + }, + }, + }, + } + + processIn := UploadConfig{ + Logs: logs, + } + + b, err := json.Marshal(processIn) + require.NoError(t, err) + + tmpDir := t.TempDir() + testutil.WriteFile(t, filepath.Join(tmpDir, "stdin"), string(b)) + + fd, err := os.OpenFile(filepath.Join(tmpDir, "stdin"), os.O_RDONLY, 0o644) + require.NoError(t, err) + + // Redirect stdin to the file containing the telemetry logs. + old := os.Stdin + os.Stdin = fd + t.Cleanup(func() { + fd.Close() + os.Stdin = old + }) + + err = Upload() + require.NoError(t, err) + + assert.Equal(t, 2, count) +}