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

feat: add method to check pvc exists #167

Merged
merged 6 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions docs/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Below you will find the step syntax next to the name of the method it utilizes.
- `<GK> [the] deployment <any-characters-except-(")> is running in namespace <any-characters-except-(")>` kdt.KubeClientSet.DeploymentIsRunning
- `<GK> [the] data in [the] ConfigMap "<any-characters-except-(")>" in namespace "<any-characters-except-(")>" has key "<any-characters-except-(")>" with value "<any-characters-except-(")>"` kdt.KubeClientSet.ConfigMapDataHasKeyAndValue
- `<GK> [the] persistentvolume <any-characters-except-(")> exists with status (Available|Bound|Released|Failed|Pending)` kdt.KubeClientSet.PersistentVolExists
- `<GK> [the] persistentvolumeclaim <any-characters-except-(")> exists with status (Available|Bound|Released|Failed|Pending) in namespace <any-characters-except-(")>` kdt.KubeClientSet.PersistentVolClaimExists
- `<GK> [the] (clusterrole|clusterrolebinding) with name <any-characters-except-(")> should be found` kdt.KubeClientSet.ClusterRbacIsFound
- `<GK> [the] ingress <non-whitespace-characters> in [the] namespace <non-whitespace-characters> [is] [available] on port <digits> and path <any-characters-except-(")>` kdt.KubeClientSet.IngressAvailable
- `<GK> [I] send <digits> tps to ingress <non-whitespace-characters> in [the] namespace <non-whitespace-characters> [available] on port <digits> and path <any-characters-except-(")> for <digits> (minutes|seconds) expecting up to <digits> error[s]` kdt.KubeClientSet.SendTrafficToIngress
Expand Down
1 change: 1 addition & 0 deletions kubedog.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
kdt.scenario.Step(`^(?:the )?deployment ([^"]*) is running in namespace ([^"]*)$`, kdt.KubeClientSet.DeploymentIsRunning)
kdt.scenario.Step(`^(?:the )?data in (?:the )?ConfigMap "([^"]*)" in namespace "([^"]*)" has key "([^"]*)" with value "([^"]*)"$`, kdt.KubeClientSet.ConfigMapDataHasKeyAndValue)
kdt.scenario.Step(`^(?:the )?persistentvolume ([^"]*) exists with status (Available|Bound|Released|Failed|Pending)$`, kdt.KubeClientSet.PersistentVolExists)
kdt.scenario.Step(`^(?:the )?persistentvolumeclaim ([^"]*) exists with status (Available|Bound|Released|Failed|Pending) in namespace ([^"]*)$`, kdt.KubeClientSet.PersistentVolClaimExists)

Check warning on line 83 in kubedog.go

View check run for this annotation

Codecov / codecov/patch

kubedog.go#L83

Added line #L83 was not covered by tests
kdt.scenario.Step(`^(?:the )?(clusterrole|clusterrolebinding) with name ([^"]*) should be found$`, kdt.KubeClientSet.ClusterRbacIsFound)
kdt.scenario.Step(`^(?:the )?ingress (\S+) in (?:the )?namespace (\S+) (?:is )?(?:available )?on port (\d+) and path ([^"]*)$`, kdt.KubeClientSet.IngressAvailable)
kdt.scenario.Step(`^(?:I )?send (\d+) tps to ingress (\S+) in (?:the )?namespace (\S+) (?:available )?on port (\d+) and path ([^"]*) for (\d+) (minutes|seconds) expecting up to (\d+) error(?:s)?$`, kdt.KubeClientSet.SendTrafficToIngress)
Expand Down
4 changes: 4 additions & 0 deletions pkg/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,10 @@
return structured.PersistentVolExists(kc.KubeInterface, name, expectedPhase)
}

func (kc *ClientSet) PersistentVolClaimExists(name, expectedPhase string, namespace string) error {
return structured.PersistentVolClaimExists(kc.KubeInterface, name, expectedPhase, namespace)

Check warning on line 324 in pkg/kube/kube.go

View check run for this annotation

Codecov / codecov/patch

pkg/kube/kube.go#L323-L324

Added lines #L323 - L324 were not covered by tests
}

func (kc *ClientSet) ClusterRbacIsFound(resourceType, name string) error {
return structured.ClusterRbacIsFound(kc.KubeInterface, resourceType, name)
}
Expand Down
14 changes: 14 additions & 0 deletions pkg/kube/structured/structured.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,20 @@
return nil
}

func PersistentVolClaimExists(kubeClientset kubernetes.Interface, name, expectedPhase string, namespace string) error {
vol, err := util.RetryOnError(&util.DefaultRetry, util.IsRetriable, func() (interface{}, error) {
return GetPersistentVolumeClaim(kubeClientset, name, namespace)
})
if err != nil {
return err
}
phase := string(vol.(*corev1.PersistentVolumeClaim).Status.Phase)
if phase != expectedPhase {
return fmt.Errorf("persistentvolumeclaim had unexpected phase %v, expected phase %v", phase, expectedPhase)
}

Check warning on line 248 in pkg/kube/structured/structured.go

View check run for this annotation

Codecov / codecov/patch

pkg/kube/structured/structured.go#L247-L248

Added lines #L247 - L248 were not covered by tests
return nil
}
garomonegro marked this conversation as resolved.
Show resolved Hide resolved

