1
0

server_pg_restore_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package service
  2. import "testing"
  3. func TestPgRestoreReadFailureError(t *testing.T) {
  4. cases := []struct {
  5. name string
  6. probeOutput string
  7. localVersion string
  8. want string
  9. }{
  10. {
  11. name: "dump from postgres 17 on older client",
  12. probeOutput: "pg_restore: error: unsupported version (1.16) in file header",
  13. localVersion: "16.4",
  14. want: "This backup was created by pg_dump from PostgreSQL 17 or newer, but the server's pg_restore is version 16.4 and cannot read it; run 'x-ui pgclient 17' on the server (or upgrade the postgresql-client package to version 17 or newer), then retry the import",
  15. },
  16. {
  17. name: "dump from postgres 16 on older client",
  18. probeOutput: "pg_restore: error: unsupported version (1.15) in file header",
  19. localVersion: "15.8",
  20. want: "This backup was created by pg_dump from PostgreSQL 16 or newer, but the server's pg_restore is version 15.8 and cannot read it; run 'x-ui pgclient 16' on the server (or upgrade the postgresql-client package to version 16 or newer), then retry the import",
  21. },
  22. {
  23. name: "archive version newer than any known mapping",
  24. probeOutput: "pg_restore: error: unsupported version (1.17) in file header",
  25. localVersion: "17.2",
  26. want: "This backup was created by a newer pg_dump than the server's pg_restore (version 17.2) can read; upgrade the postgresql-client package and retry the import",
  27. },
  28. {
  29. name: "local version could not be determined",
  30. probeOutput: "pg_restore: error: unsupported version (1.16) in file header",
  31. localVersion: "",
  32. want: "This backup was created by pg_dump from PostgreSQL 17 or newer, but the server's pg_restore is version unknown and cannot read it; run 'x-ui pgclient 17' on the server (or upgrade the postgresql-client package to version 17 or newer), then retry the import",
  33. },
  34. {
  35. name: "unrelated read failure passes through",
  36. probeOutput: "pg_restore: error: could not read from input file: end of file",
  37. localVersion: "16.4",
  38. want: "pg_restore cannot read this dump file: pg_restore: error: could not read from input file: end of file",
  39. },
  40. }
  41. for _, tc := range cases {
  42. t.Run(tc.name, func(t *testing.T) {
  43. err := pgRestoreReadFailureError(tc.probeOutput, tc.localVersion)
  44. if err == nil {
  45. t.Fatal("pgRestoreReadFailureError returned nil, want error")
  46. }
  47. if err.Error() != tc.want {
  48. t.Errorf("pgRestoreReadFailureError(%q, %q) = %q, want %q", tc.probeOutput, tc.localVersion, err.Error(), tc.want)
  49. }
  50. })
  51. }
  52. }
  53. func TestParsePgToolVersion(t *testing.T) {
  54. cases := []struct {
  55. name string
  56. in string
  57. want string
  58. }{
  59. {"plain", "pg_restore (PostgreSQL) 17.2\n", "17.2"},
  60. {"debian packaging suffix", "pg_restore (PostgreSQL) 16.10 (Debian 16.10-1.pgdg120+1)\n", "16.10"},
  61. {"three component version", "pg_restore (PostgreSQL) 9.6.24\n", "9.6.24"},
  62. {"no version present", "pg_restore malfunction\n", ""},
  63. }
  64. for _, tc := range cases {
  65. t.Run(tc.name, func(t *testing.T) {
  66. got := parsePgToolVersion(tc.in)
  67. if got != tc.want {
  68. t.Errorf("parsePgToolVersion(%q) = %q, want %q", tc.in, got, tc.want)
  69. }
  70. })
  71. }
  72. }