mirror of https://github.com/databricks/cli.git
add test for upload
This commit is contained in:
parent
e4a1f42737
commit
d7bf1dc87e
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
Loading…
Reference in New Issue