summaryrefslogtreecommitdiff
path: root/test/end-to-end/cli/cas-resolve-special.sh
blob: a97d525e9a2461c5e04537b7587045282886e5c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/bin/sh
# Copyright 2025 Huawei Cloud Computing Technology Co., Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


###
# This test checks if `just add-to-cas` correctly resolves symlinks.
##

set -eu

readonly JUST="${PWD}/bin/tool-under-test"
readonly JUST_MR="${PWD}/bin/mr-tool-under-test"
readonly DISTDIR="${TEST_TMPDIR}/distfiles"

readonly LBR_SETUP="${TEST_TMPDIR}/local-build-root-setup"
readonly LBR_A="${TEST_TMPDIR}/local-build-root-a"
readonly LBR_B="${TEST_TMPDIR}/local-build-root-b"
readonly LBR_C="${TEST_TMPDIR}/local-build-root-c"
readonly LBR_D="${TEST_TMPDIR}/local-build-root-d"
readonly LBR_E="${TEST_TMPDIR}/local-build-root-e"
readonly LBR_F="${TEST_TMPDIR}/local-build-root-f"
readonly LBR_G="${TEST_TMPDIR}/local-build-root-g"
readonly LBR_H="${TEST_TMPDIR}/local-build-root-h"
readonly LBR_I="${TEST_TMPDIR}/local-build-root-i"
readonly LBR_J="${TEST_TMPDIR}/local-build-root-j"
readonly LBR_K="${TEST_TMPDIR}/local-build-root-k"

readonly UNPACK_DIR="${TEST_TMPDIR}/unpack-dir"

readonly TEST_DATA="The content of the data file in foo"
readonly TEST_DIRS="bar/baz"
readonly DATA_PATH="bar/baz/data.txt"
readonly NON_UPWARDS_LINK_PATH="bar/baz/nonupwards"
readonly NON_UPWARDS_LINK_TARGET="data.txt"
readonly NON_UPWARDS_REWRITTEN_LINK_TARGET="../../../data.txt"
readonly UPWARDS_LINK_PATH="bar/upwards"
readonly UPWARDS_LINK_TARGET="../bar/baz"
readonly INDIRECT_LINK_PATH="bar/indirect"
readonly INDIRECT_LINK_TARGET="upwards/data.txt"
readonly CYCLE_LINK_1_PATH="bar/cycle_1"
readonly CYCLE_LINK_1_TARGET="cycle_2"
readonly CYCLE_LINK_2_PATH="bar/cycle_2"
readonly CYCLE_LINK_2_TARGET="cycle_1"

mkdir -p "${DISTDIR}"

mkdir -p log
LOGDIR="$(realpath log)"
LOG_ARCHIVE_REPO="${LOGDIR}/archive.txt"
LOG_FILE_REPO="${LOGDIR}/file.txt"

###
# Set up the archives
##

ROOT=$(pwd)
mkdir -p "foo/${TEST_DIRS}"
echo {} > foo/TARGETS
echo -n "${TEST_DATA}" > "foo/${DATA_PATH}"

# add resolvable non-upwards symlink
ln -s "${NON_UPWARDS_LINK_TARGET}" "foo/${NON_UPWARDS_LINK_PATH}"
tar cf "${DISTDIR}/foo-1.2.3_non_upwards.tar" foo \
  --sort=name --owner=root:0 --group=root:0 --mtime='UTC 1970-01-01' 2>&1
CONTENT_NON_UPWARDS=$(git hash-object "${DISTDIR}/foo-1.2.3_non_upwards.tar")
echo "Foo archive with resolvable non-upwards symlinks has content ${CONTENT_NON_UPWARDS}"

# now add also a resolvable upwards symlink
ln -s "${UPWARDS_LINK_TARGET}" "foo/${UPWARDS_LINK_PATH}"
# ...and a resolvable non-upwards symlink pointing to it
ln -s "${INDIRECT_LINK_TARGET}" "foo/${INDIRECT_LINK_PATH}"
tar cf "${DISTDIR}/foo-1.2.3_upwards.tar" foo \
  --sort=name --owner=root:0 --group=root:0 --mtime='UTC 1970-01-01' 2>&1
