From c146bcc7ac7a854a92dd0da784d4fc6fc5b746ed Mon Sep 17 00:00:00 2001 From: Francesco Romani Date: Mon, 20 Mar 2023 16:14:17 +0100 Subject: [PATCH] tracing: add Clone() for Status Enable Status objects to be deepcopied. Signed-off-by: Francesco Romani --- tracing.go | 11 +++++++++++ tracing_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/tracing.go b/tracing.go index db2d935..6b7c5cf 100644 --- a/tracing.go +++ b/tracing.go @@ -103,6 +103,17 @@ func (st Status) Repr() string { return sb.String() } +func (st Status) Clone() Status { + pods := make([]NamespacedName, len(st.Pods)) + copy(pods, st.Pods) + ret := Status{ + FingerprintExpected: st.FingerprintExpected, + FingerprintComputed: st.FingerprintComputed, + Pods: pods, + } + return ret +} + type TracingFingerprint struct { Fingerprint tracer Tracer diff --git a/tracing_test.go b/tracing_test.go index f645996..aec9285 100644 --- a/tracing_test.go +++ b/tracing_test.go @@ -172,3 +172,49 @@ func TestSignCrosscheck(t *testing.T) { t.Fatalf("signature not stable: %q vs %q", x, x2) } } + +func TestStatusClone(t *testing.T) { + orig := Status{ + FingerprintExpected: "PFPExpectedOrig", + FingerprintComputed: "PFPComputedOrig", + Pods: []NamespacedName{ + { + Namespace: "NSOrig1", + Name: "Name1", + }, + { + Namespace: "NSOrig2", + Name: "Name2", + }, + }, + } + + cloned := orig.Clone() + + origData, err := json.Marshal(orig) + if err != nil { + t.Errorf("marshal orig failed: %v", err) + } + clonedData, err := json.Marshal(cloned) + if err != nil { + t.Errorf("marshal cloned failed: %v", err) + } + if string(origData) != string(clonedData) { + t.Errorf("clone not identical:\norig=%s\ncloned=%s", string(origData), string(clonedData)) + } + + cloned.FingerprintComputed = "PFPModified2" + cloned.Pods = append(cloned.Pods, NamespacedName{ + Namespace: "NSModified2", + Name: "NameModified2", + }) + + origData2, err := json.Marshal(orig) + if err != nil { + t.Errorf("marshal orig (2) failed: %v", err) + } + + if string(origData) != string(origData2) { + t.Errorf("original modified changing the clone!\norig=%s\norig2=%s", string(origData), string(origData2)) + } +}