-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
972 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
package framework | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"github.com/onsi/ginkgo/v2" | ||
v1 "k8s.io/api/core/v1" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
clientset "k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/kubernetes/scheme" | ||
"k8s.io/client-go/tools/clientcmd" | ||
"k8s.io/client-go/util/homedir" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
var ( | ||
f Framework | ||
TestDriverPath string | ||
) | ||
|
||
type Framework struct { | ||
BaseName string | ||
Namespace *v1.Namespace | ||
ClientSet clientset.Interface | ||
DriverData *DriverDefinition | ||
} | ||
|
||
func init() { | ||
flag.StringVar(&TestDriverPath, "storage.testdriver", "", "The testdriver yaml file") | ||
} | ||
|
||
func NewDefaultFramework(baseName string) *Framework { | ||
return NewFramework(baseName, nil) | ||
} | ||
|
||
func NewFramework(baseName string, client clientset.Interface) *Framework { | ||
f := &Framework{ | ||
BaseName: baseName, | ||
ClientSet: client, | ||
} | ||
|
||
ginkgo.BeforeEach(f.BeforeEach) | ||
|
||
return f | ||
} | ||
|
||
func (f *Framework) BeforeEach(ctx context.Context) { | ||
ginkgo.DeferCleanup(f.AfterEach) | ||
|
||
driver, err := readTestDriverFile() | ||
if err != nil { | ||
ginkgo.Fail(err.Error()) | ||
} | ||
|
||
f.DriverData = driver | ||
|
||
var kubeconfig *string | ||
|
||
// Try to get kube config either from user home directory or absolute path. | ||
if home := homedir.HomeDir(); home != "" { | ||
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file") | ||
} else { | ||
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file") | ||
} | ||
|
||
// Override with KUBECONFIG env variable. | ||
kubeconfigEnv := os.Getenv("KUBECONFIG") | ||
if kubeconfigEnv != "" { | ||
*kubeconfig = kubeconfigEnv | ||
} | ||
|
||
ginkgo.By(fmt.Sprintf("Creating a kubernetes client, basename %s", f.BaseName)) | ||
|
||
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig) | ||
if err != nil { | ||
ginkgo.Fail(err.Error()) | ||
} | ||
|
||
f.ClientSet, err = clientset.NewForConfig(config) | ||
if err != nil { | ||
ginkgo.Fail(err.Error()) | ||
} | ||
|
||
ns, err := f.CreateNamespace(ctx) | ||
|
||
f.Namespace = ns | ||
} | ||
|
||
func (f *Framework) AfterEach(ctx context.Context) { | ||
defer func() { | ||
ginkgo.By(fmt.Sprintf("Destroying namespace: %s", f.Namespace.Name)) | ||
|
||
if err := f.ClientSet.CoreV1().Namespaces().Delete(ctx, f.Namespace.Name, metav1.DeleteOptions{}); err != nil { | ||
if !apierrors.IsNotFound(err) { | ||
ginkgo.By(fmt.Sprintf("Could not delete namespace: %s", f.Namespace.Name)) | ||
} else { | ||
ginkgo.By(fmt.Sprintf("Namespace was already deleted: %s", f.Namespace.Name)) | ||
} | ||
} | ||
|
||
f.Namespace = nil | ||
f.ClientSet = nil | ||
}() | ||
} | ||
|
||
func (f *Framework) CreateNamespace(ctx context.Context) (*v1.Namespace, error) { | ||
nsSpec := &v1.Namespace{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: f.BaseName, | ||
}, | ||
Status: v1.NamespaceStatus{}, | ||
} | ||
|
||
ginkgo.By(fmt.Sprintf("Create namespace: %s", f.BaseName)) | ||
|
||
ns, err := f.ClientSet.CoreV1().Namespaces().Create(ctx, nsSpec, metav1.CreateOptions{}) | ||
|
||
if err != nil { | ||
ginkgo.By(fmt.Sprintf("Failed to create namespace: %v", err)) | ||
|
||
return nil, err | ||
} | ||
|
||
return ns, nil | ||
} | ||
|
||
func readTestDriverFile() (*DriverDefinition, error) { | ||
data, err := os.ReadFile(TestDriverPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
driverData := &DriverDefinition{} | ||
|
||
if err := runtime.DecodeInto(scheme.Codecs.UniversalDecoder(), data, driverData); err != nil { | ||
return nil, err | ||
} | ||
|
||
if driverData.DriverInfo.Name == "" { | ||
return nil, fmt.Errorf("DriverInfo.Name is not set in file: %s", TestDriverPath) | ||
} | ||
|
||
return driverData, nil | ||
} | ||
|
||
type DriverDefinition struct { | ||
DriverInfo struct { | ||
Name string | ||
SupportedSizeRange struct { | ||
Max string | ||
Min string | ||
} | ||
} | ||
} | ||
|
||
func (d *DriverDefinition) GetObjectKind() schema.ObjectKind { | ||
return nil | ||
} | ||
|
||
func (d *DriverDefinition) DeepCopyObject() runtime.Object { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
module ginkgo | ||
|
||
go 1.20 | ||
|
||
require ( | ||
github.com/onsi/ginkgo/v2 v2.11.0 | ||
github.com/onsi/gomega v1.27.10 | ||
k8s.io/apimachinery v0.27.4 | ||
k8s.io/client-go v0.27.4 | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/emicklei/go-restful/v3 v3.9.0 // indirect | ||
github.com/go-logr/logr v1.2.4 // indirect | ||
github.com/go-openapi/jsonpointer v0.19.6 // indirect | ||
github.com/go-openapi/jsonreference v0.20.1 // indirect | ||
github.com/go-openapi/swag v0.22.3 // indirect | ||
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect | ||
github.com/gogo/protobuf v1.3.2 // indirect | ||
github.com/golang/protobuf v1.5.3 // indirect | ||
github.com/google/gnostic v0.5.7-v3refs // indirect | ||
github.com/google/go-cmp v0.5.9 // indirect | ||
github.com/google/gofuzz v1.1.0 // indirect | ||
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect | ||
github.com/google/uuid v1.3.0 // indirect | ||
github.com/imdario/mergo v0.3.6 // indirect | ||
github.com/josharian/intern v1.0.0 // indirect | ||
github.com/json-iterator/go v1.1.12 // indirect | ||
github.com/mailru/easyjson v0.7.7 // indirect | ||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect | ||
github.com/modern-go/reflect2 v1.0.2 // indirect | ||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect | ||
github.com/spf13/pflag v1.0.5 // indirect | ||
golang.org/x/net v0.12.0 // indirect | ||
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b // indirect | ||
golang.org/x/sys v0.10.0 // indirect | ||
golang.org/x/term v0.10.0 // indirect | ||
golang.org/x/text v0.11.0 // indirect | ||
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect | ||
golang.org/x/tools v0.9.3 // indirect | ||
google.golang.org/appengine v1.6.7 // indirect | ||
google.golang.org/protobuf v1.28.1 // indirect | ||
gopkg.in/inf.v0 v0.9.1 // indirect | ||
gopkg.in/yaml.v2 v2.4.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
k8s.io/api v0.27.4 // indirect | ||
k8s.io/klog/v2 v2.90.1 // indirect | ||
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f // indirect | ||
k8s.io/utils v0.0.0-20230209194617-a36077c30491 // indirect | ||
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect | ||
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect | ||
sigs.k8s.io/yaml v1.3.0 // indirect | ||
) |
Oops, something went wrong.