1 package helper
2
3 import (
4 "bufio"
5 "encoding/json"
6 "fmt"
7 "os/exec"
8 "strings"
9
10 . "github.com/onsi/ginkgo/v2"
11 . "github.com/onsi/gomega"
12 batchv1 "k8s.io/api/batch/v1"
13 corev1 "k8s.io/api/core/v1"
14 jsonserializer "k8s.io/apimachinery/pkg/runtime/serializer/json"
15 "k8s.io/kubectl/pkg/scheme"
16 "k8s.io/utils/pointer"
17
18 "github.com/redhat-developer/odo/pkg/podman"
19 )
20
21
22 type PodmanComponent struct {
23 componentName string
24 app string
25 }
26
27 func NewPodmanComponent(componentName string, app string) *PodmanComponent {
28 return &PodmanComponent{
29 componentName: componentName,
30 app: app,
31 }
32 }
33
34 func (o *PodmanComponent) ExpectIsDeployed() {
35 podName := fmt.Sprintf("%s-%s", o.componentName, o.app)
36 cmd := exec.Command("podman", "pod", "list", "--format", "{{.Name}}", "--noheading")
37 stdout, err := cmd.Output()
38 Expect(err).ToNot(HaveOccurred())
39 Expect(string(stdout)).To(ContainSubstring(podName))
40 }
41
42 func (o *PodmanComponent) ExpectIsNotDeployed() {
43 podName := fmt.Sprintf("%s-%s", o.componentName, o.app)
44 cmd := exec.Command("podman", "pod", "list", "--format", "{{.Name}}", "--noheading")
45 stdout, err := cmd.Output()
46 Expect(err).ToNot(HaveOccurred())
47 Expect(string(stdout)).ToNot(ContainSubstring(podName))
48 }
49
50 func (o *PodmanComponent) Exec(container string, args []string, expectedSuccess *bool) (string, string) {
51 containerName := fmt.Sprintf("%s-%s-%s", o.componentName, o.app, container)
52 cmdargs := []string{"exec", "--interactive"}
53 cmdargs = append(cmdargs, "--tty")
54 cmdargs = append(cmdargs, containerName)
55 cmdargs = append(cmdargs, args...)
56
57 command := exec.Command("podman", cmdargs...)
58 out, err := command.CombinedOutput()
59 if err != nil {
60 if exiterr, ok := err.(*exec.ExitError); ok {
61 err = fmt.Errorf("%s: %s", err, string(exiterr.Stderr))
62 }
63 fmt.Fprintln(GinkgoWriter, err)
64 }
65 if expectedSuccess != nil {
66 if *expectedSuccess {
67 Expect(err).ToNot(HaveOccurred())
68 } else {
69 Expect(err).Should(HaveOccurred())
70 }
71 }
72 return string(out), ""
73 }
74
75 func (o *PodmanComponent) GetEnvVars(container string) map[string]string {
76 envs, _ := o.Exec(container, []string{"env"}, pointer.Bool(true))
77 return splitLines(envs)
78 }
79
80 func splitLines(str string) map[string]string {
81 result := map[string]string{}
82 sc := bufio.NewScanner(strings.NewReader(str))
83 for sc.Scan() {
84 line := sc.Text()
85 parts := strings.SplitN(line, "=", 2)
86 if len(parts) < 2 {
87 continue
88 }
89 result[parts[0]] = parts[1]
90 }
91 return result
92 }
93
94 func (o *PodmanComponent) GetPodDef() *corev1.Pod {
95 podname := fmt.Sprintf("%s-%s", o.componentName, o.app)
96 serializer := jsonserializer.NewSerializerWithOptions(
97 jsonserializer.SimpleMetaFactory{},
98 scheme.Scheme,
99 scheme.Scheme,
100 jsonserializer.SerializerOptions{
101 Yaml: true,
102 },
103 )
104
105 cmd := exec.Command("podman", "generate", "kube", podname)
106 resultBytes, err := cmd.Output()
107 Expect(err).ToNot(HaveOccurred())
108 var pod corev1.Pod
109 _, _, err = serializer.Decode(resultBytes, nil, &pod)
110 Expect(err).ToNot(HaveOccurred())
111 return &pod
112 }
113
114 func (o *PodmanComponent) GetJobDef() *batchv1.Job {
115
116 panic("not implemented for Podman")
117 }
118
119 func (o *PodmanComponent) GetLabels() map[string]string {
120 podName := fmt.Sprintf("%s-%s", o.componentName, o.app)
121 cmd := exec.Command("podman", "pod", "inspect", podName, "--format", "json")
122 stdout, err := cmd.Output()
123 Expect(err).ToNot(HaveOccurred(), func() {
124 if exiterr, ok := err.(*exec.ExitError); ok {
125 err = fmt.Errorf("%s: %s", err, string(exiterr.Stderr))
126 }
127 fmt.Fprintln(GinkgoWriter, err)
128 })
129
130 var result podman.PodInspectData
131
132 err = json.Unmarshal(stdout, &result)
133 Expect(err).ToNot(HaveOccurred())
134
135 return result.Labels
136 }
137
138 func (o *PodmanComponent) GetAnnotations() map[string]string {
139 def := o.GetPodDef()
140 return def.Annotations
141 }
142
143 func (o *PodmanComponent) GetPodLogs() string {
144 podName := fmt.Sprintf("%s-%s", o.componentName, o.app)
145 cmd := exec.Command("podman", "pod", "logs", podName)
146 stdout, err := cmd.Output()
147 Expect(err).ToNot(HaveOccurred(), func() {
148 if exiterr, ok := err.(*exec.ExitError); ok {
149 err = fmt.Errorf("%s: %s", err, string(exiterr.Stderr))
150 }
151 fmt.Fprintln(GinkgoWriter, err)
152 })
153 return string(stdout)
154 }
155
156 func (o *PodmanComponent) ListImages() string {
157 cmd := exec.Command("podman", "images", "--format", "{{.Repository}}:{{.Tag}}", "--noheading")
158 stdout, err := cmd.Output()
159 Expect(err).ToNot(HaveOccurred(), func() {
160 if exiterr, ok := err.(*exec.ExitError); ok {
161 err = fmt.Errorf("%s: %s", err, string(exiterr.Stderr))
162 }
163 fmt.Fprintln(GinkgoWriter, err)
164 })
165 return string(stdout)
166 }
167
View as plain text