1 package helper
2
3 import (
4 "encoding/json"
5 "fmt"
6
7 . "github.com/onsi/gomega"
8 batchv1 "k8s.io/api/batch/v1"
9 corev1 "k8s.io/api/core/v1"
10
11 "github.com/redhat-developer/odo/pkg/labels"
12 )
13
14
15 type ClusterComponent struct {
16 name string
17 app string
18 mode string
19 namespace string
20 cli CliRunner
21 }
22
23 func NewClusterComponent(name string, app string, mode string, namespace string, cli CliRunner) *ClusterComponent {
24 return &ClusterComponent{
25 name: name,
26 app: app,
27 mode: mode,
28 namespace: namespace,
29 cli: cli,
30 }
31 }
32
33 func (o *ClusterComponent) ExpectIsDeployed() {
34 deploymentName := fmt.Sprintf("%s-%s", o.name, o.app)
35 stdout := o.cli.Run("get", "deployment", "-n", o.namespace).Out.Contents()
36 Expect(string(stdout)).To(ContainSubstring(deploymentName))
37 }
38
39 func (o *ClusterComponent) ExpectIsNotDeployed() {
40 deploymentName := fmt.Sprintf("%s-%s", o.name, o.app)
41 stdout := o.cli.Run("get", "deployment", "-n", o.namespace).Out.Contents()
42 Expect(string(stdout)).To(Not(ContainSubstring(deploymentName)))
43 }
44
45 func (o *ClusterComponent) Exec(container string, args []string, expectedSuccess *bool) (string, string) {
46 podName := o.cli.GetRunningPodNameByComponent(o.name, o.namespace)
47 return o.cli.Exec(podName, o.namespace, append([]string{"-c", container, "--"}, args...), expectedSuccess)
48 }
49
50 func (o *ClusterComponent) GetEnvVars(string) map[string]string {
51 return o.cli.GetEnvsDevFileDeployment(o.name, o.app, o.namespace)
52 }
53
54 func (o *ClusterComponent) GetLabels() map[string]string {
55 selector := labels.Builder().WithComponentName(o.name).WithAppName(o.app).WithMode(o.mode).SelectorFlag()
56 stdout := o.cli.Run("get", "deployment", selector, "-n", o.namespace, "-o", "jsonpath={.items[0].metadata.labels}").Out.Contents()
57
58 var result map[string]string
59 err := json.Unmarshal(stdout, &result)
60 Expect(err).ToNot(HaveOccurred())
61
62 return result
63 }
64
65 func (o *ClusterComponent) GetAnnotations() map[string]string {
66 selector := labels.Builder().WithComponentName(o.name).WithAppName(o.app).WithMode(o.mode).SelectorFlag()
67 stdout := o.cli.Run("get", "deployment", selector, "-n", o.namespace, "-o", "jsonpath={.items[0].metadata.annotations}").Out.Contents()
68
69 var result map[string]string
70 err := json.Unmarshal(stdout, &result)
71 Expect(err).ToNot(HaveOccurred())
72
73 return result
74 }
75
76 func (o *ClusterComponent) GetPodDef() *corev1.Pod {
77 var podDef corev1.Pod
78 podName := o.cli.GetRunningPodNameByComponent(o.name, o.namespace)
79 bufferOutput := o.cli.Run("get", "pods", podName, "-o", "json").Out.Contents()
80 err := json.Unmarshal(bufferOutput, &podDef)
81 Expect(err).ToNot(HaveOccurred())
82 return &podDef
83 }
84
85 func (o *ClusterComponent) GetJobDef() *batchv1.Job {
86 var jobDef batchv1.Job
87 var jobName string
88 Eventually(func() string {
89 jobName = o.cli.GetJobNameByComponent(o.name, o.namespace)
90 return jobName
91 }).Should(Not(BeEmpty()))
92 bufferOutput := o.cli.Run("get", "jobs", jobName, "-o", "json").Out.Contents()
93 err := json.Unmarshal(bufferOutput, &jobDef)
94 Expect(err).ToNot(HaveOccurred())
95 return &jobDef
96 }
97
98 func (o *ClusterComponent) GetPodLogs() string {
99 podName := o.cli.GetRunningPodNameByComponent(o.name, o.namespace)
100 return string(o.cli.Run("-n", o.namespace, "logs", podName).Out.Contents())
101 }
102
View as plain text