Skip to content

Commit 9e96fe1

Browse files
authored
Merge pull request #4019 from Horiodino/port-forwarding
bug fix: static port forward works in plain
2 parents c5d15cd + 8f54822 commit 9e96fe1

File tree

6 files changed

+103
-109
lines changed

6 files changed

+103
-109
lines changed

hack/test-nonplain-static-port-forward.sh

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@
66
set -euxo pipefail
77

88
INSTANCE=nonplain-static-port-forward
9-
TEMPLATE=hack/test-templates/static-port-forward.yaml
9+
TEMPLATE="$(dirname "$0")/test-templates/test-misc.yaml"
1010

11-
limactl delete -f $INSTANCE || true
11+
limactl delete -f "$INSTANCE" || true
1212

13-
limactl start --name=$INSTANCE --tty=false $TEMPLATE
13+
limactl start --name="$INSTANCE" --tty=false "$TEMPLATE"
1414

15-
limactl shell $INSTANCE -- bash -c 'until [ -e /run/nginx.pid ]; do sleep 1; done'
16-
limactl shell $INSTANCE -- bash -c 'until systemctl is-active --quiet test-server-9080; do sleep 1; done'
17-
limactl shell $INSTANCE -- bash -c 'until systemctl is-active --quiet test-server-9070; do sleep 1; done'
15+
limactl shell "$INSTANCE" -- bash -c 'until systemctl is-active --quiet nginx; do sleep 1; done'
16+
limactl shell "$INSTANCE" -- bash -c 'until systemctl is-active --quiet test-server-9080; do sleep 1; done'
17+
limactl shell "$INSTANCE" -- bash -c 'until systemctl is-active --quiet test-server-9070; do sleep 1; done'
1818

1919
curl -sSf http://127.0.0.1:9090 | grep -i 'nginx' && echo 'Static port forwarding (9090) works in normal mode!'
20-
curl -sSf http://127.0.0.1:9080 | grep -i 'Dynamic port 9080' && echo 'Dynamic port forwarding (9080) works in normal mode!'
21-
curl -sSf http://127.0.0.1:9070 | grep -i 'Dynamic port 9070' && echo 'Dynamic port forwarding (9070) works in normal mode!'
20+
curl -sSf http://127.0.0.1:29080 | grep -i 'Dynamic port 9080' && echo 'Dynamic port forwarding (29080) works in normal mode!'
21+
curl -sSf http://127.0.0.1:29070 | grep -i 'Dynamic port 9070' && echo 'Dynamic port forwarding (29070) works in normal mode!'
2222

23-
limactl delete -f $INSTANCE
23+
limactl delete -f "$INSTANCE"
2424
echo "All tests passed for normal mode - both static and dynamic ports work!"
2525
# EOF

hack/test-plain-static-port-forward.sh

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,34 @@
66
set -euxo pipefail
77

88
INSTANCE=plain-static-port-forward
9-
TEMPLATE=hack/test-templates/static-port-forward.yaml
9+
TEMPLATE="$(dirname "$0")/test-templates/test-misc.yaml"
1010

11-
limactl delete -f $INSTANCE || true
11+
limactl delete -f "$INSTANCE" || true
1212

13-
limactl start --name=$INSTANCE --plain=true --tty=false $TEMPLATE
13+
limactl start --name="$INSTANCE" --plain=true --tty=false "$TEMPLATE"
1414

15-
limactl shell $INSTANCE -- bash -c 'until [ -e /run/nginx.pid ]; do sleep 1; done'
15+
limactl shell "$INSTANCE" -- bash -c 'until systemctl is-active --quiet nginx; do sleep 1; done'
1616

17-
curl -sSf http://127.0.0.1:9090 | grep -i 'nginx' && echo 'Static port forwarding (9090) works in plain mode!'
17+
if ! curl -sSf http://127.0.0.1:9090 | grep -i 'nginx'; then
18+
echo 'ERROR: Static port forwarding (9090) does not work in plain mode!'
19+
exit 1
20+
fi
21+
echo 'Static port forwarding (9090) works in plain mode!'
1822

