mirror of
https://github.com/kyverno/kyverno.git
synced 2024-12-15 17:51:20 +00:00
re factor
This commit is contained in:
parent
b3bf9b02d2
commit
640ffd7258
2 changed files with 50 additions and 25 deletions
|
@ -7,6 +7,7 @@ import (
|
|||
|
||||
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
)
|
||||
|
||||
// GetResource
|
||||
|
@ -20,18 +21,19 @@ import (
|
|||
// - objects to initialize the client
|
||||
|
||||
type fixture struct {
|
||||
t *testing.T
|
||||
regresources map[string]string
|
||||
objects []runtime.Object
|
||||
client *Client
|
||||
t *testing.T
|
||||
objects []runtime.Object
|
||||
client *Client
|
||||
}
|
||||
|
||||
func newFixture(t *testing.T) *fixture {
|
||||
regresource := map[string]string{"group/version": "thekinds",
|
||||
"group2/version": "thekinds",
|
||||
"v1": "namespaces",
|
||||
"apps/v1": "deployments"}
|
||||
|
||||
// init groupversion
|
||||
regResource := []schema.GroupVersionResource{
|
||||
schema.GroupVersionResource{Group: "group", Version: "version", Resource: "thekinds"},
|
||||
schema.GroupVersionResource{Group: "group2", Version: "version", Resource: "thekinds"},
|
||||
schema.GroupVersionResource{Group: "", Version: "v1", Resource: "namespaces"},
|
||||
schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "deployments"},
|
||||
}
|
||||
objects := []runtime.Object{newUnstructured("group/version", "TheKind", "ns-foo", "name-foo"),
|
||||
newUnstructured("group2/version", "TheKind", "ns-foo", "name2-foo"),
|
||||
newUnstructured("group/version", "TheKind", "ns-foo", "name-bar"),
|
||||
|
@ -47,20 +49,19 @@ func newFixture(t *testing.T) *fixture {
|
|||
}
|
||||
|
||||
// set discovery Client
|
||||
client.SetDiscovery(NewFakeDiscoveryClient(regresource))
|
||||
client.SetDiscovery(NewFakeDiscoveryClient(regResource))
|
||||
|
||||
f := fixture{
|
||||
t: t,
|
||||
regresources: regresource,
|
||||
objects: objects,
|
||||
client: client,
|
||||
t: t,
|
||||
objects: objects,
|
||||
client: client,
|
||||
}
|
||||
return &f
|
||||
|
||||
}
|
||||
|
||||
func TestCRUDResource(t *testing.T) {
|
||||
t.Skip("Under Development. Reason: delay in generation of resources, so test fails at times")
|
||||
// t.Skip("Under Development. Reason: delay in generation of resources, so test fails at times")
|
||||
f := newFixture(t)
|
||||
// Get Resource
|
||||
_, err := f.client.GetResource("thekinds", "ns-foo", "name-foo")
|
||||
|
@ -118,7 +119,7 @@ func TestCSRInterface(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestGenerateResource(t *testing.T) {
|
||||
t.Skip("Under Development. Reason: delay in generation of resources, so test fails at times")
|
||||
// t.Skip("Under Development. Reason: delay in generation of resources, so test fails at times")
|
||||
|
||||
f := newFixture(t)
|
||||
//GenerateResource -> copy From
|
||||
|
|
|
@ -37,16 +37,21 @@ func NewMockClient(scheme *runtime.Scheme, objects ...runtime.Object) (*Client,
|
|||
}
|
||||
|
||||
// NewFakeDiscoveryClient returns a fakediscovery client
|
||||
func NewFakeDiscoveryClient(regResources map[string]string) *fakeDiscoveryClient {
|
||||
registeredResources := make([]schema.GroupVersionResource, len(regResources))
|
||||
for groupVersion, resource := range regResources {
|
||||
gv, err := schema.ParseGroupVersion(groupVersion)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
registeredResources = append(registeredResources, gv.WithResource(resource))
|
||||
func NewFakeDiscoveryClient(registeredResouces []schema.GroupVersionResource) *fakeDiscoveryClient {
|
||||
// Load some-preregistd resources
|
||||
res := []schema.GroupVersionResource{
|
||||
schema.GroupVersionResource{Version: "v1", Resource: "configmaps"},
|
||||
schema.GroupVersionResource{Version: "v1", Resource: "endpoints"},
|
||||
schema.GroupVersionResource{Version: "v1", Resource: "namespaces"},
|
||||
schema.GroupVersionResource{Version: "v1", Resource: "resourcequotas"},
|
||||
schema.GroupVersionResource{Version: "v1", Resource: "secrets"},
|
||||
schema.GroupVersionResource{Version: "v1", Resource: "serviceaccounts"},
|
||||
schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "daemonsets"},
|
||||
schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "deployments"},
|
||||
schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "statefulsets"},
|
||||
}
|
||||
return &fakeDiscoveryClient{registeredResouces: registeredResources}
|
||||
registeredResouces = append(registeredResouces, res...)
|
||||
return &fakeDiscoveryClient{registeredResouces: registeredResouces}
|
||||
}
|
||||
|
||||
type fakeDiscoveryClient struct {
|
||||
|
@ -85,3 +90,22 @@ func newUnstructuredWithSpec(apiVersion, kind, namespace, name string, spec map[
|
|||
u.Object["spec"] = spec
|
||||
return u
|
||||
}
|
||||
|
||||
func retry(attempts int, sleep time.Duration, fn func() error) error {
|
||||
if err := fn(); err != nil {
|
||||
if s, ok := err.(stop); ok {
|
||||
return s.error
|
||||
}
|
||||
if attempts--; attempts > 0 {
|
||||
time.Sleep(sleep)
|
||||
return retry(attempts, 2*sleep, fn)
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Custom error
|
||||
type stop struct {
|
||||
error
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue