Skip to content

Commit

Permalink
Add integration test for podman preset
Browse files Browse the repository at this point in the history
  • Loading branch information
jsliacan authored and anjannath committed Sep 8, 2022
1 parent 9afa0b9 commit 24ce8bb
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 6 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,18 @@ containerized_integration: clean

.PHONY: integration ## Run integration tests in Ginkgo
integration:
ifndef GINKGO_OPTS
export GINKGO_OPTS = --ginkgo.label-filter=""
endif
ifndef PULL_SECRET_PATH
export PULL_SECRET_PATH = $(HOME)/Downloads/crc-pull-secret
endif
ifndef BUNDLE_PATH
export BUNDLE_PATH = $(HOME)/Downloads/crc_libvirt_$(OPENSHIFT_VERSION)_$(GOARCH).$(BUNDLE_EXTENSION)
endif

integration:
@go test -timeout=60m $(REPOPATH)/test/integration -v
@go test -timeout=60m $(REPOPATH)/test/integration -v $(GINKGO_OPTS)

.PHONY: e2e ## Run e2e tests
e2e:
Expand Down
71 changes: 71 additions & 0 deletions test/integration/podman_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package test_test

import (
"os"
"path/filepath"
"runtime"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("podman preset", Label("podman-preset"), func() {

Describe("basic use", func() {

It("write to config", func() {
Expect(RunCRCExpectSuccess("config", "set", "preset", "podman")).To(ContainSubstring("Changes to configuration property 'preset' are only applied when the CRC instance is created."))
})

It("setup CRC", func() {
Expect(RunCRCExpectSuccess("setup")).To(ContainSubstring("Your system is correctly setup for using CRC"))
})

It("start CRC", func() {
Expect(RunCRCExpectSuccess("start")).To(ContainSubstring("podman runtime is now running"))
})

It("podman-env", func() {
// Do what 'eval $(crc podman-env) would do
path := os.ExpandEnv("${HOME}/.crc/bin/oc:$PATH")
csshk := os.ExpandEnv("${HOME}/.crc/machines/crc/id_ecdsa")
dh := os.ExpandEnv("unix:///${HOME}/.crc/machines/crc/docker.sock")
ch := "ssh://[email protected]:2222/run/user/1000/podman/podman.sock"
if runtime.GOOS == "windows" {
userHomeDir, _ := os.UserHomeDir()
unexpandedPath := filepath.Join(userHomeDir, ".crc/bin/oc;${PATH}")
path = os.ExpandEnv(unexpandedPath)
csshk = filepath.Join(userHomeDir, ".crc/machines/crc/id_ecdsa")
dh = "npipe:////./pipe/rc-podman"
}
if runtime.GOOS == "linux" {
ch = "ssh://[email protected]:22/run/user/1000/podman/podman.sock"
}

os.Setenv("PATH", path)
os.Setenv("CONTAINER_SSHKEY", csshk)
os.Setenv("CONTAINER_HOST", ch)
os.Setenv("DOCKER_HOST", dh)
})

It("version", func() {
Expect(RunPodmanExpectSuccess("version")).Should(MatchRegexp(`Version:[\s]*\d+\.\d+\.\d+`))
})

It("pull image", func() {
RunPodmanExpectSuccess("pull", "fedora")
})

It("run image", func() {
RunPodmanExpectSuccess("run", "fedora")
})

It("cleanup CRC", func() {
Expect(RunCRCExpectSuccess("cleanup")).To(MatchRegexp("Cleanup finished"))
})

It("unset preset in config", func() {
Expect(RunCRCExpectSuccess("config", "unset", "preset")).To(ContainSubstring("Successfully unset configuration property 'preset'"))
})
})
})
2 changes: 1 addition & 1 deletion test/integration/resize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
. "github.com/onsi/gomega"
)

