Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"pages":[{"name":"imported","displayName":"Imported Page"}]}
50 changes: 50 additions & 0 deletions acceptance/cmd/workspace/export-dir-dashboard/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,53 @@ Exporting files from /Workspace/Users/[USERNAME]/export-dir-dashboard-[UNIQUE_NA
Export complete

>>> assert_exists.py [TEST_TMP_DIR]/export/export-dir-dashboard-[UNIQUE_NAME].lvdash.json

>>> echo /Users/[USERNAME]/export-dir-dashboard-[UNIQUE_NAME]/export-dir-dashboard-[UNIQUE_NAME].lvdash.json
/Users/[USERNAME]/export-dir-dashboard-[UNIQUE_NAME]/export-dir-dashboard-[UNIQUE_NAME].lvdash.json

=== Overwrite the dashboard with new content through the workspace import API, at the path Lakeview reports

>>> [CLI] workspace import /Users/[USERNAME]/export-dir-dashboard-[UNIQUE_NAME]/export-dir-dashboard-[UNIQUE_NAME].lvdash.json --file imported.lvdash.json --format AUTO --overwrite

=== The workspace object still names the same dashboard, under both spellings of its path

>>> [CLI] workspace get-status /Users/[USERNAME]/export-dir-dashboard-[UNIQUE_NAME]/export-dir-dashboard-[UNIQUE_NAME].lvdash.json
{
"object_type": "DASHBOARD",
"resource_id": "[DASHBOARD_ID]"
}

>>> [CLI] workspace get-status /Workspace/Users/[USERNAME]/export-dir-dashboard-[UNIQUE_NAME]/export-dir-dashboard-[UNIQUE_NAME].lvdash.json
{
"object_type": "DASHBOARD",
"resource_id": "[DASHBOARD_ID]"
}

=== The dashboard has the imported content

>>> jq .serialized_dashboard | fromjson | [.pages[].name] after.json
[
"imported"
]

=== Compared with the create response, the import changed only etag and update_time

>>> jq -n --slurpfile b created.json --slurpfile a after.json $b[0] as $b | $a[0] as $a | {
updated: ([$a | keys[] | . as $k | select(($b | has($k)) and $b[$k] != $a[$k])] - ["serialized_dashboard"]),
inserted: [$a | keys[] | . as $k | select($b | has($k) | not)],
deleted: ([$b | keys[] | . as $k | select($a | has($k) | not)] - ["warehouse_id"])
}
{
"updated": [
"etag",
"update_time"
],
"inserted": [],
"deleted": []
}

=== Creating a dashboard with the same name in the same folder is a conflict

>>> contains.py 409

>>> contains.py ALREADY_EXISTS
40 changes: 38 additions & 2 deletions acceptance/cmd/workspace/export-dir-dashboard/script
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ trap cleanup EXIT

trace $CLI workspace mkdirs "$DASHBOARD_DIR"

dashboard_id=$($CLI lakeview create \
$CLI lakeview create \
--display-name "$DASHBOARD_NAME" \
--warehouse-id "$TEST_DEFAULT_WAREHOUSE_ID" \
--serialized-dashboard "$(cat dashboard.lvdash.json)" \
--json "{\"parent_path\": \"$DASHBOARD_DIR\"}" \
-o json | jq -r '.dashboard_id')
-o json > created.json
dashboard_id=$(jq -r '.dashboard_id' created.json)
add_repl "$dashboard_id" DASHBOARD_ID

mkdir -p "$TEST_TMP_DIR/export"
Expand All @@ -27,3 +28,38 @@ trace $CLI workspace export-dir "$DASHBOARD_DIR" "$TEST_TMP_DIR/export"
# drops an empty "layout": []), so the exported content is not byte-identical to
# the input. Assert the dashboard was exported rather than diffing the content.
trace assert_exists.py "$TEST_TMP_DIR/export/$DASHBOARD_NAME.lvdash.json"

# Importing a .lvdash.json over a dashboard's path through the workspace import API updates
# that dashboard in place: bundle generate --existing-path and the direct engine's create
# recovery look a dashboard up by its file path, so the file must keep naming the same dashboard.
# Lakeview reports the path without the /Workspace prefix; both spellings name the same object.
DASHBOARD_PATH="$DASHBOARD_DIR/$DASHBOARD_NAME.lvdash.json"
LAKEVIEW_PATH=$(jq -r .path created.json)
trace echo "$LAKEVIEW_PATH"

title "Overwrite the dashboard with new content through the workspace import API, at the path Lakeview reports"
trace $CLI workspace import "$LAKEVIEW_PATH" --file imported.lvdash.json --format AUTO --overwrite

title "The workspace object still names the same dashboard, under both spellings of its path"
trace $CLI workspace get-status "$LAKEVIEW_PATH" | jq '{object_type, resource_id}'
trace $CLI workspace get-status "$DASHBOARD_PATH" | jq '{object_type, resource_id}'

title "The dashboard has the imported content"
# Wait for the import to be visible: the etag changes on every write.
retry --until-not "$(jq -r .etag created.json)" $CLI lakeview get "$dashboard_id" > after.json
Comment thread
denik marked this conversation as resolved.
Resolved
trace jq '.serialized_dashboard | fromjson | [.pages[].name]' after.json

title "Compared with the create response, the import changed only etag and update_time"
# serialized_dashboard changed on purpose; the backend drops warehouse_id on some versions.
trace jq -n --slurpfile b created.json --slurpfile a after.json '$b[0] as $b | $a[0] as $a | {
updated: ([$a | keys[] | . as $k | select(($b | has($k)) and $b[$k] != $a[$k])] - ["serialized_dashboard"]),
inserted: [$a | keys[] | . as $k | select($b | has($k) | not)],
deleted: ([$b | keys[] | . as $k | select($a | has($k) | not)] - ["warehouse_id"])
}'

