Skip to content

Commit b3521d4

Browse files
committed
Enhance hot upgrade tests
Signed-off-by: Zephyr <zinsist777@gmail.com> Enhance hot upgrade tests Signed-off-by: Zephyr <zinsist777@gmail.com> wrapping binary file permissions Signed-off-by: Zephyr <zinsist777@gmail.com> rollback chmod Signed-off-by: Zephyr <zinsist777@gmail.com> rollback chmod Signed-off-by: Zephyr <zinsist777@gmail.com> Enhance hot upgrade tests Signed-off-by: Zephyr <zinsist777@gmail.com>
1 parent e1dffec commit b3521d4

File tree

1 file changed

+92
-32
lines changed

1 file changed

+92
-32
lines changed

smoke/tests/takeover_test.go

Lines changed: 92 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,34 @@ import (
88
"fmt"
99
"net/http"
1010
"os"
11+
"os/exec"
1112
"path/filepath"
1213
"strings"
1314
"testing"
1415
"time"
1516

17+
"github.com/containerd/nydus-snapshotter/config"
1618
"github.com/dragonflyoss/nydus/smoke/tests/tool"
1719
"github.com/dragonflyoss/nydus/smoke/tests/tool/test"
1820
"github.com/google/uuid"
21+
"github.com/pelletier/go-toml"
22+
"github.com/sirupsen/logrus"
1923
"github.com/stretchr/testify/require"
2024
)
2125

2226
// Environment Requirement: Containerd, nerdctl >= 0.22, nydus-snapshotter, nydusd.
2327
// Prepare: setup nydus for containerd, reference: https://github.com/dragonflyoss/nydus/blob/master/docs/containerd-env-setup.md.
2428

29+
const (
30+
hotUpgradeRepeatCount = 6
31+
configPath = "/etc/nydus/config.toml"
32+
)
33+
2534
var (
2635
snapshotter string
2736
takeoverTestImage string
2837
snapshotterSystemSock string
38+
nydusdPaths []string
2939
)
3040