CONTENT_UPWARDS=$(git hash-object "${DISTDIR}/foo-1.2.3_upwards.tar")
echo "Foo archive with resolvable upwards symlinks has content ${CONTENT_UPWARDS}"

# now add also cycles, to check for failures to resolve
ln -s "${CYCLE_LINK_1_TARGET}" "foo/${CYCLE_LINK_1_PATH}"
ln -s "${CYCLE_LINK_2_TARGET}" "foo/${CYCLE_LINK_2_PATH}"
tar cf "${DISTDIR}/foo-1.2.3_cycle.tar" foo \
  --sort=name --owner=root:0 --group=root:0 --mtime='UTC 1970-01-01' 2>&1
CONTENT_CYCLES=$(git hash-object "${DISTDIR}/foo-1.2.3_cycle.tar")
echo "Foo archive with symlink cycles has content ${CONTENT_CYCLES}"

###
# Setup sample repository config
##

touch ROOT
cat > repos.json <<EOF
{ "repositories":
  { "foo_non_upwards_unresolved":
    { "repository":
      { "type": "archive"
      , "content": "${CONTENT_NON_UPWARDS}"
      , "fetch": "http://non-existent.example.org/foo-1.2.3_non_upwards.tar"
      , "subdir": "foo"
      }
    }
  , "foo_non_upwards_resolved":
    { "repository":
      { "type": "archive"
      , "content": "${CONTENT_NON_UPWARDS}"
      , "fetch": "http://non-existent.example.org/foo-1.2.3_non_upwards.tar"
      , "subdir": "foo"
      , "pragma": {"special": "resolve-completely"}
      }
    }
  , "foo_upwards_ignore_special":
    { "repository":
      { "type": "archive"
      , "content": "${CONTENT_UPWARDS}"
      , "fetch": "http://non-existent.example.org/foo-1.2.3_upwards.tar"
      , "subdir": "foo"
      , "pragma": {"special": "ignore"}
      }
    }
  , "foo_upwards_resolve_partially":
    { "repository":
      { "type": "archive"
      , "content": "${CONTENT_UPWARDS}"
      , "fetch": "http://non-existent.example.org/foo-1.2.3_upwards.tar"
      , "subdir": "foo"
      , "pragma": {"special": "resolve-partially"}
      }
    }
  , "foo_upwards_resolve_completely":
    { "repository":
      { "type": "archive"
      , "content": "${CONTENT_UPWARDS}"
      , "fetch": "http://non-existent.example.org/foo-1.2.3_upwards.tar"
      , "subdir": "foo"
      , "pragma": {"special": "resolve-completely"}
      }
    }
  }
}
EOF

# Compute the repository configuration
CONF=$("${JUST_MR}" --norc --local-build-root "${LBR_SETUP}" \
                    --distdir "${DISTDIR}" setup --all)
cat "${CONF}"
echo

###
# Compare add-to-cas trees to set up roots
##

# Check archive with only non-upwards symlinks

mkdir -p "${UNPACK_DIR}"
tar xf "${DISTDIR}/foo-1.2.3_non_upwards.tar" -C "${UNPACK_DIR}"

echo === check non-upwards symlinks default ===

COMPUTED=$("${JUST}" add-to-cas --local-build-root "${LBR_A}" "${UNPACK_DIR}/foo") 2>&1
EXPECTED=$(jq -r '.repositories.foo_non_upwards_unresolved.workspace_root[1]' "${CONF}")
[ "${COMPUTED}" = "${EXPECTED}" ]
echo done

echo === check non-upwards symlinks resolve tree-upwards same as default ===

COMPUTED=$("${JUST}" add-to-cas --local-build-root "${LBR_B}" \
                                --resolve-special=tree-upwards "${UNPACK_DIR}/foo") 2>&1
EXPECTED=$(jq -r '.repositories.foo_non_upwards_unresolved.workspace_root[1]' "${CONF}")
[ "${COMPUTED}" = "${EXPECTED}" ]
echo done

echo === check non-upwards symlinks resolve tree-all ===

COMPUTED=$("${JUST}" add-to-cas --local-build-root "${LBR_C}" \
                                --resolve-special=tree-all "${UNPACK_DIR}/foo") 2>&1
