| 25 | } |
| 26 | |
| 27 | func TestFactoryLoadContainer(t *testing.T) { |
| 28 | root := t.TempDir() |
| 29 | // setup default container config and state for mocking |
| 30 | var ( |
| 31 | id = "1" |
| 32 | expectedHooks = configs.Hooks{ |
| 33 | configs.Prestart: configs.HookList{ |
| 34 | configs.CommandHook{Command: &configs.Command{Path: "prestart-hook"}}, |
| 35 | }, |
| 36 | configs.Poststart: configs.HookList{ |
| 37 | configs.CommandHook{Command: &configs.Command{Path: "poststart-hook"}}, |
| 38 | }, |
| 39 | configs.Poststop: configs.HookList{ |
| 40 | unserializableHook{}, |
| 41 | configs.CommandHook{Command: &configs.Command{Path: "poststop-hook"}}, |
| 42 | }, |
| 43 | } |
| 44 | expectedConfig = &configs.Config{ |
| 45 | Rootfs: "/mycontainer/root", |
| 46 | Hooks: expectedHooks, |
| 47 | Cgroups: &cgroups.Cgroup{ |
| 48 | Resources: &cgroups.Resources{}, |
| 49 | }, |
| 50 | } |
| 51 | expectedState = &State{ |
| 52 | BaseState: BaseState{ |
| 53 | InitProcessPid: 1024, |
| 54 | Config: *expectedConfig, |
| 55 | }, |
| 56 | } |
| 57 | ) |
| 58 | if err := os.Mkdir(filepath.Join(root, id), 0o700); err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | if err := marshal(filepath.Join(root, id, stateFilename), expectedState); err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | container, err := Load(root, id) |
| 65 | if err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
| 68 | if container.ID() != id { |
| 69 | t.Fatalf("expected container id %q but received %q", id, container.ID()) |
| 70 | } |
| 71 | config := container.Config() |
| 72 | if config.Rootfs != expectedConfig.Rootfs { |
| 73 | t.Fatalf("expected rootfs %q but received %q", expectedConfig.Rootfs, config.Rootfs) |
| 74 | } |
| 75 | expectedHooks[configs.Poststop] = expectedHooks[configs.Poststop][1:] // expect unserializable hook to be skipped |
| 76 | if !reflect.DeepEqual(config.Hooks, expectedHooks) { |
| 77 | t.Fatalf("expects hooks %q but received %q", expectedHooks, config.Hooks) |
| 78 | } |
| 79 | if container.initProcess.pid() != expectedState.InitProcessPid { |
| 80 | t.Fatalf("expected init pid %d but received %d", expectedState.InitProcessPid, container.initProcess.pid()) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func marshal(path string, v any) error { |