(t *testing.T, configTOML string)
| 42 | ) |
| 43 | |
| 44 | func newDaemonWithConfig(t *testing.T, configTOML string) (*Client, *daemon, func()) { |
| 45 | if testing.Short() { |
| 46 | t.Skip() |
| 47 | } |
| 48 | testutil.RequiresRoot(t) |
| 49 | var ( |
| 50 | ctrd = daemon{} |
| 51 | configTOMLDecoded srvconfig.Config |
| 52 | buf = bytes.NewBuffer(nil) |
| 53 | ) |
| 54 | |
| 55 | tempDir := t.TempDir() |
| 56 | |
| 57 | configTOMLFile := filepath.Join(tempDir, "config.toml") |
| 58 | if err := os.WriteFile(configTOMLFile, []byte(configTOML), 0600); err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | |
| 62 | if err := srvconfig.LoadConfig(context.TODO(), configTOMLFile, &configTOMLDecoded); err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | |
| 66 | var address string |
| 67 | if grpcPlugin, ok := configTOMLDecoded.Plugins["io.containerd.server.v1.grpc"].(map[string]any); ok { |
| 68 | address, _ = grpcPlugin["address"].(string) |
| 69 | } |
| 70 | if address == "" { |
| 71 | if runtime.GOOS == "windows" { |
| 72 | address = fmt.Sprintf(`\\.\pipe\containerd-containerd-test-%s`, filepath.Base(tempDir)) |
| 73 | } else { |
| 74 | address = filepath.Join(tempDir, "containerd.sock") |
| 75 | } |
| 76 | } |
| 77 | args := []string{"-c", configTOMLFile} |
| 78 | if configTOMLDecoded.Root == "" { |
| 79 | args = append(args, "--root", filepath.Join(tempDir, "root")) |
| 80 | } |
| 81 | if configTOMLDecoded.State == "" { |
| 82 | args = append(args, "--state", filepath.Join(tempDir, "state")) |
| 83 | } |
| 84 | if err := ctrd.start("containerd", address, args, buf, buf); err != nil { |
| 85 | t.Fatalf("%v: %s", err, buf.String()) |
| 86 | } |
| 87 | |
| 88 | waitCtx, waitCancel := context.WithTimeout(context.TODO(), 2*time.Second) |
| 89 | client, err := ctrd.waitForStart(waitCtx) |
| 90 | waitCancel() |
| 91 | if err != nil { |
| 92 | ctrd.Kill() |
| 93 | ctrd.Wait() |
| 94 | t.Fatalf("%v: %s", err, buf.String()) |
| 95 | } |
| 96 | |
| 97 | cleanup := func() { |
| 98 | if err := client.Close(); err != nil { |
| 99 | t.Errorf("failed to close client: %v", err) |
| 100 | } |
| 101 | if err := ctrd.Stop(); err != nil { |
no test coverage detected
searching dependent graphs…