|
5 | 5 | "fmt" |
6 | 6 | "net/http" |
7 | 7 | "sync" |
| 8 | + "sync/atomic" |
8 | 9 | "testing" |
9 | 10 |
|
10 | 11 | "github.com/databricks/databricks-sdk-go/common/environment" |
@@ -1169,3 +1170,79 @@ func TestConfig_ResolveHostMetadata_HostTypes(t *testing.T) { |
1169 | 1170 | }) |
1170 | 1171 | } |
1171 | 1172 | } |
| 1173 | + |
| 1174 | +// withDefaultHostMetadataResolverFactory installs factory for the duration of |
| 1175 | +// the current test, restoring whatever was previously set on cleanup. |
| 1176 | +// Capture/set/restore are not atomic — do not use with t.Parallel across |
| 1177 | +// multiple tests that touch the package-level default. |
| 1178 | +func withDefaultHostMetadataResolverFactory(t *testing.T, factory func(*Config) HostMetadataResolver) { |
| 1179 | + t.Helper() |
| 1180 | + prev := DefaultHostMetadataResolverFactory |
| 1181 | + DefaultHostMetadataResolverFactory = factory |
| 1182 | + t.Cleanup(func() { DefaultHostMetadataResolverFactory = prev }) |
| 1183 | +} |
| 1184 | + |
| 1185 | +func TestDefaultHostMetadataResolverFactory_UsedWhenConfigHasNoResolver(t *testing.T) { |
| 1186 | + var factoryCalls atomic.Int32 |
| 1187 | + withDefaultHostMetadataResolverFactory(t, func(c *Config) HostMetadataResolver { |
| 1188 | + factoryCalls.Add(1) |
| 1189 | + return func(ctx context.Context, host string) (*HostMetadata, error) { |
| 1190 | + return &HostMetadata{AccountID: testHMAccountID, WorkspaceID: testHMWorkspaceID}, nil |
| 1191 | + } |
| 1192 | + }) |
| 1193 | + |
| 1194 | + noopLoader := mockLoader(func(cfg *Config) error { return nil }) |
| 1195 | + cfg := &Config{Host: testHMHost, Loaders: []Loader{noopLoader}} |
| 1196 | + require.NoError(t, cfg.EnsureResolved()) |
| 1197 | + |
| 1198 | + assert.Equal(t, int32(1), factoryCalls.Load(), "factory must be invoked exactly once per resolve") |
| 1199 | + assert.Equal(t, testHMAccountID, cfg.AccountID) |
| 1200 | + assert.Equal(t, testHMWorkspaceID, cfg.WorkspaceID) |
| 1201 | +} |
| 1202 | + |
| 1203 | +func TestDefaultHostMetadataResolverFactory_PerConfigResolverTakesPrecedence(t *testing.T) { |
| 1204 | + var factoryCalls atomic.Int32 |
| 1205 | + withDefaultHostMetadataResolverFactory(t, func(c *Config) HostMetadataResolver { |
| 1206 | + factoryCalls.Add(1) |
| 1207 | + return func(ctx context.Context, host string) (*HostMetadata, error) { |
| 1208 | + return &HostMetadata{AccountID: "factory-account"}, nil |
| 1209 | + } |
| 1210 | + }) |
| 1211 | + |
| 1212 | + noopLoader := mockLoader(func(cfg *Config) error { return nil }) |
| 1213 | + cfg := &Config{ |
| 1214 | + Host: testHMHost, |
| 1215 | + Loaders: []Loader{noopLoader}, |
| 1216 | + HostMetadataResolver: func(ctx context.Context, host string) (*HostMetadata, error) { |
| 1217 | + return &HostMetadata{AccountID: testHMAccountID}, nil |
| 1218 | + }, |
| 1219 | + } |
| 1220 | + require.NoError(t, cfg.EnsureResolved()) |
| 1221 | + |
| 1222 | + assert.Equal(t, int32(0), factoryCalls.Load(), "factory must not be consulted when Config has its own resolver") |
| 1223 | + assert.Equal(t, testHMAccountID, cfg.AccountID) |
| 1224 | +} |
| 1225 | + |
| 1226 | +func TestDefaultHostMetadataResolverFactory_NilResolverFromFactoryFallsThroughToHTTP(t *testing.T) { |
| 1227 | + withDefaultHostMetadataResolverFactory(t, func(c *Config) HostMetadataResolver { |
| 1228 | + return nil |
| 1229 | + }) |
| 1230 | + |
| 1231 | + noopLoader := mockLoader(func(cfg *Config) error { return nil }) |
| 1232 | + cfg := &Config{ |
| 1233 | + Host: testHMHost, |
| 1234 | + Loaders: []Loader{noopLoader}, |
| 1235 | + HTTPTransport: fixtures.SliceTransport{ |
| 1236 | + { |
| 1237 | + Method: "GET", |
| 1238 | + Resource: "/.well-known/databricks-config", |
| 1239 | + ReuseRequest: true, |
| 1240 | + Status: 200, |
| 1241 | + Response: `{"oidc_endpoint": "` + testHMHost + `/oidc", "account_id": "` + testHMAccountID + `"}`, |
| 1242 | + }, |
| 1243 | + }, |
| 1244 | + } |
| 1245 | + require.NoError(t, cfg.EnsureResolved()) |
| 1246 | + |
| 1247 | + assert.Equal(t, testHMAccountID, cfg.AccountID) |
| 1248 | +} |
0 commit comments