Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic tests for cloud shell #90

Merged
merged 11 commits into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ require (
)

require (
github.com/onsi/ginkgo v1.11.0
github.com/onsi/gomega v1.8.1
github.com/operator-framework/operator-sdk v0.17.0
github.com/spf13/pflag v1.0.5
k8s.io/api v0.17.4
Expand Down
102 changes: 102 additions & 0 deletions test/e2e/cmd/workspaces_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package cmd

import (
"fmt"
"path/filepath"
"testing"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/che-incubator/che-workspace-operator/test/e2e/pkg/metadata"
_ "github.com/che-incubator/che-workspace-operator/test/e2e/pkg/tests"
workspaces "github.com/che-incubator/che-workspace-operator/test/e2e/pkg/workspaces"
flacatus marked this conversation as resolved.
Show resolved Hide resolved
"github.com/onsi/ginkgo"
"github.com/onsi/ginkgo/reporters"
"github.com/onsi/gomega"
)

//Create Constant file
const (
testResultsDirectory = "/tmp/artifacts"
jUnitOutputFilename = "junit-workspaces-operator.xml"
)

//SynchronizedBeforeSuite blocks is executed before run all test suites
var _ = ginkgo.SynchronizedBeforeSuite(func() []byte {
//!TODO: Try to create a specific function to call all <ginkgo suite> configuration.
fmt.Println("Starting to setup objects before run ginkgo suite")
metadata.Namespace.Name = "che-workspace-controller"

workspaces := workspaces.NewWorkspaceClient()

ns := newNamespace()
ns, err := workspaces.Kube().CoreV1().Namespaces().Create(ns)

if err != nil {
fmt.Println("Failed to create namespace")
flacatus marked this conversation as resolved.
Show resolved Hide resolved
}

if err := workspaces.CreatePluginRegistryDeployment(); err != nil {
flacatus marked this conversation as resolved.
Show resolved Hide resolved
_ = fmt.Errorf("Failed to create deployment for plugin registry")
}

if err := workspaces.CreatePluginRegistryService(); err != nil {
_ = fmt.Errorf("Failed to create plugin registry service %s", err)
}

if err := workspaces.CreateOpenshiftRoute(); err != nil {
_ = fmt.Errorf("Failed to create route in cluster %s", err)
}

if err := workspaces.CreateAllOperatorRoles(); err != nil {
_ = fmt.Errorf("Failed to create roles in clusters %s", err)
}

if err := workspaces.CreateOperatorClusterRole(); err != nil {
_ = fmt.Errorf("Failed to create roles in clusters %s", err)
flacatus marked this conversation as resolved.
Show resolved Hide resolved
}

if err := workspaces.CustomResourceDefinitions(); err != nil {
_ = fmt.Errorf("Failed to add custom resources definitions to cluster %s", err)
flacatus marked this conversation as resolved.
Show resolved Hide resolved
}

if err := workspaces.DeployWorkspacesController(); err != nil {
_ = fmt.Errorf("Failed to deploy workspace controller %s", err)
}

return nil
}, func(data []byte) {})

var _ = ginkgo.SynchronizedAfterSuite(func() {
workspaces := workspaces.NewWorkspaceClient()

if err := workspaces.Kube().CoreV1().Namespaces().Delete(metadata.Namespace.Name, &metav1.DeleteOptions{}); err != nil {
_ = fmt.Errorf("Failed to deploy workspace controller %s", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_ = fmt.Errorf("Failed to deploy workspace controller %s", err)
_ = fmt.Errorf("Failed to uninstall workspace controller %s", err)

}
}, func() {})

func TestHarnessCodeReadyWorkspaces(t *testing.T) {
flacatus marked this conversation as resolved.
Show resolved Hide resolved
gomega.RegisterFailHandler(ginkgo.Fail)

fmt.Println("Creating ginkgo reporter for Test Harness: Junit and Debug Detail reporter")
var r []ginkgo.Reporter
r = append(r, reporters.NewJUnitReporter(filepath.Join(testResultsDirectory, jUnitOutputFilename)))

fmt.Println("Running Workspace Controller e2e tests...")
ginkgo.RunSpecsWithDefaultAndCustomReporters(t, "Workspaces Controller Operator Tests", r)

}

func newNamespace() (ns *corev1.Namespace) {
flacatus marked this conversation as resolved.
Show resolved Hide resolved
return &corev1.Namespace{

TypeMeta: metav1.TypeMeta{
Kind: "Namespace",
APIVersion: corev1.SchemeGroupVersion.Version,
},
ObjectMeta: metav1.ObjectMeta{
Name: metadata.Namespace.Name,
},
}
}
7 changes: 7 additions & 0 deletions test/e2e/pkg/metadata/metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package metadata

type OperatorNamespace struct {
Name string
}

var Namespace = OperatorNamespace{}
flacatus marked this conversation as resolved.
Show resolved Hide resolved
flacatus marked this conversation as resolved.
Show resolved Hide resolved
22 changes: 22 additions & 0 deletions test/e2e/pkg/tests/cloud_shell_tests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package tests

import (
"fmt"
"github.com/che-incubator/che-workspace-operator/test/e2e/pkg/workspaces"
"github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
flacatus marked this conversation as resolved.
Show resolved Hide resolved
)
var client workspaces.CodeReady

var _ = ginkgo.Describe("[Create Cloud Shell Workspace]", func() {
ginkgo.It("Add cloud shell to cluster", func() {
label := "che.workspace_name=cloud-shell"
workspace := workspaces.NewWorkspaceClient()
_ = workspace.AddCloudShellSample()
deploy , err := client.PodDeployWaitUtil(label)
flacatus marked this conversation as resolved.
Show resolved Hide resolved
if !deploy {
fmt.Println("Cloud Shell not deployed")
}
Expect(err).NotTo(HaveOccurred())
})
})
24 changes: 24 additions & 0 deletions test/e2e/pkg/workspaces/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package workspaces

import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client/config"
)

type CodeReady struct {
flacatus marked this conversation as resolved.
Show resolved Hide resolved
rest *rest.Config
}

// NewCodeReady creates C, a workspaces used to expose common testing functions.
flacatus marked this conversation as resolved.
Show resolved Hide resolved
func NewWorkspaceClient() *CodeReady {
flacatus marked this conversation as resolved.
Show resolved Hide resolved
h := &CodeReady{}
return h
}

// Kube returns the clientset for Kubernetes upstream.
func (c *CodeReady) Kube() kubernetes.Interface {
flacatus marked this conversation as resolved.
Show resolved Hide resolved
cfg, _ := config.GetConfig()
client, _ := kubernetes.NewForConfig(cfg)
return client
}
48 changes: 48 additions & 0 deletions test/e2e/pkg/workspaces/deserialize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package workspaces

import (
"fmt"
"io/ioutil"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes/scheme"
)

func deserializePluginRegistryDeployment() (pluginRegistry *appsv1.Deployment, err error) {
if err != nil {
flacatus marked this conversation as resolved.
Show resolved Hide resolved
fmt.Println("Failed to locate operator deployment yaml, %s", err)
}
file, err := ioutil.ReadFile("deploy/registry/local/deployment.yaml")
if err != nil {
fmt.Println("Failed to locate operator deployment yaml, %s", err)
flacatus marked this conversation as resolved.
Show resolved Hide resolved
}
deployment := string(file)
decode := scheme.Codecs.UniversalDeserializer().Decode
object, _, err := decode([]byte(deployment), nil, nil)
flacatus marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
fmt.Println("Failed to deserialize yaml %s", err)
return nil, err
}
pluginRegistry = object.(*appsv1.Deployment)
return pluginRegistry, nil
}

