|
| 1 | +package phases |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | +) |
| 8 | + |
| 9 | +func TestScrubForTelemetry_BundleRootPath(t *testing.T) { |
| 10 | + tests := []struct { |
| 11 | + name string |
| 12 | + msg string |
| 13 | + bundleRoot string |
| 14 | + expected string |
| 15 | + }{ |
| 16 | + { |
| 17 | + name: "replaces bundle root in file path", |
| 18 | + msg: "failed to load /home/user/project/databricks.yml: invalid config", |
| 19 | + bundleRoot: "/home/user/project", |
| 20 | + expected: "failed to load ./databricks.yml: invalid config", |
| 21 | + }, |
| 22 | + { |
| 23 | + name: "replaces bundle root without trailing content", |
| 24 | + msg: "error at /home/user/project", |
| 25 | + bundleRoot: "/home/user/project", |
| 26 | + expected: "error at .", |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "replaces multiple occurrences", |
| 30 | + msg: "path /home/user/project/a.yml and /home/user/project/b.yml", |
| 31 | + bundleRoot: "/home/user/project", |
| 32 | + expected: "path ./a.yml and ./b.yml", |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "empty bundle root is no-op", |
| 36 | + msg: "some error", |
| 37 | + bundleRoot: "", |
| 38 | + expected: "some error", |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "empty message", |
| 42 | + msg: "", |
| 43 | + bundleRoot: "/home/user/project", |
| 44 | + expected: "", |
| 45 | + }, |
| 46 | + } |
| 47 | + |
| 48 | + for _, tt := range tests { |
| 49 | + t.Run(tt.name, func(t *testing.T) { |
| 50 | + assert.Equal(t, tt.expected, scrubForTelemetry(tt.msg, tt.bundleRoot, "")) |
| 51 | + }) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func TestScrubForTelemetry_HomeDir(t *testing.T) { |
| 56 | + tests := []struct { |
| 57 | + name string |
| 58 | + msg string |
| 59 | + homeDir string |
| 60 | + expected string |
| 61 | + }{ |
| 62 | + { |
| 63 | + name: "replaces home dir with tilde", |
| 64 | + msg: "failed to read /Users/shreyas/.databricks/config.json", |
| 65 | + homeDir: "/Users/shreyas", |
| 66 | + expected: "failed to read ~/.databricks/config.json", |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "home dir replacement after bundle root", |
| 70 | + msg: "error: /Users/shreyas/project/file.yml and /Users/shreyas/.cache/other", |
| 71 | + homeDir: "/Users/shreyas", |
| 72 | + expected: "error: ~/project/file.yml and ~/.cache/other", |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "bundle root takes priority over home dir", |
| 76 | + msg: "error at /Users/shreyas/project/databricks.yml", |
| 77 | + homeDir: "/Users/shreyas", |
| 78 | + expected: "error at ./databricks.yml", |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + for _, tt := range tests { |
| 83 | + t.Run(tt.name, func(t *testing.T) { |
| 84 | + bundleRoot := "" |
| 85 | + if tt.name == "bundle root takes priority over home dir" { |
| 86 | + bundleRoot = "/Users/shreyas/project" |
| 87 | + } |
| 88 | + assert.Equal(t, tt.expected, scrubForTelemetry(tt.msg, bundleRoot, tt.homeDir)) |
| 89 | + }) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func TestScrubForTelemetry_HomeDirRegexFallback(t *testing.T) { |
| 94 | + tests := []struct { |
| 95 | + name string |
| 96 | + msg string |
| 97 | + expected string |
| 98 | + }{ |
| 99 | + { |
| 100 | + name: "macOS home dir", |
| 101 | + msg: "failed to read /Users/otheruser/some-project/file.yml", |
| 102 | + expected: "failed to read [REDACTED_HOME_PATH]", |
| 103 | + }, |
| 104 | + { |
| 105 | + name: "Linux home dir", |
| 106 | + msg: "failed to read /home/runner/work/project/file.yml", |
| 107 | + expected: "failed to read [REDACTED_HOME_PATH]", |
| 108 | + }, |
| 109 | + { |
| 110 | + name: "home dir in middle of message", |
| 111 | + msg: "error: /Users/jane/project/a.yml: not found, try again", |
| 112 | + expected: "error: [REDACTED_HOME_PATH]: not found, try again", |
| 113 | + }, |
| 114 | + { |
| 115 | + name: "Windows home dir with backslashes", |
| 116 | + msg: `error at C:\Users\shreyas\project\file.yml`, |
| 117 | + expected: "error at [REDACTED_HOME_PATH]", |
| 118 | + }, |
| 119 | + { |
| 120 | + name: "Windows home dir with forward slashes", |
| 121 | + msg: "error at C:/Users/shreyas/project/file.yml", |
| 122 | + expected: "error at [REDACTED_HOME_PATH]", |
| 123 | + }, |
| 124 | + { |
| 125 | + name: "preserves relative paths", |
| 126 | + msg: "failed to load ./resources/job.yml", |
| 127 | + expected: "failed to load ./resources/job.yml", |
| 128 | + }, |
| 129 | + { |
| 130 | + name: "preserves workspace paths without email", |
| 131 | + msg: "uploading to /Workspace/.bundle/dev/files", |
| 132 | + expected: "uploading to /Workspace/.bundle/dev/files", |
| 133 | + }, |
| 134 | + } |
| 135 | + |
| 136 | + for _, tt := range tests { |
| 137 | + t.Run(tt.name, func(t *testing.T) { |
| 138 | + assert.Equal(t, tt.expected, scrubForTelemetry(tt.msg, "", "")) |
| 139 | + }) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +func TestScrubForTelemetry_RemainingAbsolutePaths(t *testing.T) { |
| 144 | + tests := []struct { |
| 145 | + name string |
| 146 | + msg string |
| 147 | + expected string |
| 148 | + }{ |
| 149 | + { |
| 150 | + name: "tmp path", |
| 151 | + msg: "failed to write /tmp/bundle-xyz/state.json", |
| 152 | + expected: "failed to write [REDACTED_PATH]", |
| 153 | + }, |
| 154 | + { |
| 155 | + name: "var folders path", |
| 156 | + msg: "error reading /var/folders/7t/n_tz6x9d4xj91h48pf8md5zh0000gp/T/test123/file", |
| 157 | + expected: "error reading [REDACTED_PATH]", |
| 158 | + }, |
| 159 | + { |
| 160 | + name: "etc path", |
| 161 | + msg: "config at /etc/databricks/config.json not found", |
| 162 | + expected: "config at [REDACTED_PATH] not found", |
| 163 | + }, |
| 164 | + { |
| 165 | + name: "preserves workspace paths", |
| 166 | + msg: "uploading to /Workspace/Users/dev/.bundle/files", |
| 167 | + expected: "uploading to /Workspace/Users/dev/.bundle/files", |
| 168 | + }, |
| 169 | + { |
| 170 | + name: "preserves volume paths", |
| 171 | + msg: "artifact at /Volumes/catalog/schema/volume/artifact.whl", |
| 172 | + expected: "artifact at /Volumes/catalog/schema/volume/artifact.whl", |
| 173 | + }, |
| 174 | + { |
| 175 | + name: "preserves dbfs paths", |
| 176 | + msg: "state at /dbfs/mnt/data/state.json", |
| 177 | + expected: "state at /dbfs/mnt/data/state.json", |
| 178 | + }, |
| 179 | + { |
| 180 | + name: "single component path is not matched", |
| 181 | + msg: "POST /telemetry-ext failed", |
| 182 | + expected: "POST /telemetry-ext failed", |
| 183 | + }, |
| 184 | + } |
| 185 | + |
| 186 | + for _, tt := range tests { |
| 187 | + t.Run(tt.name, func(t *testing.T) { |
| 188 | + assert.Equal(t, tt.expected, scrubForTelemetry(tt.msg, "", "")) |
| 189 | + }) |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +func TestScrubForTelemetry_Emails(t *testing.T) { |
| 194 | + tests := []struct { |
| 195 | + name string |
| 196 | + msg string |
| 197 | + expected string |
| 198 | + }{ |
| 199 | + { |
| 200 | + name: "email in workspace path", |
| 201 | + msg: "/Workspace/Users/user@example.com/.bundle/dev should contain current username", |
| 202 | + expected: "/Workspace/Users/[REDACTED_EMAIL]/.bundle/dev should contain current username", |
| 203 | + }, |
| 204 | + { |
| 205 | + name: "plain email", |
| 206 | + msg: "access denied for user@company.io", |
| 207 | + expected: "access denied for [REDACTED_EMAIL]", |
| 208 | + }, |
| 209 | + { |
| 210 | + name: "no email present", |
| 211 | + msg: "some error without emails", |
| 212 | + expected: "some error without emails", |
| 213 | + }, |
| 214 | + } |
| 215 | + |
| 216 | + for _, tt := range tests { |
| 217 | + t.Run(tt.name, func(t *testing.T) { |
| 218 | + assert.Equal(t, tt.expected, scrubForTelemetry(tt.msg, "", "")) |
| 219 | + }) |
| 220 | + } |
| 221 | +} |
| 222 | + |
| 223 | +func TestScrubForTelemetry_Combined(t *testing.T) { |
| 224 | + msg := "failed to load /Users/shreyas/myproject/databricks.yml: " + |
| 225 | + "workspace /Workspace/Users/shreyas@databricks.com/.bundle is invalid, " + |
| 226 | + "also tried /home/other/fallback/config.yml, " + |
| 227 | + "temp at /tmp/bundle-cache/state.json" |
| 228 | + |
| 229 | + got := scrubForTelemetry(msg, "/Users/shreyas/myproject", "/Users/shreyas") |
| 230 | + |
| 231 | + assert.Equal(t, |
| 232 | + "failed to load ./databricks.yml: "+ |
| 233 | + "workspace /Workspace/Users/[REDACTED_EMAIL]/.bundle is invalid, "+ |
| 234 | + "also tried [REDACTED_HOME_PATH], "+ |
| 235 | + "temp at [REDACTED_PATH]", |
| 236 | + got, |
| 237 | + ) |
| 238 | +} |
0 commit comments