backup_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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) != 4 {
  26. t.Fatalf("New table has added (want 4 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. &Access{
  39. ID: 1,
  40. UserID: 1,
  41. RepoID: 11,
  42. Mode: AccessModeRead,
  43. },
  44. &Access{
  45. ID: 2,
  46. UserID: 2,
  47. RepoID: 22,
  48. Mode: AccessModeWrite,
  49. },
  50. &AccessToken{
  51. UserID: 1,
  52. Name: "test1",
  53. Sha1: cryptoutil.SHA1("2910d03d-c0b5-4f71-bad5-c4086e4efae3"),
  54. CreatedUnix: 1588568886,
  55. UpdatedUnix: 1588572486, // 1 hour later
  56. },
  57. &AccessToken{
  58. UserID: 1,
  59. Name: "test2",
  60. Sha1: cryptoutil.SHA1("84117e17-7e67-4024-bd04-1c23e6e809d4"),
  61. CreatedUnix: 1588568886,
  62. },
  63. &AccessToken{
  64. UserID: 2,
  65. Name: "test1",
  66. Sha1: cryptoutil.SHA1("da2775ce-73dd-47ba-b9d2-bbcc346585c4"),
  67. CreatedUnix: 1588568886,
  68. },
  69. &LFSObject{
  70. RepoID: 1,
  71. OID: "ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f",
  72. Size: 100,
  73. Storage: lfsutil.StorageLocal,
  74. CreatedAt: time.Unix(1588568886, 0).UTC(),
  75. },
  76. &LFSObject{
  77. RepoID: 2,
  78. OID: "ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f",
  79. Size: 100,
  80. Storage: lfsutil.StorageLocal,
  81. CreatedAt: time.Unix(1588568886, 0).UTC(),
  82. },
  83. &LoginSource{
  84. Type: auth.PAM,
  85. Name: "My PAM",
  86. IsActived: true,
  87. Provider: pam.NewProvider(&pam.Config{
  88. ServiceName: "PAM service",
  89. }),
  90. CreatedUnix: 1588568886,
  91. UpdatedUnix: 1588572486, // 1 hour later
  92. },
  93. &LoginSource{
  94. Type: auth.GitHub,
  95. Name: "GitHub.com",
  96. IsActived: true,
  97. Provider: github.NewProvider(&github.Config{
  98. APIEndpoint: "https://api.github.com",
  99. }),
  100. CreatedUnix: 1588568886,
  101. },
  102. }
  103. for _, val := range vals {
  104. err := db.Create(val).Error
  105. if err != nil {
  106. t.Fatal(err)
  107. }
  108. }
  109. }
  110. func dumpTables(t *testing.T, db *gorm.DB) {
  111. t.Helper()
  112. for _, table := range Tables {
  113. tableName := getTableType(table)
  114. var buf bytes.Buffer
  115. err := dumpTable(db, table, &buf)
  116. if err != nil {
  117. t.Fatalf("%s: %v", tableName, err)
  118. }
  119. golden := filepath.Join("testdata", "backup", tableName+".golden.json")
  120. testutil.AssertGolden(t, golden, testutil.Update("Test_dumpAndImport"), buf.String())
  121. }
  122. }
  123. func importTables(t *testing.T, db *gorm.DB) {
  124. t.Helper()
  125. for _, table := range Tables {
  126. tableName := getTableType(table)
  127. err := func() error {
  128. golden := filepath.Join("testdata", "backup", tableName+".golden.json")
  129. f, err := os.Open(golden)
  130. if err != nil {
  131. return errors.Wrap(err, "open table file")
  132. }
  133. defer func() { _ = f.Close() }()
  134. return importTable(db, table, f)
  135. }()
  136. if err != nil {
  137. t.Fatalf("%s: %v", tableName, err)
  138. }
  139. }
  140. }