Merge pull request #14829 from saschagrunert/errors-hack-test-utils

hack/test/utils: switch to golang native error wrapping
This commit is contained in:
openshift-ci[bot]
2022-07-05 11:37:12 +00:00
committed by GitHub
6 changed files with 34 additions and 36 deletions

View File

@ -1,10 +1,10 @@
package registry package registry
import ( import (
"fmt"
"strings" "strings"
"github.com/containers/podman/v4/utils" "github.com/containers/podman/v4/utils"
"github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -57,7 +57,7 @@ func StartWithOptions(options *Options) (*Registry, error) {
// Start a registry. // Start a registry.
out, err := utils.ExecCmd(binary, args...) out, err := utils.ExecCmd(binary, args...)
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "error running %q: %s", binary, out) return nil, fmt.Errorf("error running %q: %s: %w", binary, out, err)
} }
// Parse the output. // Parse the output.
@ -68,7 +68,7 @@ func StartWithOptions(options *Options) (*Registry, error) {
} }
spl := strings.Split(s, "=") spl := strings.Split(s, "=")
if len(spl) != 2 { if len(spl) != 2 {
return nil, errors.Errorf("unexpected output format %q: want 'PODMAN_...=...'", s) return nil, fmt.Errorf("unexpected output format %q: want 'PODMAN_...=...'", s)
} }
key := spl[0] key := spl[0]
val := strings.TrimSuffix(strings.TrimPrefix(spl[1], "\""), "\"") val := strings.TrimSuffix(strings.TrimPrefix(spl[1], "\""), "\"")
@ -88,16 +88,16 @@ func StartWithOptions(options *Options) (*Registry, error) {
// Extra sanity check. // Extra sanity check.
if registry.Image == "" { if registry.Image == "" {
return nil, errors.Errorf("unexpected output %q: %q missing", out, ImageKey) return nil, fmt.Errorf("unexpected output %q: %q missing", out, ImageKey)
} }
if registry.User == "" { if registry.User == "" {
return nil, errors.Errorf("unexpected output %q: %q missing", out, UserKey) return nil, fmt.Errorf("unexpected output %q: %q missing", out, UserKey)
} }
if registry.Password == "" { if registry.Password == "" {
return nil, errors.Errorf("unexpected output %q: %q missing", out, PassKey) return nil, fmt.Errorf("unexpected output %q: %q missing", out, PassKey)
} }
if registry.Port == "" { if registry.Port == "" {
return nil, errors.Errorf("unexpected output %q: %q missing", out, PortKey) return nil, fmt.Errorf("unexpected output %q: %q missing", out, PortKey)
} }
registry.running = true registry.running = true
@ -112,7 +112,7 @@ func (r *Registry) Stop() error {
return nil return nil
} }
if _, err := utils.ExecCmd(binary, "-P", r.Port, "stop"); err != nil { if _, err := utils.ExecCmd(binary, "-P", r.Port, "stop"); err != nil {
return errors.Wrapf(err, "error stopping registry (%v) with %q", *r, binary) return fmt.Errorf("error stopping registry (%v) with %q: %w", *r, binary, err)
} }
r.running = false r.running = false
return nil return nil

View File

