This repository has been archived by the owner on Oct 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
[WIP] Replace oc commands in OperatorSource automated test with client-go #213
Open
pratikjagrut
wants to merge
5
commits into
redhat-developer:master
Choose a base branch
from
pratikjagrut:rewrite.operatorsource.test
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
03914f4
[WIP] Replace oc commands in OperatorSource automated test with clien…
2266d63
[WIP] Replace oc commands in OperatorSource automated test with clien…
c3ee06f
Merge branch 'rewrite.operatorsource.test' of github.com:pratikjagrut…
c3230ca
Add dependecy for operatorsource test.
e73455f
Merge branch 'master' into rewrite.operatorsource.test
akashshinde File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,146 +1,101 @@ | ||
package operatorsource | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"github.com/stretchr/testify/require" | ||
corev1 "k8s.io/api/core/v1" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"time" | ||
) | ||
|
||
const ShellToUse = "bash" | ||
|
||
func Shellout(command string) (string, string, error) { | ||
var stdout bytes.Buffer | ||
var stderr bytes.Buffer | ||
cmd := exec.Command(ShellToUse, "-c", command) | ||
cmd.Stdout = &stdout | ||
cmd.Stderr = &stderr | ||
err := cmd.Run() | ||
return stdout.String(), stderr.String(), err | ||
} | ||
|
||
func Test_OperatorSource_oc_commands(t *testing.T) { | ||
|
||
defer CleanUp(t) | ||
var ( | ||
Client = NewTestClient() | ||
namespace = "openshift-operators" | ||
subName = "devconsole" | ||
label = "name=devconsole-operator" | ||
subscription, suberr = Client.GetSubscription(subName, namespace) | ||
) | ||
|
||
t.Run("login", func(t *testing.T) { Login(t) }) | ||
t.Run("subscription", func(t *testing.T) { Subscription(t) }) | ||
t.Run("install plan", func(t *testing.T) { InstallPlan(t) }) | ||
t.Run("operator pod", func(t *testing.T) { OperatorPod(t) }) | ||
} | ||
func Test_OperatorSource(t *testing.T) { | ||
pods, err := Client.GetPodByLabel(label, namespace) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer CleanUp(t, &pods.Items[0]) | ||
retryInterval := time.Second * 10 | ||
timeout := time.Second * 120 | ||
|
||
func Login(t *testing.T) { | ||
// Start - Login to oc | ||
out, _, err := Shellout("oc login -u " + os.Getenv("OC_LOGIN_USERNAME") + " -p " + os.Getenv("OC_LOGIN_PASSWORD")) | ||
err = Client.WaitForOperatorDeployment(t, pods.Items[0].Name, namespace, retryInterval, timeout) | ||
if err != nil { | ||
t.Fatalf("error: %v\n", err) | ||
t.Fatal(err) | ||
} else { | ||
require.True(t, strings.Contains(out, "Login successful."), "Expecting successful login") | ||
t.Run("subscription", func(t *testing.T) { Subscription(t) }) | ||
t.Run("install plan", func(t *testing.T) { InstallPlan(t) }) | ||
t.Run("operator pod", func(t *testing.T) { OperatorPod(t) }) | ||
} | ||
} | ||
|
||
func Subscription(t *testing.T) { | ||
// 1) Verify that the subscription was created | ||
out, errout, err := Shellout("oc get sub devconsole -n openshift-operators") | ||
if err != nil { | ||
t.Logf("stdout: %s\n", out) | ||
t.Logf("stderr: %s\n", errout) | ||
t.Fatalf("error: %v\n", err) | ||
} else { | ||
require.True(t, strings.Contains(out, "devconsole"), "Expecting the subscription name to be found") | ||
require.True(t, strings.Contains(out, "installed-custom-openshift-operators"), "Expecting the subscription namespace to be found") | ||
if suberr != nil { | ||
t.Fatal(suberr) | ||
} | ||
require.Equal(t, subName, subscription.Name) | ||
require.Equal(t, "installed-custom-openshift-operators", subscription.Spec.CatalogSource) | ||
} | ||
|
||
func InstallPlan(t *testing.T) { | ||
// 2) Find the name of the install plan | ||
out, errout, err := Shellout("oc get sub devconsole -n openshift-operators -o jsonpath='{.status.installplan.name}'") | ||
var installPlan string | ||
installPlanName := subscription.Status.Install.Name | ||
installPlan, err := Client.GetInstallPlan(installPlanName, namespace) | ||
if err != nil { | ||
t.Logf("stdout: %s\n", out) | ||
t.Logf("stderr: %s\n", errout) | ||
t.Fatalf("error: %v\n", err) | ||
} else { | ||
installPlan = out | ||
t.Fatal(err) | ||
} | ||
|
||
// 3) Verify the install plan | ||
out, errout, err = Shellout(fmt.Sprintf("oc get installplan %s -n openshift-operators", installPlan)) | ||
if err != nil { | ||
t.Logf("stdout: %s\n", out) | ||
t.Logf("stderr: %s\n", errout) | ||
t.Fatalf("error: %v\n", err) | ||
} else { | ||
require.True(t, strings.Contains(out, installPlan), "Expecting the Install Plan name to be found") | ||
require.True(t, strings.Contains(out, "devconsole-operator.v0.1.0"), "Expecting the Operator release to be found") | ||
require.True(t, strings.Contains(out, "Automatic"), "Expecting the approval method to be found") | ||
require.True(t, strings.Contains(out, "true"), "Expecting the approved state to be found") | ||
require.Equal(t, "devconsole-operator.v0.1.0", installPlan.Spec.ClusterServiceVersionNames[0]) | ||
require.Equal(t, "Automatic", string(installPlan.Spec.Approval)) | ||
if !installPlan.Spec.Approved { | ||
require.FailNow(t, "Install plan approved is false") | ||
} | ||
} | ||
|
||
func OperatorPod(t *testing.T) { | ||
// Verify that the operator's pod is running | ||
out, errout, err := Shellout("oc get pods -l name=devconsole-operator -n openshift-operators -o jsonpath='{.items[*].status.phase}'") | ||
// 3) Check operator pod status, fail status != Running | ||
pods, err := Client.GetPodByLabel(label, namespace) | ||
if err != nil { | ||
t.Logf("stdout: %s\n", out) | ||
t.Logf("stderr: %s\n", errout) | ||
t.Fatalf("error: %v\n", err) | ||
} else { | ||
require.True(t, strings.Contains(out, "Running"), "Expecting the state of the Operator pod to be running") | ||
t.Fatal(err) | ||
} | ||
pod := pods.Items[0] | ||
require.Equal(t, pod.Status.Phase, corev1.PodRunning) | ||
} | ||
|
||
func CleanUp(t *testing.T) { | ||
func CleanUp(t *testing.T, pod *corev1.Pod) { | ||
// Clean up resources | ||
operatorSourceName := os.Getenv("OPSRC_NAME") | ||
operatorVersion := os.Getenv("DEVCONSOLE_OPERATOR_VERSION") | ||
|
||
out, errout, err := Shellout(fmt.Sprintf("oc delete opsrc %s -n openshift-marketplace", operatorSourceName)) | ||
err := Client.Delete("installplan", subscription.Status.Install.Name, namespace) | ||
if err != nil { | ||
t.Logf("stdout: %s\n", out) | ||
t.Logf("stderr: %s\n", errout) | ||
t.Logf("error: %v\n", err) | ||
} else { | ||
t.Logf(out) | ||
t.Logf("Error: %v\n", err) | ||
} | ||
|
||
out, errout, err = Shellout("oc delete sub devconsole -n openshift-operators") | ||
err = Client.Delete("catsrc", subscription.Spec.CatalogSource, namespace) | ||
if err != nil { | ||
t.Logf("stdout: %s\n", out) | ||
t.Logf("stderr: %s\n", errout) | ||
t.Logf("error: %v\n", err) | ||
} else { | ||
t.Logf(out) | ||
t.Logf("Error: %v\n", err) | ||
} | ||
|
||
out, errout, err = Shellout("oc delete catsrc installed-custom-openshift-operators -n openshift-operators") | ||
err = Client.Delete("sub", subName, namespace) | ||
if err != nil { | ||
t.Logf("stdout: %s\n", out) | ||
t.Logf("stderr: %s\n", errout) | ||
t.Logf("error: %v\n", err) | ||
} else { | ||
t.Logf(out) | ||
t.Logf("Error: %v\n", err) | ||
} | ||
|
||
out, errout, err = Shellout("oc delete csc installed-custom-openshift-operators -n openshift-marketplace") | ||
csv := fmt.Sprintf("devconsole-operator.v%s", operatorVersion) | ||
err = Client.Delete("csv", csv, namespace) | ||
if err != nil { | ||
t.Logf("stdout: %s\n", out) | ||
t.Logf("stderr: %s\n", errout) | ||
t.Logf("error: %v\n", err) | ||
} else { | ||
t.Logf(out) | ||
t.Logf("Error: %v\n", err) | ||
} | ||
|
||
out, errout, err = Shellout(fmt.Sprintf("oc delete csv devconsole-operator.v%s -n openshift-operators", operatorVersion)) | ||
err = Client.Delete("pod", pod.Name, namespace) | ||
if err != nil { | ||
t.Logf("stdout: %s\n", out) | ||
t.Logf("stderr: %s\n", errout) | ||
t.Logf("error: %v\n", err) | ||
} else { | ||
t.Logf(out) | ||
t.Logf("Error: %v\n", err) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pratikjagrut You are accessing 1st element of the
Pod.Items[]
without checking the array size. This would causepanic
if pod isn't created.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@akashshinde I'm checking Pods length before returning it from GetPodByLabel(), so if pod isn't created it will return nil. you can check it here: https://github.com/redhat-developer/devconsole-operator/pull/213/files#diff-31828c90d3e18decf90b57b66bbe0b66R59
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pratikjagrut What if there is the case where you get both
pod
anderror
values nil fromGetPodByLabel
? wouldn't it fail in that case ? So just for safe side I think you should check array length.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@akashshinde I have checked the Items length
if len(pods.Items) == 0
, but you are saying I should add an extra checkif pods == nil
. Okay that can be done. Thanks.