pkg/machine/e2e: do not import from cmd/podman

It should be avoided to import cmd/podman/... packages from outside of
cmd/podman. This can lead in weird hard to debug import paths but also
can have negative consequences when imported in unit tests. In this case
it will set XDG_CONFIG_HOME and thus the machine tests this dir over the
tmp HOME env variable which is set at a later point. This caused machine
files to be leaked into the actual users home dir.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
This commit is contained in:
Paul Holzinger
2022-07-07 14:13:26 +02:00
parent 4cfa8ccb27
commit 4374038cc6
3 changed files with 30 additions and 28 deletions

View File

@ -16,6 +16,7 @@ import (
"github.com/containers/podman/v4/cmd/podman/common"
"github.com/containers/podman/v4/cmd/podman/registry"
"github.com/containers/podman/v4/cmd/podman/validate"
"github.com/containers/podman/v4/pkg/domain/entities"
"github.com/containers/podman/v4/pkg/machine"
"github.com/docker/go-units"
"github.com/spf13/cobra"
@ -44,23 +45,6 @@ type listFlagType struct {
quiet bool
}
type ListReporter struct {
Name string
Default bool
Created string
Running bool
Starting bool
LastUp string
Stream string
VMType string
CPUs uint64
Memory string
DiskSize string
Port int
RemoteUsername string
IdentityPath string
}
func init() {
registry.Commands = append(registry.Commands, registry.CliCommand{
Command: lsCmd,
@ -70,7 +54,7 @@ func init() {
flags := lsCmd.Flags()
formatFlagName := "format"
flags.StringVar(&listFlag.format, formatFlagName, "{{.Name}}\t{{.VMType}}\t{{.Created}}\t{{.LastUp}}\t{{.CPUs}}\t{{.Memory}}\t{{.DiskSize}}\n", "Format volume output using JSON or a Go template")
_ = lsCmd.RegisterFlagCompletionFunc(formatFlagName, common.AutocompleteFormat(&ListReporter{}))
_ = lsCmd.RegisterFlagCompletionFunc(formatFlagName, common.AutocompleteFormat(&entities.ListReporter{}))
flags.BoolVar(&listFlag.noHeading, "noheading", false, "Do not print headers")
flags.BoolVarP(&listFlag.quiet, "quiet", "q", false, "Show only machine names")
}
@ -123,8 +107,8 @@ func list(cmd *cobra.Command, args []string) error {
return outputTemplate(cmd, machineReporter)
}
func outputTemplate(cmd *cobra.Command, responses []*ListReporter) error {
headers := report.Headers(ListReporter{}, map[string]string{
func outputTemplate(cmd *cobra.Command, responses []*entities.ListReporter) error {
headers := report.Headers(entities.ListReporter{}, map[string]string{
"LastUp": "LAST UP",
"VmType": "VM TYPE",
"CPUs": "CPUS",
@ -183,15 +167,15 @@ func streamName(imageStream string) string {
return imageStream
}
func toMachineFormat(vms []*machine.ListResponse) ([]*ListReporter, error) {
func toMachineFormat(vms []*machine.ListResponse) ([]*entities.ListReporter, error) {
cfg, err := config.ReadCustomConfig()
if err != nil {
return nil, err
}
machineResponses := make([]*ListReporter, 0, len(vms))
machineResponses := make([]*entities.ListReporter, 0, len(vms))
for _, vm := range vms {
response := new(ListReporter)
response := new(entities.ListReporter)
response.Default = vm.Name == cfg.Engine.ActiveService
response.Name = vm.Name
response.Running = vm.Running
@ -211,15 +195,15 @@ func toMachineFormat(vms []*machine.ListResponse) ([]*ListReporter, error) {
return machineResponses, nil
}
func toHumanFormat(vms []*machine.ListResponse) ([]*ListReporter, error) {
func toHumanFormat(vms []*machine.ListResponse) ([]*entities.ListReporter, error) {
cfg, err := config.ReadCustomConfig()
if err != nil {
return nil, err
}
humanResponses := make([]*ListReporter, 0, len(vms))
humanResponses := make([]*entities.ListReporter, 0, len(vms))
for _, vm := range vms {
response := new(ListReporter)
response := new(entities.ListReporter)
if vm.Name == cfg.Engine.ActiveService {
response.Name = vm.Name + "*"
response.Default = true

View File

@ -0,0 +1,18 @@
package entities
type ListReporter struct {
Name string
Default bool
Created string
Running bool
Starting bool
LastUp string
Stream string
VMType string
CPUs uint64
Memory string
DiskSize string
Port int
RemoteUsername string
IdentityPath string
}

View File

@ -5,7 +5,7 @@ import (
"time"
"github.com/containers/common/pkg/util"
"github.com/containers/podman/v4/cmd/podman/machine"
"github.com/containers/podman/v4/pkg/domain/entities"
jsoniter "github.com/json-iterator/go"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
@ -134,7 +134,7 @@ var _ = Describe("podman machine list", func() {
Expect(err).To(BeNil())
Expect(listSession2).To(Exit(0))
var listResponse []*machine.ListReporter
var listResponse []*entities.ListReporter
err = jsoniter.Unmarshal(listSession.Bytes(), &listResponse)
Expect(err).To(BeNil())