Skip to content

Commit 8c2b863

Browse files
authored
test: update scenario test env to prepare for parallel mock work (#319)
1 parent 1419105 commit 8c2b863

5 files changed

Lines changed: 32 additions & 32 deletions

File tree

scenario/component_query_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func TestQueryingAComponent(t *testing.T) {
3030
testScript := `
3131
set -euxo pipefail
3232
azldev project init
33+
ln -s /var/lib/mock build # Ensure the build output dir is writable by mock
3334
3435
cat <<EOF >test-component.spec
3536
Name: test-component

scenario/docker/Dockerfile

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,38 @@ SHELL ["/bin/bash", "-c"]
66
ARG TIMESTAMP
77
RUN echo "Image built at ${TIMESTAMP}" > /etc/azldev_scenario_testing_timestamp
88

9-
ARG USER_CONFIG
9+
ARG UID
10+
ARG GID
1011
ARG WORK_DIR
1112

12-
ENV USER_CONFIG=${USER_CONFIG}
13+
ENV UID=${UID}
14+
ENV GID=${GID}
1315

1416
LABEL name="azldev_scenario_testing"
1517

16-
# Install dependencies
17-
# * We requires shadow-utils to be able to manage users and groups
18+
# Install early dependencies
19+
# * We require shadow-utils to be able to manage users and groups
20+
RUN tdnf -y install shadow-utils
21+
22+
# Create mock group and test user up-front, using the same UID and GID as the host's user so it lines up.
23+
RUN groupadd -f -g "${GID}" mock && \
24+
useradd -u "${UID}" -g "${GID}" -m testuser && \
25+
mkdir -p /etc/sudoers.d && \
26+
echo "testuser ALL=(ALL) NOPASSWD: ALL" >/etc/sudoers.d/testuser
27+
28+
# Install additional dependencies
1829
# * We require ca-certificates to be able to interact with standard https endpoints
1930
# * We require mock to build packages
2031
# * We require sudo to run commands as root after dropping to test user
2132
RUN tdnf -y install \
22-
shadow-utils \
2333
ca-certificates \
2434
mock \
2535
sudo
2636

27-
# Set up the default user (named "testuser") with the UID and GID we were given.
28-
#
29-
# NOTE: The <<< syntax requires use of bash (instead of /bin/sh), hence the SHELL directive above.
30-
RUN groupadd -f -g "$(cut -d: -f2 <<<"${USER_CONFIG}")" testuser && \
31-
useradd -u "$(cut -d: -f1 <<<"${USER_CONFIG}")" \
32-
-g "$(cut -d: -f2 <<<"${USER_CONFIG}")" \
33-
-m \
34-
testuser && \
35-
echo "testuser ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/testuser
36-
37-
# Add the user to the 'mock' group
38-
RUN usermod -aG mock testuser && \
39-
usermod -aG root testuser
40-
4137
COPY entrypoint.sh /usr/local/bin/entrypoint.sh
4238

4339
# Set the default user
44-
USER ${USER_CONFIG}
40+
USER testuser:mock
4541

4642
# Switch to the well-known work directory that the test infrastructure will
4743
# copy files into.

scenario/docker/entrypoint.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/bin/bash
2+
set -euo pipefail
23

34
# In case some files were copied into the container after building the Dockerfile,
45
# make sure the work directory is entirely owned by the test user.

scenario/internal/containertest/containers.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"io"
1212
"os"
1313
"path"
14+
"strconv"
1415
"strings"
1516
"testing"
1617
"time"
@@ -203,6 +204,9 @@ func runCmdInContainerImpl(
203204
// expects a string pointer for the build args and consts can't be used.
204205
containerWorkDir := ContainerWorkDir
205206

207+
uidStr := strconv.Itoa(os.Getuid())
208+
gidStr := strconv.Itoa(os.Getgid())
209+
206210
// Build the container request. It is based on ./docker/Dockerfile. It will
207211
// mount the collateral.workdir directory into the container at /workdir, place
208212
// the azldev command at /azldev, and run the command in the container.
@@ -218,9 +222,10 @@ func runCmdInContainerImpl(
218222
KeepImage: true,
219223
BuildLogWriter: logger.buildLogWriter,
220224
BuildArgs: map[string]*string{
221-
"USER_CONFIG": getUserConfig(t),
222-
"WORK_DIR": &containerWorkDir,
223-
"TIMESTAMP": getRefreshTimestamp(t),
225+
"UID": &uidStr,
226+
"GID": &gidStr,
227+
"WORK_DIR": &containerWorkDir,
228+
"TIMESTAMP": getRefreshTimestamp(t),
224229
},
225230
},
226231

@@ -413,15 +418,6 @@ func getTimeoutConfig(t *testing.T, timeout time.Duration) *delayStrategy {
413418
return strategy
414419
}
415420

416-
func getUserConfig(t *testing.T) *string {
417-
t.Helper()
418-
419-
// Get the user and group for the current user.
420-
output := fmt.Sprintf("%d:%d", os.Getuid(), os.Getgid())
421-
422-
return &output
423-
}
424-
425421
func getNetworkConfig(t *testing.T, network bool) []string {
426422
t.Helper()
427423

scenario/internal/projecttest/projecttest.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ func (p *ProjectTest) RunInContainer(t *testing.T) *ProjectTestResults {
5353
// Create a script that runs the command with the provided arguments.
5454
testScript := fmt.Sprintf(`
5555
set -x
56+
57+
# Ensure the build output dir is writable by mock; we accomplish this by symlinking
58+
# to the well-known dir created by mock for this purpose.
59+
rm -rf project/build
60+
ln -s /var/lib/mock project/build
61+
5662
azldev -C project -v %s --output-format json >result.json
5763
`, strings.Join(p.commandArgs, " "))
5864

0 commit comments

Comments
 (0)