func deserializePluginRegistryService() (pluginRegistry *corev1.Service, err error) {
if err != nil {
fmt.Println("Failed to locate operator deployment yaml, %s", err)
}
file, err := ioutil.ReadFile("deploy/registry/local/service.yaml")
if err != nil {
fmt.Println("Failed to locate operator deployment yaml, %s", err)
flacatus marked this conversation as resolved.
Show resolved Hide resolved
}
deployment := string(file)
decode := scheme.Codecs.UniversalDeserializer().Decode
object, _, err := decode([]byte(deployment), nil, nil)
flacatus marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
fmt.Println("Failed to deserialize yaml %s", err)
return nil, err
}
pluginRegistry = object.(*corev1.Service)
return pluginRegistry, nil
}
76 changes: 76 additions & 0 deletions test/e2e/pkg/workspaces/oc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package workspaces

import (
"fmt"
"github.com/che-incubator/che-workspace-operator/test/e2e/pkg/metadata"
"os/exec"
"strings"
)

func (w *CodeReady) DeployWorkspacesController() (err error) {
label := "app=che-workspace-controller"
cmd := exec.Command("oc", "apply", "--namespace", metadata.Namespace.Name, "-f", "deploy/os")
output, err := cmd.CombinedOutput()
fmt.Println(string(output))
if err != nil && !strings.Contains(string(output), "AlreadyExists") {
fmt.Println(err)
return err
}

deploy , err := w.PodDeployWaitUtil(label)
if !deploy {
fmt.Println("Che Workspaces Controller not deployed")
return err
}
return err
}

func (w *CodeReady) CreateAllOperatorRoles() (err error) {
cmd := exec.Command("oc", "apply", "--namespace", metadata.Namespace.Name, "-f", "deploy")
output, err := cmd.CombinedOutput()
fmt.Println(string(output))
if err != nil && !strings.Contains(string(output), "AlreadyExists") {
fmt.Println(err)
}
return err
}

