summaryrefslogtreecommitdiff
path: root/test/end-to-end/gc/basic.sh
blob: 9db3500271f8c49603c388a043118df59bf5fba4 (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
#!/bin/sh
# Copyright 2023 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 LBR="${TEST_TMPDIR}/local-build-root"
readonly TOOLS_DIR="${TEST_TMPDIR}/tools"
readonly OUT="${TEST_TMPDIR}/out"
BUILD_ARGS="--local-build-root ${LBR}"
if [ -n "${COMPATIBLE:-}" ]; then
  BUILD_ARGS="$BUILD_ARGS --compatible"
fi

# Have tools in the "outside environment" to later demonstrate
# that the action was _not_ rerun.
mkdir -p "${TOOLS_DIR}"
cat > "${TOOLS_DIR}/tree" <<'EOF'
#!/bin/sh
mkdir -p $1/hello/world/tree
echo Hello World > $1/hello/world/tree/hello.txt
echo -n World > $1/hello/world/tree/name.txt
EOF
chmod 755 "${TOOLS_DIR}/tree"
cat > "${TOOLS_DIR}/large" <<'EOF'
#!/bin/sh
for _ in `seq 1 10000`
do
  echo This is a really large file 1234567890 >> $1
done
EOF
chmod 755 "${TOOLS_DIR}/large"


touch WORKSPACE
cat > TARGETS <<'EOF'
{ "large":
  { "type": "generic"
  , "arguments_config": ["ENV"]
  , "outs": ["out.txt"]
  , "cmds": ["${TOOLS}/large out.txt"]
  , "env": {"type": "var", "name": "ENV"}
  }
, "tree":
  { "type": "generic"
  , "arguments_config": ["ENV"]
  , "out_dirs": ["out"]
  , "cmds": ["${TOOLS}/tree out"]
  , "env": {"type": "var", "name": "ENV"}
  }
, "": {"type": "install", "dirs": [["large", "out"], ["tree", "out"]]}
}
EOF

cat TARGETS

# Build to fill the cache
"${JUST}" build ${BUILD_ARGS} -L '["env", "PATH='"${PATH}"'"]' \
          -D '{"ENV": {"TOOLS": "'${TOOLS_DIR}'"}}' 2>&1

# Demonstrate that from now on, we don't build anything any more
rm -rf "${TOOLS_DIR}"

# Verify the large file is in cache
"${JUST}" install ${BUILD_ARGS} -L '["env", "PATH='"${PATH}"'"]' -o "${OUT}/out-large" \
          -D '{"ENV": {"TOOLS": "'${TOOLS_DIR}'"}}' large 2>&1
wc -c "${OUT}/out-large/out.txt"
test $(cat "${OUT}/out-large/out.txt" | wc -c) -gt 100000

# Now test non-rotating gc; this does not affect how far the
# cache. At the end, we also repeat it serveral time to demontstrate
# that things don't fall out of cache.

LEFT_OVER_REMOVE_DIR="${LBR}/protocol-dependent/remove-me-$$-xxx"
EPHEMERAL_DIR="${LBR}/protocol-dependent/generation-0/ephemeral"

mkdir -p "${LEFT_OVER_REMOVE_DIR}/xxx"
mkdir -p "${EPHEMERAL_DIR}/xxx"

"${JUST}" gc --local-build-root "${LBR}" --no-rotate 2>&1

echo "Checking that ${EPHEMERAL_DIR} was removed"
[ -e "${EPHEMERAL_DIR}" ] && exit 1 || :
echo "Checking that ${LEFT_OVER_REMOVE_DIR} was removed"
[ -e "${LEFT_OVER_REMOVE_DIR}" ] && exit 1 || :

"${JUST}" gc --local-build-root "${LBR}" --no-rotate 2>&1
"${JUST}" gc --local-build-root "${LBR}" --no-rotate 2>&1

# collect garbage
"${JUST}" gc --local-build-root "${LBR}" 2>&1

# Use the tree
"${JUST}" build ${BUILD_ARGS} -L '["env", "PATH='"${PATH}"'"]' \
          -D '{"ENV": {"TOOLS": "'${TOOLS_DIR}'"}}' tree 2>&1

# collect garbage again
"${JUST}" gc --local-build-root "${LBR}" 2>&1

# Verify the build root is now small
tar cvf "${OUT}/root.tar" "${LBR}" 2>&1
wc -c "${OUT}/root.tar"
test $(cat "${OUT}/root.tar" | wc -c) -lt 100000

# Verify that the tree is fully in cache
"${JUST}" install ${BUILD_ARGS} -L '["env", "PATH='"${PATH}"'"]' -o "${OUT}/out-tree" \
          -D '{"ENV": {"TOOLS": "'${TOOLS_DIR}'"}}' tree 2>&1
ls -R "${OUT}/out-tree"
test -f "${OUT}/out-tree/out/hello/world/tree/hello.txt"
test -f "${OUT}/out-tree/out/hello/world/tree/name.txt"
test "$(cat "${OUT}/out-tree/out/hello/world/tree/name.txt")" = "World"

# check if all files in generation 0 have been linked and not copied
readonly NON_LINKED_FILES=$(find ${LBR}/protocol-dependent/generation-0 -type f -exec sh -c 'test $(stat -c %h {}) != 2' \; -print)
echo
echo "Files with link count!=2:"
echo "${NON_LINKED_FILES:-"<none>"}"
echo
test -z "${NON_LINKED_FILES}"

echo OK