-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix input revision comparison (#215)
Now that the struct has pointer type'd fields it cannot be compared with `==` - pointers are compared literally, by comparing the addresses the point to. So if non-nil they will never match. Co-authored-by: Jordan Olshevski <[email protected]>
- Loading branch information
Showing
2 changed files
with
125 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package watch | ||
|
||
import ( | ||
"testing" | ||
|
||
apiv1 "github.com/Azure/eno/api/v1" | ||
"github.com/stretchr/testify/assert" | ||
"k8s.io/utils/ptr" | ||
) | ||
|
||
func TestSetInputRevisions(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
comp *apiv1.Composition | ||
revs *apiv1.InputRevisions | ||
expected bool | ||
finalRevs []apiv1.InputRevisions | ||
}{ | ||
{ | ||
name: "add new revision when key is not found", | ||
comp: &apiv1.Composition{ | ||
Status: apiv1.CompositionStatus{ | ||
InputRevisions: []apiv1.InputRevisions{ | ||
{Key: "rev1", Revision: ptr.To(1)}, | ||
}, | ||
}, | ||
}, | ||
revs: &apiv1.InputRevisions{ | ||
Key: "rev2", | ||
Revision: ptr.To(2), | ||
}, | ||
expected: true, | ||
finalRevs: []apiv1.InputRevisions{ | ||
{Key: "rev1", Revision: ptr.To(1)}, | ||
{Key: "rev2", Revision: ptr.To(2)}, | ||
}, | ||
}, | ||
{ | ||
name: "update existing revision", | ||
comp: &apiv1.Composition{ | ||
Status: apiv1.CompositionStatus{ | ||
InputRevisions: []apiv1.InputRevisions{ | ||
{Key: "rev1", Revision: ptr.To(1)}, | ||
}, | ||
}, | ||
}, | ||
revs: &apiv1.InputRevisions{ | ||
Key: "rev1", | ||
Revision: ptr.To(2), | ||
}, | ||
expected: true, | ||
finalRevs: []apiv1.InputRevisions{ | ||
{Key: "rev1", Revision: ptr.To(2)}, | ||
}, | ||
}, | ||
{ | ||
name: "no update if revision is identical", | ||
comp: &apiv1.Composition{ | ||
Status: apiv1.CompositionStatus{ | ||
InputRevisions: []apiv1.InputRevisions{ | ||
{Key: "rev1", Revision: ptr.To(1)}, | ||
}, | ||
}, | ||
}, | ||
revs: &apiv1.InputRevisions{ | ||
Key: "rev1", | ||
Revision: ptr.To(1), | ||
}, | ||
expected: false, | ||
finalRevs: []apiv1.InputRevisions{ | ||
{Key: "rev1", Revision: ptr.To(1)}, | ||
}, | ||
}, | ||
{ | ||
name: "no update if revision is identical and synth generation is set", | ||
comp: &apiv1.Composition{ | ||
Status: apiv1.CompositionStatus{ | ||
InputRevisions: []apiv1.InputRevisions{ | ||
{Key: "rev1", Revision: ptr.To(1), SynthesizerGeneration: ptr.To(int64(3))}, | ||
}, | ||
}, | ||
}, | ||
revs: &apiv1.InputRevisions{ | ||
Key: "rev1", | ||
Revision: ptr.To(1), | ||
SynthesizerGeneration: ptr.To(int64(3)), | ||
}, | ||
expected: false, | ||
finalRevs: []apiv1.InputRevisions{ | ||
{Key: "rev1", Revision: ptr.To(1), SynthesizerGeneration: ptr.To(int64(3))}, | ||
}, | ||
}, | ||
{ | ||
name: "update if revision is identical but synth generation is not", | ||
comp: &apiv1.Composition{ | ||
Status: apiv1.CompositionStatus{ | ||
InputRevisions: []apiv1.InputRevisions{ | ||
{Key: "rev1", Revision: ptr.To(1), SynthesizerGeneration: ptr.To(int64(3))}, | ||
}, | ||
}, | ||
}, | ||
revs: &apiv1.InputRevisions{ | ||
Key: "rev1", | ||
Revision: ptr.To(1), | ||
SynthesizerGeneration: ptr.To(int64(5)), | ||
}, | ||
expected: true, | ||
finalRevs: []apiv1.InputRevisions{ | ||
{Key: "rev1", Revision: ptr.To(1), SynthesizerGeneration: ptr.To(int64(5))}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := setInputRevisions(tt.comp, tt.revs) | ||
assert.Equal(t, tt.expected, result) | ||
assert.Equal(t, tt.finalRevs, tt.comp.Status.InputRevisions) | ||
}) | ||
} | ||
} |