backup_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2020 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package db
  5. import (
  6. "bytes"
  7. "os"
  8. "path/filepath"
  9. "testing"
  10. "time"
  11. "github.com/pkg/errors"
  12. "gorm.io/gorm"
  13. "gogs.io/gogs/internal/auth"
  14. "gogs.io/gogs/internal/auth/github"
  15. "gogs.io/gogs/internal/auth/pam"
  16. "gogs.io/gogs/internal/cryptoutil"
  17. "gogs.io/gogs/internal/lfsutil"
  18. "gogs.io/gogs/internal/testutil"
  19. )
  20. func Test_dumpAndImport(t *testing.T) {
  21. if testing.Short() {
  22. t.Skip()
  23. }
  24. t.Parallel()
  25. if len(Tables) != 3 {
  26. t.Fatalf("New table has added (want 3 got %d), please add new tests for the table and update this check", len(Tables))
  27. }
  28. db := initTestDB(t, "dumpAndImport", Tables...)
  29. setupDBToDump(t, db)
  30. dumpTables(t, db)
  31. importTables(t, db)
  32. // Dump and assert golden again to make sure data aren't changed.
  33. dumpTables(t, db)
  34. }
  35. func setupDBToDump(t *testing.T, db *gorm.DB) {
  36. t.Helper()
  37. vals := []interface{}{
  38. &AccessToken{
  39. UserID: 1,
  40. Name: "test1",
  41. Sha1: cryptoutil.SHA1("2910d03d-c0b5-4f71-bad5-c4086e4efae3"),
  42. CreatedUnix: 1588568886,
  43. UpdatedUnix: 1588572486, // 1 hour later
  44. },
  45. &AccessToken{
  46. UserID: 1,
  47. Name: "test2",
  48. Sha1: cryptoutil.SHA1("84117e17-7e67-4024-bd04-1c23e6e809d4"),
  49. CreatedUnix: 1588568886,
  50. },
  51. &AccessToken{
  52. UserID: 2,
  53. Name: "test1",
  54. Sha1: cryptoutil.SHA1("da2775ce-73dd-47ba-b9d2-bbcc346585c4"),
  55. CreatedUnix: 1588568886,
  56. },
  57. &LFSObject{
  58. RepoID: 1,
  59. OID: "ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f",
  60. Size: 100,
  61. Storage: lfsutil.StorageLocal,
  62. CreatedAt: time.Unix(1588568886, 0).UTC(),
  63. },
  64. &LFSObject{
  65. RepoID: 2,
  66. OID: "ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f",
  67. Size: 100,
  68. Storage: lfsutil.StorageLocal,
  69. CreatedAt: time.Unix(1588568886, 0).UTC(),
  70. },
  71. &LoginSource{
  72. Type: auth.PAM,
  73. Name: "My PAM",
  74. IsActived: true,
  75. Provider: pam.NewProvider(&pam.Config{
  76. ServiceName: "PAM service",
  77. }),
  78. CreatedUnix: 1588568886,
  79. UpdatedUnix: 1588572486, // 1 hour later
  80. },
  81. &LoginSource{
  82. Type: auth.GitHub,
  83. Name: "GitHub.com",
  84. IsActived: true,
  85. Provider: github.NewProvider(&github.Config{
  86. APIEndpoint: "https://api.github.com",
  87. }),
  88. CreatedUnix: 1588568886,
  89. },
  90. }
  91. for _, val := range vals {
  92. err := db.Create(val).Error
  93. if err != nil {
  94. t.Fatal(err)
  95. }
  96. }
  97. }
  98. func dumpTables(t *testing.T, db *gorm.DB) {
  99. t.Helper()
  100. for _, table := range Tables {
  101. tableName := getTableType(table)
  102. var buf bytes.Buffer
  103. err := dumpTable(db, table, &buf)
  104. if err != nil {
  105. t.Fatalf("%s: %v", tableName, err)
  106. }
  107. golden := filepath.Join("testdata", "backup", tableName+".golden.json")
  108. testutil.AssertGolden(t, golden, testutil.Update("Test_dumpAndImport"), buf.String())
  109. }
  110. }
  111. func importTables(t *testing.T, db *gorm.DB) {
  112. t.Helper()
  113. for _, table := range Tables {
  114. tableName := getTableType(table)
  115. err := func() error {
  116. golden := filepath.Join("testdata", "backup", tableName+".golden.json")
  117. f, err := os.Open(golden)
  118. if err != nil {
  119. return errors.Wrap(err, "open table file")
  120. }
  121. defer func() { _ = f.Close() }()
  122. return importTable(db, table, f)
  123. }()
  124. if err != nil {
  125. t.Fatalf("%s: %v", tableName, err)
  126. }
  127. }
  128. }