|
| 1 | +package dbr |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io/fs" |
| 6 | + "runtime" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/databricks/cli/libs/env" |
| 10 | + "github.com/databricks/cli/libs/fakefs" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +func requireLinux(t *testing.T) { |
| 15 | + if runtime.GOOS != "linux" { |
| 16 | + t.Skipf("skipping test on %s", runtime.GOOS) |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +func configureStatFunc(t *testing.T, fi fs.FileInfo, err error) { |
| 21 | + originalFunc := statFunc |
| 22 | + statFunc = func(name string) (fs.FileInfo, error) { |
| 23 | + assert.Equal(t, "/databricks", name) |
| 24 | + return fi, err |
| 25 | + } |
| 26 | + |
| 27 | + t.Cleanup(func() { |
| 28 | + statFunc = originalFunc |
| 29 | + }) |
| 30 | +} |
| 31 | + |
| 32 | +func TestDetect_NotLinux(t *testing.T) { |
| 33 | + if runtime.GOOS == "linux" { |
| 34 | + t.Skip("skipping test on Linux OS") |
| 35 | + } |
| 36 | + |
| 37 | + ctx := context.Background() |
| 38 | + assert.False(t, detect(ctx)) |
| 39 | +} |
| 40 | + |
| 41 | +func TestDetect_Env(t *testing.T) { |
| 42 | + requireLinux(t) |
| 43 | + |
| 44 | + // Configure other checks to pass. |
| 45 | + configureStatFunc(t, fakefs.FileInfo{FakeDir: true}, nil) |
| 46 | + |
| 47 | + t.Run("empty", func(t *testing.T) { |
| 48 | + ctx := env.Set(context.Background(), "DATABRICKS_RUNTIME_VERSION", "") |
| 49 | + assert.False(t, detect(ctx)) |
| 50 | + }) |
| 51 | + |
| 52 | + t.Run("non-empty cluster", func(t *testing.T) { |
| 53 | + ctx := env.Set(context.Background(), "DATABRICKS_RUNTIME_VERSION", "15.4") |
| 54 | + assert.True(t, detect(ctx)) |
| 55 | + }) |
| 56 | + |
| 57 | + t.Run("non-empty serverless", func(t *testing.T) { |
| 58 | + ctx := env.Set(context.Background(), "DATABRICKS_RUNTIME_VERSION", "client.1.13") |
| 59 | + assert.True(t, detect(ctx)) |
| 60 | + }) |
| 61 | +} |
| 62 | + |
| 63 | +func TestDetect_Stat(t *testing.T) { |
| 64 | + requireLinux(t) |
| 65 | + |
| 66 | + // Configure other checks to pass. |
| 67 | + ctx := env.Set(context.Background(), "DATABRICKS_RUNTIME_VERSION", "non-empty") |
| 68 | + |
| 69 | + t.Run("error", func(t *testing.T) { |
| 70 | + configureStatFunc(t, nil, fs.ErrNotExist) |
| 71 | + assert.False(t, detect(ctx)) |
| 72 | + }) |
| 73 | + |
| 74 | + t.Run("not a directory", func(t *testing.T) { |
| 75 | + configureStatFunc(t, fakefs.FileInfo{}, nil) |
| 76 | + assert.False(t, detect(ctx)) |
| 77 | + }) |
| 78 | + |
| 79 | + t.Run("directory", func(t *testing.T) { |
| 80 | + configureStatFunc(t, fakefs.FileInfo{FakeDir: true}, nil) |
| 81 | + assert.True(t, detect(ctx)) |
| 82 | + }) |
| 83 | +} |
0 commit comments