func ValidatePrometheusVolumeClaimTemplatesName(kubeClientset kubernetes.Interface, statefulsetName, namespace, volumeClaimTemplatesName string) error {
// Prometheus StatefulSets deployed, then validate volumeClaimTemplate name.
// Validation required:
Expand Down
14 changes: 14 additions & 0 deletions pkg/kube/structured/structured_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,20 @@
return pvs.(*corev1.PersistentVolume), nil
}

func GetPersistentVolumeClaim(kubeClientset kubernetes.Interface, name string, namespace string) (*corev1.PersistentVolumeClaim, error) {
if err := common.ValidateClientset(kubeClientset); err != nil {
return nil, err
}

Check warning on line 102 in pkg/kube/structured/structured_helper.go

View check run for this annotation

Codecov / codecov/patch

pkg/kube/structured/structured_helper.go#L101-L102

Added lines #L101 - L102 were not covered by tests

pvc, err := util.RetryOnError(&util.DefaultRetry, util.IsRetriable, func() (interface{}, error) {
return kubeClientset.CoreV1().PersistentVolumeClaims(namespace).Get(context.Background(), name, metav1.GetOptions{})
})
if err != nil {
return nil, errors.Wrap(err, "failed to get persistentvolumeclaim")
}
return pvc.(*corev1.PersistentVolumeClaim), nil
}

func GetStatefulSetList(kubeClientset kubernetes.Interface, namespace string) (*appsv1.StatefulSetList, error) {
if err := common.ValidateClientset(kubeClientset); err != nil {
return nil, err
Expand Down
84 changes: 70 additions & 14 deletions pkg/kube/structured/structured_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,21 @@ import (
)

const (
configMapType = "configmap"
deploymentType = "deployment"
serviceType = "service"
hpaType = "horizontalpodautoscaler"
pdbType = "poddisruptionbudget"
saType = "serviceaccount"
clusterRoleType = "clusterrole"
clusterRoleBindingType = "clusterrolebinding"
nodeType = "node"
daemonSetType = "daemonset"
persistentVolumeType = "persistentvolume"
statefulSetType = "statefulset"
secretType = "secret"
ingressType = "ingress"
configMapType = "configmap"
deploymentType = "deployment"
serviceType = "service"
hpaType = "horizontalpodautoscaler"
pdbType = "poddisruptionbudget"
saType = "serviceaccount"
clusterRoleType = "clusterrole"
clusterRoleBindingType = "clusterrolebinding"
nodeType = "node"
daemonSetType = "daemonset"
persistentVolumeType = "persistentvolume"
persistentVolumeClaimType = "persistentVolumeClaim"
statefulSetType = "statefulset"
secretType = "secret"
ingressType = "ingress"
)

func TestNodesWithSelectorShouldBe(t *testing.T) {
Expand Down Expand Up @@ -480,6 +481,50 @@ func TestPersistentVolExists(t *testing.T) {
}
}

func TestPersistentVolClaimExists(t *testing.T) {
type args struct {
kubeClientset kubernetes.Interface
name string
namespace string
expectedPhase string
}
// expectedPhase: Available|Bound|Released|Failed|Pending
persistentvolumeClaimName := "persistentvolumeclaim1"
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "pvc found",
args: args{
kubeClientset: fake.NewSimpleClientset(getResource(t, persistentVolumeClaimType, persistentvolumeClaimName)),
name: persistentvolumeClaimName,
namespace: "",
expectedPhase: "Bound",
},
wantErr: false,
},
{
name: "pvc not found Test",
args: args{
kubeClientset: fake.NewSimpleClientset(getResource(t, persistentVolumeClaimType, "testabc")),
name: persistentvolumeClaimName,
namespace: "",
expectedPhase: "",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := PersistentVolClaimExists(tt.args.kubeClientset, tt.args.name, tt.args.expectedPhase, tt.args.namespace); (err != nil) != tt.wantErr {
t.Errorf("PersistentVolClaimExists() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestValidatePrometheusVolumeClaimTemplatesName(t *testing.T) {
type args struct {
kubeClientset kubernetes.Interface
Expand Down Expand Up @@ -833,6 +878,17 @@ func getResourceWithAll(t *testing.T, resourceType, name, namespace, label strin
Labels: labels,
},
}
case persistentVolumeClaimType:
return &corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: labels,
},
Status: corev1.PersistentVolumeClaimStatus{
Phase: corev1.ClaimBound,
},
}
case statefulSetType:
return &appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Expand Down
Loading