backup_test.go 4.1 KB

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