backup_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. "context"
  8. "os"
  9. "path/filepath"
  10. "testing"
  11. "time"
  12. "github.com/pkg/errors"
  13. "github.com/stretchr/testify/require"
  14. "gorm.io/gorm"
  15. "gogs.io/gogs/internal/auth"
  16. "gogs.io/gogs/internal/auth/github"
  17. "gogs.io/gogs/internal/auth/pam"
  18. "gogs.io/gogs/internal/cryptoutil"
  19. "gogs.io/gogs/internal/dbtest"
  20. "gogs.io/gogs/internal/lfsutil"
  21. "gogs.io/gogs/internal/testutil"
  22. )
  23. func TestDumpAndImport(t *testing.T) {
  24. if testing.Short() {
  25. t.Skip()
  26. }
  27. t.Parallel()
  28. if len(Tables) != 5 {
  29. t.Fatalf("New table has added (want 5 got %d), please add new tests for the table and update this check", len(Tables))
  30. }
  31. db := dbtest.NewDB(t, "dumpAndImport", Tables...)
  32. setupDBToDump(t, db)
  33. dumpTables(t, db)
  34. importTables(t, db)
  35. // Dump and assert golden again to make sure data aren't changed.
  36. dumpTables(t, db)
  37. }
  38. func setupDBToDump(t *testing.T, db *gorm.DB) {
  39. vals := []interface{}{
  40. &Access{
  41. ID: 1,
  42. UserID: 1,
  43. RepoID: 11,
  44. Mode: AccessModeRead,
  45. },
  46. &Access{
  47. ID: 2,
  48. UserID: 2,
  49. RepoID: 22,
  50. Mode: AccessModeWrite,
  51. },
  52. &AccessToken{
  53. UserID: 1,
  54. Name: "test1",
  55. Sha1: cryptoutil.SHA1("2910d03d-c0b5-4f71-bad5-c4086e4efae3"),
  56. SHA256: cryptoutil.SHA256(cryptoutil.SHA1("2910d03d-c0b5-4f71-bad5-c4086e4efae3")),
  57. CreatedUnix: 1588568886,
  58. UpdatedUnix: 1588572486, // 1 hour later
  59. },
  60. &AccessToken{
  61. UserID: 1,
  62. Name: "test2",
  63. Sha1: cryptoutil.SHA1("84117e17-7e67-4024-bd04-1c23e6e809d4"),
  64. SHA256: cryptoutil.SHA256(cryptoutil.SHA1("84117e17-7e67-4024-bd04-1c23e6e809d4")),
  65. CreatedUnix: 1588568886,
  66. },
  67. &AccessToken{
  68. UserID: 2,
  69. Name: "test1",
  70. Sha1: cryptoutil.SHA1("da2775ce-73dd-47ba-b9d2-bbcc346585c4"),
  71. SHA256: cryptoutil.SHA256(cryptoutil.SHA1("da2775ce-73dd-47ba-b9d2-bbcc346585c4")),
  72. CreatedUnix: 1588568886,
  73. },
  74. &AccessToken{
  75. UserID: 2,
  76. Name: "test2",
  77. Sha1: cryptoutil.SHA256(cryptoutil.SHA1("1b2dccd1-a262-470f-bb8c-7fc73192e9bb"))[:40],
  78. SHA256: cryptoutil.SHA256(cryptoutil.SHA1("1b2dccd1-a262-470f-bb8c-7fc73192e9bb")),
  79. CreatedUnix: 1588568886,
  80. },
  81. &Action{
  82. ID: 1,
  83. UserID: 1,
  84. OpType: ActionCreateBranch,
  85. ActUserID: 1,
  86. ActUserName: "alice",
  87. RepoID: 1,
  88. RepoUserName: "alice",
  89. RepoName: "example",
  90. RefName: "main",
  91. IsPrivate: false,
  92. Content: `{"Len":1,"Commits":[],"CompareURL":""}`,
  93. CreatedUnix: 1588568886,
  94. },
  95. &Action{
  96. ID: 2,
  97. UserID: 1,
  98. OpType: ActionCommitRepo,
  99. ActUserID: 1,
  100. ActUserName: "alice",
  101. RepoID: 1,
  102. RepoUserName: "alice",
  103. RepoName: "example",
  104. RefName: "main",
  105. IsPrivate: false,
  106. Content: `{"Len":1,"Commits":[],"CompareURL":""}`,
  107. CreatedUnix: 1588568886,
  108. },
  109. &Action{
  110. ID: 3,
  111. UserID: 1,
  112. OpType: ActionDeleteBranch,
  113. ActUserID: 1,
  114. ActUserName: "alice",
  115. RepoID: 1,
  116. RepoUserName: "alice",
  117. RepoName: "example",
  118. RefName: "main",
  119. IsPrivate: false,
  120. CreatedUnix: 1588568886,
  121. },
  122. &LFSObject{
  123. RepoID: 1,
  124. OID: "ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f",
  125. Size: 100,
  126. Storage: lfsutil.StorageLocal,
  127. CreatedAt: time.Unix(1588568886, 0).UTC(),
  128. },
  129. &LFSObject{
  130. RepoID: 2,
  131. OID: "ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f",
  132. Size: 100,
  133. Storage: lfsutil.StorageLocal,
  134. CreatedAt: time.Unix(1588568886, 0).UTC(),
  135. },
  136. &LoginSource{
  137. Type: auth.PAM,
  138. Name: "My PAM",
  139. IsActived: true,
  140. Provider: pam.NewProvider(&pam.Config{
  141. ServiceName: "PAM service",
  142. }),
  143. CreatedUnix: 1588568886,
  144. UpdatedUnix: 1588572486, // 1 hour later
  145. },
  146. &LoginSource{
  147. Type: auth.GitHub,
  148. Name: "GitHub.com",
  149. IsActived: true,
  150. Provider: github.NewProvider(&github.Config{
  151. APIEndpoint: "https://api.github.com",
  152. }),
  153. CreatedUnix: 1588568886,
  154. },
  155. }
  156. for _, val := range vals {
  157. err := db.Create(val).Error
  158. require.NoError(t, err)
  159. }
  160. }
  161. func dumpTables(t *testing.T, db *gorm.DB) {
  162. ctx := context.Background()
  163. for _, table := range Tables {
  164. tableName := getTableType(table)
  165. var buf bytes.Buffer
  166. err := dumpTable(ctx, db, table, &buf)
  167. if err != nil {
  168. t.Fatalf("%s: %v", tableName, err)
  169. }
  170. golden := filepath.Join("testdata", "backup", tableName+".golden.json")
  171. testutil.AssertGolden(t, golden, testutil.Update("TestDumpAndImport"), buf.String())
  172. }
  173. }
  174. func importTables(t *testing.T, db *gorm.DB) {
  175. ctx := context.Background()
  176. for _, table := range Tables {
  177. tableName := getTableType(table)
  178. err := func() error {
  179. golden := filepath.Join("testdata", "backup", tableName+".golden.json")
  180. f, err := os.Open(golden)
  181. if err != nil {
  182. return errors.Wrap(err, "open table file")
  183. }
  184. defer func() { _ = f.Close() }()
  185. return importTable(ctx, db, table, f)
  186. }()
  187. if err != nil {
  188. t.Fatalf("%s: %v", tableName, err)
  189. }
  190. }
  191. }