Skip to content
This repository has been archived by the owner on Oct 3, 2019. It is now read-only.

[WIP] Replace oc commands in OperatorSource automated test with client-go #213

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ required = [
"k8s.io/gengo/args",
"sigs.k8s.io/controller-tools/pkg/crd/generator",
"github.com/golang/protobuf/proto",
"github.com/coreos/go-semver",
]

[[override]]
Expand Down Expand Up @@ -107,3 +108,12 @@ required = [
"pkg/apis",
"pkg/apis/devconsole/v1alpha1",
]

[[constraint]]
name = "github.com/operator-framework/operator-lifecycle-manager"
revision = "aeb24aeb363b25c8f9e8267f27579c1483dacc42"

[[constraint]]
name = "github.com/coreos/go-semver"
revision = "8ab6407b697782a06568d4b7f1db25550ec2e4c6"

147 changes: 51 additions & 96 deletions test/operatorsource/basic_test.go
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])
Copy link
Collaborator

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 cause panic if pod isn't created.

Copy link
Author

@pratikjagrut pratikjagrut May 20, 2019

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

Copy link
Collaborator

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 and error values nil from GetPodByLabel ? wouldn't it fail in that case ? So just for safe side I think you should check array length.

Copy link
Author

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 check if pods == nil. Okay that can be done. Thanks.

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)
}
}
Loading