19-
if curl -sSf http://127.0.0.1:9080 2>/dev/null; then
20-
echo 'ERROR: Dynamic port 9080 should not be forwarded in plain mode!'
23+
if curl -sSf http://127.0.0.1:29080 2>/dev/null; then
24+
echo 'ERROR: Dynamic port 29080 should not be forwarded in plain mode!'
2125
exit 1
2226
else
23-
echo 'Dynamic port 9080 is correctly NOT forwarded in plain mode.'
27+
echo 'Dynamic port 29080 is correctly NOT forwarded in plain mode.'
2428
fi
2529

26-
if curl -sSf http://127.0.0.1:9070 2>/dev/null; then
27-
echo 'ERROR: Dynamic port 9070 should not be forwarded in plain mode!'
30+
if curl -sSf http://127.0.0.1:29070 2>/dev/null; then
31+
echo 'ERROR: Dynamic port 29070 should not be forwarded in plain mode!'
2832
exit 1
2933
else
30-
echo 'Dynamic port 9070 is correctly NOT forwarded in plain mode.'
34+
echo 'Dynamic port 29070 is correctly NOT forwarded in plain mode.'
3135
fi
3236

33-
limactl delete -f $INSTANCE
37+
limactl delete -f "$INSTANCE"
3438
echo "All tests passed for plain mode - only static ports work!"
3539
# EOF

hack/test-templates.sh

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ declare -A CHECKS=(
5151
["snapshot-offline"]=""
5252
["clone"]=""
5353
["port-forwards"]="1"
54-
["static-port-forwards"]=""
5554
["vmnet"]=""
5655
["disk"]=""
5756
["user-v2"]=""
@@ -61,6 +60,7 @@ declare -A CHECKS=(
6160
["param-env-variables"]=""
6261
["set-user"]=""
6362
["preserve-env"]="1"
63+
["static-port-forwards"]=""
6464
)
6565

6666
case "$NAME" in
@@ -95,12 +95,7 @@ case "$NAME" in
9595
CHECKS["provision-yq"]="1"
9696
CHECKS["param-env-variables"]="1"
9797
CHECKS["set-user"]="1"
98-
;;
99-
"static-port-forward")
10098
CHECKS["static-port-forwards"]="1"
101-
CHECKS["port-forwards"]=""
102-
CHECKS["container-engine"]=""
103-
CHECKS["restart"]=""
10499
;;
105100
"docker")
106101
CONTAINER_ENGINE="docker"
@@ -463,13 +458,6 @@ if [[ -n ${CHECKS["port-forwards"]} ]]; then
463458
set +x
464459
fi
465460

466-
if [[ -n ${CHECKS["static-port-forwards"]} ]]; then
467-
INFO "Testing static port forwarding functionality"
468-
"${scriptdir}/test-plain-static-port-forward.sh" "$NAME"
469-
"${scriptdir}/test-nonplain-static-port-forward.sh" "$NAME"
470-
INFO "All static port forwarding tests passed!"
471-
fi
472-
473461
if [[ -n ${CHECKS["vmnet"]} ]]; then
474462
INFO "Testing vmnet functionality"
475463
guestip="$(limactl shell "$NAME" ip -4 -j addr show dev lima0 | jq -r '.[0].addr_info[0].local')"
@@ -639,3 +627,10 @@ limactl delete "$NAME"
639627
if [[ -n ${CHECKS["mount-path-with-spaces"]} ]]; then
640628
rm -rf "/tmp/lima test dir with spaces"
641629
fi
630+
631+
if [[ -n ${CHECKS["static-port-forwards"]} ]]; then
632+
INFO "Testing static port forwarding functionality"
633+
"${scriptdir}/test-plain-static-port-forward.sh" "$NAME"
634+
"${scriptdir}/test-nonplain-static-port-forward.sh" "$NAME"
635+
INFO "All static port forwarding tests passed!"
636+
fi

hack/test-templates/static-port-forward.yaml

Lines changed: 0 additions & 64 deletions
This file was deleted.

