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
117 changes: 111 additions & 6 deletions src/components/groups/LinkBucketDialog.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import * as VueRuntime from 'vue'
import { defineComponent, h, ref } from 'vue'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { compileClientComponent, content, element, flush, moduleDefault, mountApp, typeValue } from '@/test/clientRender'
import { click, compileClientComponent, content, element, flush, moduleDefault, mountApp, typeValue } from '@/test/clientRender'
import * as Api from '@/lib/api'
import * as BucketNaming from '@/lib/bucketNaming'
import * as LinkedFolders from '@/lib/linkedFolders'
import * as Storage from '@/lib/storage'
import * as StorageRules from '@/lib/storageRules'
import * as Utils from '@/lib/utils'
import type { RootsState } from '@/composables/useImportRoots'

const createBackendImport = vi.fn()
const folders = {
roots: ref<Api.ImportRoot[]>([]),
state: ref<RootsState>('ready'),
error: ref<string | null>(null),
load: vi.fn(),
}
const runBackendImport = vi.fn()
const Slot = defineComponent((_, { slots }) => () => h('div', slots.default?.()))
const ButtonStub = defineComponent({
Expand All @@ -22,10 +30,14 @@ const InputStub = defineComponent({
h('input', { ...attrs, value: props.modelValue, onInput: (event: { target: { value: string } }) => emit('update:modelValue', event.target.value) }),
})
const SelectStub = defineComponent({
props: { modelValue: { type: String, default: '' }, ariaLabel: String },
props: { modelValue: { type: String, default: '' }, ariaLabel: String, options: { type: Array, default: () => [] } },
emits: ['update:modelValue'],
setup: (props, { emit }) => () =>
h('select', { 'aria-label': props.ariaLabel, value: props.modelValue, onInput: (event: { target: { value: string } }) => emit('update:modelValue', event.target.value) }),
h(
'select',
{ 'aria-label': props.ariaLabel, value: props.modelValue, onInput: (event: { target: { value: string } }) => emit('update:modelValue', event.target.value) },
(props.options as { value: string; label: string }[]).map((option) => h('option', { value: option.value }, option.label)),
),
})

const Dialog = compileClientComponent(new URL('./LinkBucketDialog.vue', import.meta.url), {
Expand All @@ -42,28 +54,31 @@ const Dialog = compileClientComponent(new URL('./LinkBucketDialog.vue', import.m
'@/components/ui/Input.vue': moduleDefault(InputStub),
'@/components/ui/Notice.vue': moduleDefault(Slot),
'@/components/ui/Select.vue': moduleDefault(SelectStub),
'@/composables/useImportRoots': { useImportRoots: () => folders },
'@/composables/useAruna': { useAruna: () => ({ apiBaseUrl: ref('https://api'), authToken: ref('t'), sessionEpoch: ref(0) }) },
'@/lib/connectivity': { OFFLINE_WRITE_HINT: 'offline', useConnectivity: () => ({ writesDisabled: ref(false) }) },
'@/lib/api': { ...Api, createBackendImport, runBackendImport },
'@/lib/bucketNaming': BucketNaming,
'@/lib/linkedFolders': LinkedFolders,
'@/lib/storage': Storage,
'@/lib/storageRules': StorageRules,
'@/lib/utils': Utils,
})

const SPIRE = { backend_id: 's1', group_id: 'g1', kind: 's3', name: 'spire', public_config: { endpoint: 'https://s3' } }
const SPIRE: Api.GroupBackendResponse = { backend_id: 's1', group_id: 'g1', kind: 's3', name: 'spire', public_config: { endpoint: 'https://s3' } }
const LINKED = { import_id: 'i1', backend_id: 's1', backend_prefix: 'lab-old/', bucket: 'old', key_prefix: '', created_at: '' }
type Root = Parameters<typeof content>[0]
const byId = (root: Root, suffix: string) =>
element(root, (node) => node.tag === 'input' && String(node.props.id ?? '').endsWith(suffix))
const byLabel = (root: Root, label: string) => element(root, (node) => node.props['aria-label'] === label)
const save = (root: Root) => element(root, (node) => node.tag === 'button' && node.props.type === 'submit')

async function mount() {
async function mount(backend: Api.GroupBackendResponse = SPIRE, backends: Api.GroupBackendResponse[] = [SPIRE]) {
const open = ref(false)
const events: unknown[] = []
const mounted = await mountApp(
defineComponent(() => () =>
h(Dialog, { open: open.value, groupId: 'g1', backend: SPIRE, links: [LINKED], onSaved: (error?: string) => events.push(error) }),
h(Dialog, { open: open.value, groupId: 'g1', backend, backends, links: [LINKED], onSaved: (error?: string) => events.push(error) }),
),
)
open.value = true
Expand All @@ -80,6 +95,10 @@ async function submit(root: Root) {
beforeEach(() => {
createBackendImport.mockReset().mockResolvedValue({ import_id: 'i2' })
runBackendImport.mockReset().mockResolvedValue({ job_id: 'j' })
folders.roots.value = []
folders.state.value = 'ready'
folders.error.value = null
folders.load.mockClear()
})

describe('LinkBucketDialog', () => {
Expand Down Expand Up @@ -114,4 +133,90 @@ describe('LinkBucketDialog', () => {
await submit(root)
expect(String(events[0])).toContain('Linked, but the first sync did not start')
})

describe('local folder', () => {
const folder = (name: string, root: string): Api.GroupBackendResponse => ({
backend_id: name,
group_id: 'g1',
kind: 'fs',
name,
public_config: { root },
})
const readOnly = folder('raw', 'projects')
const others = [
readOnly,
SPIRE,
{ ...SPIRE, backend_id: 's2', name: 'removed-s3', disabled: true },
folder('frozen', 'archive'),
folder('open-folder', 'scratch'),
folder('unlisted', 'gone'),
]
const approved = [
{ name: 'projects', writable: false },
{ name: 'archive', writable: false },
{ name: 'scratch', writable: true },
]

it('links a writable folder without asking where to write', async () => {
folders.roots.value = approved
const { root } = await mount(folder('open-folder', 'scratch'), others)
expect(content(root)).toContain('Link a folder')
expect(() => byLabel(root, 'Write new uploads to')).toThrow()
await typeValue(byId(root, '-aruna'), 'runs')
await submit(root)
expect(createBackendImport.mock.calls[0][1]).not.toHaveProperty('write_backend_id')
})

it('needs a writable storage for a read-only folder and offers only those', async () => {
folders.roots.value = approved
const { root } = await mount(readOnly, others)
await typeValue(byId(root, '-aruna'), 'runs')
expect(content(root)).toContain('This folder is read-only')
expect(save(root).props.disabled).toBe(true)
const options = content(byLabel(root, 'Write new uploads to'))
expect(options).toContain('spire')
expect(options).toContain('open-folder')
for (const refused of ['removed-s3', 'frozen', 'unlisted', 'raw']) expect(options).not.toContain(refused)
await typeValue(byLabel(root, 'Write new uploads to'), 's1')
expect(save(root).props.disabled).toBe(false)
await submit(root)
expect(createBackendImport.mock.calls[0][1]).toMatchObject({ backend_id: 'raw', write_backend_id: 's1' })
})

it('says so when no other storage can take the writes', async () => {
folders.roots.value = approved
const { root } = await mount(readOnly, [readOnly])
await typeValue(byId(root, '-aruna'), 'runs')
expect(content(root)).toContain('Add another storage that takes them first.')
expect(save(root).props.disabled).toBe(true)
})

it('waits for the approved folders and reports a failed read', async () => {
folders.state.value = 'loading'
const { root } = await mount(readOnly, others)
await typeValue(byId(root, '-aruna'), 'runs')
expect(content(root)).toContain('Loading approved folders…')
expect(save(root).props.disabled).toBe(true)
folders.state.value = 'failed'
folders.error.value = 'node down'
await flush()
expect(content(root)).toContain('node down')
expect(save(root).props.disabled).toBe(true)
await click(element(root, (node) => node.tag === 'button' && content(node) === 'Try again'))
expect(folders.load).toHaveBeenCalledTimes(1)
})

it('refuses a folder that is no longer approved or has odd names', async () => {
folders.roots.value = approved
const { root } = await mount(folder('unlisted', 'gone'), others)
await typeValue(byId(root, '-aruna'), 'runs')
expect(content(root)).toContain('The node operator no longer approves this folder.')
expect(save(root).props.disabled).toBe(true)
const open = await mount(folder('open-folder', 'scratch'), others)
await typeValue(byId(open.root, '-aruna'), 'runs')
await typeValue(byId(open.root, '-folder'), 'a/../b')
expect(content(open.root)).toContain('Use a relative folder')
expect(save(open.root).props.disabled).toBe(true)
})
})
})
53 changes: 50 additions & 3 deletions src/components/groups/LinkBucketDialog.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
// Link an S3 bucket of a storage to an Aruna bucket, then run its first sync.
// Link an S3 bucket or local folder of a storage to an Aruna bucket, then run its first sync.
import { computed, ref, useId, watch } from 'vue'
import Button from '@/components/ui/Button.vue'
import Dialog from '@/components/ui/Dialog.vue'
Expand All @@ -14,17 +14,21 @@ import Input from '@/components/ui/Input.vue'
import Notice from '@/components/ui/Notice.vue'
import Select from '@/components/ui/Select.vue'
import { useAruna } from '@/composables/useAruna'
import { useImportRoots } from '@/composables/useImportRoots'
import { OFFLINE_WRITE_HINT, useConnectivity } from '@/lib/connectivity'
import { ApiError, createBackendImport, runBackendImport, type BackendImport, type GroupBackendResponse } from '@/lib/api'
import { NAMING_OPTIONS, namingPattern, type NamingChoice } from '@/lib/bucketNaming'
import { folderPath } from '@/lib/linkedFolders'
import { folderProblem } from '@/lib/storage'
import { arunaConflict, bucketProblem, linkConflict, splitsBuckets } from '@/lib/storageRules'
import { errorMessage } from '@/lib/utils'

const props = defineProps<{
open: boolean
groupId: string
backend: GroupBackendResponse | null
/** All storages of the group, to pick where a read-only folder writes. */
backends: GroupBackendResponse[]
links: BackendImport[]
}>()
const emit = defineEmits<{ (e: 'update:open', value: boolean): void; (e: 'saved', runError?: string): void }>()
Expand All @@ -39,16 +43,43 @@ const s3Bucket = ref('')
const folder = ref('')
const arunaBucket = ref('')
const naming = ref<NamingChoice>('plain')
const writer = ref('')
const saving = ref(false)
const submitError = ref<string | null>(null)

const folderMode = computed(() => props.backend?.kind === 'fs')
const { roots, state: rootsState, error: rootsError, load: reloadRoots } = useImportRoots(
computed(() => props.groupId),
computed(() => props.open && folderMode.value),
)
const rootInfo = computed(() => roots.value.find((root) => root.name === props.backend?.public_config.root))
// A read-only folder cannot take new uploads, so another storage must.
const needsWriter = computed(() => folderMode.value && rootsState.value === 'ready' && rootInfo.value?.writable !== true)
const writable = (other: GroupBackendResponse) =>
other.kind !== 'fs' || roots.value.some((root) => root.name === other.public_config.root && root.writable)
const writers = computed(() =>
props.backends
.filter((other) => other.backend_id !== props.backend?.backend_id && !other.disabled && writable(other))
.map((other) => ({ value: other.backend_id, label: other.name })),
)
const rootProblem = computed(() => {
if (!folderMode.value || rootsState.value !== 'ready') return null
return rootInfo.value ? null : 'The node operator no longer approves this folder.'
})

const splits = computed(() => Boolean(props.backend && splitsBuckets(props.backend)))
const storagePath = computed(() => (splits.value ? `${s3Bucket.value.trim()}/${folderPath(folder.value)}` : folderPath(folder.value)))
const bucket = computed(() => arunaBucket.value.trim() || (splits.value ? s3Bucket.value.trim() : ''))
const ready = computed(() => Boolean(bucket.value && (!splits.value || s3Bucket.value.trim())))
const ready = computed(
() =>
Boolean(bucket.value && (!splits.value || s3Bucket.value.trim())) &&
(!folderMode.value || (rootsState.value === 'ready' && !rootProblem.value)) &&
(!needsWriter.value || writers.value.some((option) => option.value === writer.value)),
)
const problem = computed(() => {
if (!props.backend || !ready.value) return null
return (
(folderMode.value ? folderProblem(storagePath.value) : null) ??
bucketProblem(bucket.value) ??
arunaConflict(props.links, bucket.value, '') ??
linkConflict(props.backend, props.links, storagePath.value, null, true)
Expand All @@ -62,6 +93,7 @@ watch(
s3Bucket.value = ''
folder.value = ''
arunaBucket.value = ''
writer.value = ''
naming.value = 'plain'
submitError.value = null
},
Expand All @@ -86,6 +118,7 @@ async function submit() {
backend_prefix: storagePath.value,
bucket: bucket.value,
key_prefix: '',
...(needsWriter.value ? { write_backend_id: writer.value } : {}),
write_pattern: namingPattern(naming.value, ''),
},
client,
Expand All @@ -112,7 +145,7 @@ async function submit() {
<Dialog :open="open" @update:open="(value: boolean) => emit('update:open', value)">
<DialogContent class="max-w-xl">
<DialogHeader>
<DialogTitle>Link an S3 bucket</DialogTitle>
<DialogTitle>{{ folderMode ? 'Link a folder' : 'Link an S3 bucket' }}</DialogTitle>
<DialogDescription>
Files already in it show up in Aruna. Nothing is copied.
<StorageFeatureLink question="elsewhere" class="ml-1" />
Expand Down Expand Up @@ -142,6 +175,20 @@ async function submit() {
<Select v-model="naming" :options="NAMES" aria-label="New uploads are named" class="mt-1" />
</div>
</div>
<p v-if="rootsState === 'loading'" class="text-xs text-muted-foreground">Loading approved folders…</p>
<Notice v-else-if="rootsState === 'failed'" tone="error">
{{ rootsError }}
<button type="button" class="ml-1 underline" @click="reloadRoots">Try again</button>
</Notice>
<p v-if="rootProblem" class="text-xs text-destructive">{{ rootProblem }}</p>
<div v-if="needsWriter && rootInfo">
<label class="text-xs font-medium">Write new uploads to</label>
<Select v-model="writer" :options="writers" placeholder="Pick a storage" aria-label="Write new uploads to" class="mt-1" />
<p class="mt-1 text-[11px] text-muted-foreground">
This folder is read-only, so Aruna cannot write new files into it.
{{ writers.length ? 'Pick another storage that takes them.' : 'Add another storage that takes them first.' }}
</p>
</div>
<p v-if="problem" class="text-xs text-destructive">{{ problem }}</p>
<p v-else-if="ready" class="text-xs text-muted-foreground">
After linking: <span class="font-mono text-foreground">{{ storagePath || '/' }}</span> ⇆ Aruna bucket
Expand Down
20 changes: 16 additions & 4 deletions src/components/groups/NewBucketsDialog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ const InputStub = defineComponent({
h('input', { ...attrs, value: props.modelValue, onInput: (event: { target: { value: string } }) => emit('update:modelValue', event.target.value) }),
})
const SelectStub = defineComponent({
props: { modelValue: { type: String, default: '' }, ariaLabel: String },
props: { modelValue: { type: String, default: '' }, ariaLabel: String, options: { type: Array, default: () => [] } },
emits: ['update:modelValue'],
setup: (props, { emit }) => () =>
h('select', { 'aria-label': props.ariaLabel, value: props.modelValue, onInput: (event: { target: { value: string } }) => emit('update:modelValue', event.target.value) }),
h(
'select',
{ 'aria-label': props.ariaLabel, value: props.modelValue, onInput: (event: { target: { value: string } }) => emit('update:modelValue', event.target.value) },
(props.options as { value: string; label: string }[]).map((option) => h('option', { value: option.value }, option.label)),
),
})

const Dialog = compileClientComponent(new URL('./NewBucketsDialog.vue', import.meta.url), {
Expand Down Expand Up @@ -50,11 +54,11 @@ type Root = Parameters<typeof content>[0]
const field = (root: Root, label: string) => element(root, (node) => node.props['aria-label'] === label)
const save = (root: Root) => element(root, (node) => node.tag === 'button' && node.props.type === 'submit')

async function mount(routing: Record<string, unknown>) {
async function mount(routing: Record<string, unknown>, backends: unknown[] = [SPIRE]) {
const open = ref(false)
const mounted = await mountApp(
defineComponent(() => () =>
h(Dialog, { open: open.value, groupId: 'g1', groupName: 'spire', backends: [SPIRE], routing: { group_id: 'g1', warnings: [], ...routing }, links: [] }),
h(Dialog, { open: open.value, groupId: 'g1', groupName: 'spire', backends, routing: { group_id: 'g1', warnings: [], ...routing }, links: [] }),
),
)
open.value = true
Expand Down Expand Up @@ -102,4 +106,12 @@ describe('NewBucketsDialog', () => {
await submit(root)
expect(putGroupRouting).toHaveBeenCalledWith('g1', null)
})

it('does not offer a local folder as the storage for new buckets', async () => {
const folder = { backend_id: 'f1', group_id: 'g1', kind: 'fs', name: 'raw-folder', public_config: { root: 'projects' } }
const { root } = await mount({}, [SPIRE, folder])
const options = content(field(root, 'Storage'))
expect(options).toContain('spire')
expect(options).not.toContain('raw-folder')
})
})
4 changes: 2 additions & 2 deletions src/components/groups/NewBucketsDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { useAruna } from '@/composables/useAruna'
import { OFFLINE_WRITE_HINT, useConnectivity } from '@/lib/connectivity'
import type { BackendImport, GroupBackendResponse, GroupRoutingResponse } from '@/lib/api'
import { fillTemplate, templateProblem } from '@/lib/bucketNaming'
import { defaultBucket, defaultConflict, splitsBuckets, suggestBucket } from '@/lib/storageRules'
import { canRoute, defaultBucket, defaultConflict, splitsBuckets, suggestBucket } from '@/lib/storageRules'
import { errorMessage } from '@/lib/utils'

const props = defineProps<{
Expand Down Expand Up @@ -46,7 +46,7 @@ const ARUNA = 'aruna'
const storeOptions = computed(() => [
{ value: ARUNA, label: 'Aruna storage' },
...props.backends
.filter((backend) => !backend.disabled || backend.backend_id === store.value)
.filter((backend) => canRoute(backend) && (!backend.disabled || backend.backend_id === store.value))
.map((backend) => ({ value: backend.backend_id, label: backend.name })),
])
const backend = computed(() => props.backends.find((entry) => entry.backend_id === store.value) ?? null)
Expand Down
Loading
Loading