|
| 1 | +package root_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/databricks/cli/cmd/root" |
| 9 | + "github.com/databricks/cli/libs/env" |
| 10 | + "github.com/databricks/databricks-sdk-go/useragent" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +// skillNames returns n distinct skill names of exactly length bytes. |
| 15 | +func skillNames(n, length int) []string { |
| 16 | + names := make([]string, 0, n) |
| 17 | + for i := range n { |
| 18 | + prefix := fmt.Sprintf("s%02d", i) |
| 19 | + names = append(names, prefix+strings.Repeat("x", length-len(prefix))) |
| 20 | + } |
| 21 | + return names |
| 22 | +} |
| 23 | + |
| 24 | +// pairsSize is the length of " skill/<name>" for every name. |
| 25 | +func pairsSize(names []string) int { |
| 26 | + size := 0 |
| 27 | + for _, name := range names { |
| 28 | + size += len(" skill/") + len(name) |
| 29 | + } |
| 30 | + return size |
| 31 | +} |
| 32 | + |
| 33 | +func TestParseSkills(t *testing.T) { |
| 34 | + // 38-byte names (the longest current skill) make 45-byte pairs: five fill |
| 35 | + // 225 bytes exactly and a sixth doesn't fit. |
| 36 | + fiveFit := skillNames(5, 38) |
| 37 | + sixTooMany := skillNames(6, 38) |
| 38 | + // 64-, 64-, 64- and 22-byte pairs make 214 bytes, so " skill/abcd" (11) |
| 39 | + // lands exactly on 225 and nothing more fits. |
| 40 | + exact := []string{strings.Repeat("a", 57), strings.Repeat("b", 57), strings.Repeat("c", 57), "databricks-core", "abcd"} |
| 41 | + |
| 42 | + tests := []struct { |
| 43 | + name string |
| 44 | + raw string |
| 45 | + want []string |
| 46 | + }{ |
| 47 | + {"empty", "", nil}, |
| 48 | + {"single", "databricks-core", []string{"databricks-core"}}, |
| 49 | + {"multiple", "a,b,c", []string{"a", "b", "c"}}, |
| 50 | + {"trims and lowercases", " Databricks-Core , JOBS ", []string{"databricks-core", "jobs"}}, |
| 51 | + {"drops empty entries", ",,a,,", []string{"a"}}, |
| 52 | + {"allows underscore and dot", "a_b.c-d", []string{"a_b.c-d"}}, |
| 53 | + {"drops disallowed skill-name characters", "ok,bad name,a:b,a/b,a+b", []string{"ok"}}, |
| 54 | + {"drops leading punctuation", "-a,.b,_c", nil}, |
| 55 | + {"dedupes preserving first occurrence", "b,a,b,a,c", []string{"b", "a", "c"}}, |
| 56 | + {"dedupes after normalization", "Core,core, CORE", []string{"core"}}, |
| 57 | + {"drops entry longer than 64 bytes", strings.Repeat("a", 65) + ",ok", []string{"ok"}}, |
| 58 | + {"keeps entry of exactly 64 bytes", strings.Repeat("a", 64), []string{strings.Repeat("a", 64)}}, |
| 59 | + {"keeps five of the longest skill names", strings.Join(fiveFit, ","), fiveFit}, |
| 60 | + {"drops pairs over the budget", strings.Join(sixTooMany, ","), sixTooMany[:5]}, |
| 61 | + {"fills the budget exactly", strings.Join(exact, ",") + ",d", exact}, |
| 62 | + } |
| 63 | + for _, tt := range tests { |
| 64 | + t.Run(tt.name, func(t *testing.T) { |
| 65 | + got := root.ParseSkills(t.Context(), tt.raw) |
| 66 | + assert.Equal(t, tt.want, got) |
| 67 | + assert.LessOrEqual(t, pairsSize(got), root.MaxSkillsUserAgentLength) |
| 68 | + }) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func TestWithSkillsInUserAgent(t *testing.T) { |
| 73 | + tests := []struct { |
| 74 | + name string |
| 75 | + skills string |
| 76 | + disableTelemetry string |
| 77 | + want string |
| 78 | + }{ |
| 79 | + {"unset", "", "", ""}, |
| 80 | + {"set", "databricks-core,databricks-jobs", "", "skill/databricks-core skill/databricks-jobs"}, |
| 81 | + {"telemetry disabled", "databricks-core", "1", ""}, |
| 82 | + } |
| 83 | + for _, tt := range tests { |
| 84 | + t.Run(tt.name, func(t *testing.T) { |
| 85 | + // Always set both: an empty value in the context overrides the host environment. |
| 86 | + ctx := env.Set(t.Context(), root.SkillsInvokedEnvVar, tt.skills) |
| 87 | + ctx = env.Set(ctx, "DATABRICKS_CLI_DISABLE_TELEMETRY", tt.disableTelemetry) |
| 88 | + ua := useragent.FromContext(root.WithSkillsInUserAgent(ctx)) |
| 89 | + if tt.want == "" { |
| 90 | + assert.NotContains(t, ua, "skill/") |
| 91 | + } else { |
| 92 | + assert.Contains(t, ua, tt.want) |
| 93 | + } |
| 94 | + }) |
| 95 | + } |
| 96 | +} |
0 commit comments