EXPECTED=$(jq -r '.repositories.foo_non_upwards_resolved.workspace_root[1]' "${CONF}")
[ "${COMPUTED}" = "${EXPECTED}" ]
echo done

echo === check non-upwards symlinks resolve all same as tree-all ===

COMPUTED=$("${JUST}" add-to-cas --local-build-root "${LBR_D}" \
                                --resolve-special=tree-all "${UNPACK_DIR}/foo") 2>&1
EXPECTED=$(jq -r '.repositories.foo_non_upwards_resolved.workspace_root[1]' "${CONF}")
[ "${COMPUTED}" = "${EXPECTED}" ]
echo done

# Check archive with upwards symlinks confined to the tree

rm -rf "${UNPACK_DIR}/*"
tar xf "${DISTDIR}/foo-1.2.3_upwards.tar" -C "${UNPACK_DIR}"

echo === check confined upwards symlinks resolve ignore ===

COMPUTED=$("${JUST}" add-to-cas --local-build-root "${LBR_E}" \
                                --resolve-special=ignore "${UNPACK_DIR}/foo") 2>&1
EXPECTED=$(jq -r '.repositories.foo_upwards_ignore_special.workspace_root[1]' "${CONF}")
[ "${COMPUTED}" = "${EXPECTED}" ]
echo done

echo === check confined upwards symlinks resolve tree-upwards ===

COMPUTED=$("${JUST}" add-to-cas --local-build-root "${LBR_F}" \
                                --resolve-special=tree-upwards "${UNPACK_DIR}/foo") 2>&1
EXPECTED=$(jq -r '.repositories.foo_upwards_resolve_partially.workspace_root[1]' "${CONF}")
[ "${COMPUTED}" = "${EXPECTED}" ]
echo done

echo === check confined upwards symlinks resolve tree-all ===

COMPUTED=$("${JUST}" add-to-cas --local-build-root "${LBR_G}" \
                                --resolve-special=tree-all "${UNPACK_DIR}/foo") 2>&1
EXPECTED=$(jq -r '.repositories.foo_upwards_resolve_completely.workspace_root[1]' "${CONF}")
[ "${COMPUTED}" = "${EXPECTED}" ]
echo done

echo === check confined upwards symlinks resolve all same as tree-all ===

COMPUTED=$("${JUST}" add-to-cas --local-build-root "${LBR_H}" \
                                --resolve-special=all "${UNPACK_DIR}/foo") 2>&1
EXPECTED=$(jq -r '.repositories.foo_upwards_resolve_completely.workspace_root[1]' "${CONF}")
[ "${COMPUTED}" = "${EXPECTED}" ]
echo done

echo === check confined upwards symlinks default fail ===

"${JUST}" add-to-cas --local-build-root "${LBR_I}" "${UNPACK_DIR}/foo" 2>&1 && exit 1 || :
echo failed as expected

# Check non-confined upwards symlinks

# replace non-upwards link to make it upwards pointing to file copy outside
# tree; resulting resolved tree should be the same
cp "${UNPACK_DIR}/foo/${DATA_PATH}" "${UNPACK_DIR}"
rm "${UNPACK_DIR}/foo/${NON_UPWARDS_LINK_PATH}"
ln -s "${NON_UPWARDS_REWRITTEN_LINK_TARGET}" "${UNPACK_DIR}/foo/${NON_UPWARDS_LINK_PATH}"

echo === check non-confined upwards symlinks resolve all ===

COMPUTED=$("${JUST}" add-to-cas --local-build-root "${LBR_J}" \
                                --resolve-special=all "${UNPACK_DIR}/foo") 2>&1
EXPECTED=$(jq -r '.repositories.foo_upwards_resolve_completely.workspace_root[1]' "${CONF}")
[ "${COMPUTED}" = "${EXPECTED}" ]
echo done

# Check that cycles are detected

rm -rf "${UNPACK_DIR}/*"
tar xf "${DISTDIR}/foo-1.2.3_cycle.tar" -C "${UNPACK_DIR}"

echo === check cycle fail ===

"${JUST}" add-to-cas --local-build-root "${LBR_K}" \
                     --resolve-special=all "${UNPACK_DIR}/foo" 2>&1 && exit 1 || :
echo failed as expected

echo OK