3141
type TakeoverTestSuit struct {
@@ -80,50 +90,60 @@ func (f *TakeoverTestSuit) TestFailover(t *testing.T) {
8090
time.Sleep(5 * time.Second)
8191

8292
// check the container by requesting its wait url
83-
runArgs := tool.GetRunArgs(t, imageName)
84-
resp, err := http.Get(runArgs.WaitURL)
85-
require.NoError(t, err, "access to the wait url of the recovered container")
86-
defer resp.Body.Close()
87-
if resp.StatusCode/100 != 2 {
88-
t.Fatalf("Failed to access the wait url of the recovered container")
89-
}
93+
checkContainerAccess(t, imageName)
9094
}
9195

92-
func (f *TakeoverTestSuit) TestHotUpgrade(t *testing.T) {
96+
func (f *TakeoverTestSuit) TestAPIHotUpgrade(t *testing.T) {
9397
imageName := f.testImage
9498

9599
containerName := uuid.NewString()
96100
tool.RunContainerSimple(t, imageName, snapshotter, containerName, false)
97101
defer f.rmContainer(containerName)
98102

99-
// hot upgrade nydusd
100-
newNydusdPath := os.Getenv("NEW_NYDUSD_BINARY_PATH")
101-
if newNydusdPath == "" {
102-
newNydusdPath = "target/release/nydusd"
103-
}
104-
nydusdPath, err := filepath.Abs("../" + newNydusdPath)
105-
require.NoErrorf(t, err, "get the abs path of new nydusd path (%s)", newNydusdPath)
106-
err = os.Chmod(nydusdPath, 0755)
107-
require.NoErrorf(t, err, "chmod nydusd binary file (%s)", nydusdPath)
103+
// api trigger hot upgrade nydusd
104+
for i := 0; i < HotUpgradeRepeatCount; i++ {
105+
nydusdPath := nydusdPaths[i%2]
106+
logrus.Debugf("API hot upgrade round %d, nydusd_path = %s", i+1, nydusdPath)
108107

109-
upgradeReq := &tool.UpgradeRequest{
110-
NydusdPath: nydusdPath,
111-
Version: getNydusdVersion(nydusdPath),
112-
Policy: "rolling",
108+
upgradeReq := &tool.UpgradeRequest{
109+
NydusdPath: nydusdPath,
110+
Version: getNydusdVersion(nydusdPath),
111+
Policy: "rolling",
112+
}
113+
err := f.snapshotterCli.Upgrade(upgradeReq)
114+
require.NoError(t, err, "call the snapshotter to upgrade nydus daemons")
115+
116+
// wait for the nydus daemons recover
117+
time.Sleep(5 * time.Second)
118+
119+
// check the container by requesting its wait url
120+
checkContainerAccess(t, imageName)
113121
}
114-
err = f.snapshotterCli.Upgrade(upgradeReq)
115-
require.NoError(t, err, "call the snapshotter to upgrade nydus daemons")
122+
}
116123

117-
// wait for the nydus daemons recover
118-
time.Sleep(5 * time.Second)
124+
func (f *TakeoverTestSuit) TestRestartSnapshotterHotUpgrade(t *testing.T) {
125+
imageName := f.testImage
119126

120-
// check the container by requesting its wait url
121-
runArgs := tool.GetRunArgs(t, imageName)
122-
resp, err := http.Get(runArgs.WaitURL)
123-
require.NoError(t, err, "access to the wait url of the recovered container")
124-
defer resp.Body.Close()
125-
if resp.StatusCode/100 != 2 {
126-
t.Fatalf("Failed to access the wait url of the recovered container")
127+
containerName := uuid.NewString()
128+
tool.RunContainerSimple(t, imageName, snapshotter, containerName, false)
129+
defer f.rmContainer(containerName)
130+
131+
// restart snapshotter trigger hot upgrade nydusd
132+
for i := 0; i < HotUpgradeRepeatCount; i++ {
133+
nydusdPath := nydusdPaths[i%2]
134+
logrus.Debugf("Restart hot upgrade round %d, nydusd_path = %s", i+1, nydusdPath)
135+
136+
setNydusdPathInConfig(t, nydusdPath)
137+
138+
cmd := exec.Command("sudo", "systemctl", "restart", "nydus-snapshotter")
139+
err := cmd.Run()
140+
require.NoError(t, err, "restart nydus-snapshotter")
141+
142+
// wait for the nydus daemons recover
143+
time.Sleep(5 * time.Second)
144+
145+
// check the container by requesting its wait url
146+
checkContainerAccess(t, imageName)
127147
}
128148
}
129149

@@ -140,6 +160,33 @@ func getNydusdVersion(nydusdPath string) string {
140160
return version
141161
}
142162

163+
func setNydusdPathInConfig(t *testing.T, newNydusdPath string) {
164+
data, err := os.ReadFile(configPath)
165+
require.NoError(t, err, "read snapshotter config.toml")
166+
167+
cfg := &config.SnapshotterConfig{}
168+
err = toml.Unmarshal(data, cfg)
169+
require.NoError(t, err, "unmarshal snapshotter config.toml")
170+
171+
cfg.DaemonConfig.NydusdPath = newNydusdPath
172+
173+
newData, err := toml.Marshal(cfg)
174+
require.NoError(t, err, "marshal config.toml")
175+
176+
err = os.WriteFile(configPath, newData, 0644)
177+
require.NoError(t, err, "write config.toml")
178+
}
179+
180+
func checkContainerAccess(t *testing.T, imageName string) {
181+
runArgs := tool.GetRunArgs(t, imageName)
182+
resp, err := http.Get(runArgs.WaitURL)
183+
require.NoError(t, err, "access to the wait url of the recovered container")
184+
defer resp.Body.Close()
185+
if resp.StatusCode/100 != 2 {
186+
t.Fatalf("Failed to access the wait url of the recovered container")
187+
}
188+
}
189+
143190
func TestTakeover(t *testing.T) {
144191
if v, ok := os.LookupEnv("TAKEOVER_TEST"); !ok || v != "true" {
145192
t.Skip("skipping takeover test")
@@ -156,6 +203,19 @@ func TestTakeover(t *testing.T) {
156203
if snapshotterSystemSock == "" {
157204
snapshotterSystemSock = defaultSnapshotterSystemSock
158205
}
206+
newNydusdPath := os.Getenv("NEW_NYDUSD_BINARY_PATH")
207+
if newNydusdPath == "" {
208+
newNydusdPath = "target/release/nydusd"
209+
}
210+
absNewNydusdPath, err := filepath.Abs("../" + newNydusdPath)
211+
require.NoErrorf(t, err, "get the abs path of new nydusd path (%s)", newNydusdPath)
212+
err = os.Chmod(absNewNydusdPath, 0755)
213+
require.NoErrorf(t, err, "chmod nydusd binary file (%s)", absNewNydusdPath)
214+
215+
nydusdPaths = []string{
216+
absNewNydusdPath,
217+
"/usr/local/bin/nydusd",
218+
}
159219
suite := NewTakeoverTestSuit(t)
160220
defer suite.clear()
161221

0 commit comments

Comments
 (0)