Skip to content

Commit

Permalink
add 2 test case
Browse files Browse the repository at this point in the history
  • Loading branch information
targetoee committed Oct 7, 2023
1 parent 8b26c37 commit 5bcb504
Show file tree
Hide file tree
Showing 5 changed files with 692 additions and 0 deletions.
146 changes: 146 additions & 0 deletions test/e2e/framework/helpers/k8s/k8s_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ import (
batchv1 "k8s.io/api/batch/v1"
v1 "k8s.io/api/core/v1"
authv1 "k8s.io/api/rbac/v1"
rbacv1 "k8s.io/api/rbac/v1"
schedulingv1 "k8s.io/api/scheduling/v1"
storagev1 "k8s.io/api/storage/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/httpstream"
Expand Down Expand Up @@ -318,6 +320,10 @@ func (k *KubeCtl) GetService(serviceName string, namespace string) (*v1.Service,
return k.clientSet.CoreV1().Services(namespace).Get(context.TODO(), serviceName, metav1.GetOptions{})
}

func (k *KubeCtl) CreateService(service *v1.Service, namespace string) (*v1.Service, error) {
return k.clientSet.CoreV1().Services(namespace).Create(context.TODO(), service, metav1.CreateOptions{})
}

// Func to create a namespace provided a name
func (k *KubeCtl) CreateNamespace(namespace string, annotations map[string]string) (*v1.Namespace, error) {
// create namespace
Expand Down Expand Up @@ -840,6 +846,10 @@ func (k *KubeCtl) DeleteServiceAccount(accountName string, namespace string) err
return k.clientSet.CoreV1().ServiceAccounts(namespace).Delete(context.TODO(), accountName, metav1.DeleteOptions{})
}

func (k *KubeCtl) CreateClusterRole(clusterRole *rbacv1.ClusterRole) (*rbacv1.ClusterRole, error) {
return k.clientSet.RbacV1().ClusterRoles().Create(context.TODO(), clusterRole, metav1.CreateOptions{})
}

func (k *KubeCtl) CreateClusterRoleBinding(
roleName string,
role string,
Expand All @@ -859,6 +869,10 @@ func (k *KubeCtl) CreateClusterRoleBinding(
}, metav1.CreateOptions{})
}

func (k *KubeCtl) DeleteClusterRole(roleName string) error {
return k.clientSet.RbacV1().ClusterRoles().Delete(context.TODO(), roleName, metav1.DeleteOptions{})
}

func (k *KubeCtl) DeleteClusterRoleBindings(roleName string) error {
return k.clientSet.RbacV1().ClusterRoleBindings().Delete(context.TODO(), roleName, metav1.DeleteOptions{})
}
Expand Down Expand Up @@ -1361,3 +1375,135 @@ func (k *KubeCtl) DeleteWorkloadAndPods(objectName string, wlType WorkloadType,
err = k.WaitForPodCount(namespace, 0, 10*time.Second)
gomega.Ω(err).ShouldNot(gomega.HaveOccurred())
}

func (k *KubeCtl) CreatePersistentVolume(pv *v1.PersistentVolume)(*v1.PersistentVolume, error) {
return k.clientSet.CoreV1().PersistentVolumes().Create(context.TODO(), pv, metav1.CreateOptions{})
}

func (k *KubeCtl) CreatePersistentVolumeClaim(pvc *v1.PersistentVolumeClaim, ns string) (*v1.PersistentVolumeClaim, error) {
return k.clientSet.CoreV1().PersistentVolumeClaims(ns).Create(context.TODO(), pvc, metav1.CreateOptions{})
}

func (k *KubeCtl) CreateStorageClass(sc *storagev1.StorageClass)(*storagev1.StorageClass, error) {
return k.clientSet.StorageV1().StorageClasses().Create(context.TODO(), sc, metav1.CreateOptions{})
}

func (k *KubeCtl) GetPersistentVolume(name string) (*v1.PersistentVolume, error) {
pv, err := k.clientSet.CoreV1().PersistentVolumes().Get(context.TODO(), name, metav1.GetOptions{})
return pv, err
}

func (k *KubeCtl) WaitForPersistentVolumeAvailable(name string, timeout time.Duration) error {
return wait.PollImmediate(time.Second, timeout, k.isPersistentVolumeAvailable(name))
}