hack/test-templates/test-misc.yaml

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,57 @@ provision:
3030
- mode: dependency
3131
script: "touch /tmp/param-$PARAM_DEPENDENCY"
3232
- mode: system
33-
script: "touch /tmp/param-$PARAM_SYSTEM"
33+
script: |
34+
touch /tmp/param-$PARAM_SYSTEM
35+
36+
# port forwarding test setup
37+
apt-get update
38+
apt-get install -y nginx python3
39+
systemctl enable nginx
40+
systemctl start nginx
41+
42+
cat > /etc/systemd/system/test-server-9080.service << 'EOF'
43+
[Unit]
44+
Description=Test Server on Port 9080
45+
After=network.target
46+
47+
[Service]
48+
Type=simple
49+
User=root
50+
ExecStart=/usr/bin/python3 -m http.server 9080 --bind 127.0.0.1
51+
Restart=always
52+
53+
[Install]
54+
WantedBy=multi-user.target
55+
EOF
56+
57+
cat > /etc/systemd/system/test-server-9070.service << 'EOF'
58+
[Unit]
59+
Description=Test Server on Port 9070
60+
After=network.target
61+
62+
[Service]
63+
Type=simple
64+
User=root
65+
ExecStart=/usr/bin/python3 -m http.server 9070 --bind 127.0.0.1
66+
Restart=always
67+
68+
[Install]
69+
WantedBy=multi-user.target
70+
EOF
71+
72+
mkdir -p /var/www/html-9080
73+
mkdir -p /var/www/html-9070
74+
75+
echo '<html><body><h1>Dynamic port 9080</h1></body></html>' > /var/www/html-9080/index.html
76+
echo '<html><body><h1>Dynamic port 9070</h1></body></html>' > /var/www/html-9070/index.html
77+
78+
systemctl daemon-reload
79+
systemctl enable test-server-9080
80+
systemctl enable test-server-9070
81+
systemctl start test-server-9080
82+
systemctl start test-server-9070
83+
3484
- mode: user
3585
script: "touch /tmp/param-$PARAM_USER"
3686
- mode: data
@@ -41,7 +91,6 @@ provision:
4191
- mode: yq
4292
path: "/tmp/param-{{.Param.YQ}}.json"
4393
expression: .YQ = "{{.Param.YQ}}"
44-
user: "{{.User}}"
4594

4695
probes:
4796
- mode: readiness
@@ -61,3 +110,14 @@ user:
61110
uid: 4711
62111
# Ubuntu has identical /bin/bash and /usr/bin/bash
63112
shell: /usr/bin/bash
113+
114+
portForwards:
115+
- guestPort: 80
116+
hostPort: 9090
117+
static: true
118+
- guestPort: 9080
119+
hostPort: 29080
120+
static: false
121+
- guestPort: 9070
122+
hostPort: 29070
123+
static: false

pkg/hostagent/hostagent.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -536,13 +536,10 @@ sudo chown -R "${USER}" /run/host-services`
536536
})
537537
}
538538

539-
if !*a.instConfig.Plain {
540-
staticPortForwards, err := a.separateStaticPortForwards()
541-
if err != nil {
542-
errs = append(errs, err)
543-
}
544-
a.addStaticPortForwardsFromList(ctx, staticPortForwards)
539+
staticPortForwards := a.separateStaticPortForwards()
540+
a.addStaticPortForwardsFromList(ctx, staticPortForwards)
545541

542+
if !*a.instConfig.Plain {
546543
go a.watchGuestAgentEvents(ctx)
547544
if a.showProgress {
548545
cloudInitDone := make(chan struct{})
@@ -704,7 +701,10 @@ func (a *HostAgent) addStaticPortForwardsFromList(ctx context.Context, staticPor
704701
}
705702
}
706703

707-
func (a *HostAgent) separateStaticPortForwards() ([]limatype.PortForward, error) {
704+
// separateStaticPortForwards separates static port forwards from a.instConfig.PortForwards,
705+
// updates a.instConfig.PortForwards to contain only non-static port forwards,
706+
// and returns the list of static port forwards.
707+
func (a *HostAgent) separateStaticPortForwards() []limatype.PortForward {
708708
staticPortForwards := make([]limatype.PortForward, 0, len(a.instConfig.PortForwards))
709709
nonStaticPortForwards := make([]limatype.PortForward, 0, len(a.instConfig.PortForwards))
710710

@@ -722,8 +722,7 @@ func (a *HostAgent) separateStaticPortForwards() ([]limatype.PortForward, error)
722722
logrus.Debugf("Static port forwards: %d, Non-static port forwards: %d", len(staticPortForwards), len(nonStaticPortForwards))
723723

724724
a.instConfig.PortForwards = nonStaticPortForwards
725-
726-
return staticPortForwards, nil
725+
return staticPortForwards
727726
}
728727

729728
func isGuestAgentSocketAccessible(ctx context.Context, client *guestagentclient.GuestAgentClient) bool {

0 commit comments

Comments
 (0)