Skip to content

Commit 12b61e1

Browse files
committed
add tests for extractPlusVersionValues()
1 parent 9492957 commit 12b61e1

File tree

2 files changed

+78
-13
lines changed

2 files changed

+78
-13
lines changed

client/nginx.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"reflect"
1212
"regexp"
1313
"slices"
14-
"strconv"
1514
"strings"
1615
"sync"
1716
"time"
@@ -43,11 +42,12 @@ var (
4342
)
4443

4544
var (
46-
ErrParameterRequired = errors.New("parameter is required")
47-
ErrServerNotFound = errors.New("server not found")
48-
ErrServerExists = errors.New("server already exists")
49-
ErrNotSupported = errors.New("not supported")
50-
ErrInvalidTimeout = errors.New("invalid timeout")
45+
ErrParameterRequired = errors.New("parameter is required")
46+
ErrServerNotFound = errors.New("server not found")
47+
ErrServerExists = errors.New("server already exists")
48+
ErrNotSupported = errors.New("not supported")
49+
ErrInvalidTimeout = errors.New("invalid timeout")
50+
ErrPlusVersionNotFound = errors.New("plus version not found in the input string")
5151
)
5252

5353
// NginxClient lets you access NGINX Plus API.
@@ -2031,18 +2031,13 @@ func (client *NginxClient) GetWorkers(ctx context.Context) ([]*Workers, error) {
20312031

20322032
var rePlus = regexp.MustCompile(`-r(\d+)`)
20332033

2034-
// extractPlusVersionValues
2034+
// extractPlusVersionValues.
20352035
func extractPlusVersionValues(input string) (int, error) {
20362036
var rValue int
20372037
matches := rePlus.FindStringSubmatch(input)
20382038

20392039
if len(matches) < 1 {
2040-
return 0, fmt.Errorf("no matches found in the input string")
2041-
}
2042-
2043-
rValue, err := strconv.Atoi(matches[1])
2044-
if err != nil {
2045-
return 0, fmt.Errorf("failed to convert rValue to integer: %w", err)
2040+
return 0, fmt.Errorf("%w [%s]", ErrPlusVersionNotFound, input)
20462041
}
20472042

20482043
return rValue, nil

client/nginx_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,3 +910,73 @@ func TestGetMaxAPIVersionClient(t *testing.T) {
910910
t.Fatalf("expected %v, got %v", c.apiVersion, maxVer)
911911
}
912912
}
913+
914+
func TestExtractPlusVersion(t *testing.T) {
915+
t.Parallel()
916+
tests := []struct {
917+
name string
918+
version string
919+
expected int
920+
}{
921+
{
922+
name: "r32",
923+
version: "nginx-plus-r32",
924+
expected: 32,
925+
},
926+
{
927+
name: "r32p1",
928+
version: "nginx-plus-r32-p1",
929+
expected: 32,
930+
},
931+
{
932+
name: "r32p2",
933+
version: "nginx-plus-r32-p2",
934+
expected: 32,
935+
},
936+
{
937+
name: "r33",
938+
version: "nginx-plus-r33",
939+
expected: 33,
940+
},
941+
}
942+
943+
for _, test := range tests {
944+
t.Run(test.name, func(t *testing.T) {
945+
t.Parallel()
946+
version, err := extractPlusVersionValues(test.version)
947+
if err != nil {
948+
t.Error(err)
949+
}
950+
if version != test.expected {
951+
t.Errorf("values do not match, got: %d, expected %d)", version, test.expected)
952+
}
953+
})
954+
}
955+
}
956+
957+
func TestExtractPlusVersionNegativeCase(t *testing.T) {
958+
t.Parallel()
959+
tests := []struct {
960+
name string
961+
version string
962+
}{
963+
{
964+
name: "no-number",
965+
version: "nginx-plus-rxx",
966+
},
967+
{
968+
name: "extra-chars",
969+
version: "nginx-plus-rxx4343",
970+
},
971+
}
972+
973+
for _, test := range tests {
974+
t.Run(test.name, func(t *testing.T) {
975+
t.Parallel()
976+
_, err := extractPlusVersionValues(test.version)
977+
if err == nil {
978+
t.Errorf("Expected error but got %v", err)
979+
}
980+
})
981+
}
982+
}

0 commit comments

Comments
 (0)