func (k *KubeCtl) WaitForPersistentVolumeClaimPresent(namespace string, name string, timeout time.Duration) error {
return wait.PollImmediate(time.Second, timeout, k.isPersistentVolumeClaimPresent(namespace, name))
}

func (k *KubeCtl) isPersistentVolumeAvailable(name string) wait.ConditionFunc {
return func() (bool, error) {
pv, err := k.GetPersistentVolume(name)
if err != nil {
return false, err
}
if pv.Status.Phase == v1.VolumeAvailable {
return true, nil
}
return false, nil
}

Check failure on line 1414 in test/e2e/framework/helpers/k8s/k8s_utils.go

View workflow job for this annotation

GitHub Actions / build

SA1019: wait.PollImmediate is deprecated: This method does not return errors from context, use PollWithContextTimeout. Note that the new method will no longer return ErrWaitTimeout and instead return errors defined by the context package. Will be removed in a future release. (staticcheck)
}

func (k *KubeCtl) isPersistentVolumeClaimPresent(namespace string, name string) wait.ConditionFunc {
return func() (bool, error) {

Check failure on line 1418 in test/e2e/framework/helpers/k8s/k8s_utils.go

View workflow job for this annotation

GitHub Actions / build

SA1019: wait.PollImmediate is deprecated: This method does not return errors from context, use PollWithContextTimeout. Note that the new method will no longer return ErrWaitTimeout and instead return errors defined by the context package. Will be removed in a future release. (staticcheck)
_, err := k.clientSet.CoreV1().PersistentVolumeClaims(namespace).Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
return false, err
}
return true, nil
}
}

func (k *KubeCtl) GetPvcNameListFromNs(namespace string) ([]string, error) {
var arr []string
pvcList, err := k.clientSet.CoreV1().PersistentVolumeClaims(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}

for _, item := range pvcList.Items {
arr = append(arr, item.Name)
}
return arr, nil
}

func (k *KubeCtl) GetPvNameListFromNs(namespace string) ([]string, error) {
var arr []string
pvList, err := k.clientSet.CoreV1().PersistentVolumes().List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}

for _, item := range pvList.Items {
if item.Spec.ClaimRef.Namespace == namespace{
arr = append(arr, item.Name)
}
}
return arr, nil
}

func (k *KubeCtl) DeletePersistentVolume(pvName string) error {
err := k.clientSet.CoreV1().PersistentVolumes().Delete(context.TODO(), pvName, metav1.DeleteOptions{})
if err != nil {
return err
}
return nil
}

func(k *KubeCtl) DeletePersistentVolumeClaim(pvcName string, namespace string) error {
err := k.clientSet.CoreV1().PersistentVolumeClaims(namespace).Delete(context.TODO(), pvcName, metav1.DeleteOptions{})
if err != nil {
return err
}
return nil
}

func (k *KubeCtl) DeletePVCs(namespace string) error {
// Delete all PVC by namespace
var PvcList, err = k.GetPvcNameListFromNs(namespace)
if err != nil {
return err
}

for _, each := range PvcList {
err = k.DeletePersistentVolumeClaim(each, namespace)
if err != nil {
return err
}
}
return nil
}

func (k *KubeCtl) DeletePVs(namespace string) error {
// Delete all PV by namespace
var PvcList, err = k.GetPvNameListFromNs(namespace)
if err != nil {
return err
}

for _, item := range PvcList {
err = k.DeletePersistentVolume(item)
if err != nil {
return err
}
}
return nil
}

func (k *KubeCtl) DeleteStorageClass(scName string) error {
err := k.clientSet.StorageV1().StorageClasses().Delete(context.TODO(), scName, metav1.DeleteOptions{})
if err != nil {
return err
}
return nil
}
23 changes: 23 additions & 0 deletions test/e2e/framework/helpers/k8s/pod_conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ type TestPodConfig struct {
RestartPolicy v1.RestartPolicy
Command []string
InitContainerSleepSecs int
PvcName string
PvName string
VolumeName string
}

func InitTestPod(conf TestPodConfig) (*v1.Pod, error) { //nolint:funlen
Expand Down Expand Up @@ -216,6 +219,26 @@ func InitTestPod(conf TestPodConfig) (*v1.Pod, error) { //nolint:funlen
},
}
}
if conf.PvcName != "" || conf.PvName != "" {
if conf.VolumeName == "" {
conf.VolumeName = "vol-" + common.RandSeq(5)
}
if conf.PvcName != "" {
pod.Spec.Volumes = []v1.Volume{
{
Name: conf.VolumeName,
VolumeSource: v1.VolumeSource{
PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
ClaimName: conf.PvcName,
},
},
},
}
} else if conf.PvName != "" {
pod.Spec.Volumes = []v1.Volume {
}
}
}
return pod, nil
}