func (w *CodeReady) CreateOpenshiftRoute() (err error) {
flacatus marked this conversation as resolved.
Show resolved Hide resolved
cmd := exec.Command("oc", "apply", "--namespace", metadata.Namespace.Name, "-f", "deploy/registry/local/os")
output, err := cmd.CombinedOutput()
fmt.Println(string(output))
if err != nil && !strings.Contains(string(output), "AlreadyExists") {
fmt.Println(err)
}
return err
}

func (w *CodeReady) CustomResourceDefinitions() (err error) {
cmd := exec.Command("oc", "apply", "--namespace", metadata.Namespace.Name, "-f", "deploy/crds")
flacatus marked this conversation as resolved.
Show resolved Hide resolved
output, err := cmd.CombinedOutput()
if err != nil && !strings.Contains(string(output), "AlreadyExists") {
fmt.Println(err)
}
return err
}

func (w *CodeReady) CreateOperatorClusterRole() (err error) {
cmd := exec.Command("oc", "apply", "--namespace", metadata.Namespace.Name, "-f", "deploy/role.yaml")
flacatus marked this conversation as resolved.
Show resolved Hide resolved
flacatus marked this conversation as resolved.
Show resolved Hide resolved
output, err := cmd.CombinedOutput()
if err != nil && !strings.Contains(string(output), "AlreadyExists") {
fmt.Println(err)
}
return err
}

func (w *CodeReady) AddCloudShellSample() (err error) {
const namespace = "che-workspace-controller"
flacatus marked this conversation as resolved.
Show resolved Hide resolved
cmd := exec.Command("oc", "apply", "--namespace", namespace, "-f", "samples/cloud-shell.yaml")
output, err := cmd.CombinedOutput()
fmt.Println(string(output))
if err != nil && !strings.Contains(string(output), "AlreadyExists") {
fmt.Println(err)
}
return err
}

89 changes: 89 additions & 0 deletions test/e2e/pkg/workspaces/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package workspaces

import (
"errors"
"fmt"
"time"

"github.com/che-incubator/che-workspace-operator/test/e2e/pkg/metadata"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
)

func (w *CodeReady) PodDeployWaitUtil(label string) (deployed bool, err error) {
timeout := time.After(15 * time.Minute)
tick := time.Tick(1 * time.Second)

for {
select {
case <-timeout:
return false, errors.New("timed out")
case <-tick:
desc := w.WaitForPodBySelectorRunning(metadata.Namespace.Name, label, 180)
if desc != nil {
} else {
return true, nil
}
flacatus marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

// return a condition function that indicates whether the given pod is
// currently running
func (w *CodeReady) isPodRunning(podName, namespace string) wait.ConditionFunc {
return func() (bool, error) {
pod, _ := w.Kube().CoreV1().Pods(namespace).Get(podName, metav1.GetOptions{})
age := time.Since(pod.GetCreationTimestamp().Time).Seconds()

switch pod.Status.Phase {
case v1.PodRunning:
fmt.Println("Pod started after", age, "seconds")
return true, nil
case v1.PodFailed, v1.PodSucceeded:
return false, nil
}
return false, nil
}
}

// Poll up to timeout seconds for pod to enter running state.
// Returns an error if the pod never enters the running state.
func (w *CodeReady) waitForPodRunning(namespace, podName string, timeout time.Duration) error {
return wait.PollImmediate(time.Second, timeout, w.isPodRunning(podName, namespace))
}

// Returns the list of currently scheduled or running pods in `namespace` with the given selector
func (w *CodeReady) ListPods(namespace, selector string) (*v1.PodList, error) {
listOptions := metav1.ListOptions{LabelSelector: selector}
podList, err := w.Kube().CoreV1().Pods(namespace).List(listOptions)

if err != nil {
return nil, err
}
return podList, nil
}

// Wait up to timeout seconds for all pods in 'namespace' with given 'selector' to enter running state.
// Returns an error if no pods are found or not all discovered pods enter running state.
func (w *CodeReady) WaitForPodBySelectorRunning(namespace, selector string, timeout int) error {
podList, err := w.ListPods(namespace, selector)

if err != nil {
return err
}
if len(podList.Items) == 0 {
fmt.Println("Pod not created yet with selector " + selector + " in namespace " + namespace)

return fmt.Errorf("Pod not created yet in %s with label %s", namespace, selector)
}

for _, pod := range podList.Items {
fmt.Println("Pod " + pod.Name + " created in namespace " + namespace + "...Checking startup data.")
if err := w.waitForPodRunning(namespace, pod.Name, time.Duration(timeout)*time.Second); err != nil {
return err
}
}

return nil
}
Loading