Skip to content

Commit 0f31088

Browse files
committed
Parse postgres version output
1 parent 20033b1 commit 0f31088

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

pkg/collect/postgres.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"database/sql"
55
"encoding/json"
66
"fmt"
7+
"regexp"
78

89
_ "github.com/lib/pq"
910
"github.com/pkg/errors"
@@ -24,7 +25,14 @@ func Postgres(ctx *Context, databaseCollector *troubleshootv1beta1.Database) (ma
2425
databaseConnection.Error = err.Error()
2526
} else {
2627
databaseConnection.IsConnected = true
27-
databaseConnection.Version = version
28+
29+
postgresVersion, err := parsePostgresVersion(version)
30+
if err != nil {
31+
databaseConnection.Version = "Unknown"
32+
databaseConnection.Error = err.Error()
33+
} else {
34+
databaseConnection.Version = postgresVersion
35+
}
2836
}
2937
}
3038

@@ -44,3 +52,14 @@ func Postgres(ctx *Context, databaseCollector *troubleshootv1beta1.Database) (ma
4452

4553
return postgresOutput, nil
4654
}
55+
56+
func parsePostgresVersion(postgresVersion string) (string, error) {
57+
re := regexp.MustCompile("PostgreSQL ([0-9.]*)")
58+
matches := re.FindStringSubmatch(postgresVersion)
59+
if len(matches) < 2 {
60+
return "", errors.Errorf("postgres version did not match regex: %q", postgresVersion)
61+
}
62+
63+
return matches[1], nil
64+
65+
}

pkg/collect/postgres_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package collect
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
8+
"go.undefinedlabs.com/scopeagent"
9+
)
10+
11+
func Test_parsePostgresVersion(t *testing.T) {
12+
tests := []struct {
13+
postgresVersion string
14+
expect string
15+
}{
16+
{
17+
// docker run -d --name pgnine -e POSTGRES_PASSWORD=password postgres:9
18+
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",
19+
expect: "9.6.17",
20+
},
21+
{
22+
// docker run -d --name pgten -e POSTGRES_PASSWORD=password postgres:10
23+
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",
24+
expect: "10.12",
25+
},
26+
{
27+
// docker run -d --name pgeleven -e POSTGRES_PASSWORD=password postgres:11
28+
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",
29+
expect: "11.7",
30+
},
31+
{
32+
// docker run -d --name pgtwelve -e POSTGRES_PASSWORD=password postgres:12
33+
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",
34+
expect: "12.2",
35+
},
36+
}
37+
for _, test := range tests {
38+
t.Run(test.postgresVersion, func(t *testing.T) {
39+
scopetest := scopeagent.StartTest(t)
40+
defer scopetest.End()
41+
42+
req := require.New(t)
43+
actual, err := parsePostgresVersion(test.postgresVersion)
44+
req.NoError(err)
45+
46+
assert.Equal(t, test.expect, actual)
47+
48+
})
49+
}
50+
}

0 commit comments

Comments
 (0)