Skip to content

Commit

Permalink
add tests for extractPlusVersionValues()
Browse files Browse the repository at this point in the history
  • Loading branch information
pdabelf5 committed Nov 20, 2024
1 parent 9492957 commit a69d0d0
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 8 deletions.
17 changes: 9 additions & 8 deletions client/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ var (
)

var (
ErrParameterRequired = errors.New("parameter is required")
ErrServerNotFound = errors.New("server not found")
ErrServerExists = errors.New("server already exists")
ErrNotSupported = errors.New("not supported")
ErrInvalidTimeout = errors.New("invalid timeout")
ErrParameterRequired = errors.New("parameter is required")
ErrServerNotFound = errors.New("server not found")
ErrServerExists = errors.New("server already exists")
ErrNotSupported = errors.New("not supported")
ErrInvalidTimeout = errors.New("invalid timeout")
ErrPlusVersionNotFound = errors.New("plus version not found in the input string")
)

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

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

// extractPlusVersionValues
// extractPlusVersionValues.
func extractPlusVersionValues(input string) (int, error) {
var rValue int
matches := rePlus.FindStringSubmatch(input)

if len(matches) < 1 {
return 0, fmt.Errorf("no matches found in the input string")
return 0, fmt.Errorf("%w [%s]", ErrPlusVersionNotFound, input)
}

rValue, err := strconv.Atoi(matches[1])
if err != nil {
return 0, fmt.Errorf("failed to convert rValue to integer: %w", err)
return 0, fmt.Errorf("failed to convert NGINX Plus release to integer: %w", err)
}

return rValue, nil
Expand Down
70 changes: 70 additions & 0 deletions client/nginx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -910,3 +910,73 @@ func TestGetMaxAPIVersionClient(t *testing.T) {
t.Fatalf("expected %v, got %v", c.apiVersion, maxVer)
}
}

func TestExtractPlusVersion(t *testing.T) {
t.Parallel()
tests := []struct {
name string
version string
expected int
}{
{
name: "r32",
version: "nginx-plus-r32",
expected: 32,
},
{
name: "r32p1",
version: "nginx-plus-r32-p1",
expected: 32,
},
{
name: "r32p2",
version: "nginx-plus-r32-p2",
expected: 32,
},
{
name: "r33",
version: "nginx-plus-r33",
expected: 33,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
version, err := extractPlusVersionValues(test.version)
if err != nil {
t.Error(err)
}
if version != test.expected {
t.Errorf("values do not match, got: %d, expected %d)", version, test.expected)
}
})
}
}

func TestExtractPlusVersionNegativeCase(t *testing.T) {
t.Parallel()
tests := []struct {
name string
version string
}{
{
name: "no-number",
version: "nginx-plus-rxx",
},
{
name: "extra-chars",
version: "nginx-plus-rxx4343",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
_, err := extractPlusVersionValues(test.version)
if err == nil {
t.Errorf("Expected error but got %v", err)
}
})
}
}

0 comments on commit a69d0d0

Please sign in to comment.