Skip to content

Commit

Permalink
feat(bdd, jiva, -ve test) add BDD test for invalid yamls during jiva …
Browse files Browse the repository at this point in the history
…provisioning (openebs-archive#1062)

This commit adds BDD test for validating yamls

Signed-off-by: mittachaitu <[email protected]>
  • Loading branch information
sai chaithanya authored and Amit Kumar Das committed May 28, 2019
1 parent cf2f9f0 commit 320469d
Show file tree
Hide file tree
Showing 8 changed files with 314 additions and 29 deletions.
14 changes: 7 additions & 7 deletions pkg/kubernetes/persistentvolumeclaim/v1alpha1/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,6 @@ func (k *Kubeclient) withDefaults() {
}
}

// WithNamespace sets the kubernetes client against
// the provided namespace
func (k *Kubeclient) WithNamespace(namespace string) *Kubeclient {
k.namespace = namespace
return k
}

// WithClientSet sets the kubernetes client against
// the kubeclient instance
func WithClientSet(c *kubernetes.Clientset) KubeclientBuildOption {
Expand Down Expand Up @@ -156,6 +149,13 @@ func NewKubeClient(opts ...KubeclientBuildOption) *Kubeclient {
return k
}

// WithNamespace sets the kubernetes client against
// the provided namespace
func (k *Kubeclient) WithNamespace(namespace string) *Kubeclient {
k.namespace = namespace
return k
}

func (k *Kubeclient) getClientsetForPathOrDirect() (*kubernetes.Clientset, error) {
if k.kubeConfigPath != "" {
return k.getClientsetForPath(k.kubeConfigPath)
Expand Down
14 changes: 7 additions & 7 deletions pkg/kubernetes/pod/v1alpha1/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,6 @@ func (k *KubeClient) withDefaults() {
}
}

// WithNamespace sets the kubernetes client against
// the provided namespace
func (k *KubeClient) WithNamespace(namespace string) *KubeClient {
k.namespace = namespace
return k
}

// WithClientSet sets the kubernetes client against
// the KubeClient instance
func WithClientSet(c *clientset.Clientset) KubeClientBuildOption {
Expand Down Expand Up @@ -136,6 +129,13 @@ func NewKubeClient(opts ...KubeClientBuildOption) *KubeClient {
return k
}

// WithNamespace sets the kubernetes namespace against
// the provided namespace
func (k *KubeClient) WithNamespace(namespace string) *KubeClient {
k.namespace = namespace
return k
}

func (k *KubeClient) getClientsetForPathOrDirect() (*clientset.Clientset, error) {
if k.kubeConfigPath != "" {
return k.getClientsetForPath(k.kubeConfigPath)
Expand Down
3 changes: 2 additions & 1 deletion tests/framework/v1alpha1/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ func checkComponentStatus(namespace, lselector string, Count int) (pods *corev1.
var err error
Eventually(func() int {
pods, err = pod.
NewKubeClient().WithNamespace(namespace).
NewKubeClient().
WithNamespace(namespace).
List(metav1.ListOptions{LabelSelector: lselector})
Expect(err).ShouldNot(HaveOccurred())
return pod.
Expand Down
200 changes: 200 additions & 0 deletions tests/jiva/invalidconfig/invalidconfig_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
/*
Copyright 2019 The OpenEBS Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package invalidconfig

import (
"fmt"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
apis "github.com/openebs/maya/pkg/apis/openebs.io/v1alpha1"

ns "github.com/openebs/maya/pkg/kubernetes/namespace/v1alpha1"
pvc "github.com/openebs/maya/pkg/kubernetes/persistentvolumeclaim/v1alpha1"
sc "github.com/openebs/maya/pkg/kubernetes/storageclass/v1alpha1"
corev1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var (
// defaultReplicaLabel represents the jiva replica
defaultReplicaLabel = "openebs.io/replica=jiva-replica"
// defaultCtrlLabel represents the jiva controller
defaultCtrlLabel = "openebs.io/controller=jiva-controller"
openebsProvisioner = "openebs.io/provisioner-iscsi"
accessModes = []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce}
capacity = "5G"
scObj *storagev1.StorageClass
pvcObj *corev1.PersistentVolumeClaim
nsObj *corev1.Namespace
// openebsCASConfigValue contains invalid name ReplicaCount:
openebsCASConfigValue = "- name: ReplicaCount:\n Value: " + replicaCount
)

var _ = Describe("[jiva] [-ve] TEST INVALID CAS CONFIGURATIONS IN SC", func() {
var (
nsName = "validation-ns1"
scName = "jiva-invalid-config-sc"
pvcName = "jiva-volume-claim"
pvcLabel = "openebs.io/persistent-volume-claim" + "=" + pvcName
replicaLabel = defaultReplicaLabel + "," + pvcLabel
ctrlLabel = defaultCtrlLabel + "," + pvcLabel
)
BeforeEach(func() {
var err error
annotations := map[string]string{
string(apis.CASTypeKey): string(apis.JivaVolume),
string(apis.CASConfigKey): openebsCASConfigValue,
}

By("building a namespace")
nsObj, err = ns.NewBuilder().
WithName(nsName).
APIObject()
Expect(err).ShouldNot(HaveOccurred(), "while building namespace {%s}", nsName)

By("building a storageclass")
scObj, err = sc.NewBuilder().
WithName(scName).
WithAnnotations(annotations).
WithProvisioner(openebsProvisioner).Build()
Expect(err).ShouldNot(HaveOccurred(), "while building storageclass {%s}", scName)

By("building a persistentvolumeclaim")
pvcObj, err = pvc.NewBuilder().
WithName(pvcName).
WithNamespace(nsName).
WithStorageClass(scName).
WithAccessModes(accessModes).
WithCapacity(capacity).Build()
Expect(err).ShouldNot(HaveOccurred(), "while building persistentvolumeclaim {%s} in namespace {%s}", pvcName, nsName)

By("creating a namespace")
_, err = ops.NSClient.Create(nsObj)
Expect(err).To(BeNil(), "while creating namespace {%s}", nsObj.Name)

By("creating a storageclass")
_, err = ops.SCClient.Create(scObj)
Expect(err).To(BeNil(), "while creating storageclass {%s}", scObj.Name)

})

When("jiva persistentvolumeclaim referring to invalid storageclass is applied", func() {
It("should not create Jiva controller and replica pods", func() {

By("creating a persistentvolumeclaim")
_, err := ops.PVCClient.WithNamespace(nsName).Create(pvcObj)
Expect(err).To(BeNil(), "while creating persistentvolumeclaim {%s} in namespace {%s}", pvcObj.Name, nsName)

By("verifying controller pod count")
controllerPodCount := ops.GetPodRunningCountEventually(nsName, ctrlLabel, 1)
Expect(controllerPodCount).To(Equal(0), "while checking jiva controller pod count")

By("verifying replica pod count")
replicaPodCount := ops.GetPodRunningCountEventually(nsName, replicaLabel, repCountInt)
Expect(replicaPodCount).To(Equal(0), "while checking jiva replica pod count")
})
})

AfterEach(func() {
By("deleting persistentvolumeclaim")
err := ops.PVCClient.Delete(pvcName, &metav1.DeleteOptions{})
Expect(err).To(BeNil(), "while deleting persistentvolumeclaim {%s} in namespace {%s}", pvcObj.Name, nsName)

By("deleting storageclass")
err = ops.SCClient.Delete(scName, &metav1.DeleteOptions{})
Expect(err).To(BeNil(), "while deleting storrageclass {%s}", scObj.Name)

By("deleting namespace")
err = ops.NSClient.Delete(nsName, &metav1.DeleteOptions{})
Expect(err).To(BeNil(), "while deleting namespace {%s}", nsName)
})

})

var _ = Describe("[jiva] [-ve] TEST INVALID CONFIGURATIONS IN persistentvolumeclaim", func() {
var (
nsName = "validation-ns2"
scName = "jiva-valid-config-sc"
pvcName = "jiva-invalid-config-volume-claim"
openebsCASConfigValue = "- name: ReplicaCount\n Value: " + replicaCount
// invalidPVCLabel contains invalid label value
invalidPVCLabel = map[string]string{"name": "jiva-invalid-config-volume-claim:"}
)
BeforeEach(func() {
annotations := map[string]string{
string(apis.CASTypeKey): string(apis.JivaVolume),
string(apis.CASConfigKey): openebsCASConfigValue,
}
var err error

By("building a namespace")
nsObj, err = ns.NewBuilder().
WithName(nsName).
APIObject()
Expect(err).ShouldNot(HaveOccurred(), "while building namespace {%s}", nsName)

By("building a storageclass")
scObj, err = sc.NewBuilder().
WithName(scName).
WithAnnotations(annotations).
WithProvisioner(openebsProvisioner).Build()
Expect(err).ShouldNot(HaveOccurred(), "while building storageclass {%s}", scName)

By("building a persistentvolumeclaim")
pvcObj, err = pvc.NewBuilder().
WithName(pvcName).
WithNamespace(nsName).
WithLabels(invalidPVCLabel).
WithStorageClass(scName).
WithAccessModes(accessModes).
WithCapacity(capacity).Build()
Expect(err).ShouldNot(HaveOccurred(), "while building persistentvolumeclaim {%s} in namespace {%s}", pvcName, nsName)

By("creating a namespace")
_, err = ops.NSClient.Create(nsObj)
Expect(err).To(BeNil(), "while creating namespace {%s}", nsObj.Name)

By("createing a storageclass")
_, err = ops.SCClient.Create(scObj)
Expect(err).To(BeNil(), "while creating storageclass {%s}", scObj.Name)
})

When("We apply invalid persistentvolumeclaim yaml in k8s cluster", func() {
It("PVC creation should give error because of invalid pvc yaml", func() {
By(fmt.Sprintf("create PVC named {%s} in Namespace: {%s}", pvcName, nsName))
_, err := ops.PVCClient.WithNamespace(nsName).Create(pvcObj)
Expect(err).NotTo(BeNil(), "while creating persistentvolumeclaim {%s} in namespace {%s}", pvcObj.Name, nsName)
})
})

AfterEach(func() {
By("deleting persistentvolumeclaim")
err := ops.PVCClient.Delete(pvcName, &metav1.DeleteOptions{})
Expect(err).NotTo(BeNil(), "while deleting persistentvolumeclaim {%s} in namespace {%s}", pvcName, nsName)

By("deleting storageclass")
err = ops.SCClient.Delete(scName, &metav1.DeleteOptions{})
Expect(err).To(BeNil(), "while deleting storageclass {%s}", scName)

By("deleting namespace")
err = ops.NSClient.Delete(nsName, &metav1.DeleteOptions{})
Expect(err).To(BeNil(), "while deleting namespace {%s}", nsName)
})

})
69 changes: 69 additions & 0 deletions tests/jiva/invalidconfig/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
Copyright 2019 The OpenEBS Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package invalidconfig

import (
"flag"
"strconv"

"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
tests "github.com/openebs/maya/tests"
"github.com/openebs/maya/tests/artifacts"

// auth plugins
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
)

var (
kubeConfigPath string
replicaCount string
repCountInt int
ops *tests.Operations
)

func TestSource(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Test openebs by applying invalid configuration in sc and pvc")
}

func init() {
flag.StringVar(&kubeConfigPath, "kubeconfig", "", "path to kubeconfig to invoke kubernetes API calls")
flag.StringVar(&replicaCount, "replicas", "", "No.of storage replicas need to be created")
}

var _ = BeforeSuite(func() {
var err error

ops = tests.NewOperations(tests.WithKubeConfigPath(kubeConfigPath))

repCountInt, err = strconv.Atoi(replicaCount)
Expect(err).ShouldNot(HaveOccurred(), "while converting replicaCount to integer{%s}", replicaCount)

By("Waiting for maya-apiserver pod to come into running state")
podCount := ops.GetPodRunningCountEventually(string(artifacts.OpenebsNamespace), string(artifacts.MayaAPIServerLabelSelector), 1)
Expect(podCount).To(Equal(1))

By("Waiting for openebs-provisioner pod to come into running state")
podCount = ops.GetPodRunningCountEventually(string(artifacts.OpenebsNamespace), string(artifacts.OpenEBSProvisionerLabelSelector), 1)
Expect(podCount).To(Equal(1))
})

var _ = AfterSuite(func() {
})
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ var _ = BeforeSuite(func() {
defaultInstallerList = append(defaultInstallerList, defaultInstaller)
}

podKubeClient := pod.NewKubeClient(pod.WithKubeConfigPath(kubeConfigPath)).WithNamespace(string(artifacts.OpenebsNamespace))
podKubeClient := pod.NewKubeClient(
pod.WithKubeConfigPath(kubeConfigPath)).
WithNamespace(string(artifacts.OpenebsNamespace))
// Check for maya-apiserver pod to get created and running
_ = getPodList(podKubeClient, string(artifacts.OpenebsNamespace), string(artifacts.MayaAPIServerLabelSelector), 1)

Expand Down
Loading

0 comments on commit 320469d

Please sign in to comment.