Skip to content

Commit

Permalink
tracing: add Clone() for Status
Browse files Browse the repository at this point in the history
Enable Status objects to be deepcopied.

Signed-off-by: Francesco Romani <[email protected]>
  • Loading branch information
ffromani committed Mar 20, 2023
1 parent dcab3ab commit c146bcc
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
11 changes: 11 additions & 0 deletions tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 46 additions & 0 deletions tracing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

0 comments on commit c146bcc

Please sign in to comment.