Skip to content

Commit

Permalink
pkg/utils: Test ParseRelease
Browse files Browse the repository at this point in the history
  • Loading branch information
HarryMichal committed Jul 15, 2021
1 parent ff21f61 commit c693a66
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions src/pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package utils

import (
"strconv"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -72,3 +73,101 @@ func TestImageReferenceCanBeID(t *testing.T) {
})
}
}

func TestParseRelease(t *testing.T) {
testCases := []struct {
name string
inputDistro string
inputRelease string
output string
ok bool
err error
errMsg string
}{
{
name: "Fedora; f34; valid",
inputDistro: "fedora",
inputRelease: "f34",
output: "34",
ok: true,
},
{
name: "Fedora; 33; valid",
inputDistro: "fedora",
inputRelease: "33",
output: "33",
ok: true,
},
{
name: "Fedora; -3; invalid; less than 0",
inputDistro: "fedora",
inputRelease: "-3",
ok: false,
errMsg: "release must be a positive integer",
},
{
name: "Fedora; foo; invalid; non-numeric",
inputDistro: "fedora",
inputRelease: "foo",
ok: false,
err: strconv.ErrSyntax,
},
{
name: "RHEL; 8.3; valid",
inputDistro: "rhel",
inputRelease: "8.3",
output: "8.3",
ok: true,
},
{
name: "RHEL; 8.42; valid",
inputDistro: "rhel",
inputRelease: "8.42",
output: "8.42",
ok: true,
},
{
name: "RHEL; 8; invalid; missing point release",
inputDistro: "rhel",
inputRelease: "8",
ok: false,
errMsg: "release must have a '.'",
},
{
name: "RHEL; 8.2foo; invalid; non-float",
inputDistro: "rhel",
inputRelease: "8.2foo",
ok: false,
err: strconv.ErrSyntax,
},
{
name: "RHEL; -2.1; invalid; less than 0",
inputDistro: "rhel",
inputRelease: "-2.1",
ok: false,
errMsg: "release must be a positive number",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
release, err := ParseRelease(tc.inputDistro, tc.inputRelease)

if tc.ok {
assert.NoError(t, err)
} else {
assert.Error(t, err)

if tc.err != nil {
assert.ErrorIs(t, err, tc.err)
}

if tc.errMsg != "" {
assert.EqualError(t, err, tc.errMsg)
}
}

assert.Equal(t, tc.output, release)
})
}
}

0 comments on commit c693a66

Please sign in to comment.