mirror of
https://github.com/kyverno/kyverno.git
synced 2025-04-09 02:29:22 +00:00
checking metrics in generate e2e
Signed-off-by: NoSkillGirl <singhpooja240393@gmail.com>
This commit is contained in:
parent
28b053e54e
commit
cfa8ae0135
4 changed files with 98 additions and 25 deletions
54
test/e2e/common/common.go
Normal file
54
test/e2e/common/common.go
Normal file
|
@ -0,0 +1,54 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func ProcessMetrics(newStr, e2ePolicyName string, e2eTime time.Time) bool {
|
||||
fmt.Println("e2ePolicyName: ", e2ePolicyName, "e2eTime: ", e2eTime)
|
||||
var action, policyName string
|
||||
var timeInTimeFormat time.Time
|
||||
var err error
|
||||
splitByNewLine := strings.Split(newStr, "\n")
|
||||
for _, lineSplitedByNewLine := range splitByNewLine {
|
||||
if strings.HasPrefix(lineSplitedByNewLine, "kyverno_policy_changes_info{") {
|
||||
fmt.Println("--------------------------------------------------------")
|
||||
fmt.Println(lineSplitedByNewLine)
|
||||
splitByComma := strings.Split(lineSplitedByNewLine, ",")
|
||||
for _, lineSplitedByComma := range splitByComma {
|
||||
if strings.HasPrefix(lineSplitedByComma, "policy_change_type=") {
|
||||
splitByQuote := strings.Split(lineSplitedByComma, "\"")
|
||||
action = splitByQuote[1]
|
||||
}
|
||||
if strings.HasPrefix(lineSplitedByComma, "policy_name=") {
|
||||
splitByQuote := strings.Split(lineSplitedByComma, "\"")
|
||||
policyName = splitByQuote[1]
|
||||
}
|
||||
if strings.HasPrefix(lineSplitedByComma, "timestamp=") {
|
||||
splitByQuote := strings.Split(lineSplitedByComma, "\"")
|
||||
layout := "2006-01-02 15:04:05 -0700 MST"
|
||||
timeInTimeFormat, err = time.Parse(layout, splitByQuote[1])
|
||||
if err != nil {
|
||||
fmt.Println("error occurred: ", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("action: ", action)
|
||||
fmt.Println("policyName: ", policyName)
|
||||
fmt.Println("timeInTimeFormat: ", timeInTimeFormat)
|
||||
if policyName == e2ePolicyName {
|
||||
diff := e2eTime.Sub(timeInTimeFormat)
|
||||
if diff < 0 {
|
||||
if action == "created" {
|
||||
fmt.Println("************policy created**************")
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
|
@ -124,6 +124,8 @@ var NetworkPolicyGenerateTests = []struct {
|
|||
NetworkPolicyName string
|
||||
// ResourceNamespace - Namespace for which Resources are Created
|
||||
ResourceNamespace string
|
||||
// PolicyName - Name of the Policy
|
||||
PolicyName string
|
||||
// Clone - Set Clone Value
|
||||
Clone bool
|
||||
// CloneClusterRoleName
|
||||
|
@ -145,6 +147,7 @@ var NetworkPolicyGenerateTests = []struct {
|
|||
TestName: "test-generate-policy-for-namespace-with-label",
|
||||
NetworkPolicyName: "allow-dns",
|
||||
ResourceNamespace: "test",
|
||||
PolicyName: "add-networkpolicy",
|
||||
Clone: false,
|
||||
Sync: true,
|
||||
Data: genNetworkPolicyYaml,
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/kyverno/kyverno/test/e2e"
|
||||
commonE2E "github.com/kyverno/kyverno/test/e2e/common"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"sigs.k8s.io/yaml"
|
||||
|
||||
|
@ -326,17 +327,25 @@ func Test_Generate_NetworkPolicy(t *testing.T) {
|
|||
return errors.New("deleting Namespace")
|
||||
})
|
||||
|
||||
// check metrics before policy craetion
|
||||
callMetrics()
|
||||
// ====================================
|
||||
// ======== Create Generate NetworkPolicy Policy =============
|
||||
By("Creating Generate NetworkPolicy Policy")
|
||||
timeBeforePolicyCreation := time.Now()
|
||||
_, err = e2eClient.CreateNamespacedResourceYaml(clPolGVR, npPolNS, test.Data)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
// ============================================
|
||||
|
||||
// check metrics after policy craetion
|
||||
callMetrics()
|
||||
// check metrics
|
||||
policySyncBool := false
|
||||
e2e.GetWithRetry(time.Duration(1), 15, func() error {
|
||||
metricsString := callMetrics()
|
||||
policySyncBool = commonE2E.ProcessMetrics(metricsString, test.PolicyName, timeBeforePolicyCreation)
|
||||
if policySyncBool == false {
|
||||
return errors.New("policy not created")
|
||||
}
|
||||
return nil
|
||||
})
|
||||
Expect(policySyncBool).To(Equal(true))
|
||||
|
||||
// ======= Create Namespace ==================
|
||||
By(fmt.Sprintf("Creating Namespace which triggers generate %s", npPolNS))
|
||||
|
@ -387,7 +396,7 @@ func Test_Generate_NetworkPolicy(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func callMetrics() {
|
||||
func callMetrics() string {
|
||||
requestObj := e2e.APIRequest{
|
||||
URL: "http://localhost:8000/metrics",
|
||||
Type: "GET",
|
||||
|
@ -397,10 +406,7 @@ func callMetrics() {
|
|||
buf := new(bytes.Buffer)
|
||||
buf.ReadFrom(response.Body)
|
||||
newStr := buf.String()
|
||||
fmt.Println("==============================================================")
|
||||
fmt.Println(newStr)
|
||||
fmt.Println("==============================================================")
|
||||
|
||||
return newStr
|
||||
}
|
||||
|
||||
func Test_Generate_Namespace_Label_Actions(t *testing.T) {
|
||||
|
|
|
@ -3,6 +3,7 @@ package metrics
|
|||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
@ -13,9 +14,9 @@ import (
|
|||
|
||||
func Test_MetricsServerAvailability(t *testing.T) {
|
||||
RegisterTestingT(t)
|
||||
// if os.Getenv("E2E") == "" {
|
||||
// t.Skip("Skipping E2E Test")
|
||||
// }
|
||||
if os.Getenv("E2E") == "" {
|
||||
t.Skip("Skipping E2E Test")
|
||||
}
|
||||
requestObj := e2e.APIRequest{
|
||||
URL: "http://localhost:8000/metrics",
|
||||
Type: "GET",
|
||||
|
@ -25,14 +26,18 @@ func Test_MetricsServerAvailability(t *testing.T) {
|
|||
buf := new(bytes.Buffer)
|
||||
buf.ReadFrom(response.Body)
|
||||
newStr := buf.String()
|
||||
fmt.Println("==============================================================")
|
||||
fmt.Println(newStr)
|
||||
fmt.Println("==============================================================")
|
||||
processMetrics(newStr, "multi-tenancy", time.Now())
|
||||
|
||||
layout := "2006-01-02 15:04:05 -0700 MST"
|
||||
timeInTimeFormat, err := time.Parse(layout, "2021-06-20 18:04:50 +0000 UTC")
|
||||
if err != nil {
|
||||
fmt.Println("error occurred: ", err)
|
||||
}
|
||||
processMetrics(newStr, "multi-tenancy", timeInTimeFormat)
|
||||
Expect(response.StatusCode).To(Equal(200))
|
||||
}
|
||||
|
||||
func processMetrics(newStr, e2ePolicyName string, e2eTime time.Time) {
|
||||
fmt.Println("e2eTime: ", e2eTime)
|
||||
var action, policyName string
|
||||
var timeInTimeFormat time.Time
|
||||
var err error
|
||||
|
@ -54,23 +59,28 @@ func processMetrics(newStr, e2ePolicyName string, e2eTime time.Time) {
|
|||
}
|
||||
if strings.HasPrefix(lineSplitedByComma, "timestamp=") {
|
||||
splitByQuote := strings.Split(lineSplitedByComma, "\"")
|
||||
timeInTimeFormat, err = time.Parse(splitByQuote[1], "2014-11-17 23:02:03 +0000 UTC")
|
||||
layout := "2006-01-02 15:04:05 -0700 MST"
|
||||
timeInTimeFormat, err = time.Parse(layout, splitByQuote[1])
|
||||
if err != nil {
|
||||
fmt.Println("error: ", err)
|
||||
fmt.Println("error occurred: ", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
if policyName == e2ePolicyName {
|
||||
diff := e2eTime.Sub(timeInTimeFormat)
|
||||
// fmt.Println(diff)
|
||||
if diff < 0 {
|
||||
// fmt.Println("-------less------")
|
||||
if action == "created" {
|
||||
fmt.Println("************policy created**************")
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
fmt.Println("action: ", action)
|
||||
fmt.Println("policyName: ", policyName)
|
||||
fmt.Println("timeInTimeFormat: ", timeInTimeFormat)
|
||||
|
||||
diff := time.Now().Sub(timeInTimeFormat)
|
||||
fmt.Println(diff)
|
||||
|
||||
diff = timeInTimeFormat.Sub(time.Now())
|
||||
fmt.Println(diff)
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue