Skip to content

Commit

Permalink
Compatibility with podman v3 (#6474)
Browse files Browse the repository at this point in the history
* Use podman generate kube instead podman kube generate

* get env from exec -it env (to be continued)

* Fix podman test

* Export function to make validate tests pass

* review

* Check podman tests result

Co-authored-by: Parthvi Vala <pvala@redhat.com>
  • Loading branch information
feloy and valaparthvi authored Jan 9, 2023
1 parent 32ef20d commit 020009b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 40 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/podman-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,5 @@ jobs:
run: make install

- name: Run Integration tests
# TODO Make the job fail when this step fails once we check the compatibility with podman v3.
# See https://github.com/redhat-developer/odo/issues/6446
continue-on-error: true
run: |
make test-integration-podman
2 changes: 1 addition & 1 deletion pkg/podman/podman.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (o *PodmanCli) KubeGenerate(name string) (*corev1.Pod, error) {
},
)

cmd := exec.Command(o.podmanCmd, "kube", "generate", name)
cmd := exec.Command(o.podmanCmd, "generate", "kube", name)
klog.V(3).Infof("executing %v", cmd.Args)
resultBytes, err := cmd.Output()
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions tests/helper/component_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ type Component interface {
GetEnvVars() map[string]string
}

func NewComponent(name string, app string, namespace string, cli CliRunner) Component {
func NewComponent(componentName string, app string, containerName string, namespace string, cli CliRunner) Component {
if NeedsCluster(CurrentSpecReport().Labels()) {
return NewClusterComponent(name, app, namespace, cli)
return NewClusterComponent(componentName, app, namespace, cli)
} else {
return NewPodmanComponent(name, app)
return NewPodmanComponent(componentName, app, containerName)
}
}
50 changes: 34 additions & 16 deletions tests/helper/component_podman.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package helper

import (
"bufio"
"fmt"
"os/exec"
"strings"

. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
Expand All @@ -12,57 +14,73 @@ import (

// PodmanComponent is an abstraction for a Devfile Component deployed on podman
type PodmanComponent struct {
name string
app string
componentName string
app string
containerName string
}

func NewPodmanComponent(name string, app string) *PodmanComponent {
func NewPodmanComponent(componentName string, app string, containerName string) *PodmanComponent {
return &PodmanComponent{
name: name,
app: app,
componentName: componentName,
app: app,
containerName: containerName,
}
}

func (o *PodmanComponent) ExpectIsDeployed() {
podName := fmt.Sprintf("%s-%s", o.name, o.app)
podName := fmt.Sprintf("%s-%s", o.componentName, o.app)
cmd := exec.Command("podman", "pod", "list", "--format", "{{.Name}}", "--noheading")
stdout, err := cmd.Output()
Expect(err).ToNot(HaveOccurred())
Expect(string(stdout)).To(ContainSubstring(podName))
}

func (o *PodmanComponent) ExpectIsNotDeployed() {
podName := fmt.Sprintf("%s-%s", o.name, o.app)
podName := fmt.Sprintf("%s-%s", o.componentName, o.app)
cmd := exec.Command("podman", "pod", "list", "--format", "{{.Name}}", "--noheading")
stdout, err := cmd.Output()
Expect(err).ToNot(HaveOccurred())
Expect(string(stdout)).ToNot(ContainSubstring(podName))
}

func (o *PodmanComponent) Exec(container string, args ...string) string {
containerName := fmt.Sprintf("%s-%s-%s", o.name, o.app, container)
containerName := fmt.Sprintf("%s-%s-%s", o.componentName, o.app, container)
cmdargs := []string{"exec", "--interactive"}
cmdargs = append(cmdargs, "--tty")
cmdargs = append(cmdargs, containerName)
cmdargs = append(cmdargs, args...)

command := exec.Command("podman", cmdargs...)
out, err := command.Output()
if err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
err = fmt.Errorf("%s: %s", err, string(exiterr.Stderr))
}
}
Expect(err).ToNot(HaveOccurred())
return string(out)
}

func (o *PodmanComponent) GetEnvVars() map[string]string {
podName := fmt.Sprintf("%s-%s", o.name, o.app)
podDef := getPodDef(podName)
res := map[string]string{}
for _, env := range podDef.Spec.Containers[0].Env {
res[env.Name] = env.Value
envs := o.Exec(o.containerName, "env")
return splitLines(envs)
}

func splitLines(str string) map[string]string {
result := map[string]string{}
sc := bufio.NewScanner(strings.NewReader(str))
for sc.Scan() {
line := sc.Text()
parts := strings.SplitN(line, "=", 2)
if len(parts) < 2 {
continue
}
result[parts[0]] = parts[1]
}
return res
return result
}

func getPodDef(podname string) *corev1.Pod {
func GetPodDef(podname string) *corev1.Pod {
serializer := jsonserializer.NewSerializerWithOptions(
jsonserializer.SimpleMetaFactory{},
scheme.Scheme,
Expand All @@ -72,7 +90,7 @@ func getPodDef(podname string) *corev1.Pod {
},
)

cmd := exec.Command("podman", "kube", "generate", podname)
cmd := exec.Command("podman", "generate", "kube", podname)
resultBytes, err := cmd.Output()
Expect(err).ToNot(HaveOccurred())
var pod corev1.Pod
Expand Down
8 changes: 4 additions & 4 deletions tests/integration/cmd_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ var _ = Describe("odo delete command tests", func() {
devSession.Kill()
devSession.WaitEnd()

component := helper.NewComponent(cmpName, "app", commonVar.Project, commonVar.CliRunner)
component := helper.NewComponent(cmpName, "app", "runtime", commonVar.Project, commonVar.CliRunner)
component.ExpectIsDeployed()
})

Expand Down Expand Up @@ -229,7 +229,7 @@ var _ = Describe("odo delete command tests", func() {
}
})
By("deleting the deployment", func() {
component := helper.NewComponent(cmpName, "app", commonVar.Project, commonVar.CliRunner)
component := helper.NewComponent(cmpName, "app", "runtime", commonVar.Project, commonVar.CliRunner)
component.ExpectIsNotDeployed()
})
})
Expand Down Expand Up @@ -264,7 +264,7 @@ var _ = Describe("odo delete command tests", func() {
Expect(stdOut).To(ContainSubstring(cmpName))
})
By("deleting the deployment", func() {
component := helper.NewComponent(cmpName, "app", commonVar.Project, commonVar.CliRunner)
component := helper.NewComponent(cmpName, "app", "runtime", commonVar.Project, commonVar.CliRunner)
component.ExpectIsNotDeployed()
})
By("ensuring that devfile.yaml and .odo still exists", func() {
Expand Down Expand Up @@ -301,7 +301,7 @@ var _ = Describe("odo delete command tests", func() {
Expect(stdOut).To(ContainSubstring(cmpName))
})
By("deleting the deployment", func() {
component := helper.NewComponent(cmpName, "app", commonVar.Project, commonVar.CliRunner)
component := helper.NewComponent(cmpName, "app", "runtime", commonVar.Project, commonVar.CliRunner)
component.ExpectIsNotDeployed()
})

Expand Down
22 changes: 11 additions & 11 deletions tests/integration/cmd_dev_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ var _ = Describe("odo dev command tests", func() {
helper.ReplaceString(filepath.Join(commonVar.Context, "server.js"), "App started", "App is super started")

// File should exist, and its content should match what we initially set it to
component := helper.NewComponent(cmpName, "app", commonVar.Project, commonVar.CliRunner)
component := helper.NewComponent(cmpName, "app", "runtime", commonVar.Project, commonVar.CliRunner)
execResult := component.Exec("runtime", "cat", "/projects/"+filepath.Base(fileAPath))
Expect(execResult).To(ContainSubstring(fileAText))
})
Expand Down Expand Up @@ -255,7 +255,7 @@ var _ = Describe("odo dev command tests", func() {
})

It("should delete component from the cluster", func() {
component := helper.NewComponent(cmpName, "app", commonVar.Project, commonVar.CliRunner)
component := helper.NewComponent(cmpName, "app", "runtime", commonVar.Project, commonVar.CliRunner)
component.ExpectIsNotDeployed()
})
})
Expand Down Expand Up @@ -438,7 +438,7 @@ ComponentSettings:
})

