Document 0 as a special case in RV comparison

This commit is contained in:
Michael Aspinwall 2025-10-01 00:02:03 +00:00
parent 84f85712be
commit 9757d8d8ef
4 changed files with 32 additions and 1 deletions

View file

@ -40,6 +40,8 @@ func (i InvalidResourceVersion) Error() string {
// The function will return an error if the resource version is not a properly
// formatted positive integer, but has no restriction on length. A properly
// formatted integer will not contain leading zeros or non integer characters.
// Zero is also considered an invalid value as it is used as a special value in
// list/watch events and will never be a live resource version.
func CompareResourceVersion(a, b string) (int, error) {
if !isWellFormed(a) {
return 0, InvalidResourceVersion{rv: a}

View file

@ -34,12 +34,36 @@ func TestCompareResourceVersion(t *testing.T) {
b: "200",
expected: -1,
},
{
name: "a is zero, invalid",
a: "0",
b: "1",
err: true,
},
{
name: "both zero",
a: "0",
b: "0",
err: true,
},
{
name: "a greater than b",
a: "200",
b: "100",
expected: 1,
},
{
name: "b is 0, invalid",
a: "1",
b: "0",
err: true,
},
{
name: "a equal to b small",
a: "1",
b: "1",
expected: 0,
},
{
name: "a equal to b",
a: "100",

View file

@ -57,6 +57,10 @@ func (m *HaveValidResourceVersionMatcher) Match(actual interface{}) (success boo
m.failureReason = fmt.Sprintf("resource version requires %d bits (more than 128)", val.BitLen())
return false, nil
}
if val.Cmp(big.NewInt(0)) == 0 {
m.failureReason = "the resource version is zero which is not valid"
return false, nil
}
return true, nil
}

View file

@ -38,7 +38,8 @@ func TestHaveValidResourceVersionMatch(t *testing.T) {
{
name: "zero resource version",
resourceVersion: "0",
expectedMatch: true,
expectedMatch: false,
expectedFailure: "Expected resource version to be a valid uint128, but got \"0\": the resource version is zero which is not valid",
},
{
name: "negative resource version",