title "Creating a dashboard with the same name in the same folder is a conflict"
# Create recovery treats ALREADY_EXISTS and RESOURCE_ALREADY_EXISTS alike; the backend returns
# either, with wording that varies by version, so only the final status and the code are checked.
musterr $CLI lakeview create --display-name "$DASHBOARD_NAME" --serialized-dashboard "$(cat imported.lvdash.json)" --json "{\"parent_path\": \"$DASHBOARD_DIR\"}" --debug 2> LOG.conflict
grep '< HTTP/' LOG.conflict | tail -n 1 | trace contains.py ' 409' > /dev/null
trace contains.py 'ALREADY_EXISTS' < LOG.conflict > /dev/null
7 changes: 7 additions & 0 deletions acceptance/cmd/workspace/export-dir-dashboard/test.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ Cloud = true
# so cover it on Linux/macOS only.
GOOS.windows = false

# Lakeview reads after a write can stay stale on cloud for longer than retry.py's ~2s
# default (5 attempts x 500ms), as in acceptance/bundle/resources/dashboards.
Env.RETRY_MAX_ATTEMPTS = "20"
Env.RETRY_INTERVAL_MS = "1000"

Ignore = [
"export",
"created.json",
"after.json",
]
120 changes: 0 additions & 120 deletions integration/assumptions/dashboard_assumptions_test.go

This file was deleted.

84 changes: 73 additions & 11 deletions libs/testserver/dashboards.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,18 +208,9 @@ func (s *FakeWorkspace) DashboardUpdate(req Request) Response {
}
}
updated := *dashboard

// Bump etag on every write, matching cloud behavior.
prevEtag, err := strconv.Atoi(updated.Etag)
if err != nil {
return Response{
Body: map[string]string{
"message": "Invalid etag: " + updated.Etag,
},
StatusCode: 400,
}
if resp, ok := updated.bumpEtag(); !ok {
return resp
}
updated.Etag = strconv.Itoa(prevEtag + 1)

if updateReq.SerializedDashboard != updated.InputSerializedDashboard {
updated.InputSerializedDashboard = updateReq.SerializedDashboard
Expand Down Expand Up @@ -378,3 +369,74 @@ func (s *FakeWorkspace) moveDashboardFile(oldPath, newPath, dashboardId string)
s.files[entry.Info.Path] = entry
}
}