It("should not trigger a push", func() {
component := helper.NewComponent(cmpName, "app", commonVar.Project, commonVar.CliRunner)
component := helper.NewComponent(cmpName, "app", "runtime", commonVar.Project, commonVar.CliRunner)
execResult := component.Exec("runtime", "cat", "/projects/server.js")
Expect(execResult).To(ContainSubstring("App started"))
Expect(execResult).ToNot(ContainSubstring("App is super started"))
Expand All @@ -458,7 +458,7 @@ ComponentSettings:
It("should trigger a push", func() {
_, _, _, err := devSession.WaitSync()
Expect(err).ToNot(HaveOccurred())
component := helper.NewComponent(cmpName, "app", commonVar.Project, commonVar.CliRunner)
component := helper.NewComponent(cmpName, "app", "runtime", commonVar.Project, commonVar.CliRunner)
execResult := component.Exec("runtime", "cat", "/projects/server.js")
Expect(execResult).To(ContainSubstring("App is super started"))
})
Expand Down Expand Up @@ -859,7 +859,7 @@ ComponentSettings:
})

It("3. should check if the env variable has a correct value", func() {
component := helper.NewComponent(devfileCmpName, "app", commonVar.Project, commonVar.CliRunner)
component := helper.NewComponent(devfileCmpName, "app", "runtime", commonVar.Project, commonVar.CliRunner)
envVars := component.GetEnvVars()
// check if the env variable has a correct value. This value was substituted from in devfile from variable
Expect(envVars["FOO"]).To(Equal("bar"))
Expand All @@ -882,7 +882,7 @@ ComponentSettings:
})

