databricks-cli/acceptance/config_test.go

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

152 lines
3.7 KiB
Go
Raw Normal View History

package acceptance_test
import (
"os"
"path/filepath"
"slices"
"strings"
"testing"
"dario.cat/mergo"
"github.com/BurntSushi/toml"
"github.com/databricks/cli/libs/testdiff"
"github.com/databricks/cli/libs/testserver"
ignore "github.com/sabhiram/go-gitignore"
"github.com/stretchr/testify/require"
)
const configFilename = "test.toml"
type TestConfig struct {
// Place to describe what's wrong with this test. Does not affect how the test is run.
Badness *string
// Which OSes the test is enabled on. Each string is compared against runtime.GOOS.
// If absent, default to true.
GOOS map[string]bool
// If true, run this test when running locally with a testserver
Local *bool
// If true, run this test when running with cloud env configured
Cloud *bool
// If true, run this test when running with cloud env configured and -short is not passed
// This also sets -tail when -v is passed.
CloudSlow *bool
// If true and Cloud=true, run this test only if unity catalog is available in the cloud environment
RequiresUnityCatalog *bool
// List of additional replacements to apply on this test.
// Old is a regexp, New is a replacement expression.
Repls []testdiff.Replacement
// List of server stubs to load. Example configuration:
//
// [[Server]]
// Pattern = "POST /api/2.1/jobs/create"
// Response.Body = '''
// {
// "job_id": 1111
// }
// '''
Server []ServerStub
// Record the requests made to the server and write them as output to
// out.requests.txt
RecordRequests *bool
// List of request headers to include when recording requests.
IncludeRequestHeaders []string
// List of gitignore patterns to ignore when checking output files
Ignore []string
CompiledIgnoreObject *ignore.GitIgnore
}
type ServerStub struct {
// The HTTP method and path to match. Examples:
// 1. /api/2.0/clusters/list (matches all methods)
// 2. GET /api/2.0/clusters/list
Pattern string
// The response body to return.
Response testserver.Response
Add synchronous logger for telemetry (#2432) ## Changes This PR adds a synchronous telemetry logger for the CLI with a max timeout of 3 seconds. Due to the 3-second timeout configuration, this is only meant to be used in long-running commands. This is a short-term solution. Eventually, we'd like to transition to a daemon process to upload the telemetry logs to amortise the costs of configuring authentication and maintaining a warm pool of HTTP connections, as well as a better UX for the end user. Note that users can set the `DATABRICKS_CLI_DISABLE_TELEMETRY` environment variable to disable telemetry collection. ## Why To collect telemetry, which was previously inaccessible to us, and answer questions like which templates customers like to use and which DABs features would be safe to deprecate. ## Tests Unit and acceptance tests. Also manually verified that the telemetry upload works: ``` (artifact-playground) ➜ cli git:(sync-logger) cli selftest send-telemetry --debug 15:58:20 Info: start pid=40386 version=0.0.0-dev+a2825ca89a23 args="cli, selftest, send-telemetry, --debug" 15:58:20 Debug: Loading DEFAULT profile from /Users/shreyas.goenka/.databrickscfg pid=40386 sdk=true 15:58:20 Info: completed execution pid=40386 exit_code=0 15:58:21 Debug: POST /telemetry-ext > { > "items": null, > "protoLogs": [ > "{\"frontend_log_event_id\":\"82d29b3a-d5ff-48f3-8a21-dae6e08d2999\",\"entry\":{\"databricks_cli_log\":{\"... (232 more bytes)", > "{\"frontend_log_event_id\":\"d6be8220-7db8-45d9-97d6-4c09c25e2664\",\"entry\":{\"databricks_cli_log\":{\"... (232 more bytes)" > ], > "uploadTime": 1741186700967 > } < HTTP/2.0 200 OK < { < "errors": null, < "numProtoSuccess": 2, < "numSuccess": 0 < } pid=40386 sdk=true ```
2025-03-12 13:05:10 +00:00
// Artificial delay in seconds to simulate slow responses.
DelaySeconds *float64
}
// FindConfigs finds all the config relevant for this test,
// ordered from the most outermost (at acceptance/) to current test directory (identified by dir).
// Argument dir must be a relative path from the root of acceptance tests (<project_root>/acceptance/).
func FindConfigs(t *testing.T, dir string) []string {
configs := []string{}
for {
path := filepath.Join(dir, configFilename)
_, err := os.Stat(path)
if err == nil {
configs = append(configs, path)
}
if dir == "" || dir == "." {
break
}
dir = filepath.Dir(dir)
if err == nil || os.IsNotExist(err) {
continue
}
t.Fatalf("Error while reading %s: %s", path, err)
}
slices.Reverse(configs)
return configs
}
// LoadConfig loads the config file. Non-leaf configs are cached.
func LoadConfig(t *testing.T, dir string) (TestConfig, string) {
configs := FindConfigs(t, dir)
if len(configs) == 0 {
return TestConfig{}, "(no config)"
}
result := DoLoadConfig(t, configs[0])
for _, cfgName := range configs[1:] {
cfg := DoLoadConfig(t, cfgName)
err := mergo.Merge(&result, cfg, mergo.WithOverride, mergo.WithoutDereference, mergo.WithAppendSlice)
if err != nil {
t.Fatalf("Error during config merge: %s: %s", cfgName, err)
}
}
result.CompiledIgnoreObject = ignore.CompileIgnoreLines(result.Ignore...)
return result, strings.Join(configs, ", ")
}
func DoLoadConfig(t *testing.T, path string) TestConfig {
bytes, err := os.ReadFile(path)
if err != nil {
t.Fatalf("failed to read config: %s", err)
}
var config TestConfig
meta, err := toml.Decode(string(bytes), &config)
require.NoError(t, err)
keys := meta.Undecoded()
if len(keys) > 0 {
t.Fatalf("Undecoded keys in %s: %#v", path, keys)
}
return config
}