Skip to content

Commit

Permalink
feat: adding UT for qtree non exported case (#3085)
Browse files Browse the repository at this point in the history
* feat: adding UT for qtree non exported case

* feat: adding UT for qtree empty tree quota with historicalLabels case
  • Loading branch information
Hardikl authored Aug 7, 2024
1 parent 0a2d140 commit 673f31b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 28 deletions.
4 changes: 2 additions & 2 deletions cmd/collectors/zapi/plugins/qtree/qtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,10 @@ func (q *Qtree) handlingQuotaMetrics(quotas []*node.Node, data *matrix.Matrix, q
continue
}
case quotaType == "tree":
if tree == "" {
if !q.historicalLabels && tree == "" {
continue
}
}
*quotaCount++

// In 22.05, populate metrics with qtree labels
if q.historicalLabels {
Expand All @@ -294,6 +293,7 @@ func (q *Qtree) handlingQuotaMetrics(quotas []*node.Node, data *matrix.Matrix, q
continue
}
}
*quotaCount++

for attribute, m := range q.data.GetMetrics() {
objectElem := quota.GetChildS(attribute)
Expand Down
65 changes: 41 additions & 24 deletions cmd/collectors/zapi/plugins/qtree/qtree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,49 +42,66 @@ func TestHandlingQuotaMetrics(t *testing.T) {
quotas = x.GetChildren()
}

// Case 1: with historicalLabels = false
q1 := NewQtree()
q1.historicalLabels = false
testLabels(t, q1, quotas, nil, "astra_300.trident_qtree_pool_trident_TIXRBILLKA.trident_pvc_2a6d71d9_1c78_4e9a_84a2_59d316adfae9..disk-limit.tree", 3, 6, 5)

// Case 2: with historicalLabels = true
q2 := NewQtree()
data := matrix.New(q2.Parent+".Qtree", "qtree", "qtree")
qtreeInstance, _ := data.NewInstance("" + "." + "abcd_root" + "." + "abcde")
qtreeInstance.SetLabel("export_policy", "default")
qtreeInstance.SetLabel("oplocks", "enabled")
qtreeInstance.SetLabel("security_style", "unix")
qtreeInstance.SetLabel("status", "normal")

exportOptions := node.NewS("export_options")
instanceKeys := exportOptions.NewChildS("instance_keys", "")
// apply all instance keys, instance labels from qtree.yaml to all quota metrics
keys := []string{"export_policy", "oplocks", "security_style", "status"}
for _, key := range keys {
instanceKeys.NewChildS("", key)
}
q2.data.SetExportOptions(exportOptions)
q2.historicalLabels = true
testLabels(t, q2, quotas, data, "abcde.abcd_root..root.disk-used.user", 3, 4, 10)

// Case 1: with historicalLabels = false, total 4 quotas, 2 user/group quota, 1 empty qtree tree quota and 1 non-empty tree quota,
// 1 empty tree quota will be skipped and 5 labels[qtree, svm, type, unit, volume] would be exported for tree quota.
q1 := NewQtree()
testLabels(t, q1, false, quotas, nil, "astra_300.trident_qtree_pool_trident_TIXRBILLKA.trident_pvc_2a6d71d9_1c78_4e9a_84a2_59d316adfae9..disk-limit.tree", 3, 6, 5)

// Case 2: with historicalLabels = true, total 4 quotas, 2 user/group quota, 1 empty qtree tree quota and 1 non-empty tree quota,
// 1 empty tree quota will be skipped and 6 labels[qtree, svm, type, unit, user, volume] would be exported for user/group quota.
q2 := NewQtree()
testLabels(t, q2, false, quotas, nil, "abcde.abcd_root..root.disk-used.user", 3, 6, 6)

// Case 3: with historicalLabels = true, total 4 quotas, 2 user/group quota, 1 empty qtree tree quota and 1 non-empty tree quota,
// all quotas with 9 labels [export_policy, oplocks, qtree, security_style, status, svm, type, unit, volume] would be exported.
q3 := NewQtree()
data := matrix.New(q3.Parent+".Qtree", "qtree", "qtree")
q3.data.SetExportOptions(exportOptions)
qtreeInstance1, _ := data.NewInstance("" + "." + "volume1" + "." + "svm1")
addLabels(qtreeInstance1)
qtreeInstance2, _ := data.NewInstance("trident_pvc_2a6d71d9_1c78_4e9a_84a2_59d316adfae9" + "." + "trident_qtree_pool_trident_TIXRBILLKA" + "." + "astra_300")
addLabels(qtreeInstance2)
qtreeInstance3, _ := data.NewInstance("" + "." + "abcd_root" + "." + "abcde")
addLabels(qtreeInstance3)
testLabels(t, q3, true, quotas, data, "svm1.volume1...disk-used.tree", 4, 8, 9)
}

func addLabels(qtreeInstance *matrix.Instance) {
qtreeInstance.SetLabel("export_policy", "default")
qtreeInstance.SetLabel("oplocks", "enabled")
qtreeInstance.SetLabel("security_style", "unix")
qtreeInstance.SetLabel("status", "normal")
}

func testLabels(t *testing.T, q *Qtree, quotas []*node.Node, data *matrix.Matrix, quotaInstanceKey string, expectedQuotaCount int, expectedQuotaMetricCount int, expectedQuotaLabels int) {
func testLabels(t *testing.T, q *Qtree, historicalLabels bool, quotas []*node.Node, data *matrix.Matrix, quotaInstanceKey string, expectedQuotaCount int, expectedQuotaMetricCount int, expectedQuotaLabels int) {
quotaCount := 0
numMetrics := 0
err := q.handlingQuotaMetrics(quotas, data, &quotaCount, &numMetrics)
if err != nil {
quotaLabels := 0

q.historicalLabels = historicalLabels
if err := q.handlingQuotaMetrics(quotas, data, &quotaCount, &numMetrics); err != nil {
t.Errorf("handlingQuotaMetrics returned an error: %v", err)
}

if quotaInstance := q.data.GetInstance(quotaInstanceKey); quotaInstance != nil {
quotaLabels = len(quotaInstance.GetLabels())
}

if quotaCount != expectedQuotaCount {
t.Errorf("quotaCount = %d; want %d", quotaCount, expectedQuotaCount)
}
if numMetrics != expectedQuotaMetricCount {
t.Errorf("numMetrics = %d; want %d", numMetrics, expectedQuotaMetricCount)
}

quotaInstance := q.data.GetInstance(quotaInstanceKey)
if len(quotaInstance.GetLabels()) != expectedQuotaLabels {
t.Errorf("labels = %d; want %d", len(quotaInstance.GetLabels()), expectedQuotaLabels)
if quotaLabels != expectedQuotaLabels {
t.Errorf("labels = %d; want %d", quotaLabels, expectedQuotaLabels)
}
}
17 changes: 15 additions & 2 deletions cmd/collectors/zapi/plugins/qtree/testdata/quotas.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<netapp xmlns="http://www.netapp.com/filer/admin" version="1.231">
<results status="passed">
<attributes-list>

<quota>
<disk-limit>8388608</disk-limit>
<disk-used>198532</disk-used>
Expand Down Expand Up @@ -62,7 +61,21 @@
<volume>abcd_root</volume>
<vserver>abcde</vserver>
</quota>
<quota>
<disk-limit>1024000</disk-limit>
<disk-used>198532</disk-used>
<file-limit>-</file-limit>
<files-used>6</files-used>
<quota-target />
<quota-type>tree</quota-type>
<soft-disk-limit>-</soft-disk-limit>
<soft-file-limit>-</soft-file-limit>
<threshold>-</threshold>
<tree />
<volume>volume1</volume>
<vserver>svm1</vserver>
</quota>
</attributes-list>
<num-records>3</num-records>
<num-records>4</num-records>
</results>
</netapp>

0 comments on commit 673f31b

Please sign in to comment.