|
| 1 | +// Copyright 2022 The Cockroach Authors. |
| 2 | +// |
| 3 | +// Use of this software is governed by the CockroachDB Software License |
| 4 | +// included in the /LICENSE file. |
| 5 | + |
| 6 | +package backuputils |
| 7 | + |
| 8 | +import ( |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +func TestAbsoluteBackupPathInCollectionURI(t *testing.T) { |
| 15 | + testcases := []struct { |
| 16 | + name string |
| 17 | + collectionURI, backupURI string |
| 18 | + expected string |
| 19 | + error string |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "collection URI rooted at /", |
| 23 | + collectionURI: "nodelocal://1/", |
| 24 | + backupURI: "nodelocal://1/foo/bar/baz", |
| 25 | + expected: "/foo/bar/baz", |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "backup URI contains trailing slash", |
| 29 | + collectionURI: "nodelocal://1/", |
| 30 | + backupURI: "nodelocal://1/foo/bar/baz/", |
| 31 | + expected: "/foo/bar/baz", |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "collection URI contains non-empty path", |
| 35 | + collectionURI: "s3://backup/foo/bar", |
| 36 | + backupURI: "s3://backup/foo/bar/baz", |
| 37 | + expected: "/baz", |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "collection and backup URI ends in trailing slash", |
| 41 | + collectionURI: "external://backup/foo/bar/", |
| 42 | + backupURI: "external://backup/foo/bar/baz/qux/", |
| 43 | + expected: "/baz/qux", |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "backupURI is the same as collection URI", |
| 47 | + collectionURI: "gs://backup/foo/bar", |
| 48 | + backupURI: "gs://backup/foo/bar", |
| 49 | + expected: "/", |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "backup URI not in collection URI", |
| 53 | + collectionURI: "gs://backup/foo/bar", |
| 54 | + backupURI: "gs://backup/baz", |
| 55 | + error: "backup URI not contained within collection URI", |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "backup URI does not share same scheme or host as collection URI", |
| 59 | + collectionURI: "nodelocal://1/foo/bar", |
| 60 | + backupURI: "gs://backup/foo/bar/baz", |
| 61 | + error: "backup URI does not share the same scheme and host as collection URI", |
| 62 | + }, |
| 63 | + } |
| 64 | + |
| 65 | + for _, tc := range testcases { |
| 66 | + t.Run(tc.name, func(t *testing.T) { |
| 67 | + result, err := AbsoluteBackupPathInCollectionURI(tc.collectionURI, tc.backupURI) |
| 68 | + if tc.error != "" { |
| 69 | + require.Error(t, err) |
| 70 | + require.ErrorContains(t, err, tc.error) |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + require.NoError(t, err) |
| 75 | + require.Equal(t, tc.expected, result) |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func TestNormalizeSubdir(t *testing.T) { |
| 81 | + testcases := []struct { |
| 82 | + name string |
| 83 | + input string |
| 84 | + normalized string |
| 85 | + error string |
| 86 | + }{ |
| 87 | + { |
| 88 | + name: "subdir without leading slash", |
| 89 | + input: "2025/08/26-112233.44", |
| 90 | + normalized: "/2025/08/26-112233.44", |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "subdir with leading and trailing slashes", |
| 94 | + input: "/2025/08/26-112233.44/", |
| 95 | + normalized: "/2025/08/26-112233.44", |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "subdir with only trailing slash", |
| 99 | + input: "2025/08/26-112233.44/", |
| 100 | + normalized: "/2025/08/26-112233.44", |
| 101 | + }, |
| 102 | + { |
| 103 | + name: "subdir already normalized", |
| 104 | + input: "/2025/08/26-112233.44", |
| 105 | + normalized: "/2025/08/26-112233.44", |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "invalid subdir format", |
| 109 | + input: "2023-10-05_123456.78", |
| 110 | + error: "does not match expected format YYYY/MM/DD-HHMMSS.SS", |
| 111 | + }, |
| 112 | + } |
| 113 | + |
| 114 | + for _, tc := range testcases { |
| 115 | + t.Run(tc.name, func(t *testing.T) { |
| 116 | + result, err := NormalizeSubdir(tc.input) |
| 117 | + if tc.error != "" { |
| 118 | + require.Error(t, err) |
| 119 | + require.ErrorContains(t, err, tc.error) |
| 120 | + return |
| 121 | + } |
| 122 | + |
| 123 | + require.NoError(t, err) |
| 124 | + require.Equal(t, tc.normalized, result) |
| 125 | + }) |
| 126 | + } |
| 127 | +} |
0 commit comments