Skip to content

Commit

Permalink
tests: add integration test for pvc config
Browse files Browse the repository at this point in the history
Signed-off-by: Niladri Halder <[email protected]>
  • Loading branch information
niladrih committed Jul 1, 2024
1 parent 88bc07a commit 0268e39
Show file tree
Hide file tree
Showing 5 changed files with 457 additions and 9 deletions.
18 changes: 16 additions & 2 deletions pkg/kubernetes/api/apps/v1/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ limitations under the License.
package v1alpha1

import (
templatespec "github.com/openebs/dynamic-localpv-provisioner/pkg/kubernetes/api/core/v1/podtemplatespec"
stringer "github.com/openebs/maya/pkg/apis/stringer/v1alpha1"
errors "github.com/pkg/errors"
"github.com/pkg/errors"
appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

templatespec "github.com/openebs/dynamic-localpv-provisioner/pkg/kubernetes/api/core/v1/podtemplatespec"
)

// Predicate abstracts conditional logic w.r.t the deployment instance
Expand Down Expand Up @@ -96,6 +97,19 @@ func (b *Builder) WithName(name string) *Builder {
return b
}

// WithGenerateName sets the Name field of deployment with a random value with the provided value as a prefix.
func (b *Builder) WithGenerateName(prefix string) *Builder {
if len(prefix) == 0 {
b.errors = append(
b.errors,
errors.New("failed to build deployment: missing prefix for generateName"),
)
return b
}
b.deployment.object.GenerateName = prefix + "-"
return b
}

// WithNamespace sets the Namespace field of deployment with provided value.
func (b *Builder) WithNamespace(namespace string) *Builder {
if len(namespace) == 0 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/kubernetes/api/core/v1/persistentvolumeclaim/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package persistentvolumeclaim

import (
errors "github.com/pkg/errors"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
)
Expand Down Expand Up @@ -72,7 +72,7 @@ func (b *Builder) WithGenerateName(name string) *Builder {
return b
}

b.pvc.object.GenerateName = name
b.pvc.object.GenerateName = name + "-"
return b
}

Expand Down
18 changes: 18 additions & 0 deletions pkg/kubernetes/api/storage/v1/storageclass/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,24 @@ func WithLabels(labels map[string]string) StorageClassOption {
}
}

func WithAnnotations(annotations map[string]string) StorageClassOption {
return func(s *storagev1.StorageClass) error {
if len(annotations) == 0 {
return errors.New("Failed to set Annotations. " +
"Input is invalid.")
}

if s.ObjectMeta.Annotations == nil {
s.ObjectMeta.Annotations = map[string]string{}
}
for key, value := range annotations {
s.ObjectMeta.Annotations[key] = value
}

return nil
}
}

func WithParameters(parameters map[string]string) StorageClassOption {
return func(s *storagev1.StorageClass) error {
if len(parameters) == 0 {
Expand Down
27 changes: 22 additions & 5 deletions tests/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"bytes"
"context"
"fmt"

//"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -50,15 +49,15 @@ import (
"k8s.io/client-go/tools/remotecommand"

deploy "github.com/openebs/dynamic-localpv-provisioner/pkg/kubernetes/api/apps/v1/deployment"
container "github.com/openebs/dynamic-localpv-provisioner/pkg/kubernetes/api/core/v1/container"
event "github.com/openebs/dynamic-localpv-provisioner/pkg/kubernetes/api/core/v1/event"
"github.com/openebs/dynamic-localpv-provisioner/pkg/kubernetes/api/core/v1/container"
"github.com/openebs/dynamic-localpv-provisioner/pkg/kubernetes/api/core/v1/event"
pv "github.com/openebs/dynamic-localpv-provisioner/pkg/kubernetes/api/core/v1/persistentvolume"
pvc "github.com/openebs/dynamic-localpv-provisioner/pkg/kubernetes/api/core/v1/persistentvolumeclaim"
pod "github.com/openebs/dynamic-localpv-provisioner/pkg/kubernetes/api/core/v1/pod"
"github.com/openebs/dynamic-localpv-provisioner/pkg/kubernetes/api/core/v1/pod"
pts "github.com/openebs/dynamic-localpv-provisioner/pkg/kubernetes/api/core/v1/podtemplatespec"
k8svolume "github.com/openebs/dynamic-localpv-provisioner/pkg/kubernetes/api/core/v1/volume"
sc "github.com/openebs/dynamic-localpv-provisioner/pkg/kubernetes/api/storage/v1/storageclass"
ndmconfig "github.com/openebs/dynamic-localpv-provisioner/pkg/kubernetes/ndmconfig"
"github.com/openebs/dynamic-localpv-provisioner/pkg/kubernetes/ndmconfig"
)

const (
Expand Down Expand Up @@ -476,6 +475,24 @@ func (ops *Operations) GetPVNameFromPVCName(namespace, pvcName string) string {
return p.Spec.VolumeName
}

// GetNodeAffinityLabelKeysFromPv returns the label keys for NodeSelector MatchExpressions in a PV.
func (ops *Operations) GetNodeAffinityLabelKeysFromPv(pvName string) ([]string, error) {
pv, err := ops.PVClient.Get(context.TODO(), pvName, metav1.GetOptions{})
if err != nil {
return nil, err
}

var nodeAffinityLabelKeys []string

for _, selectorTerm := range pv.Spec.NodeAffinity.Required.NodeSelectorTerms {
for _, matchExpression := range selectorTerm.MatchExpressions {
nodeAffinityLabelKeys = append(nodeAffinityLabelKeys, matchExpression.Key)
}
}

return nodeAffinityLabelKeys, nil
}

// isNotFound returns true if the original
// cause of error was due to castemplate's
// not found error or kubernetes not found
Expand Down
Loading

0 comments on commit 0268e39

Please sign in to comment.