2020-08-06 10:46:10 +05:30
|
|
|
package generate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2021-04-09 01:14:08 +02:00
|
|
|
|
2021-06-21 16:56:16 +05:30
|
|
|
commonE2E "github.com/kyverno/kyverno/test/e2e/common"
|
2021-04-09 01:14:08 +02:00
|
|
|
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
2020-08-06 10:46:10 +05:30
|
|
|
)
|
|
|
|
|
2022-07-05 17:34:09 +02:00
|
|
|
func runTestCases(t *testing.T, testCases ...testCase) {
|
2022-06-03 21:08:27 +02:00
|
|
|
setup(t)
|
2020-08-06 10:46:10 +05:30
|
|
|
|
2022-07-05 17:34:09 +02:00
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.TestName, func(t *testing.T) {
|
2022-06-03 21:08:27 +02:00
|
|
|
e2eClient := createClient()
|
2022-06-03 15:38:33 +02:00
|
|
|
|
|
|
|
t.Cleanup(func() {
|
2022-07-05 17:34:09 +02:00
|
|
|
deleteResources(e2eClient, test.ExpectedResources...)
|
2022-06-03 15:38:33 +02:00
|
|
|
})
|
|
|
|
|
2022-06-03 21:08:27 +02:00
|
|
|
// sanity check
|
2022-07-05 17:34:09 +02:00
|
|
|
By("Verifying expected resources do not exist yet in the cluster ...")
|
|
|
|
expectResourcesNotFound(e2eClient, test.ExpectedResources...)
|
2020-08-06 10:46:10 +05:30
|
|
|
|
2022-06-03 21:08:27 +02:00
|
|
|
// create source resources
|
2022-07-05 17:34:09 +02:00
|
|
|
if len(test.SourceResources) > 0 {
|
|
|
|
By("Creating source resources ...")
|
|
|
|
createResources(t, e2eClient, test.SourceResources...)
|
|
|
|
}
|
2020-08-06 10:46:10 +05:30
|
|
|
|
2022-06-03 21:08:27 +02:00
|
|
|
// create policy
|
2022-07-05 17:34:09 +02:00
|
|
|
By("Creating cluster policy ...")
|
|
|
|
policy := createResource(t, e2eClient, test.ClusterPolicy)
|
2022-06-03 21:08:27 +02:00
|
|
|
Expect(commonE2E.PolicyCreated(policy.GetName())).To(Succeed())
|
2020-08-06 10:46:10 +05:30
|
|
|
|
2022-06-03 21:08:27 +02:00
|
|
|
// create trigger
|
2022-07-05 17:34:09 +02:00
|
|
|
By("Creating trigger resource ...")
|
|
|
|
createResource(t, e2eClient, test.TriggerResource)
|
2021-06-03 00:18:28 +05:30
|
|
|
|
2022-07-05 17:34:09 +02:00
|
|
|
time.Sleep(time.Second * 5)
|
2021-06-03 00:18:28 +05:30
|
|
|
|
2022-07-05 17:34:09 +02:00
|
|
|
for _, step := range test.Steps {
|
|
|
|
Expect(step(e2eClient)).To(Succeed())
|
|
|
|
}
|
2021-07-09 18:01:46 -07:00
|
|
|
|
2022-06-03 21:08:27 +02:00
|
|
|
// verify expected resources
|
2022-07-05 17:34:09 +02:00
|
|
|
By("Verifying resource expectations ...")
|
|
|
|
expectResources(e2eClient, test.ExpectedResources...)
|
2021-06-03 00:18:28 +05:30
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 17:34:09 +02:00
|
|
|
func Test_ClusterRole_ClusterRoleBinding_Sets(t *testing.T) {
|
|
|
|
runTestCases(t, clusterRoleTests...)
|
|
|
|
}
|
2021-06-03 00:18:28 +05:30
|
|
|
|
2022-07-05 17:34:09 +02:00
|
|
|
func Test_Role_RoleBinding_Sets(t *testing.T) {
|
|
|
|
runTestCases(t, roleTests...)
|
|
|
|
}
|
2021-07-09 18:01:46 -07:00
|
|
|
|
2022-07-05 17:34:09 +02:00
|
|
|
func Test_Generate_NetworkPolicy(t *testing.T) {
|
|
|
|
runTestCases(t, networkPolicyGenerateTests...)
|
|
|
|
}
|
2021-06-03 00:18:28 +05:30
|
|
|
|
2022-07-05 17:34:09 +02:00
|
|
|
func Test_Generate_Namespace_Label_Actions(t *testing.T) {
|
|
|
|
runTestCases(t, generateNetworkPolicyOnNamespaceWithoutLabelTests...)
|
2021-06-03 00:18:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func loopElement(found bool, elementObj interface{}) bool {
|
|
|
|
if found == true {
|
|
|
|
return found
|
|
|
|
}
|
|
|
|
switch typedelementObj := elementObj.(type) {
|
|
|
|
case map[string]interface{}:
|
|
|
|
for k, v := range typedelementObj {
|
|
|
|
if k == "protocol" {
|
|
|
|
if v == "TCP" {
|
|
|
|
found = true
|
|
|
|
return found
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
found = loopElement(found, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case []interface{}:
|
|
|
|
found = loopElement(found, typedelementObj[0])
|
|
|
|
case string:
|
|
|
|
return found
|
|
|
|
case int64:
|
|
|
|
return found
|
|
|
|
default:
|
|
|
|
fmt.Println("unexpected type :", fmt.Sprintf("%T", elementObj))
|
|
|
|
return found
|
|
|
|
}
|
|
|
|
return found
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Generate_Synchronize_Flag(t *testing.T) {
|
2022-07-05 17:34:09 +02:00
|
|
|
runTestCases(t, generateSynchronizeFlagTests...)
|
2020-08-06 10:46:10 +05:30
|
|
|
}
|
2021-06-08 01:06:00 +05:30
|
|
|
|
|
|
|
func Test_Source_Resource_Update_Replication(t *testing.T) {
|
2022-07-05 17:34:09 +02:00
|
|
|
runTestCases(t, sourceResourceUpdateReplicationTests...)
|
2021-06-08 01:06:00 +05:30
|
|
|
}
|
2021-07-01 10:09:43 +05:30
|
|
|
|
|
|
|
func Test_Generate_Policy_Deletion_for_Clone(t *testing.T) {
|
2022-07-05 17:34:09 +02:00
|
|
|
runTestCases(t, generatePolicyDeletionforCloneTests...)
|
2021-07-01 10:09:43 +05:30
|
|
|
}
|
2022-09-08 10:17:09 +05:30
|
|
|
|
|
|
|
func Test_Generate_Multiple_Clone(t *testing.T) {
|
|
|
|
runTestCases(t, generatePolicyMultipleCloneTests...)
|
|
|
|
}
|