|
1 | 1 | package operatorsource |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "bytes" |
5 | 4 | "fmt" |
| 5 | + "github.com/stretchr/testify/require" |
| 6 | + corev1 "k8s.io/api/core/v1" |
6 | 7 | "os" |
7 | | - "os/exec" |
8 | | - "strings" |
9 | 8 | "testing" |
10 | | - |
11 | | - "github.com/stretchr/testify/require" |
| 9 | + "time" |
12 | 10 | ) |
13 | 11 |
|
14 | | -const ShellToUse = "bash" |
15 | | - |
16 | | -func Shellout(command string) (string, string, error) { |
17 | | - var stdout bytes.Buffer |
18 | | - var stderr bytes.Buffer |
19 | | - cmd := exec.Command(ShellToUse, "-c", command) |
20 | | - cmd.Stdout = &stdout |
21 | | - cmd.Stderr = &stderr |
22 | | - err := cmd.Run() |
23 | | - return stdout.String(), stderr.String(), err |
24 | | -} |
25 | | - |
26 | | -func Test_OperatorSource_oc_commands(t *testing.T) { |
| 12 | +var ( |
| 13 | + Client = NewTestClient() |
| 14 | + namespace = "openshift-operators" |
| 15 | + subName = "devconsole" |
| 16 | + label = "name=devconsole-operator" |
| 17 | + subscription, suberr = Client.GetSubscription(subName, namespace) |
| 18 | +) |
27 | 19 |
|
28 | | - defer CleanUp(t) |
| 20 | +func Test_OperatorSource(t *testing.T) { |
29 | 21 |
|
30 | | - t.Run("login", func(t *testing.T) { Login(t) }) |
31 | | - t.Run("subscription", func(t *testing.T) { Subscription(t) }) |
32 | | - t.Run("install plan", func(t *testing.T) { InstallPlan(t) }) |
33 | | - t.Run("operator pod", func(t *testing.T) { OperatorPod(t) }) |
34 | | -} |
| 22 | + pod, err := Client.GetPodByLabel(label, namespace) |
| 23 | + if err != nil { |
| 24 | + t.Fatal(err) |
| 25 | + } |
| 26 | + defer CleanUp(t, pod) |
| 27 | + retryInterval := time.Second * 10 |
| 28 | + timeout := time.Second * 120 |
35 | 29 |
|
36 | | -func Login(t *testing.T) { |
37 | | - // Start - Login to oc |
38 | | - out, _, err := Shellout("oc login -u " + os.Getenv("OC_LOGIN_USERNAME") + " -p " + os.Getenv("OC_LOGIN_PASSWORD")) |
| 30 | + err = Client.WaitForOperatorDeployment(t, pod.Name, namespace, retryInterval, timeout) |
39 | 31 | if err != nil { |
40 | | - t.Fatalf("error: %v\n", err) |
| 32 | + t.Fatal(err) |
41 | 33 | } else { |
42 | | - require.True(t, strings.Contains(out, "Login successful."), "Expecting successful login") |
| 34 | + t.Run("subscription", func(t *testing.T) { Subscription(t) }) |
| 35 | + t.Run("install plan", func(t *testing.T) { InstallPlan(t) }) |
| 36 | + t.Run("operator pod", func(t *testing.T) { OperatorPod(t) }) |
43 | 37 | } |
44 | 38 | } |
45 | 39 |
|
46 | 40 | func Subscription(t *testing.T) { |
47 | 41 | // 1) Verify that the subscription was created |
48 | | - out, errout, err := Shellout("oc get sub devconsole -n openshift-operators") |
49 | | - if err != nil { |
50 | | - t.Logf("stdout: %s\n", out) |
51 | | - t.Logf("stderr: %s\n", errout) |
52 | | - t.Fatalf("error: %v\n", err) |
53 | | - } else { |
54 | | - require.True(t, strings.Contains(out, "devconsole"), "Expecting the subscription name to be found") |
55 | | - require.True(t, strings.Contains(out, "installed-custom-openshift-operators"), "Expecting the subscription namespace to be found") |
| 42 | + if suberr != nil { |
| 43 | + t.Fatal(suberr) |
56 | 44 | } |
| 45 | + fmt.Printf("Subscription Name: %s\nCatalog Source: %s\n", subscription.Name, subscription.Spec.CatalogSource) |
| 46 | + require.Equal(t, subName, subscription.Name) |
| 47 | + require.Equal(t, "installed-custom-openshift-operators", subscription.Spec.CatalogSource) |
57 | 48 | } |
58 | 49 |
|
59 | 50 | func InstallPlan(t *testing.T) { |
60 | 51 | // 2) Find the name of the install plan |
61 | | - out, errout, err := Shellout("oc get sub devconsole -n openshift-operators -o jsonpath='{.status.installplan.name}'") |
62 | | - var installPlan string |
| 52 | + installPlanName := subscription.Status.Install.Name |
| 53 | + fmt.Printf("Install Plan Name: %s\n", installPlanName) |
| 54 | + installPlan, err := Client.GetInstallPlan(installPlanName, namespace) |
63 | 55 | if err != nil { |
64 | | - t.Logf("stdout: %s\n", out) |
65 | | - t.Logf("stderr: %s\n", errout) |
66 | | - t.Fatalf("error: %v\n", err) |
67 | | - } else { |
68 | | - installPlan = out |
| 56 | + t.Fatal(err) |
69 | 57 | } |
70 | | - |
71 | | - // 3) Verify the install plan |
72 | | - out, errout, err = Shellout(fmt.Sprintf("oc get installplan %s -n openshift-operators", installPlan)) |
73 | | - if err != nil { |
74 | | - t.Logf("stdout: %s\n", out) |
75 | | - t.Logf("stderr: %s\n", errout) |
76 | | - t.Fatalf("error: %v\n", err) |
77 | | - } else { |
78 | | - require.True(t, strings.Contains(out, installPlan), "Expecting the Install Plan name to be found") |
79 | | - require.True(t, strings.Contains(out, "devconsole-operator.v0.1.0"), "Expecting the Operator release to be found") |
80 | | - require.True(t, strings.Contains(out, "Automatic"), "Expecting the approval method to be found") |
81 | | - require.True(t, strings.Contains(out, "true"), "Expecting the approved state to be found") |
| 58 | + fmt.Printf("CSV: %v\n", installPlan.Spec.ClusterServiceVersionNames[0]) |
| 59 | + fmt.Printf("Install Plan Approval: %v\n", installPlan.Spec.Approval) |
| 60 | + fmt.Printf("Install Plan Approved: %v\n", installPlan.Spec.Approved) |
| 61 | + |
| 62 | + require.Equal(t, "devconsole-operator.v0.1.0", installPlan.Spec.ClusterServiceVersionNames[0]) |
| 63 | + require.Equal(t, "Automatic", string(installPlan.Spec.Approval)) |
| 64 | + if !installPlan.Spec.Approved { |
| 65 | + require.FailNow(t, "Install plan approved is false") |
82 | 66 | } |
83 | 67 | } |
84 | 68 |
|
85 | 69 | func OperatorPod(t *testing.T) { |
86 | | - // Verify that the operator's pod is running |
87 | | - out, errout, err := Shellout("oc get pods -l name=devconsole-operator -n openshift-operators -o jsonpath='{.items[*].status.phase}'") |
| 70 | + // 3) Check operator pod status, fail status != Running |
| 71 | + pod, err := Client.GetPodByLabel(label, namespace) |
88 | 72 | if err != nil { |
89 | | - t.Logf("stdout: %s\n", out) |
90 | | - t.Logf("stderr: %s\n", errout) |
91 | | - t.Fatalf("error: %v\n", err) |
92 | | - } else { |
93 | | - require.True(t, strings.Contains(out, "Running"), "Expecting the state of the Operator pod to be running") |
| 73 | + t.Fatal(err) |
94 | 74 | } |
| 75 | + fmt.Printf("Pod Name: %v\nPod status: %v\n", pod.Name, pod.Status.Phase) |
| 76 | + require.Equal(t, pod.Status.Phase, corev1.PodRunning) |
95 | 77 | } |
96 | 78 |
|
97 | | -func CleanUp(t *testing.T) { |
| 79 | +func CleanUp(t *testing.T, pod *corev1.Pod) { |
98 | 80 | // Clean up resources |
99 | | - operatorSourceName := os.Getenv("OPSRC_NAME") |
100 | 81 | operatorVersion := os.Getenv("DEVCONSOLE_OPERATOR_VERSION") |
101 | 82 |
|
102 | | - out, errout, err := Shellout(fmt.Sprintf("oc delete opsrc %s -n openshift-marketplace", operatorSourceName)) |
| 83 | + err := Client.Delete("installplan", subscription.Status.Install.Name, namespace) |
103 | 84 | if err != nil { |
104 | | - t.Logf("stdout: %s\n", out) |
105 | | - t.Logf("stderr: %s\n", errout) |
106 | | - t.Logf("error: %v\n", err) |
107 | | - } else { |
108 | | - t.Logf(out) |
| 85 | + t.Logf("Error: %v\n", err) |
109 | 86 | } |
110 | 87 |
|
111 | | - out, errout, err = Shellout("oc delete sub devconsole -n openshift-operators") |
| 88 | + err = Client.Delete("catsrc", subscription.Spec.CatalogSource, namespace) |
112 | 89 | if err != nil { |
113 | | - t.Logf("stdout: %s\n", out) |
114 | | - t.Logf("stderr: %s\n", errout) |
115 | | - t.Logf("error: %v\n", err) |
116 | | - } else { |
117 | | - t.Logf(out) |
| 90 | + t.Logf("Error: %v\n", err) |
118 | 91 | } |
119 | 92 |
|
120 | | - out, errout, err = Shellout("oc delete catsrc installed-custom-openshift-operators -n openshift-operators") |
| 93 | + err = Client.Delete("sub", subName, namespace) |
121 | 94 | if err != nil { |
122 | | - t.Logf("stdout: %s\n", out) |
123 | | - t.Logf("stderr: %s\n", errout) |
124 | | - t.Logf("error: %v\n", err) |
125 | | - } else { |
126 | | - t.Logf(out) |
| 95 | + t.Logf("Error: %v\n", err) |
127 | 96 | } |
128 | 97 |
|
129 | | - out, errout, err = Shellout("oc delete csc installed-custom-openshift-operators -n openshift-marketplace") |
| 98 | + csv := fmt.Sprintf("devconsole-operator.v%s", operatorVersion) |
| 99 | + err = Client.Delete("csv", csv, namespace) |
130 | 100 | if err != nil { |
131 | | - t.Logf("stdout: %s\n", out) |
132 | | - t.Logf("stderr: %s\n", errout) |
133 | | - t.Logf("error: %v\n", err) |
134 | | - } else { |
135 | | - t.Logf(out) |
| 101 | + t.Logf("Error: %v\n", err) |
136 | 102 | } |
137 | 103 |
|
138 | | - out, errout, err = Shellout(fmt.Sprintf("oc delete csv devconsole-operator.v%s -n openshift-operators", operatorVersion)) |
| 104 | + err = Client.Delete("pod", pod.Name, namespace) |
139 | 105 | if err != nil { |
140 | | - t.Logf("stdout: %s\n", out) |
141 | | - t.Logf("stderr: %s\n", errout) |
142 | | - t.Logf("error: %v\n", err) |
143 | | - } else { |
144 | | - t.Logf(out) |
| 106 | + t.Logf("Error: %v\n", err) |
145 | 107 | } |
146 | 108 | } |
0 commit comments