mirror of
https://github.com/replicatedhq/troubleshoot.git
synced 2026-04-15 07:16:34 +00:00
Merge pull request #192 from replicatedhq/pg-collector
Parse postgres version output
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"regexp"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/pkg/errors"
|
||||
@@ -24,7 +25,14 @@ func Postgres(ctx *Context, databaseCollector *troubleshootv1beta1.Database) (ma
|
||||
databaseConnection.Error = err.Error()
|
||||
} else {
|
||||
databaseConnection.IsConnected = true
|
||||
databaseConnection.Version = version
|
||||
|
||||
postgresVersion, err := parsePostgresVersion(version)
|
||||
if err != nil {
|
||||
databaseConnection.Version = "Unknown"
|
||||
databaseConnection.Error = err.Error()
|
||||
} else {
|
||||
databaseConnection.Version = postgresVersion
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,3 +52,14 @@ func Postgres(ctx *Context, databaseCollector *troubleshootv1beta1.Database) (ma
|
||||
|
||||
return postgresOutput, nil
|
||||
}
|
||||
|
||||
func parsePostgresVersion(postgresVersion string) (string, error) {
|
||||
re := regexp.MustCompile("PostgreSQL ([0-9.]*)")
|
||||
matches := re.FindStringSubmatch(postgresVersion)
|
||||
if len(matches) < 2 {
|
||||
return "", errors.Errorf("postgres version did not match regex: %q", postgresVersion)
|
||||
}
|
||||
|
||||
return matches[1], nil
|
||||
|
||||
}
|
||||
|
||||
50
pkg/collect/postgres_test.go
Normal file
50
pkg/collect/postgres_test.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package collect
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.undefinedlabs.com/scopeagent"
|
||||
)
|
||||
|
||||
func Test_parsePostgresVersion(t *testing.T) {
|
||||
tests := []struct {
|
||||
postgresVersion string
|
||||
expect string
|
||||
}{
|
||||
{
|
||||
// docker run -d --name pgnine -e POSTGRES_PASSWORD=password postgres:9
|
||||
postgresVersion: "PostgreSQL 9.6.17 on x86_64-pc-linux-gnu (Debian 9.6.17-2.pgdg90+1), compiled by gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516, 64-bit",
|
||||
expect: "9.6.17",
|
||||
},
|
||||
{
|
||||
// docker run -d --name pgten -e POSTGRES_PASSWORD=password postgres:10
|
||||
postgresVersion: "PostgreSQL 10.12 (Debian 10.12-2.pgdg90+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516, 64-bit",
|
||||
expect: "10.12",
|
||||
},
|
||||
{
|
||||
// docker run -d --name pgeleven -e POSTGRES_PASSWORD=password postgres:11
|
||||
postgresVersion: "PostgreSQL 11.7 (Debian 11.7-2.pgdg90+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516, 64-bit",
|
||||
expect: "11.7",
|
||||
},
|
||||
{
|
||||
// docker run -d --name pgtwelve -e POSTGRES_PASSWORD=password postgres:12
|
||||
postgresVersion: "PostgreSQL 12.2 (Debian 12.2-2.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit",
|
||||
expect: "12.2",
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.postgresVersion, func(t *testing.T) {
|
||||
scopetest := scopeagent.StartTest(t)
|
||||
defer scopetest.End()
|
||||
|
||||
req := require.New(t)
|
||||
actual, err := parsePostgresVersion(test.postgresVersion)
|
||||
req.NoError(err)
|
||||
|
||||
assert.Equal(t, test.expect, actual)
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user