summaryrefslogtreecommitdiff
path: root/test/end-to-end/just-mr/gc-repo.sh
blob: b6bccf856c9dc70b1489f9d40c9269d4556100f7 (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
#!/bin/sh
# Copyright 2024 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.


set -eu

readonly JUST="${PWD}/bin/tool-under-test"
readonly JUST_MR="${PWD}/bin/mr-tool-under-test"
readonly WORK="${PWD}/work"
readonly GIT_REPO="${PWD}/git-repository"
readonly BIN="${TEST_TMPDIR}/bin"
mkdir -p "${BIN}"
readonly DISTDIR="${PWD}/distdir"
mkdir -p "${DISTDIR}"
readonly ARCHIVE_CONTENT="${TEST_TMPDIR}/archive"
readonly SCRATCH_LBR="${TEST_TMPDIR}/throw-away-build-root"
readonly SCRATCH_FF="${TEST_TMPDIR}/scratch-space-for-foreign-file"
readonly SCRATCH_TREE="${TEST_TMPDIR}/scratch-space-for-git-tree"
readonly LBR="${TEST_TMPDIR}/local-build-root"
readonly OUT="${TEST_TMPDIR}/out"
mkdir -p "${OUT}"
readonly LOG="${PWD}/log"
mkdir -p "${LOG}"

# Set up git repo
mkdir -p "${GIT_REPO}"
cd "${GIT_REPO}"
mkdir data
echo Hello world > data/text
seq 1 10 > data/numbers
git init 2>&1
git branch -m stable-1.0 2>&1
git config user.email "nobody@example.org" 2>&1
git config user.name "Nobody" 2>&1
git add . 2>&1
git commit -m "Sample data" 2>&1
git show
GIT_COMMIT=$(git log -n 1 --format="%H")
GIT_TREE=$(git log -n 1 --format="%T")
echo
echo "Created commit ${GIT_COMMIT} with tree ${GIT_TREE}"
echo

## As we simulate remote fetching, create a mock git binary
cat > "${BIN}/mock-git" <<EOF
#!/bin/sh
if [ "\$1" = "fetch" ]
then
  git fetch "${GIT_REPO}" stable-1.0
else
  git "\$@"
fi
EOF
chmod 755 "${BIN}/mock-git"

# Set up archive

mkdir -p "${ARCHIVE_CONTENT}"
cd "${ARCHIVE_CONTENT}"
echo archive content > description.txt
seq 1 20 > data
ARCHIVE_TREE=$("${JUST}" add-to-cas --local-build-root "${SCRATCH_LBR}" .)
tar cvf "${DISTDIR}/archive.tar" .
ARCHIVE=$("${JUST}" add-to-cas --local-build-root "${SCRATCH_LBR}" "${DISTDIR}/archive.tar")
echo
echo "Created archive with content ${ARCHIVE} holding tree ${ARCHIVE_TREE}"
echo

# Set up a "foreign file"

echo Here be dragons > "${DISTDIR}/foreign.txt"
FOREIGN=$("${JUST}" add-to-cas --local-build-root "${SCRATCH_LBR}" "${DISTDIR}/foreign.txt")
rm -rf "${SCRATCH_FF}"
mkdir -p "${SCRATCH_FF}"
cp "${DISTDIR}/foreign.txt" "${SCRATCH_FF}/data.txt"
FOREIGN_TREE=$("${JUST}" add-to-cas --local-build-root "${SCRATCH_LBR}" "${SCRATCH_FF}")
echo
echo "Foreign file ${FOREIGN} creating tree ${FOREIGN_TREE}"
echo

# Set up a "git tree"
cat > "${BIN}/mock-foreign-vcs" <<'EOF'
#!/bin/sh

mkdir -p foo/bar/baz
echo foo data > foo/data.txt
echo bar data > foo/bar/data.txt
echo baz data > foo/bar/baz/data.txt
EOF
chmod 755 "${BIN}/mock-foreign-vcs"
rm -rf "${SCRATCH_TREE}"
mkdir -p "${SCRATCH_TREE}"
(cd "${SCRATCH_TREE}" && "${BIN}/mock-foreign-vcs")
FOREIGN_GIT_TREE=$("${JUST}" add-to-cas --local-build-root "${SCRATCH_LBR}" "${SCRATCH_TREE}")
echo
echo "Git tree ${FOREIGN_GIT_TREE}"
echo

# Create workspace with just-mr repository configuration
mkdir -p "${WORK}"
cd "${WORK}"
touch ROOT
cat > repos.json <<EOF
{ "repositories":
  { "":
    { "repository": {"type": "file", "path": "."}
    , "bindings":
      { "git": "git"
      , "archive": "archive"
      , "foreign": "foreign_file"
      , "tree": "git_tree"
      }
    }
  , "git":
    { "repository":
      { "type": "git"
      , "commit": "${GIT_COMMIT}"
      , "repository": "mockprotocol://example.com/repo"
      , "branch": "stable-1.0"
      }
    }
  , "archive":
    { "repository":
      { "type": "archive"
      , "content": "${ARCHIVE}"
      , "fetch": "http://nonexistent.example.com/archive.tar"
      }
    }
  , "foreign_file":
    { "repository":
      { "type": "foreign file"
      , "content": "${FOREIGN}"
      , "fetch": "http://nonexistent.example.com/foreign.txt"
      , "name": "data.txt"
      }
    }
  , "git_tree":
    { "repository":
      { "type": "git tree"
      , "id": "${FOREIGN_GIT_TREE}"
      , "cmd": ["${BIN}/mock-foreign-vcs"]
      }
    }
  }
}
EOF
cat repos.json


# Set up repos. This should get everything in the local build root.
"${JUST_MR}" --norc --local-build-root "${LBR}" --git "${BIN}/mock-git" \
             --distdir "${DISTDIR}" -L '["env", "PATH='"${PATH}"'"]' \
   setup  > "${OUT}/conf-file-name" 2> "${LOG}/log-1"
cat "${LOG}/log-1"
echo
CONFIG="$(cat "${OUT}/conf-file-name")"
cat "${CONFIG}"
echo

## Verify git repo is set up correctly
TREE_FOUND="$(jq -r '.repositories.git.workspace_root[1]' "${CONFIG}")"
[ "${TREE_FOUND}" = "${GIT_TREE}" ]
GIT_LOCATION="$(jq -r '.repositories.git.workspace_root[2]' "${CONFIG}")"

## Verify the archive is set up correctly
TREE_FOUND="$(jq -r '.repositories.archive.workspace_root[1]' "${CONFIG}")"
[ "${TREE_FOUND}" = "${ARCHIVE_TREE}" ]
ARCHIVE_LOCATION="$(jq -r '.repositories.archive.workspace_root[2]' "${CONFIG}")"

## Sanity check for foreign file
TREE_FOUND="$(jq -r '.repositories.foreign_file.workspace_root[1]' "${CONFIG}")"
[ "${TREE_FOUND}" = "${FOREIGN_TREE}" ]
FOREIGN_LOCATION="$(jq -r '.repositories.foreign_file.workspace_root[2]' "${CONFIG}")"

## Sanity check for git tree
TREE_FOUND="$(jq -r '.repositories.git_tree.workspace_root[1]' "${CONFIG}")"
[ "${TREE_FOUND}" = "${FOREIGN_GIT_TREE}" ]
GIT_TREE_LOCATION="$(jq -r '.repositories.git_tree.workspace_root[2]' "${CONFIG}")"

# Clean up original repositories
rm -rf "${GIT_REPO}"
rm -rf "${DISTDIR}"
rm -f "${BIN}/mock-foreign-vcs"

# Fully clean the non-repo cache
"${JUST}" gc --local-build-root "${LBR}" 2>&1
"${JUST}" gc --local-build-root "${LBR}" 2>&1

# Rotate repo cache
"${JUST_MR}" --norc --local-build-root "${LBR}" gc-repo 2>&1

## Verify the mirrored locations are gone
[ -e "${GIT_LOCATION}" ] && exit 1 || :
[ -e "${ARCHIVE_LOCATION}" ] && exit 1 || :
[ -e "${FOREIGN_LOCATION}" ] && exit 1 || :
[ -e "${GIT_TREE_LOCATION}" ] && exit 1 || :

# Setup repos again
"${JUST_MR}" --norc --local-build-root "${LBR}" -L '["env", "PATH='"${PATH}"'"]' \
    setup  > "${OUT}/conf-file-name" 2> "${LOG}/log-2"
cat "${LOG}/log-2"
echo
CONFIG="$(cat "${OUT}/conf-file-name")"
cat "${CONFIG}"
echo

## Verify git repo is set up correctly
TREE_FOUND="$(jq -r '.repositories.git.workspace_root[1]' "${CONFIG}")"
[ "${TREE_FOUND}" = "${GIT_TREE}" ]

## Verify the archive is set up correctly
TREE_FOUND="$(jq -r '.repositories.archive.workspace_root[1]' "${CONFIG}")"
[ "${TREE_FOUND}" = "${ARCHIVE_TREE}" ]

## Sanity check for foreign file
TREE_FOUND="$(jq -r '.repositories.foreign_file.workspace_root[1]' "${CONFIG}")"
[ "${TREE_FOUND}" = "${FOREIGN_TREE}" ]

## Sanity check for git tree
TREE_FOUND="$(jq -r '.repositories.git_tree.workspace_root[1]' "${CONFIG}")"
[ "${TREE_FOUND}" = "${FOREIGN_GIT_TREE}" ]

echo OK