From 472f79f08e2fbb0fcc07abbe6fed221e18a4f36f Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Wed, 22 Jun 2022 13:21:45 +0200 Subject: test/testvol: move Containerfile into testvol dir I think it is confusion to have this Containerfile in the repo root. It is used for the tests only so we should move it into the same dir. Also adapt the Makefile target to use the new path and add the current date as tag instead of using latest which can break CI easily when we have to update the image. Signed-off-by: Paul Holzinger --- test/testvol/Containerfile | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 test/testvol/Containerfile (limited to 'test/testvol/Containerfile') diff --git a/test/testvol/Containerfile b/test/testvol/Containerfile new file mode 100644 index 000000000..6ff45064b --- /dev/null +++ b/test/testvol/Containerfile @@ -0,0 +1,10 @@ +FROM golang:1.15-alpine AS build-img +COPY ./test/testvol/ /go/src/github.com/containers/podman/cmd/testvol/ +COPY ./vendor /go/src/github.com/containers/podman/vendor/ +WORKDIR /go/src/github.com/containers/podman +RUN go build -o /testvol ./cmd/testvol + +FROM alpine +COPY --from=build-img /testvol /usr/local/bin +WORKDIR / +ENTRYPOINT ["/usr/local/bin/testvol"] -- cgit v1.2.3-54-g00ecf From fcc25afa55a57629359c4aac5864883b00eb15ba Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Wed, 22 Jun 2022 13:33:37 +0200 Subject: test/testvol: update container image golang to 1.18 Update the golang verion for the testvol image to the latest version 1.18. This requires us to build with GO111MODULE=off. Use the FQDN to prevent the shortnames prompt. Also add --network none to the podman build command to make sure we are only using the copied deps and nothing else. Signed-off-by: Paul Holzinger --- Makefile | 2 +- test/testvol/Containerfile | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'test/testvol/Containerfile') diff --git a/Makefile b/Makefile index 4d60ad4c8..a7d1ba59e 100644 --- a/Makefile +++ b/Makefile @@ -229,7 +229,7 @@ test/testvol/testvol: $(wildcard test/testvol/*.go) .PHONY: volume-plugin-test-img volume-plugin-test-img: - podman build -t quay.io/libpod/volume-plugin-test-img:$$(date +%Y%m%d) -f ./test/testvol/Containerfile . + ./bin/podman build --network none -t quay.io/libpod/volume-plugin-test-img:$$(date +%Y%m%d) -f ./test/testvol/Containerfile . .PHONY: test/goecho/goecho test/goecho/goecho: $(wildcard test/goecho/*.go) diff --git a/test/testvol/Containerfile b/test/testvol/Containerfile index 6ff45064b..de44798fe 100644 --- a/test/testvol/Containerfile +++ b/test/testvol/Containerfile @@ -1,8 +1,8 @@ -FROM golang:1.15-alpine AS build-img +FROM docker.io/library/golang:1.18-alpine AS build-img COPY ./test/testvol/ /go/src/github.com/containers/podman/cmd/testvol/ COPY ./vendor /go/src/github.com/containers/podman/vendor/ WORKDIR /go/src/github.com/containers/podman -RUN go build -o /testvol ./cmd/testvol +RUN GO111MODULE=off go build -o /testvol ./cmd/testvol FROM alpine COPY --from=build-img /testvol /usr/local/bin -- cgit v1.2.3-54-g00ecf From 6e8953abfc4693937c73e22ca6eddebf909d4d93 Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Wed, 22 Jun 2022 14:34:32 +0200 Subject: test/testvol: rework testvol binary Add 4 new subcommands to the testvol binary, instead of just serving the volume api it now also can create/list/remove plugins. This is required to test new functionality where volumes are create outside of podman in the plugin. Podman should then be able to pick up the new volumes. The new testvol commands are: - serve: serve the podman api like the the testvol command before - create: create a volume with the given name - list: list all volume names - remove: remove the volume with the given name Also make a small update to the testvol Containerfile so that it can build correctly. Signed-off-by: Paul Holzinger --- Makefile | 2 +- test/testvol/Containerfile | 7 +++---- test/testvol/create.go | 26 ++++++++++++++++++++++++++ test/testvol/list.go | 32 ++++++++++++++++++++++++++++++++ test/testvol/main.go | 23 +++++++++++++++-------- test/testvol/remove.go | 26 ++++++++++++++++++++++++++ test/testvol/util.go | 29 +++++++++++++++++++++++++++++ 7 files changed, 132 insertions(+), 13 deletions(-) create mode 100644 test/testvol/create.go create mode 100644 test/testvol/list.go create mode 100644 test/testvol/remove.go create mode 100644 test/testvol/util.go (limited to 'test/testvol/Containerfile') diff --git a/Makefile b/Makefile index a7d1ba59e..3ed522b32 100644 --- a/Makefile +++ b/Makefile @@ -225,7 +225,7 @@ test/checkseccomp/checkseccomp: $(wildcard test/checkseccomp/*.go) .PHONY: test/testvol/testvol test/testvol/testvol: $(wildcard test/testvol/*.go) - $(GOCMD) build $(BUILDFLAGS) $(GO_LDFLAGS) '$(LDFLAGS_PODMAN)' -o $@ ./test/testvol + $(GOCMD) build -o $@ ./test/testvol .PHONY: volume-plugin-test-img volume-plugin-test-img: diff --git a/test/testvol/Containerfile b/test/testvol/Containerfile index de44798fe..32448f5a9 100644 --- a/test/testvol/Containerfile +++ b/test/testvol/Containerfile @@ -1,10 +1,9 @@ FROM docker.io/library/golang:1.18-alpine AS build-img -COPY ./test/testvol/ /go/src/github.com/containers/podman/cmd/testvol/ -COPY ./vendor /go/src/github.com/containers/podman/vendor/ +COPY ./ /go/src/github.com/containers/podman/ WORKDIR /go/src/github.com/containers/podman -RUN GO111MODULE=off go build -o /testvol ./cmd/testvol +RUN GO111MODULE=off go build -o /testvol ./test/testvol FROM alpine COPY --from=build-img /testvol /usr/local/bin WORKDIR / -ENTRYPOINT ["/usr/local/bin/testvol"] +ENTRYPOINT ["/usr/local/bin/testvol", "serve"] diff --git a/test/testvol/create.go b/test/testvol/create.go new file mode 100644 index 000000000..d29300f0b --- /dev/null +++ b/test/testvol/create.go @@ -0,0 +1,26 @@ +package main + +import ( + pluginapi "github.com/docker/go-plugins-helpers/volume" + "github.com/spf13/cobra" +) + +var createCmd = &cobra.Command{ + Use: "create NAME", + Short: "create a volume", + Long: `Create a volume in the volume plugin listening on --sock-name`, + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + return createVol(config.sockName, args[0]) + }, +} + +func createVol(sockName, volName string) error { + plugin, err := getPlugin(sockName) + if err != nil { + return err + } + createReq := new(pluginapi.CreateRequest) + createReq.Name = volName + return plugin.CreateVolume(createReq) +} diff --git a/test/testvol/list.go b/test/testvol/list.go new file mode 100644 index 000000000..fea615a70 --- /dev/null +++ b/test/testvol/list.go @@ -0,0 +1,32 @@ +package main + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +var listCmd = &cobra.Command{ + Use: "list", + Short: "list all volumes", + Long: `List all volumes from the volume plugin listening on --sock-name`, + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, args []string) error { + return listVol(config.sockName) + }, +} + +func listVol(sockName string) error { + plugin, err := getPlugin(sockName) + if err != nil { + return err + } + vols, err := plugin.ListVolumes() + if err != nil { + return err + } + for _, vol := range vols { + fmt.Println(vol.Name) + } + return nil +} diff --git a/test/testvol/main.go b/test/testvol/main.go index 30ab365b3..99c6fb694 100644 --- a/test/testvol/main.go +++ b/test/testvol/main.go @@ -14,13 +14,20 @@ import ( ) var rootCmd = &cobra.Command{ - Use: "testvol", - Short: "testvol - volume plugin for Podman", + Use: "testvol", + Short: "testvol - volume plugin for Podman testing", + PersistentPreRunE: before, + SilenceUsage: true, +} + +var serveCmd = &cobra.Command{ + Use: "serve", + Short: "serve the volume plugin on the unix socket", Long: `Creates simple directory volumes using the Volume Plugin API for testing volume plugin functionality`, + Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { return startServer(config.sockName) }, - PersistentPreRunE: before, } // Configuration for the volume plugin @@ -37,9 +44,12 @@ var config = cliConfig{ } func init() { - rootCmd.Flags().StringVar(&config.sockName, "sock-name", config.sockName, "Name of unix socket for plugin") - rootCmd.Flags().StringVar(&config.path, "path", "", "Path to initialize state and mount points") + rootCmd.PersistentFlags().StringVar(&config.sockName, "sock-name", config.sockName, "Name of unix socket for plugin") rootCmd.PersistentFlags().StringVar(&config.logLevel, "log-level", config.logLevel, "Log messages including and over the specified level: debug, info, warn, error, fatal, panic") + + serveCmd.Flags().StringVar(&config.path, "path", "", "Path to initialize state and mount points") + + rootCmd.AddCommand(serveCmd, createCmd, removeCmd, listCmd) } func before(cmd *cobra.Command, args []string) error { @@ -59,11 +69,8 @@ func before(cmd *cobra.Command, args []string) error { func main() { if err := rootCmd.Execute(); err != nil { - logrus.Errorf("Running volume plugin: %v", err) os.Exit(1) } - - os.Exit(0) } // startServer runs the HTTP server and responds to requests diff --git a/test/testvol/remove.go b/test/testvol/remove.go new file mode 100644 index 000000000..2839b0b50 --- /dev/null +++ b/test/testvol/remove.go @@ -0,0 +1,26 @@ +package main + +import ( + pluginapi "github.com/docker/go-plugins-helpers/volume" + "github.com/spf13/cobra" +) + +var removeCmd = &cobra.Command{ + Use: "remove NAME", + Short: "remove a volume", + Long: `Remove a volume in the volume plugin listening on --sock-name`, + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + return removeVol(config.sockName, args[0]) + }, +} + +func removeVol(sockName, volName string) error { + plugin, err := getPlugin(sockName) + if err != nil { + return err + } + removeReq := new(pluginapi.RemoveRequest) + removeReq.Name = volName + return plugin.RemoveVolume(removeReq) +} diff --git a/test/testvol/util.go b/test/testvol/util.go new file mode 100644 index 000000000..7a0aeba86 --- /dev/null +++ b/test/testvol/util.go @@ -0,0 +1,29 @@ +package main + +import ( + "path/filepath" + "strings" + + "github.com/containers/podman/v4/libpod/plugin" +) + +const pluginSockDir = "/run/docker/plugins" + +func getSocketPath(pathOrName string) string { + if filepath.IsAbs(pathOrName) { + return pathOrName + } + + // only a name join it with the default path + return filepath.Join(pluginSockDir, pathOrName+".sock") +} + +func getPluginName(pathOrName string) string { + return strings.TrimSuffix(filepath.Base(pathOrName), ".sock") +} + +func getPlugin(sockNameOrPath string) (*plugin.VolumePlugin, error) { + path := getSocketPath(sockNameOrPath) + name := getPluginName(sockNameOrPath) + return plugin.GetVolumePlugin(name, path) +} -- cgit v1.2.3-54-g00ecf