Bladeren bron

label relation bidirectional and unit test w cassandra

sunguroku 5 jaren geleden
bovenliggende
commit
d43116e89d
3 gewijzigde bestanden met toevoegingen van 130 en 6 verwijderingen
  1. 32 0
      internal/helm/grapher/object_test.go
  2. 20 5
      internal/helm/grapher/relation.go
  3. 78 1
      internal/helm/grapher/relation_test.go

+ 32 - 0
internal/helm/grapher/object_test.go

@@ -16,11 +16,43 @@ var c1 = grapher.Object{
 var c2 = grapher.Object{
 	Kind: "Service",
 	Name: "my-release-cassandra-headless",
+	Relations: grapher.Relations{
+		LabelRels: []grapher.LabelRel{
+			grapher.LabelRel{
+				Relation: grapher.Relation{
+					Source: 1,
+					Target: 4,
+				},
+			},
+			grapher.LabelRel{
+				Relation: grapher.Relation{
+					Source: 1,
+					Target: 5,
+				},
+			},
+		},
+	},
 }
 
 var c3 = grapher.Object{
 	Kind: "Service",
 	Name: "my-release-cassandra",
+	Relations: grapher.Relations{
+		LabelRels: []grapher.LabelRel{
+			grapher.LabelRel{
+				Relation: grapher.Relation{
+					Source: 2,
+					Target: 4,
+				},
+			},
+			grapher.LabelRel{
+				Relation: grapher.Relation{
+					Source: 2,
+					Target: 5,
+				},
+			},
+		},
+	},
 }
 
 var c4 = grapher.Object{

+ 20 - 5
internal/helm/grapher/relation.go

@@ -1,6 +1,7 @@
 package grapher
 
 import (
+	"fmt"
 	"strconv"
 )
 
@@ -75,7 +76,7 @@ func (parsed *ParsedObjs) GetControlRel() {
 					pod := Object{
 						ID:      cid,
 						Kind:    "Pod",
-						Name:    obj.Name + "-" + strconv.Itoa(i), // tentative name pre-deploy
+						Name:    obj.Name + "-" + strconv.Itoa(j), // tentative name pre-deploy
 						RawYAML: template,
 						Relations: Relations{
 							ControlRels: []ControlRel{
@@ -99,6 +100,7 @@ func (parsed *ParsedObjs) GetControlRel() {
 // GetLabelRel is sdflk
 func (parsed *ParsedObjs) GetLabelRel() {
 	for i, o := range parsed.Objects {
+		// Skip Pods
 		yaml := o.RawYAML
 		matchLabels := []MatchLabel{}
 		matchExpressions := []MatchExpression{}
@@ -134,7 +136,7 @@ func (parsed *ParsedObjs) GetLabelRel() {
 		// fmt.Println("matchLabels", matchLabels)
 		// fmt.Println("matchExp", matchExpressions)
 
-		targetID := parsed.findLabelsBySelector(matchLabels, matchExpressions)
+		targetID := parsed.findLabelsBySelector(o.ID, matchLabels, matchExpressions)
 		lrels := o.Relations.LabelRels
 		for _, tid := range targetID {
 			newrel := LabelRel{
@@ -146,6 +148,7 @@ func (parsed *ParsedObjs) GetLabelRel() {
 			lrels = append(lrels, newrel)
 		}
 
+		fmt.Println(lrels)
 		parsed.Objects[i].Relations.LabelRels = lrels
 	}
 }
@@ -160,10 +163,15 @@ func addMatchLabels(matchLabels []MatchLabel, ml map[string]interface{}) []Match
 	return matchLabels
 }
 
-func (parsed *ParsedObjs) findLabelsBySelector(ml []MatchLabel, me []MatchExpression) []int {
+func (parsed *ParsedObjs) findLabelsBySelector(parentID int, ml []MatchLabel, me []MatchExpression) []int {
 	matchedObjs := []int{}
-	for _, o := range parsed.Objects {
-		// find objects that match labels
+	for i, o := range parsed.Objects {
+		// find Pods that match labels
+
+		if o.Kind != "Pod" {
+			continue
+		}
+
 		labels := getField(o.RawYAML, "metadata", "labels")
 		match := 0
 		for _, l := range ml {
@@ -172,6 +180,13 @@ func (parsed *ParsedObjs) findLabelsBySelector(ml []MatchLabel, me []MatchExpres
 			}
 		}
 		if match == len(ml) && match > 0 {
+			newrel := LabelRel{
+				Relation{
+					Source: parentID,
+					Target: o.ID,
+				},
+			}
+			parsed.Objects[i].Relations.LabelRels = append(parsed.Objects[i].Relations.LabelRels, newrel)
 			matchedObjs = append(matchedObjs, o.ID)
 		}
 	}

+ 78 - 1
internal/helm/grapher/relation_test.go

@@ -26,6 +26,20 @@ var c7 = grapher.Object{
 				Replicas: 2,
 			},
 		},
+		LabelRels: []grapher.LabelRel{
+			grapher.LabelRel{
+				Relation: grapher.Relation{
+					Source: 3,
+					Target: 4,
+				},
+			},
+			grapher.LabelRel{
+				Relation: grapher.Relation{
+					Source: 3,
+					Target: 5,
+				},
+			},
+		},
 	},
 }
 
@@ -41,6 +55,26 @@ var c5 = grapher.Object{
 				Replicas: 2,
 			},
 		},
+		LabelRels: []grapher.LabelRel{
+			grapher.LabelRel{
+				Relation: grapher.Relation{
+					Source: 1,
+					Target: 4,
+				},
+			},
+			grapher.LabelRel{
+				Relation: grapher.Relation{
+					Source: 2,
+					Target: 4,
+				},
+			},
+			grapher.LabelRel{
+				Relation: grapher.Relation{
+					Source: 3,
+					Target: 4,
+				},
+			},
+		},
 	},
 }
 
@@ -56,6 +90,26 @@ var c6 = grapher.Object{
 				Replicas: 2,
 			},
 		},
+		LabelRels: []grapher.LabelRel{
+			grapher.LabelRel{
+				Relation: grapher.Relation{
+					Source: 1,
+					Target: 5,
+				},
+			},
+			grapher.LabelRel{
+				Relation: grapher.Relation{
+					Source: 2,
+					Target: 5,
+				},
+			},
+			grapher.LabelRel{
+				Relation: grapher.Relation{
+					Source: 3,
+					Target: 5,
+				},
+			},
+		},
 	},
 }
 
@@ -143,7 +197,30 @@ func TestLabelRels(t *testing.T) {
 			Objects: objects,
 		}
 
+		parsed.GetControlRel()
 		parsed.GetLabelRel()
-		t.Errorf("label")
+
+		for i, o := range parsed.Objects {
+			e := r.Expected[i]
+			if len(e.Relations.LabelRels) != len(o.Relations.LabelRels) {
+				t.Errorf("Number of LabelRel differs for %s of type %s. Expected %d. Got %d",
+					e.Name, e.Kind, len(e.Relations.LabelRels), len(o.Relations.LabelRels))
+			}
+
+			for j, rrel := range o.Relations.LabelRels {
+				expRrel := e.Relations.LabelRels[j]
+
+				if expRrel.Relation.Source != rrel.Relation.Source {
+					t.Errorf("Source in ControlRel differs for %s of type %s. Expected %d. Got %d",
+						o.Name, o.Kind, expRrel.Relation.Source, rrel.Relation.Source)
+				}
+
+				if expRrel.Relation.Target != rrel.Relation.Target {
+					t.Errorf("Target in ControlRel differs for %s of type %s. Expected %d. Got %d",
+						o.Name, o.Kind, expRrel.Relation.Target, rrel.Relation.Target)
+				}
+			}
+		}
+
 	}
 }