|
| 1 | +package mutator |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/databricks/cli/bundle" |
| 10 | + "github.com/databricks/cli/bundle/config" |
| 11 | + "github.com/databricks/cli/bundle/config/paths" |
| 12 | + "github.com/databricks/cli/bundle/config/resources" |
| 13 | + "github.com/databricks/databricks-sdk-go/service/compute" |
| 14 | + "github.com/databricks/databricks-sdk-go/service/pipelines" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | +) |
| 17 | + |
| 18 | +func touchEmptyFile(t *testing.T, path string) { |
| 19 | + err := os.MkdirAll(filepath.Dir(path), 0700) |
| 20 | + require.NoError(t, err) |
| 21 | + f, err := os.Create(path) |
| 22 | + require.NoError(t, err) |
| 23 | + f.Close() |
| 24 | +} |
| 25 | + |
| 26 | +func TestExpandGlobPathsInPipelines(t *testing.T) { |
| 27 | + dir := t.TempDir() |
| 28 | + |
| 29 | + touchEmptyFile(t, filepath.Join(dir, "test1.ipynb")) |
| 30 | + touchEmptyFile(t, filepath.Join(dir, "test/test2.ipynb")) |
| 31 | + touchEmptyFile(t, filepath.Join(dir, "test/test3.ipynb")) |
| 32 | + touchEmptyFile(t, filepath.Join(dir, "test1.jar")) |
| 33 | + touchEmptyFile(t, filepath.Join(dir, "test/test2.jar")) |
| 34 | + touchEmptyFile(t, filepath.Join(dir, "test/test3.jar")) |
| 35 | + touchEmptyFile(t, filepath.Join(dir, "test1.py")) |
| 36 | + touchEmptyFile(t, filepath.Join(dir, "test/test2.py")) |
| 37 | + touchEmptyFile(t, filepath.Join(dir, "test/test3.py")) |
| 38 | + |
| 39 | + b := &bundle.Bundle{ |
| 40 | + Config: config.Root{ |
| 41 | + Path: dir, |
| 42 | + Resources: config.Resources{ |
| 43 | + Pipelines: map[string]*resources.Pipeline{ |
| 44 | + "pipeline": { |
| 45 | + Paths: paths.Paths{ |
| 46 | + ConfigFilePath: filepath.Join(dir, "resource.yml"), |
| 47 | + }, |
| 48 | + PipelineSpec: &pipelines.PipelineSpec{ |
| 49 | + Libraries: []pipelines.PipelineLibrary{ |
| 50 | + { |
| 51 | + Notebook: &pipelines.NotebookLibrary{ |
| 52 | + Path: "./**/*.ipynb", |
| 53 | + }, |
| 54 | + }, |
| 55 | + { |
| 56 | + Jar: "./*.jar", |
| 57 | + }, |
| 58 | + { |
| 59 | + File: &pipelines.FileLibrary{ |
| 60 | + Path: "./**/*.py", |
| 61 | + }, |
| 62 | + }, |
| 63 | + { |
| 64 | + Maven: &compute.MavenLibrary{ |
| 65 | + Coordinates: "org.jsoup:jsoup:1.7.2", |
| 66 | + }, |
| 67 | + }, |
| 68 | + { |
| 69 | + Notebook: &pipelines.NotebookLibrary{ |
| 70 | + Path: "./test1.ipynb", |
| 71 | + }, |
| 72 | + }, |
| 73 | + { |
| 74 | + Notebook: &pipelines.NotebookLibrary{ |
| 75 | + Path: "/Workspace/Users/[email protected]/test.ipynb", |
| 76 | + }, |
| 77 | + }, |
| 78 | + { |
| 79 | + Notebook: &pipelines.NotebookLibrary{ |
| 80 | + Path: "dbfs:/[email protected]/test.ipynb", |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + } |
| 90 | + |
| 91 | + m := ExpandPipelineGlobPaths() |
| 92 | + err := bundle.Apply(context.Background(), b, m) |
| 93 | + require.NoError(t, err) |
| 94 | + |
| 95 | + libraries := b.Config.Resources.Pipelines["pipeline"].Libraries |
| 96 | + require.Len(t, libraries, 9) |
| 97 | + |
| 98 | + // Making sure glob patterns are expanded correctly |
| 99 | + require.True(t, containsNotebook(libraries, filepath.Join("test", "test2.ipynb"))) |
| 100 | + require.True(t, containsNotebook(libraries, filepath.Join("test", "test3.ipynb"))) |
| 101 | + require.True(t, containsFile(libraries, filepath.Join("test", "test2.py"))) |
| 102 | + require.True(t, containsFile(libraries, filepath.Join("test", "test3.py"))) |
| 103 | + |
| 104 | + // Making sure exact file references work as well |
| 105 | + require.True(t, containsNotebook(libraries, "test1.ipynb")) |
| 106 | + |
| 107 | + // Making sure absolute pass to remote FS file references work as well |
| 108 | + require. True( t, containsNotebook( libraries, "/Workspace/Users/[email protected]/test.ipynb")) |
| 109 | + require. True( t, containsNotebook( libraries, "dbfs:/[email protected]/test.ipynb")) |
| 110 | + |
| 111 | + // Making sure other libraries are not replaced |
| 112 | + require.True(t, containsJar(libraries, "./*.jar")) |
| 113 | + require.True(t, containsMaven(libraries, "org.jsoup:jsoup:1.7.2")) |
| 114 | +} |
| 115 | + |
| 116 | +func containsNotebook(libraries []pipelines.PipelineLibrary, path string) bool { |
| 117 | + for _, l := range libraries { |
| 118 | + if l.Notebook != nil && l.Notebook.Path == path { |
| 119 | + return true |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return false |
| 124 | +} |
| 125 | + |
| 126 | +func containsJar(libraries []pipelines.PipelineLibrary, path string) bool { |
| 127 | + for _, l := range libraries { |
| 128 | + if l.Jar == path { |
| 129 | + return true |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + return false |
| 134 | +} |
| 135 | + |
| 136 | +func containsMaven(libraries []pipelines.PipelineLibrary, coordinates string) bool { |
| 137 | + for _, l := range libraries { |
| 138 | + if l.Maven != nil && l.Maven.Coordinates == coordinates { |
| 139 | + return true |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + return false |
| 144 | +} |
| 145 | + |
| 146 | +func containsFile(libraries []pipelines.PipelineLibrary, path string) bool { |
| 147 | + for _, l := range libraries { |
| 148 | + if l.File != nil && l.File.Path == path { |
| 149 | + return true |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + return false |
| 154 | +} |
0 commit comments