var _ = Describe("vary VM parameters: memory cpus, disk", func() {
var _ = Describe("vary VM parameters: memory cpus, disk", Label("openshift-preset", "vm-resize"), func() {

Describe("use default values", func() {

Expand Down
17 changes: 13 additions & 4 deletions test/integration/testsuite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,23 @@ var userHome string
var versionInfo VersionAnswer

var bundlePath string
var ginkgoOpts string
var pullSecretPath string

func TestTest(t *testing.T) {

RegisterFailHandler(Fail)

// fetch the current (reporter) config
_, reporterConfig := GinkgoConfiguration()
suiteConfig, reporterConfig := GinkgoConfiguration()

err := os.MkdirAll("out", 0775)
if err != nil {
logrus.Infof("failed to create directory: %v", err)
}
reporterConfig.JUnitReport = filepath.Join("out", "integration.xml")

RunSpecs(t, "Test Suite", reporterConfig)
RunSpecs(t, "Test Suite", suiteConfig, reporterConfig)

}

Expand All @@ -64,13 +66,20 @@ var _ = BeforeSuite(func() {
Expect(bundlePath).To(BeAnExistingFile())
}

// pull-secret location
ginkgoOpts = os.Getenv("GINKGO_OPTS")
if err != nil {

logrus.Infof("Error: Could not read GINKGO_OPTS.")
logrus.Infof("%v", err)
Expect(err).NotTo(HaveOccurred())
}

pullSecretPath = os.Getenv("PULL_SECRET_PATH") // this env var should contain location of pull-secret file
if err != nil {

logrus.Infof("Error: You need to set PULL_SECRET_PATH to find CRC useful.")
logrus.Infof("%v", err)
Expect(err).NotTo(HaveOccurred())
}
Expect(pullSecretPath).To(BeAnExistingFile()) // not checking if it's a valid pull secret file

})
86 changes: 86 additions & 0 deletions test/integration/utilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"os"
"os/exec"
"runtime"
"strings"
"syscall"
"time"
Expand All @@ -24,6 +25,12 @@ type CRCBuilder struct {
timeout <-chan time.Time
}

// PodmanBuilder is used to build, customize, and execute a podman-remote command.
type PodmanBuilder struct {
cmd *exec.Cmd
timeout <-chan time.Time
}

// NewCRCCommand returns a CRCBuilder for running CRC.
func NewCRCCommand(args ...string) *CRCBuilder {
cmd := exec.Command("crc", args...)
Expand All @@ -34,49 +41,109 @@ func NewCRCCommand(args ...string) *CRCBuilder {
}
}

// NewPodmanCommand returns a PodmanBuilder for running CRC.
func NewPodmanCommand(args ...string) *PodmanBuilder {

cmd := exec.Command("podman", args...)

switch runtime.GOOS {
case "linux":
cmd = exec.Command("podman-remote", args...)
case "windows":
cmd = exec.Command("podman.exe", args...)
}

return &PodmanBuilder{
cmd: cmd,
}
}

// WithTimeout sets the given timeout and returns itself.
func (b *CRCBuilder) WithTimeout(t <-chan time.Time) *CRCBuilder {
b.timeout = t
return b
}

// WithTimeout sets the given timeout and returns itself.
func (b *PodmanBuilder) WithTimeout(t <-chan time.Time) *PodmanBuilder {
b.timeout = t
return b
}

// WithStdinData sets the given data to stdin and returns itself.
func (b CRCBuilder) WithStdinData(data string) *CRCBuilder {
b.cmd.Stdin = strings.NewReader(data)
return &b
}

// WithStdinData sets the given data to stdin and returns itself.
func (b PodmanBuilder) WithStdinData(data string) *PodmanBuilder {
b.cmd.Stdin = strings.NewReader(data)
return &b
}

// WithStdinReader sets the given reader and returns itself.
func (b CRCBuilder) WithStdinReader(reader io.Reader) *CRCBuilder {
b.cmd.Stdin = reader
return &b
}

// WithStdinReader sets the given reader and returns itself.
func (b PodmanBuilder) WithStdinReader(reader io.Reader) *PodmanBuilder {
b.cmd.Stdin = reader
return &b
}

// ExecOrDie runs the executable or dies if error occurs.
func (b CRCBuilder) ExecOrDie() string {
stdout, err := b.Exec()
Expect(err).To(Not(HaveOccurred()))
return stdout
}

// ExecOrDie runs the executable or dies if error occurs.
func (b PodmanBuilder) ExecOrDie() string {
stdout, err := b.Exec()
Expect(err).To(Not(HaveOccurred()))
return stdout
}

// ExecOrDieWithLogs runs the executable or dies if error occurs.
func (b CRCBuilder) ExecOrDieWithLogs() (string, string) {
stdout, stderr, err := b.ExecWithFullOutput()
Expect(err).To(Not(HaveOccurred()))
return stdout, stderr
}

// ExecOrDieWithLogs runs the executable or dies if error occurs.
func (b PodmanBuilder) ExecOrDieWithLogs() (string, string) {
stdout, stderr, err := b.ExecWithFullOutput()
Expect(err).To(Not(HaveOccurred()))
return stdout, stderr
}

// Exec runs the executable.
func (b CRCBuilder) Exec() (string, error) {
stdout, _, err := b.ExecWithFullOutput()
return stdout, err
}

// Exec runs the executable.
func (b PodmanBuilder) Exec() (string, error) {
stdout, _, err := b.ExecWithFullOutput()
return stdout, err
}

// ExecWithFullOutput runs the executable and returns the stdout and stderr.
func (b CRCBuilder) ExecWithFullOutput() (string, string, error) {
return Exec(b.cmd, b.timeout)
}

// ExecWithFullOutput runs the executable and returns the stdout and stderr.
func (b PodmanBuilder) ExecWithFullOutput() (string, string, error) {
return Exec(b.cmd, b.timeout)
}

func Exec(cmd *exec.Cmd, timeout <-chan time.Time) (string, string, error) {
var stdout, stderr bytes.Buffer
cmd.Stdout, cmd.Stderr = &stdout, &stderr
Expand Down Expand Up @@ -116,6 +183,11 @@ func RunCRCExpectSuccess(args ...string) string {
return NewCRCCommand(args...).ExecOrDie()
}

// RunPodmanExpectSuccess is a convenience wrapper over podman-remote
func RunPodmanExpectSuccess(args ...string) string {
return NewPodmanCommand(args...).ExecOrDie()
}

// RunCRCExpectFail is a convenience wrapper over CRCBuilder
// if err != nil: return stderr, nil
// if err == nil: return stdout, err
Expand All @@ -130,6 +202,20 @@ func RunCRCExpectFail(args ...string) (string, error) {
return stderr, nil
}

// RunPodmanExpectFail is a convenience wrapper over PodmanBuilder
// if err != nil: return stderr, nil
// if err == nil: return stdout, err
func RunPodmanExpectFail(args ...string) (string, error) {
stdout, stderr, err := NewPodmanCommand(args...).ExecWithFullOutput()

if err == nil {
err = fmt.Errorf("Expected error but exited without error")
return stdout, err
}

return stderr, nil
}

// Send command to CRC VM via SSH
func SendCommandToVM(cmd string) (string, error) {
client := machine.NewClient(constants.DefaultName, false, crcConfig.New(crcConfig.NewEmptyInMemoryStorage()))
Expand Down

0 comments on commit 24ce8bb

Please sign in to comment.