@@ -8,24 +8,34 @@ import (
8
8
"fmt"
9
9
"net/http"
10
10
"os"
11
+ "os/exec"
11
12
"path/filepath"
12
13
"strings"
13
14
"testing"
14
15
"time"
15
16
17
+ "github.com/containerd/nydus-snapshotter/config"
16
18
"github.com/dragonflyoss/nydus/smoke/tests/tool"
17
19
"github.com/dragonflyoss/nydus/smoke/tests/tool/test"
18
20
"github.com/google/uuid"
21
+ "github.com/pelletier/go-toml"
22
+ "github.com/sirupsen/logrus"
19
23
"github.com/stretchr/testify/require"
20
24
)
21
25
22
26
// Environment Requirement: Containerd, nerdctl >= 0.22, nydus-snapshotter, nydusd.
23
27
// Prepare: setup nydus for containerd, reference: https://github.com/dragonflyoss/nydus/blob/master/docs/containerd-env-setup.md.
24
28
29
+ const (
30
+ hotUpgradeRepeatCount = 6
31
+ configPath = "/etc/nydus/config.toml"
32
+ )
33
+
25
34
var (
26
35
snapshotter string
27
36
takeoverTestImage string
28
37
snapshotterSystemSock string
38
+ nydusdPaths []string
29
39
)
30
40
31
41
type TakeoverTestSuit struct {
@@ -80,50 +90,60 @@ func (f *TakeoverTestSuit) TestFailover(t *testing.T) {
80
90
time .Sleep (5 * time .Second )
81
91
82
92
// 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 )
90
94
}
91
95
92
- func (f * TakeoverTestSuit ) TestHotUpgrade (t * testing.T ) {
96
+ func (f * TakeoverTestSuit ) TestAPIHotUpgrade (t * testing.T ) {
93
97
imageName := f .testImage
94
98
95
99
containerName := uuid .NewString ()
96
100
tool .RunContainerSimple (t , imageName , snapshotter , containerName , false )
97
101
defer f .rmContainer (containerName )
98
102
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 )
108
107
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 )
113
121
}
114
- err = f .snapshotterCli .Upgrade (upgradeReq )
115
- require .NoError (t , err , "call the snapshotter to upgrade nydus daemons" )
122
+ }
116
123
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
119
126
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 )
127
147
}
128
148
}
129
149
@@ -140,6 +160,33 @@ func getNydusdVersion(nydusdPath string) string {
140
160
return version
141
161
}
142
162
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
+
143
190
func TestTakeover (t * testing.T ) {
144
191
if v , ok := os .LookupEnv ("TAKEOVER_TEST" ); ! ok || v != "true" {
145
192
t .Skip ("skipping takeover test" )
@@ -156,6 +203,19 @@ func TestTakeover(t *testing.T) {
156
203
if snapshotterSystemSock == "" {
157
204
snapshotterSystemSock = defaultSnapshotterSystemSock
158
205
}
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
+ }
159
219
suite := NewTakeoverTestSuit (t )
160
220
defer suite .clear ()
161
221
0 commit comments