It("should check if the env variable has a correct value", func() {
component := helper.NewComponent(devfileCmpName, "app", commonVar.Project, commonVar.CliRunner)
component := helper.NewComponent(devfileCmpName, "app", "runtime", commonVar.Project, commonVar.CliRunner)
envVars := component.GetEnvVars()
// check if the env variable has a correct value. This value was substituted from in devfile from variable
Expect(envVars["FOO"]).To(Equal("baz"))
Expand All @@ -909,7 +909,7 @@ ComponentSettings:
})

It("should check if the env variable has a correct value", func() {
component := helper.NewComponent(devfileCmpName, "app", commonVar.Project, commonVar.CliRunner)
component := helper.NewComponent(devfileCmpName, "app", "runtime", commonVar.Project, commonVar.CliRunner)
envVars := component.GetEnvVars()
// check if the env variable has a correct value. This value was substituted from in devfile from variable
Expect(envVars["FOO"]).To(Equal("baz"))
Expand Down Expand Up @@ -938,7 +938,7 @@ ComponentSettings:
})

It("should check if the env variable has a correct value", func() {
component := helper.NewComponent(devfileCmpName, "app", commonVar.Project, commonVar.CliRunner)
component := helper.NewComponent(devfileCmpName, "app", "runtime", commonVar.Project, commonVar.CliRunner)
envVars := component.GetEnvVars()
// check if the env variable has a correct value. This value was substituted from in devfile from variable
Expect(envVars["FOO"]).To(Equal("baz"))
Expand All @@ -961,7 +961,7 @@ ComponentSettings:
err := helper.RunDevMode(helper.DevSessionOpts{
RunOnPodman: podman,
}, func(session *gexec.Session, out, err []byte, ports map[string]string) {
component := helper.NewComponent(devfileCmpName, "app", commonVar.Project, commonVar.CliRunner)
component := helper.NewComponent(devfileCmpName, "app", "runtime", commonVar.Project, commonVar.CliRunner)
output := component.Exec("runtime", "ls", "-lai", "/projects")
helper.MatchAllInOutput(output, []string{"test_env_variable", "test_build_env_variable"})
})
Expand All @@ -984,7 +984,7 @@ ComponentSettings:
err := helper.RunDevMode(helper.DevSessionOpts{
RunOnPodman: podman,
}, func(session *gexec.Session, out, err []byte, ports map[string]string) {
component := helper.NewComponent(devfileCmpName, "app", commonVar.Project, commonVar.CliRunner)
component := helper.NewComponent(devfileCmpName, "app", "runtime", commonVar.Project, commonVar.CliRunner)
output := component.Exec("runtime", "ls", "-lai", "/projects")
helper.MatchAllInOutput(output, []string{"test_build_env_variable1", "test_build_env_variable2", "test_env_variable1", "test_env_variable2"})
})
Expand All @@ -1007,7 +1007,7 @@ ComponentSettings:
err := helper.RunDevMode(helper.DevSessionOpts{
RunOnPodman: podman,
}, func(session *gexec.Session, out, err []byte, ports map[string]string) {
component := helper.NewComponent(devfileCmpName, "app", commonVar.Project, commonVar.CliRunner)
component := helper.NewComponent(devfileCmpName, "app", "runtime", commonVar.Project, commonVar.CliRunner)
output := component.Exec("runtime", "ls", "-lai", "/projects")
helper.MatchAllInOutput(output, []string{"build env variable with space", "env with space"})
})
Expand Down

0 comments on commit 020009b

Please sign in to comment.