// dashboardEntry finds the workspace object of a dashboard by the path Lakeview returns for it
// (Dashboard.Path, without the /Workspace prefix), which the real Workspace API accepts as well.
// Only dashboards are resolved this way. The caller must hold the lock.
func (s *FakeWorkspace) dashboardEntry(filePath string) (FileEntry, bool) {
if strings.HasPrefix(filePath, "/Workspace/") {
return FileEntry{}, false
}
entry, ok := s.files[path.Join("/Workspace", filePath)]
if !ok || entry.Info.ObjectType != workspace.ObjectTypeDashboard {
return FileEntry{}, false
}
return entry, true
}

// bumpEtag advances the etag, as the cloud does on every write. ok is false, with the response to
// return, when the stored etag is not a number.
func (d *fakeDashboard) bumpEtag() (Response, bool) {
prevEtag, err := strconv.Atoi(d.Etag)
if err != nil {
return Response{
Body: map[string]string{
"message": "Invalid etag: " + d.Etag,
},
StatusCode: 400,
}, false
}
d.Etag = strconv.Itoa(prevEtag + 1)
return Response{}, true
}

// WorkspaceImportOverDashboard serves an AUTO-format /workspace/import with overwrite onto the
// .lvdash.json path of an existing dashboard. The real API updates that dashboard in place: the
// path keeps the dashboard's identity (get-status resource_id), and the import sets its
// serialized_dashboard, etag and update_time. ok is false when the request is not such an import.
func (s *FakeWorkspace) WorkspaceImportOverDashboard(filePath string, body []byte, overwrite bool) (Response, bool) {
if !overwrite {
return Response{}, false
}
filePath = path.Clean("/" + filePath)

defer s.LockUnlock()()

entry, ok := s.files[filePath]
if !ok {
entry, ok = s.dashboardEntry(filePath)
filePath = entry.Info.Path
}
if !ok || entry.Info.ObjectType != workspace.ObjectTypeDashboard {
return Response{}, false
}
dashboard, ok := s.Dashboards.ReadStrong(entry.Info.ResourceId)
if !ok {
return Response{
StatusCode: http.StatusInternalServerError,
Body: fmt.Sprintf("internal error: no dashboard %s for workspace object %s", entry.Info.ResourceId, filePath),
}, true
}
updated := *dashboard
if resp, ok := updated.bumpEtag(); !ok {
return resp, true
}
updated.InputSerializedDashboard = string(body)
updated.SerializedDashboard = transformSerializedDashboard(string(body), "", "")
updated.UpdateTime = nextTimestamp()
s.Dashboards.Write(updated.DashboardId, &updated)

entry.Data = []byte(updated.SerializedDashboard)
s.files[filePath] = entry
return Response{}, true
}
2 changes: 2 additions & 0 deletions libs/testserver/fake_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,8 @@ func (s *FakeWorkspace) WorkspaceGetStatus(requestPath string, returnGitInfo boo
info = dirInfo
} else if entry, ok := s.files[cleaned]; ok {
info = entry.Info
} else if entry, ok := s.dashboardEntry(cleaned); ok {
info = entry.Info
} else {
// Match the real Workspace API wording, which echoes the requested path.
return Response{
Expand Down
3 changes: 3 additions & 0 deletions libs/testserver/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ func workspaceImport(w *FakeWorkspace, form ImportForm) Response {
if strings.HasSuffix(strings.ToLower(form.Path), ".zip") {
return w.WorkspaceImportZipFolder(form.Path, form.Content, form.Overwrite)
}
if resp, ok := w.WorkspaceImportOverDashboard(form.Path, form.Content, form.Overwrite); ok {
return resp
}
if unsupported, ok := autoImportUnsupported(form.Path); ok {
return unsupported
}
Expand Down
Loading