@ -2,6 +2,7 @@ package integration
import ( import (
"bytes" "bytes"
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"math/rand" "math/rand"
@ -30,7 +31,6 @@ import (
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
. "github.com/onsi/gomega/gexec" . "github.com/onsi/gomega/gexec"
"github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -618,14 +618,14 @@ func (p *PodmanTestIntegration) RunHealthCheck(cid string) error {
restart := p.Podman([]string{"restart", cid}) restart := p.Podman([]string{"restart", cid})
restart.WaitWithDefaultTimeout() restart.WaitWithDefaultTimeout()
if restart.ExitCode() != 0 { if restart.ExitCode() != 0 {
return errors.Errorf("unable to restart %s", cid) return fmt.Errorf("unable to restart %s", cid)
} }
} }
} }
fmt.Printf("Waiting for %s to pass healthcheck\n", cid) fmt.Printf("Waiting for %s to pass healthcheck\n", cid)
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
} }
return errors.Errorf("unable to detect %s as running", cid) return fmt.Errorf("unable to detect %s as running", cid)
} }
func (p *PodmanTestIntegration) CreateSeccompJSON(in []byte) (string, error) { func (p *PodmanTestIntegration) CreateSeccompJSON(in []byte) (string, error) {

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@ -8,7 +9,6 @@ import (
"time" "time"
"github.com/docker/go-plugins-helpers/volume" "github.com/docker/go-plugins-helpers/volume"
"github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -80,16 +80,16 @@ func startServer(socketPath string) error {
if config.path == "" { if config.path == "" {
path, err := ioutil.TempDir("", "test_volume_plugin") path, err := ioutil.TempDir("", "test_volume_plugin")
if err != nil { if err != nil {
return errors.Wrapf(err, "error getting directory for plugin") return fmt.Errorf("error getting directory for plugin: %w", err)
} }
config.path = path config.path = path
} else { } else {
pathStat, err := os.Stat(config.path) pathStat, err := os.Stat(config.path)
if err != nil { if err != nil {
return errors.Wrapf(err, "unable to access requested plugin state directory") return fmt.Errorf("unable to access requested plugin state directory: %w", err)
} }
if !pathStat.IsDir() { if !pathStat.IsDir() {
return errors.Errorf("cannot use %v as plugin state dir as it is not a directory", config.path) return fmt.Errorf("cannot use %v as plugin state dir as it is not a directory", config.path)
} }
} }
@ -98,7 +98,7 @@ func startServer(socketPath string) error {
server := volume.NewHandler(handle) server := volume.NewHandler(handle)
if err := server.ServeUnix(socketPath, 0); err != nil { if err := server.ServeUnix(socketPath, 0); err != nil {
return errors.Wrapf(err, "error starting server") return fmt.Errorf("error starting server: %w", err)
} }
return nil return nil
} }
@ -147,7 +147,7 @@ func (d *DirDriver) Create(opts *volume.CreateRequest) error {
logrus.Infof("Hit Create() endpoint") logrus.Infof("Hit Create() endpoint")
if _, exists := d.volumes[opts.Name]; exists { if _, exists := d.volumes[opts.Name]; exists {
return errors.Errorf("volume with name %s already exists", opts.Name) return fmt.Errorf("volume with name %s already exists", opts.Name)
} }
newVol := new(dirVol) newVol := new(dirVol)
@ -161,7 +161,7 @@ func (d *DirDriver) Create(opts *volume.CreateRequest) error {
volPath := filepath.Join(d.volumesPath, opts.Name) volPath := filepath.Join(d.volumesPath, opts.Name)
if err := os.Mkdir(volPath, 0755); err != nil { if err := os.Mkdir(volPath, 0755); err != nil {
return errors.Wrapf(err, "error making volume directory") return fmt.Errorf("error making volume directory: %w", err)
} }
newVol.path = volPath newVol.path = volPath
@ -204,7 +204,7 @@ func (d *DirDriver) Get(req *volume.GetRequest) (*volume.GetResponse, error) {
vol, exists := d.volumes[req.Name] vol, exists := d.volumes[req.Name]
if !exists { if !exists {
logrus.Debugf("Did not find volume %s", req.Name) logrus.Debugf("Did not find volume %s", req.Name)
return nil, errors.Errorf("no volume with name %s found", req.Name) return nil, fmt.Errorf("no volume with name %s found", req.Name)
} }
logrus.Debugf("Found volume %s", req.Name) logrus.Debugf("Found volume %s", req.Name)
@ -228,19 +228,19 @@ func (d *DirDriver) Remove(req *volume.RemoveRequest) error {
vol, exists := d.volumes[req.Name] vol, exists := d.volumes[req.Name]
if !exists { if !exists {
logrus.Debugf("Did not find volume %s", req.Name) logrus.Debugf("Did not find volume %s", req.Name)
return errors.Errorf("no volume with name %s found", req.Name) return fmt.Errorf("no volume with name %s found", req.Name)
} }
logrus.Debugf("Found volume %s", req.Name) logrus.Debugf("Found volume %s", req.Name)
if len(vol.mounts) > 0 { if len(vol.mounts) > 0 {
logrus.Debugf("Cannot remove %s, is mounted", req.Name) logrus.Debugf("Cannot remove %s, is mounted", req.Name)
return errors.Errorf("volume %s is mounted and cannot be removed", req.Name) return fmt.Errorf("volume %s is mounted and cannot be removed", req.Name)
} }
delete(d.volumes, req.Name) delete(d.volumes, req.Name)
if err := os.RemoveAll(vol.path); err != nil { if err := os.RemoveAll(vol.path); err != nil {
return errors.Wrapf(err, "error removing mountpoint of volume %s", req.Name) return fmt.Errorf("error removing mountpoint of volume %s: %w", req.Name, err)
} }
logrus.Debugf("Removed volume %s", req.Name) logrus.Debugf("Removed volume %s", req.Name)
@ -260,7 +260,7 @@ func (d *DirDriver) Path(req *volume.PathRequest) (*volume.PathResponse, error)
vol, exists := d.volumes[req.Name] vol, exists := d.volumes[req.Name]
if !exists { if !exists {
logrus.Debugf("Cannot locate volume %s", req.Name) logrus.Debugf("Cannot locate volume %s", req.Name)
return nil, errors.Errorf("no volume with name %s found", req.Name) return nil, fmt.Errorf("no volume with name %s found", req.Name)
} }
return &volume.PathResponse{ return &volume.PathResponse{
@ -278,7 +278,7 @@ func (d *DirDriver) Mount(req *volume.MountRequest) (*volume.MountResponse, erro
vol, exists := d.volumes[req.Name] vol, exists := d.volumes[req.Name]
if !exists { if !exists {
logrus.Debugf("Cannot locate volume %s", req.Name) logrus.Debugf("Cannot locate volume %s", req.Name)
return nil, errors.Errorf("no volume with name %s found", req.Name) return nil, fmt.Errorf("no volume with name %s found", req.Name)
} }
vol.mounts[req.ID] = true vol.mounts[req.ID] = true
@ -298,13 +298,13 @@ func (d *DirDriver) Unmount(req *volume.UnmountRequest) error {
vol, exists := d.volumes[req.Name] vol, exists := d.volumes[req.Name]
if !exists { if !exists {
logrus.Debugf("Cannot locate volume %s", req.Name) logrus.Debugf("Cannot locate volume %s", req.Name)
return errors.Errorf("no volume with name %s found", req.Name) return fmt.Errorf("no volume with name %s found", req.Name)
} }
mount := vol.mounts[req.ID] mount := vol.mounts[req.ID]
if !mount { if !mount {
logrus.Debugf("Volume %s is not mounted by %s", req.Name, req.ID) logrus.Debugf("Volume %s is not mounted by %s", req.Name, req.ID)
return errors.Errorf("volume %s is not mounted by %s", req.Name, req.ID) return fmt.Errorf("volume %s is not mounted by %s", req.Name, req.ID)
} }
delete(vol.mounts, req.ID) delete(vol.mounts, req.ID)

View File

@ -1,26 +1,25 @@
package utils package utils
import ( import (
"fmt"
"net" "net"
"strconv" "strconv"
"github.com/pkg/errors"
) )
// Find a random, open port on the host. // Find a random, open port on the host.
func GetRandomPort() (int, error) { func GetRandomPort() (int, error) {
l, err := net.Listen("tcp", ":0") l, err := net.Listen("tcp", ":0")
if err != nil { if err != nil {
return 0, errors.Wrapf(err, "unable to get free TCP port") return 0, fmt.Errorf("unable to get free TCP port: %w", err)
} }
defer l.Close() defer l.Close()
_, randomPort, err := net.SplitHostPort(l.Addr().String()) _, randomPort, err := net.SplitHostPort(l.Addr().String())
if err != nil { if err != nil {
return 0, errors.Wrapf(err, "unable to determine free port") return 0, fmt.Errorf("unable to determine free port: %w", err)
} }
rp, err := strconv.Atoi(randomPort) rp, err := strconv.Atoi(randomPort)
if err != nil { if err != nil {
return 0, errors.Wrapf(err, "unable to convert random port to int") return 0, fmt.Errorf("unable to convert random port to int: %w", err)
} }
return rp, nil return rp, nil
} }

View File

@ -17,7 +17,6 @@ import (
"github.com/containers/podman/v4/pkg/rootless" "github.com/containers/podman/v4/pkg/rootless"
systemdDbus "github.com/coreos/go-systemd/v22/dbus" systemdDbus "github.com/coreos/go-systemd/v22/dbus"
"github.com/godbus/dbus/v5" "github.com/godbus/dbus/v5"
"github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -77,7 +76,7 @@ func getCgroupProcess(procFile string, allowRoot bool) (string, error) {
line := scanner.Text() line := scanner.Text()
parts := strings.SplitN(line, ":", 3) parts := strings.SplitN(line, ":", 3)
if len(parts) != 3 { if len(parts) != 3 {
return "", errors.Errorf("cannot parse cgroup line %q", line) return "", fmt.Errorf("cannot parse cgroup line %q", line)
} }
if strings.HasPrefix(line, "0::") { if strings.HasPrefix(line, "0::") {
cgroup = line[3:] cgroup = line[3:]
@ -88,7 +87,7 @@ func getCgroupProcess(procFile string, allowRoot bool) (string, error) {
} }
} }
if len(cgroup) == 0 || (!allowRoot && cgroup == "/") { if len(cgroup) == 0 || (!allowRoot && cgroup == "/") {
return "", errors.Errorf("could not find cgroup mount in %q", procFile) return "", fmt.Errorf("could not find cgroup mount in %q", procFile)
} }
return cgroup, nil return cgroup, nil
} }
@ -133,7 +132,7 @@ func moveUnderCgroup(cgroup, subtree string, processes []uint32) error {
line := scanner.Text() line := scanner.Text()
parts := strings.SplitN(line, ":", 3) parts := strings.SplitN(line, ":", 3)
if len(parts) != 3 { if len(parts) != 3 {
return errors.Errorf("cannot parse cgroup line %q", line) return fmt.Errorf("cannot parse cgroup line %q", line)
} }
// root cgroup, skip it // root cgroup, skip it

View File

@ -3,7 +3,7 @@
package utils package utils
import "github.com/pkg/errors" import "errors"
func RunUnderSystemdScope(pid int, slice string, unitName string) error { func RunUnderSystemdScope(pid int, slice string, unitName string) error {
return errors.New("not implemented for windows") return errors.New("not implemented for windows")