gitee.com/mysnapcore/mysnapd@v0.1.0/cmd/snap/cmd_debug_seeding_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2020 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package main_test 21 22 import ( 23 "fmt" 24 "io/ioutil" 25 "net/http" 26 27 . "gopkg.in/check.v1" 28 29 snap "gitee.com/mysnapcore/mysnapd/cmd/snap" 30 ) 31 32 var newPreseedNewSnapdSameSysKey = ` 33 { 34 "result": { 35 "preseed-start-time": "2020-07-24T21:41:33.838194712Z", 36 "preseed-system-key": { 37 "apparmor-features": [ 38 "caps", 39 "dbus", 40 "domain", 41 "file", 42 "mount", 43 "namespaces", 44 "network", 45 "network_v8", 46 "policy", 47 "ptrace", 48 "query", 49 "rlimit", 50 "signal" 51 ], 52 "apparmor-parser-features": [ 53 "unsafe" 54 ], 55 "apparmor-parser-mtime": 1589907589, 56 "build-id": "cb94e5eeee4cf7ecda53f8308a984cb155b55732", 57 "cgroup-version": "1", 58 "nfs-home": false, 59 "overlay-root": "", 60 "seccomp-compiler-version": "e6e309ad8aee052e5aa695dfaa040328ae1559c5 2.4.3 9b218ef9a4e508dd8a7f848095cb8875d10a4bf28428ad81fdc3f8dac89108f7 bpf-actlog", 61 "seccomp-features": [ 62 "allow", 63 "errno", 64 "kill_process", 65 "kill_thread", 66 "log", 67 "trace", 68 "trap", 69 "user_notif" 70 ], 71 "version": 10 72 }, 73 "preseed-time": "2020-07-24T21:41:43.156401424Z", 74 "preseeded": true, 75 "seed-restart-system-key": { 76 "apparmor-features": [ 77 "caps", 78 "dbus", 79 "domain", 80 "file", 81 "mount", 82 "namespaces", 83 "network", 84 "network_v8", 85 "policy", 86 "ptrace", 87 "query", 88 "rlimit", 89 "signal" 90 ], 91 "apparmor-parser-features": [ 92 "unsafe" 93 ], 94 "apparmor-parser-mtime": 1589907589, 95 "build-id": "cb94e5eeee4cf7ecda53f8308a984cb155b55732", 96 "cgroup-version": "1", 97 "nfs-home": false, 98 "overlay-root": "", 99 "seccomp-compiler-version": "e6e309ad8aee052e5aa695dfaa040328ae1559c5 2.4.3 9b218ef9a4e508dd8a7f848095cb8875d10a4bf28428ad81fdc3f8dac89108f7 bpf-actlog", 100 "seccomp-features": [ 101 "allow", 102 "errno", 103 "kill_process", 104 "kill_thread", 105 "log", 106 "trace", 107 "trap", 108 "user_notif" 109 ], 110 "version": 10 111 }, 112 "seed-restart-time": "2020-07-24T21:42:16.646098923Z", 113 "seed-start-time": "0001-01-01T00:00:00Z", 114 "seed-time": "2020-07-24T21:42:20.518607Z", 115 "seeded": true 116 }, 117 "status": "OK", 118 "status-code": 200, 119 "type": "sync" 120 }` 121 122 var newPreseedNewSnapdDiffSysKey = ` 123 { 124 "result": { 125 "preseed-start-time": "2020-07-24T21:41:33.838194712Z", 126 "preseed-system-key": { 127 "apparmor-features": [ 128 "caps", 129 "dbus", 130 "domain", 131 "file", 132 "mount", 133 "namespaces", 134 "network", 135 "network_v8", 136 "policy", 137 "ptrace", 138 "query", 139 "rlimit", 140 "signal" 141 ], 142 "apparmor-parser-features": [ 143 "unsafe" 144 ], 145 "apparmor-parser-mtime": 1589907589, 146 "build-id": "cb94e5eeee4cf7ecda53f8308a984cb155b55732", 147 "cgroup-version": "1", 148 "nfs-home": false, 149 "overlay-root": "", 150 "seccomp-compiler-version": "e6e309ad8aee052e5aa695dfaa040328ae1559c5 2.4.3 9b218ef9a4e508dd8a7f848095cb8875d10a4bf28428ad81fdc3f8dac89108f7 bpf-actlog", 151 "seccomp-features": [ 152 "allow", 153 "errno", 154 "kill_process", 155 "kill_thread", 156 "log", 157 "trace", 158 "trap", 159 "user_notif" 160 ], 161 "version": 10 162 }, 163 "preseed-time": "2020-07-24T21:41:43.156401424Z", 164 "preseeded": true, 165 "seed-restart-system-key": { 166 "apparmor-features": [ 167 "caps", 168 "dbus", 169 "domain", 170 "file", 171 "mount", 172 "namespaces", 173 "network", 174 "policy", 175 "ptrace", 176 "query", 177 "rlimit", 178 "signal" 179 ], 180 "apparmor-parser-features": [ 181 "unsafe" 182 ], 183 "apparmor-parser-mtime": 1589907589, 184 "build-id": "cb94e5eeee4cf7ecda53f8308a984cb155b55732", 185 "cgroup-version": "1", 186 "nfs-home": false, 187 "overlay-root": "", 188 "seccomp-compiler-version": "e6e309ad8aee052e5aa695dfaa040328ae1559c5 2.4.3 9b218ef9a4e508dd8a7f848095cb8875d10a4bf28428ad81fdc3f8dac89108f7 bpf-actlog", 189 "seccomp-features": [ 190 "allow", 191 "errno", 192 "kill", 193 "log", 194 "trace", 195 "trap", 196 "user_notif" 197 ], 198 "version": 10 199 }, 200 "seed-restart-time": "2020-07-24T21:42:16.646098923Z", 201 "seed-start-time": "0001-01-01T00:00:00Z", 202 "seed-time": "2020-07-24T21:42:20.518607Z", 203 "seeded": true 204 }, 205 "status": "OK", 206 "status-code": 200, 207 "type": "sync" 208 }` 209 210 // a system that was not preseeded at all 211 var noPreseedingJSON = ` 212 { 213 "result": { 214 "seed-time": "2019-07-04T19:16:10.548793375-05:00", 215 "seeded": true 216 }, 217 "status": "OK", 218 "status-code": 200, 219 "type": "sync" 220 }` 221 222 var seedingError = `{ 223 "result": { 224 "preseed-start-time": "2020-07-24T21:41:33.838194712Z", 225 "preseed-time": "2020-07-24T21:41:43.156401424Z", 226 "preseeded": true, 227 "seed-error": "cannot perform the following tasks:\n- xxx" 228 }, 229 "status": "OK", 230 "status-code": 200, 231 "type": "sync" 232 }` 233 234 // a system that was preseeded, but didn't record the new keys 235 // this is the case for a system that was preseeded and then seeded with an old 236 // snapd, but then is refreshed to a version of snapd that supports snap debug 237 // seeding, where we want to still have sensible output 238 var oldPreseedingJSON = `{ 239 "result": { 240 "preseed-start-time": "0001-01-01T00:00:00Z", 241 "preseed-time": "0001-01-01T00:00:00Z", 242 "seed-restart-time": "2019-07-04T19:14:10.548793375-05:00", 243 "seed-start-time": "0001-01-01T00:00:00Z", 244 "seed-time": "2019-07-04T19:16:10.548793375-05:00", 245 "seeded": true, 246 "preseeded": true 247 }, 248 "status": "OK", 249 "status-code": 200, 250 "type": "sync" 251 }` 252 253 var stillSeeding = `{ 254 "result": { 255 "preseed-start-time": "2020-07-24T21:41:33.838194712Z", 256 "preseed-time": "2020-07-24T21:41:43.156401424Z", 257 "preseeded": true 258 }, 259 "status": "OK", 260 "status-code": 200, 261 "type": "sync" 262 }` 263 264 var stillSeedingNoPreseed = `{ 265 "result": {}, 266 "status": "OK", 267 "status-code": 200, 268 "type": "sync" 269 }` 270 271 func (s *SnapSuite) TestDebugSeeding(c *C) { 272 tt := []struct { 273 jsonResp string 274 expStdout string 275 expStderr string 276 expErr string 277 comment string 278 hasUnicode bool 279 }{ 280 { 281 jsonResp: newPreseedNewSnapdSameSysKey, 282 expStdout: ` 283 seeded: true 284 preseeded: true 285 image-preseeding: 9.318s 286 seed-completion: 3.873s 287 `[1:], 288 comment: "new preseed keys, same system-key", 289 }, 290 { 291 jsonResp: newPreseedNewSnapdDiffSysKey, 292 expStdout: ` 293 seeded: true 294 preseeded: true 295 image-preseeding: 9.318s 296 seed-completion: 3.873s 297 preseed-system-key: { 298 "apparmor-features": [ 299 "caps", 300 "dbus", 301 "domain", 302 "file", 303 "mount", 304 "namespaces", 305 "network", 306 "network_v8", 307 "policy", 308 "ptrace", 309 "query", 310 "rlimit", 311 "signal" 312 ], 313 "apparmor-parser-features": [ 314 "unsafe" 315 ], 316 "apparmor-parser-mtime": 1589907589, 317 "build-id": "cb94e5eeee4cf7ecda53f8308a984cb155b55732", 318 "cgroup-version": "1", 319 "nfs-home": false, 320 "overlay-root": "", 321 "seccomp-compiler-version": "e6e309ad8aee052e5aa695dfaa040328ae1559c5 2.4.3 9b218ef9a4e508dd8a7f848095cb8875d10a4bf28428ad81fdc3f8dac89108f7 bpf-actlog", 322 "seccomp-features": [ 323 "allow", 324 "errno", 325 "kill_process", 326 "kill_thread", 327 "log", 328 "trace", 329 "trap", 330 "user_notif" 331 ], 332 "version": 10 333 } 334 seed-restart-system-key: { 335 "apparmor-features": [ 336 "caps", 337 "dbus", 338 "domain", 339 "file", 340 "mount", 341 "namespaces", 342 "network", 343 "policy", 344 "ptrace", 345 "query", 346 "rlimit", 347 "signal" 348 ], 349 "apparmor-parser-features": [ 350 "unsafe" 351 ], 352 "apparmor-parser-mtime": 1589907589, 353 "build-id": "cb94e5eeee4cf7ecda53f8308a984cb155b55732", 354 "cgroup-version": "1", 355 "nfs-home": false, 356 "overlay-root": "", 357 "seccomp-compiler-version": "e6e309ad8aee052e5aa695dfaa040328ae1559c5 2.4.3 9b218ef9a4e508dd8a7f848095cb8875d10a4bf28428ad81fdc3f8dac89108f7 bpf-actlog", 358 "seccomp-features": [ 359 "allow", 360 "errno", 361 "kill", 362 "log", 363 "trace", 364 "trap", 365 "user_notif" 366 ], 367 "version": 10 368 } 369 `[1:], 370 comment: "new preseed keys, different system-key", 371 }, 372 { 373 jsonResp: noPreseedingJSON, 374 expStdout: ` 375 seeded: true 376 preseeded: false 377 seed-completion: -- 378 `[1:], 379 comment: "not preseeded no unicode", 380 }, 381 { 382 jsonResp: noPreseedingJSON, 383 expStdout: ` 384 seeded: true 385 preseeded: false 386 seed-completion: – 387 `[1:], 388 comment: "not preseeded", 389 hasUnicode: true, 390 }, 391 { 392 jsonResp: oldPreseedingJSON, 393 expStdout: ` 394 seeded: true 395 preseeded: true 396 image-preseeding: 0s 397 seed-completion: 2m0s 398 `[1:], 399 comment: "old preseeded json", 400 }, 401 { 402 jsonResp: stillSeeding, 403 expStdout: ` 404 seeded: false 405 preseeded: true 406 image-preseeding: 9.318s 407 seed-completion: -- 408 `[1:], 409 comment: "preseeded, still seeding no unicode", 410 }, 411 { 412 jsonResp: stillSeeding, 413 expStdout: ` 414 seeded: false 415 preseeded: true 416 image-preseeding: 9.318s 417 seed-completion: – 418 `[1:], 419 hasUnicode: true, 420 comment: "preseeded, still seeding", 421 }, 422 { 423 jsonResp: stillSeedingNoPreseed, 424 expStdout: ` 425 seeded: false 426 preseeded: false 427 seed-completion: -- 428 `[1:], 429 comment: "not preseeded, still seeding no unicode", 430 }, 431 { 432 jsonResp: stillSeedingNoPreseed, 433 expStdout: ` 434 seeded: false 435 preseeded: false 436 seed-completion: – 437 `[1:], 438 hasUnicode: true, 439 comment: "not preseeded, still seeding", 440 }, 441 { 442 jsonResp: seedingError, 443 expStdout: ` 444 seeded: false 445 seed-error: | 446 cannot perform the following tasks: 447 - xxx 448 preseeded: true 449 image-preseeding: 9.318s 450 seed-completion: -- 451 `[1:], 452 comment: "preseeded, error during seeding", 453 }, 454 } 455 456 for _, t := range tt { 457 comment := Commentf(t.comment) 458 n := 0 459 s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) { 460 n++ 461 switch n { 462 case 1: 463 c.Assert(r.Method, Equals, "GET", comment) 464 c.Assert(r.URL.Path, Equals, "/v2/debug", comment) 465 c.Assert(r.URL.RawQuery, Equals, "aspect=seeding", comment) 466 data, err := ioutil.ReadAll(r.Body) 467 c.Assert(err, IsNil, comment) 468 c.Assert(string(data), Equals, "", comment) 469 fmt.Fprintln(w, t.jsonResp) 470 default: 471 c.Fatalf("expected to get 1 request, now on %d", n) 472 } 473 }) 474 args := []string{"debug", "seeding"} 475 if t.hasUnicode { 476 args = append(args, "--unicode=always") 477 } 478 rest, err := snap.Parser(snap.Client()).ParseArgs(args) 479 if t.expErr != "" { 480 c.Assert(err, ErrorMatches, t.expErr, comment) 481 c.Assert(s.Stdout(), Equals, "", comment) 482 c.Assert(s.Stderr(), Equals, t.expStderr, comment) 483 continue 484 } 485 c.Assert(err, IsNil, comment) 486 c.Assert(rest, DeepEquals, []string{}, comment) 487 c.Assert(s.Stdout(), Equals, t.expStdout, comment) 488 c.Assert(s.Stderr(), Equals, "", comment) 489 c.Assert(n, Equals, 1, comment) 490 491 s.ResetStdStreams() 492 } 493 }