Skip to content

Commit

Permalink
chore: nrt: test: rewrite TestFingerprintFromNRT
Browse files Browse the repository at this point in the history
Rewrite TestFingerprintFromNRT as tabular test.
The test grown a bit wild and it's time to cleanup. No expected
change (bar a slight increase by side effect) in coverage.

Signed-off-by: Francesco Romani <fromani@redhat.com>
  • Loading branch information
ffromani committed Jun 1, 2023
1 parent 5360937 commit f5d7100
Showing 1 changed file with 72 additions and 36 deletions.
108 changes: 72 additions & 36 deletions pkg/noderesourcetopology/cache/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
)

func TestFingerprintFromNRT(t *testing.T) {
nrtRef := &topologyv1alpha2.NodeResourceTopology{
nrt := &topologyv1alpha2.NodeResourceTopology{
ObjectMeta: metav1.ObjectMeta{
Name: "node-0",
},
Expand All @@ -45,44 +45,80 @@ func TestFingerprintFromNRT(t *testing.T) {
},
}

var pfp string

nrt := nrtRef.DeepCopy()
pfp, _ = podFingerprintForNodeTopology(nrt, apiconfig.CacheResyncAutodetect)
if pfp != "" {
t.Errorf("misdetected fingerprint from missing annotations")
}

nrt = nrtRef.DeepCopy()
nrt.Annotations = map[string]string{}
pfp, _ = podFingerprintForNodeTopology(nrt, apiconfig.CacheResyncAutodetect)
if pfp != "" {
t.Errorf("misdetected fingerprint from empty annotations")
}

pfpTestAnn := "test-ann"
nrt = nrtRef.DeepCopy()
nrt.Annotations = map[string]string{
podfingerprint.Annotation: pfpTestAnn,
}
pfp, _ = podFingerprintForNodeTopology(nrt, apiconfig.CacheResyncAutodetect)
if pfp != pfpTestAnn {
t.Errorf("misdetected fingerprint as %q expected %q", pfp, pfpTestAnn)
pfpTestAttr := "test-attr"

tcases := []struct {
description string
anns map[string]string
attrs []topologyv1alpha2.AttributeInfo
expectedPFP string
}{
{
description: "no anns, attr",
expectedPFP: "",
},
{
description: "no attrs, empty anns",
anns: map[string]string{},
expectedPFP: "",
},
{
description: "no attrs, empty pfp ann",
anns: map[string]string{
podfingerprint.Annotation: "",
},
expectedPFP: "",
},
{
description: "no attrs, pfp ann",
anns: map[string]string{
podfingerprint.Annotation: pfpTestAnn,
},
expectedPFP: pfpTestAnn,
},
{
description: "attr overrides, pfp ann",
anns: map[string]string{
podfingerprint.Annotation: pfpTestAnn,
},
attrs: []topologyv1alpha2.AttributeInfo{
{
Name: podfingerprint.Attribute,
Value: pfpTestAttr,
},
},
expectedPFP: pfpTestAttr,
},
{
description: "attr, no ann",
attrs: []topologyv1alpha2.AttributeInfo{
{
Name: podfingerprint.Attribute,
Value: pfpTestAttr,
},
},
expectedPFP: pfpTestAttr,
},
}

// test attribute overrides annotation
pfpTestAttr := "test-attr"
nrt = nrtRef.DeepCopy()
nrt.Annotations = map[string]string{
podfingerprint.Annotation: pfpTestAnn,
}
nrt.Attributes = append(nrt.Attributes, topologyv1alpha2.AttributeInfo{
Name: podfingerprint.Attribute,
Value: pfpTestAttr,
})
pfp, _ = podFingerprintForNodeTopology(nrt, apiconfig.CacheResyncAutodetect)
if pfp != pfpTestAttr {
t.Errorf("misdetected fingerprint as %q expected %q", pfp, pfpTestAttr)
for _, tcase := range tcases {
t.Run(tcase.description, func(t *testing.T) {
nrtObj := nrt.DeepCopy()
if tcase.anns != nil {
nrtObj.Annotations = make(map[string]string)
for key, value := range tcase.anns {
nrtObj.Annotations[key] = value
}
}
for _, attr := range tcase.attrs {
nrtObj.Attributes = append(nrtObj.Attributes, attr)
}
pfp, _ := podFingerprintForNodeTopology(nrtObj, apiconfig.CacheResyncAutodetect)
if pfp != tcase.expectedPFP {
t.Errorf("misdetected fingerprint as %q expected %q (anns=%v attrs=%v)", pfp, tcase.expectedPFP, nrtObj.Annotations, nrtObj.Attributes)
}
})
}
}

Expand Down

0 comments on commit f5d7100

Please sign in to comment.