add test for upload

This commit is contained in:
Shreyas Goenka 2025-02-03 05:19:26 +01:00
parent e4a1f42737
commit d7bf1dc87e
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
4 changed files with 90 additions and 3 deletions

View File

@ -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

View File

@ -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)

View File

@ -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,
}

View File

@ -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)
}