Skip to content

Commit 85a4d53

Browse files
committed
,,
1 parent 9dd2795 commit 85a4d53

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

cmd/labs/project/installer_test.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,47 @@ func respondWithJSON(t *testing.T, w http.ResponseWriter, v any) {
121121
w.Write(raw)
122122
}
123123

124+
type fileTree struct {
125+
Path string
126+
MaxDepth int
127+
}
128+
129+
func (ft fileTree) String() string {
130+
lines := ft.listFiles(ft.Path, ft.MaxDepth)
131+
return strings.Join(lines, "\n")
132+
}
133+
134+
func (ft fileTree) listFiles(dir string, depth int) (lines []string) {
135+
if ft.MaxDepth > 0 && depth > ft.MaxDepth {
136+
return []string{fmt.Sprintf("deeper than %d levels", ft.MaxDepth)}
137+
}
138+
fileInfo, err := os.ReadDir(dir)
139+
if err != nil {
140+
return []string{err.Error()}
141+
}
142+
for _, entry := range fileInfo {
143+
lines = append(lines, fmt.Sprintf("%s%s", ft.getIndent(depth), entry.Name()))
144+
if entry.IsDir() {
145+
subdir := filepath.Join(dir, entry.Name())
146+
lines = append(lines, ft.listFiles(subdir, depth+1)...)
147+
}
148+
}
149+
return lines
150+
}
151+
152+
func (ft fileTree) getIndent(depth int) string {
153+
return "│" + strings.Repeat(" ", depth*2) + "├─ "
154+
}
155+
124156
func TestInstallerWorksForReleases(t *testing.T) {
157+
defer func() {
158+
if !t.Failed() {
159+
return
160+
}
161+
t.Logf("file tree:\n%s", fileTree{
162+
Path: filepath.Dir(t.TempDir()),
163+
})
164+
}()
125165
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
126166
if r.URL.Path == "/databrickslabs/blueprint/v0.3.15/labs.yml" {
127167
raw, err := os.ReadFile("testdata/installed-in-home/.databricks/labs/blueprint/lib/labs.yml")
@@ -192,6 +232,14 @@ func TestInstallerWorksForReleases(t *testing.T) {
192232
}
193233

194234
func TestInstallerWorksForDevelopment(t *testing.T) {
235+
defer func() {
236+
if !t.Failed() {
237+
return
238+
}
239+
t.Logf("file tree:\n%s", fileTree{
240+
Path: filepath.Dir(t.TempDir()),
241+
})
242+
}()
195243
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
196244
if r.URL.Path == "/api/2.0/clusters/list" {
197245
respondWithJSON(t, w, compute.ListClustersResponse{
@@ -302,3 +350,65 @@ account_id = abc
302350
r.RunBackground()
303351
r.WaitForTextPrinted("setting up important infrastructure", 5*time.Second)
304352
}
353+
354+
func TestUpgraderWorksForReleases(t *testing.T) {
355+
defer func() {
356+
if !t.Failed() {
357+
return
358+
}
359+
t.Logf("file tree:\n%s", fileTree{
360+
Path: filepath.Dir(t.TempDir()),
361+
})
362+
}()
363+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
364+
if r.URL.Path == "/databrickslabs/blueprint/v0.4.0/labs.yml" {
365+
raw, err := os.ReadFile("testdata/installed-in-home/.databricks/labs/blueprint/lib/labs.yml")
366+
if err != nil {
367+
panic(err)
368+
}
369+
w.Write(raw)
370+
return
371+
}
372+
if r.URL.Path == "/repos/databrickslabs/blueprint/zipball/v0.4.0" {
373+
raw, err := zipballFromFolder("testdata/installed-in-home/.databricks/labs/blueprint/lib")
374+
if err != nil {
375+
panic(err)
376+
}
377+
w.Header().Add("Content-Type", "application/octet-stream")
378+
w.Write(raw)
379+
return
380+
}
381+
if r.URL.Path == "/api/2.0/clusters/get" {
382+
respondWithJSON(t, w, &compute.ClusterDetails{
383+
State: compute.StateRunning,
384+
})
385+
return
386+
}
387+
t.Logf("Requested: %s", r.URL.Path)
388+
t.FailNow()
389+
}))
390+
defer server.Close()
391+
392+
ctx := installerContext(t, server)
393+
394+
newHome := copyTestdata(t, "testdata/installed-in-home")
395+
ctx = env.WithUserHomeDir(ctx, newHome)
396+
397+
py, _ := python.DetectExecutable(ctx)
398+
py, _ = filepath.Abs(py)
399+
ctx = env.Set(ctx, "PYTHON_BIN", py)
400+
401+
cachePath := project.PathInLabs(ctx, "blueprint", "cache")
402+
bs := []byte(`{"refreshed_at": "2033-01-01T00:00:00.92857+02:00","data": [{"tag_name": "v0.4.0"}]}`)
403+
err := os.WriteFile(filepath.Join(cachePath, "databrickslabs-blueprint-releases.json"), bs, ownerRW)
404+
require.NoError(t, err)
405+
406+
// simulate the case of GitHub Actions
407+
ctx = env.Set(ctx, "DATABRICKS_HOST", server.URL)
408+
ctx = env.Set(ctx, "DATABRICKS_TOKEN", "...")
409+
ctx = env.Set(ctx, "DATABRICKS_CLUSTER_ID", "installer-cluster")
410+
ctx = env.Set(ctx, "DATABRICKS_WAREHOUSE_ID", "installer-warehouse")
411+
412+
r := internal.NewCobraTestRunnerWithContext(t, ctx, "labs", "upgrade", "blueprint")
413+
r.RunAndExpectOutput("setting up important infrastructure")
414+
}

0 commit comments

Comments
 (0)