Expand Down
127 changes: 127 additions & 0 deletions test/e2e/framework/helpers/k8s/pv_conf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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 k8s

import (
v1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"k8s.io/apimachinery/pkg/api/resource"
)

type PvConfig struct {
Name string
Labels map[string]string
Capacity string
AccessModes []v1.PersistentVolumeAccessMode
Source string
Path string
NodeAffinity *v1.VolumeNodeAffinity
StorageClass string
}

func InitPersistentVolume(conf PvConfig) (*v1.PersistentVolume, error) {
pv := &v1.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{
Name: conf.Name,
},
Spec: v1.PersistentVolumeSpec{
Capacity: v1.ResourceList{
v1.ResourceStorage: resource.MustParse(conf.Capacity),
},
AccessModes: conf.AccessModes,
PersistentVolumeReclaimPolicy: v1.PersistentVolumeReclaimRetain,
StorageClassName: conf.StorageClass,
},
}
if conf.Source == "Local" {
pv.Spec.PersistentVolumeSource = v1.PersistentVolumeSource{
Local: &v1.LocalVolumeSource{
Path: conf.Path,
},
}
if conf.NodeAffinity == nil {
// Create fake condition which won't exclude anything
pv.Spec.NodeAffinity = &v1.VolumeNodeAffinity{
Required: &v1.NodeSelector{
NodeSelectorTerms: []v1.NodeSelectorTerm{
{
MatchExpressions: []v1.NodeSelectorRequirement {
{
Key: "fakeKey",
Operator: v1.NodeSelectorOpNotIn,
Values: []string{"fakeValue"},
},
},
},
},
},
}
} else {
pv.Spec.NodeAffinity = conf.NodeAffinity
}
}
return pv, nil
}

type ScConfig struct {
Name string
Provisioner string
Parameters map[string]string
}

func InitStorageClass(conf ScConfig) (*storagev1.StorageClass, error) {
sc := &storagev1.StorageClass{
ObjectMeta: metav1.ObjectMeta{
Name: conf.Name,
},
Provisioner: conf.Provisioner,
Parameters: conf.Parameters,
}
return sc, nil
}

type PvcConfig struct {
Name string
Capacity string
VolumeName string
StorageClassName string
}

func InitPersistentVolumeClaim (conf PvcConfig)(*v1.PersistentVolumeClaim, error) {
pvc := &v1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: conf.Name,
},
Spec: v1.PersistentVolumeClaimSpec {
AccessModes: []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce},
Resources: v1.ResourceRequirements{
Requests: v1.ResourceList{
v1.ResourceStorage: resource.MustParse(conf.Capacity),
},
},
},
}
if conf.VolumeName != "" {
pvc.Spec.VolumeName = conf.VolumeName
}
if conf.StorageClassName != "" {
pvc.Spec.StorageClassName = &conf.StorageClassName
}
return pvc, nil
}
50 changes: 50 additions & 0 deletions test/e2e/persistent_volume/persistent_volume_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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 persistent_volume

import(
"path/filepath"
"testing"

"github.com/onsi/ginkgo/v2"
"github.com/onsi/ginkgo/v2/reporters"
"github.com/onsi/gomega"

"github.com/apache/yunikorn-k8shim/test/e2e/framework/configmanager"
)

func init() {
configmanager.YuniKornTestConfig.ParseFlags()
}

func TestPersistentVolume(t *testing.T) {
ginkgo.ReportAfterSuite("TestPersistentVolume", func (report ginkgo.Report){
err := reporters.GenerateJUnitReportWithConfig(
report,
filepath.Join(configmanager.YuniKornTestConfig.LogDir, "TEST-persistent_volume_junit.xml"),
reporters.JunitReportConfig{OmitSpecLabels: true},
)
Ω(err).NotTo(HaveOccurred())
})
gomega.RegisterFailHandler(ginkgo.Fail)
ginkgo.RunSpecs(t, "TestPersistentVolume", ginkgo.Label("TestPersistentVolume"))
}

var Ω = gomega.Ω
var HaveOccurred = gomega.HaveOccurred
Loading

0 comments on commit 5bcb504

Please sign in to comment.