mirror of
https://github.com/kyverno/kyverno.git
synced 2024-12-15 17:51:20 +00:00
838d02c475
* - support wildcards for namespaces * do not annotate resource, unless policy is an autogen policy * close HTTP body * improve messages * remove policy store Policy store was not fully implemented and simply provided a way to list all polices and get a policy by name, which can be done via standard client-go interfaces. We need to revisit and design a better PolicyStore that provides fast lookups for matching policies based on names, namespaces, etc. * handle wildcard namespaces in background processing * fix unit tests 1) remove platform dependent path usage 2) remove policy store * add test case for mutate with wildcard namespaces
76 lines
1.8 KiB
Go
76 lines
1.8 KiB
Go
package policystatus
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"k8s.io/apimachinery/pkg/labels"
|
|
"testing"
|
|
"time"
|
|
|
|
v1 "github.com/nirmata/kyverno/pkg/api/kyverno/v1"
|
|
)
|
|
|
|
type dummyStore struct {
|
|
}
|
|
|
|
func (d dummyStore) Get(policyName string) (*v1.ClusterPolicy, error) {
|
|
return &v1.ClusterPolicy{}, nil
|
|
}
|
|
|
|
type dummyStatusUpdater struct {
|
|
}
|
|
|
|
func (d dummyStatusUpdater) UpdateStatus(status v1.PolicyStatus) v1.PolicyStatus {
|
|
status.RulesAppliedCount++
|
|
return status
|
|
}
|
|
|
|
func (d dummyStatusUpdater) PolicyName() string {
|
|
return "policy1"
|
|
}
|
|
|
|
|
|
type dummyLister struct {
|
|
}
|
|
|
|
func (dl dummyLister) List(selector labels.Selector) (ret []*v1.ClusterPolicy, err error) {
|
|
return nil, fmt.Errorf("not implemented")
|
|
}
|
|
|
|
func (dl dummyLister) Get(name string) (*v1.ClusterPolicy, error) {
|
|
return nil, fmt.Errorf("not implemented")
|
|
}
|
|
|
|
func (dl dummyLister) GetPolicyForPolicyViolation(pv *v1.ClusterPolicyViolation) ([]*v1.ClusterPolicy, error) {
|
|
return nil, fmt.Errorf("not implemented")
|
|
}
|
|
|
|
func (dl dummyLister) GetPolicyForNamespacedPolicyViolation(pv *v1.PolicyViolation) ([]*v1.ClusterPolicy, error) {
|
|
return nil, fmt.Errorf("not implemented")
|
|
}
|
|
|
|
func (dl dummyLister) ListResources(selector labels.Selector) (ret []*v1.ClusterPolicy, err error) {
|
|
return nil, fmt.Errorf("not implemented")
|
|
}
|
|
|
|
func TestKeyToMutex(t *testing.T) {
|
|
expectedCache := `{"policy1":{"rulesAppliedCount":100}}`
|
|
|
|
stopCh := make(chan struct{})
|
|
s := NewSync(nil, dummyLister{})
|
|
for i := 0; i < 100; i++ {
|
|
go s.updateStatusCache(stopCh)
|
|
}
|
|
|
|
for i := 0; i < 100; i++ {
|
|
go s.Listener.Send(dummyStatusUpdater{})
|
|
}
|
|
|
|
<-time.After(time.Second * 3)
|
|
stopCh <- struct{}{}
|
|
|
|
cacheRaw, _ := json.Marshal(s.cache.data)
|
|
if string(cacheRaw) != expectedCache {
|
|
t.Errorf("\nTestcase Failed\nGot:\n%v\nExpected:\n%v\n", string(cacheRaw), expectedCache)
|
|
}
|
|
}
|