2020-08-06 10:46:10 +05:30
|
|
|
package generate
|
|
|
|
|
2022-06-03 21:08:27 +02:00
|
|
|
import (
|
|
|
|
"github.com/kyverno/kyverno/test/e2e"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Cluster Policy GVR
|
|
|
|
clPolGVR = e2e.GetGVR("kyverno.io", "v1", "clusterpolicies")
|
|
|
|
|
|
|
|
// Namespace GVR
|
|
|
|
nsGVR = e2e.GetGVR("", "v1", "namespaces")
|
|
|
|
|
|
|
|
// ClusterRole GVR
|
|
|
|
crGVR = e2e.GetGVR("rbac.authorization.k8s.io", "v1", "clusterroles")
|
|
|
|
|
|
|
|
// ClusterRoleBinding GVR
|
|
|
|
crbGVR = e2e.GetGVR("rbac.authorization.k8s.io", "v1", "clusterrolebindings")
|
|
|
|
|
|
|
|
// Role GVR
|
|
|
|
rGVR = e2e.GetGVR("rbac.authorization.k8s.io", "v1", "roles")
|
|
|
|
|
|
|
|
// RoleBinding GVR
|
|
|
|
rbGVR = e2e.GetGVR("rbac.authorization.k8s.io", "v1", "rolebindings")
|
|
|
|
|
|
|
|
// ConfigMap GVR
|
|
|
|
cmGVR = e2e.GetGVR("", "v1", "configmaps")
|
|
|
|
|
|
|
|
// NetworkPolicy GVR
|
|
|
|
npGVR = e2e.GetGVR("networking.k8s.io", "v1", "networkpolicies")
|
|
|
|
|
|
|
|
// ClusterPolicy Namespace
|
|
|
|
clPolNS = ""
|
|
|
|
|
|
|
|
// NetworkPolicy Namespace
|
|
|
|
npPolNS = ""
|
|
|
|
)
|
|
|
|
|
|
|
|
type resource struct {
|
|
|
|
gvr schema.GroupVersionResource
|
|
|
|
ns string
|
|
|
|
raw []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func clusteredResource(gvr schema.GroupVersionResource, raw []byte) resource {
|
|
|
|
return resource{gvr, "", raw}
|
|
|
|
}
|
|
|
|
|
|
|
|
func namespacedResource(gvr schema.GroupVersionResource, ns string, raw []byte) resource {
|
|
|
|
return resource{gvr, ns, raw}
|
|
|
|
}
|
|
|
|
|
|
|
|
type expectedResource struct {
|
|
|
|
gvr schema.GroupVersionResource
|
|
|
|
ns string
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
2020-11-17 13:07:30 -08:00
|
|
|
// RoleTests is E2E Test Config for Role and RoleBinding
|
2020-08-06 10:46:10 +05:30
|
|
|
// TODO:- Clone for Role and RoleBinding
|
|
|
|
var RoleTests = []struct {
|
2022-05-17 08:19:03 +02:00
|
|
|
// TestName - Name of the Test
|
2020-08-06 10:46:10 +05:30
|
|
|
TestName string
|
2022-06-03 21:08:27 +02:00
|
|
|
// ClusterPolicy - ClusterPolicy yaml file
|
|
|
|
ClusterPolicy resource
|
|
|
|
// SourceResources - Source resources yaml files
|
|
|
|
SourceResources []resource
|
|
|
|
// TriggerResource - Trigger resource yaml files
|
|
|
|
TriggerResource resource
|
|
|
|
// ExpectedResources - Expected resources to pass the test
|
|
|
|
ExpectedResources []expectedResource
|
2020-08-06 10:46:10 +05:30
|
|
|
}{
|
|
|
|
{
|
2022-06-03 21:08:27 +02:00
|
|
|
TestName: "test-role-rolebinding-without-clone",
|
|
|
|
ClusterPolicy: clusteredResource(clPolGVR, roleRoleBindingYamlWithSync),
|
|
|
|
TriggerResource: clusteredResource(nsGVR, namespaceYaml),
|
|
|
|
ExpectedResources: []expectedResource{
|
|
|
|
{rGVR, "test", "ns-role"},
|
|
|
|
{rbGVR, "test", "ns-role-binding"},
|
|
|
|
},
|
2020-08-06 10:46:10 +05:30
|
|
|
},
|
|
|
|
{
|
2022-06-03 21:08:27 +02:00
|
|
|
TestName: "test-role-rolebinding-withsync-without-clone",
|
|
|
|
ClusterPolicy: clusteredResource(clPolGVR, roleRoleBindingYamlWithSync),
|
|
|
|
TriggerResource: clusteredResource(nsGVR, namespaceYaml),
|
|
|
|
ExpectedResources: []expectedResource{
|
|
|
|
{rGVR, "test", "ns-role"},
|
|
|
|
{rbGVR, "test", "ns-role-binding"},
|
|
|
|
},
|
2020-08-06 10:46:10 +05:30
|
|
|
},
|
|
|
|
{
|
2022-06-03 21:08:27 +02:00
|
|
|
TestName: "test-role-rolebinding-with-clone",
|
|
|
|
ClusterPolicy: clusteredResource(clPolGVR, roleRoleBindingYamlWithClone),
|
|
|
|
SourceResources: []resource{
|
|
|
|
namespacedResource(rGVR, "default", sourceRoleYaml),
|
|
|
|
namespacedResource(rbGVR, "default", sourceRoleBindingYaml),
|
|
|
|
},
|
|
|
|
TriggerResource: clusteredResource(nsGVR, namespaceYaml),
|
|
|
|
ExpectedResources: []expectedResource{
|
|
|
|
{rGVR, "test", "ns-role"},
|
|
|
|
{rbGVR, "test", "ns-role-binding"},
|
|
|
|
},
|
2020-08-06 10:46:10 +05:30
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-11-17 13:07:30 -08:00
|
|
|
// ClusterRoleTests - E2E Test Config for ClusterRole and ClusterRoleBinding
|
2020-08-06 10:46:10 +05:30
|
|
|
var ClusterRoleTests = []struct {
|
2022-05-17 08:19:03 +02:00
|
|
|
// TestName - Name of the Test
|
2020-08-06 10:46:10 +05:30
|
|
|
TestName string
|
2022-06-03 21:08:27 +02:00
|
|
|
// ClusterPolicy - ClusterPolicy yaml file
|
|
|
|
ClusterPolicy resource
|
|
|
|
// SourceResources - Source resources yaml files
|
|
|
|
SourceResources []resource
|
|
|
|
// TriggerResource - Trigger resource yaml files
|
|
|
|
TriggerResource resource
|
|
|
|
// ExpectedResources - Expected resources to pass the test
|
|
|
|
ExpectedResources []expectedResource
|
2020-08-06 10:46:10 +05:30
|
|
|
}{
|
|
|
|
{
|
2022-06-03 21:08:27 +02:00
|
|
|
TestName: "test-clusterrole-clusterrolebinding-without-clone",
|
|
|
|
ClusterPolicy: clusteredResource(clPolGVR, genClusterRoleYamlWithSync),
|
|
|
|
TriggerResource: clusteredResource(nsGVR, namespaceYaml),
|
|
|
|
ExpectedResources: []expectedResource{
|
|
|
|
{crGVR, "", "ns-cluster-role"},
|
|
|
|
{crbGVR, "", "ns-cluster-role-binding"},
|
|
|
|
},
|
2020-08-06 10:46:10 +05:30
|
|
|
},
|
|
|
|
{
|
2022-06-03 21:08:27 +02:00
|
|
|
TestName: "test-clusterrole-clusterrolebinding-with-sync-without-clone",
|
|
|
|
ClusterPolicy: clusteredResource(clPolGVR, genClusterRoleYamlWithSync),
|
|
|
|
TriggerResource: clusteredResource(nsGVR, namespaceYaml),
|
|
|
|
ExpectedResources: []expectedResource{
|
|
|
|
{crGVR, "", "ns-cluster-role"},
|
|
|
|
{crbGVR, "", "ns-cluster-role-binding"},
|
|
|
|
},
|
2020-08-06 10:46:10 +05:30
|
|
|
},
|
|
|
|
{
|
2022-06-03 21:08:27 +02:00
|
|
|
TestName: "test-clusterrole-clusterrolebinding-with-sync-with-clone",
|
|
|
|
ClusterPolicy: clusteredResource(clPolGVR, clusterRoleRoleBindingYamlWithClone),
|
|
|
|
SourceResources: []resource{
|
|
|
|
clusteredResource(crGVR, baseClusterRoleData),
|
|
|
|
clusteredResource(crbGVR, baseClusterRoleBindingData),
|
|
|
|
},
|
|
|
|
TriggerResource: clusteredResource(nsGVR, namespaceYaml),
|
|
|
|
ExpectedResources: []expectedResource{
|
|
|
|
{crGVR, "", "cloned-cluster-role"},
|
|
|
|
{crbGVR, "", "cloned-cluster-role-binding"},
|
|
|
|
},
|
2020-08-06 10:46:10 +05:30
|
|
|
},
|
|
|
|
}
|
2021-06-03 00:18:28 +05:30
|
|
|
|
|
|
|
// NetworkPolicyGenerateTests - E2E Test Config for NetworkPolicyGenerateTests
|
|
|
|
var NetworkPolicyGenerateTests = []struct {
|
2022-05-17 08:19:03 +02:00
|
|
|
// TestName - Name of the Test
|
2021-06-03 00:18:28 +05:30
|
|
|
TestName string
|
2022-06-03 21:08:27 +02:00
|
|
|
// ClusterPolicy - ClusterPolicy yaml file
|
|
|
|
ClusterPolicy resource
|
|
|
|
// SourceResources - Source resources yaml files
|
|
|
|
SourceResources []resource
|
|
|
|
// TriggerResource - Trigger resource yaml files
|
|
|
|
TriggerResource resource
|
|
|
|
// ExpectedResources - Expected resources to pass the test
|
|
|
|
ExpectedResources []expectedResource
|
2021-06-03 00:18:28 +05:30
|
|
|
}{
|
|
|
|
{
|
2022-06-03 21:08:27 +02:00
|
|
|
TestName: "test-generate-policy-for-namespace-with-label",
|
|
|
|
ClusterPolicy: clusteredResource(clPolGVR, genNetworkPolicyYaml),
|
|
|
|
TriggerResource: clusteredResource(nsGVR, namespaceWithLabelYaml),
|
|
|
|
ExpectedResources: []expectedResource{
|
|
|
|
{npGVR, "test", "allow-dns"},
|
|
|
|
},
|
2021-06-03 00:18:28 +05:30
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// NetworkPolicyGenerateTests - E2E Test Config for NetworkPolicyGenerateTests
|
|
|
|
var GenerateNetworkPolicyOnNamespaceWithoutLabelTests = []struct {
|
2022-05-17 08:19:03 +02:00
|
|
|
// TestName - Name of the Test
|
2021-06-03 00:18:28 +05:30
|
|
|
TestName string
|
|
|
|
// NetworkPolicyName - Name of the NetworkPolicy to be Created
|
|
|
|
NetworkPolicyName string
|
|
|
|
// GeneratePolicyName - Name of the Policy to be Created/Updated
|
|
|
|
GeneratePolicyName string
|
|
|
|
// ResourceNamespace - Namespace for which Resources are Created
|
|
|
|
ResourceNamespace string
|
|
|
|
// Clone - Set Clone Value
|
|
|
|
Clone bool
|
|
|
|
// CloneClusterRoleName
|
|
|
|
ClonerClusterRoleName string
|
|
|
|
// CloneClusterRoleBindingName
|
|
|
|
ClonerClusterRoleBindingName string
|
|
|
|
// CloneSourceRoleData - Source ClusterRole Name from which ClusterRole is Cloned
|
|
|
|
CloneSourceClusterRoleData []byte
|
|
|
|
// CloneSourceRoleBindingData - Source ClusterRoleBinding Name from which ClusterRoleBinding is Cloned
|
|
|
|
CloneSourceClusterRoleBindingData []byte
|
|
|
|
// CloneNamespace - Namespace where Roles are Cloned
|
|
|
|
CloneNamespace string
|
|
|
|
// Sync - Set Synchronize
|
|
|
|
Sync bool
|
|
|
|
// Data - The Yaml file of the ClusterPolicy of the ClusterRole and ClusterRoleBinding - ([]byte{})
|
|
|
|
Data []byte
|
|
|
|
// Data - The Yaml file of the ClusterPolicy of the ClusterRole and ClusterRoleBinding - ([]byte{})
|
|
|
|
UpdateData []byte
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
TestName: "test-generate-policy-for-namespace-label-actions",
|
|
|
|
ResourceNamespace: "test",
|
|
|
|
NetworkPolicyName: "allow-dns",
|
|
|
|
GeneratePolicyName: "add-networkpolicy",
|
|
|
|
Clone: false,
|
|
|
|
Sync: true,
|
|
|
|
Data: genNetworkPolicyYaml,
|
|
|
|
UpdateData: updatGenNetworkPolicyYaml,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// NetworkPolicyGenerateTests - E2E Test Config for NetworkPolicyGenerateTests
|
|
|
|
var GenerateSynchronizeFlagTests = []struct {
|
2022-05-17 08:19:03 +02:00
|
|
|
// TestName - Name of the Test
|
2021-06-03 00:18:28 +05:30
|
|
|
TestName string
|
|
|
|
// NetworkPolicyName - Name of the NetworkPolicy to be Created
|
|
|
|
NetworkPolicyName string
|
|
|
|
// GeneratePolicyName - Name of the Policy to be Created/Updated
|
|
|
|
GeneratePolicyName string
|
|
|
|
// ResourceNamespace - Namespace for which Resources are Created
|
|
|
|
ResourceNamespace string
|
|
|
|
// Clone - Set Clone Value
|
|
|
|
Clone bool
|
|
|
|
// CloneClusterRoleName
|
|
|
|
ClonerClusterRoleName string
|
|
|
|
// CloneClusterRoleBindingName
|
|
|
|
ClonerClusterRoleBindingName string
|
|
|
|
// CloneSourceRoleData - Source ClusterRole Name from which ClusterRole is Cloned
|
|
|
|
CloneSourceClusterRoleData []byte
|
|
|
|
// CloneSourceRoleBindingData - Source ClusterRoleBinding Name from which ClusterRoleBinding is Cloned
|
|
|
|
CloneSourceClusterRoleBindingData []byte
|
|
|
|
// CloneNamespace - Namespace where Roles are Cloned
|
|
|
|
CloneNamespace string
|
|
|
|
// Sync - Set Synchronize
|
|
|
|
Sync bool
|
|
|
|
// Data - The Yaml file of the ClusterPolicy of the ClusterRole and ClusterRoleBinding - ([]byte{})
|
|
|
|
Data []byte
|
|
|
|
// Data - The Yaml file of the ClusterPolicy of the ClusterRole and ClusterRoleBinding - ([]byte{})
|
|
|
|
UpdateData []byte
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
TestName: "test-generate-policy-for-namespace-with-label",
|
|
|
|
NetworkPolicyName: "allow-dns",
|
|
|
|
GeneratePolicyName: "add-networkpolicy",
|
|
|
|
ResourceNamespace: "test",
|
|
|
|
Clone: false,
|
|
|
|
Sync: true,
|
|
|
|
Data: genNetworkPolicyYaml,
|
|
|
|
UpdateData: updateSynchronizeInGeneratePolicyYaml,
|
|
|
|
},
|
|
|
|
}
|
2021-06-08 01:06:00 +05:30
|
|
|
|
|
|
|
// ClusterRoleTests - E2E Test Config for ClusterRole and ClusterRoleBinding
|
|
|
|
var SourceResourceUpdateReplicationTests = []struct {
|
2022-05-17 08:19:03 +02:00
|
|
|
// TestName - Name of the Test
|
2021-06-08 01:06:00 +05:30
|
|
|
TestName string
|
|
|
|
// ClusterRoleName - Name of the ClusterRole to be Created
|
|
|
|
ResourceNamespace string
|
|
|
|
// Clone - Set Clone Value
|
|
|
|
Clone bool
|
|
|
|
// CloneNamespace - Namespace where Roles are Cloned
|
|
|
|
CloneNamespace string
|
|
|
|
// Sync - Set Synchronize
|
|
|
|
Sync bool
|
|
|
|
// Data - The Yaml file of the ClusterPolicy - ([]byte{})
|
|
|
|
Data []byte
|
|
|
|
// ConfigMapName - name of configMap
|
|
|
|
ConfigMapName string
|
|
|
|
// CloneSourceConfigMapData - Source ConfigMap Yaml
|
|
|
|
CloneSourceConfigMapData []byte
|
2021-06-21 20:12:14 +05:30
|
|
|
// PolicyName - Name of the Policy
|
|
|
|
PolicyName string
|
2021-06-08 01:06:00 +05:30
|
|
|
}{
|
|
|
|
{
|
|
|
|
TestName: "test-clone-source-resource-update-replication",
|
|
|
|
ResourceNamespace: "test",
|
|
|
|
Clone: true,
|
|
|
|
Sync: true,
|
|
|
|
Data: genCloneConfigMapPolicyYaml,
|
|
|
|
ConfigMapName: "game-demo",
|
|
|
|
CloneNamespace: "default",
|
|
|
|
CloneSourceConfigMapData: cloneSourceResource,
|
2021-06-21 20:12:14 +05:30
|
|
|
PolicyName: "generate-policy",
|
2021-06-08 01:06:00 +05:30
|
|
|
},
|
|
|
|
}
|
2021-07-01 10:09:43 +05:30
|
|
|
|
|
|
|
var GeneratePolicyDeletionforCloneTests = []struct {
|
2022-05-17 08:19:03 +02:00
|
|
|
// TestName - Name of the Test
|
2021-07-01 10:09:43 +05:30
|
|
|
TestName string
|
|
|
|
// ClusterRoleName - Name of the ClusterRole to be Created
|
|
|
|
ResourceNamespace string
|
|
|
|
// Clone - Set Clone Value
|
|
|
|
Clone bool
|
|
|
|
// CloneNamespace - Namespace where Roles are Cloned
|
|
|
|
CloneNamespace string
|
|
|
|
// Sync - Set Synchronize
|
|
|
|
Sync bool
|
|
|
|
// Data - The Yaml file of the ClusterPolicy - ([]byte{})
|
|
|
|
Data []byte
|
|
|
|
// ConfigMapName - name of configMap
|
|
|
|
ConfigMapName string
|
|
|
|
// CloneSourceConfigMapData - Source ConfigMap Yaml
|
|
|
|
CloneSourceConfigMapData []byte
|
|
|
|
// PolicyName - Name of the Policy
|
|
|
|
PolicyName string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
TestName: "test-clone-source-resource-update-replication",
|
|
|
|
ResourceNamespace: "test",
|
|
|
|
Clone: true,
|
|
|
|
Sync: true,
|
|
|
|
Data: genCloneConfigMapPolicyYaml,
|
|
|
|
ConfigMapName: "game-demo",
|
|
|
|
CloneNamespace: "default",
|
|
|
|
CloneSourceConfigMapData: cloneSourceResource,
|
|
|
|
PolicyName: "generate-policy",
|
|
|
|
},
|
|
|
|
}
|