backup_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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) != 6 {
  29. t.Fatalf("New table has added (want 6 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. &Follow{
  123. ID: 1,
  124. UserID: 1,
  125. FollowID: 2,
  126. },
  127. &Follow{
  128. ID: 2,
  129. UserID: 2,
  130. FollowID: 1,
  131. },
  132. &LFSObject{
  133. RepoID: 1,
  134. OID: "ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f",
  135. Size: 100,
  136. Storage: lfsutil.StorageLocal,
  137. CreatedAt: time.Unix(1588568886, 0).UTC(),
  138. },
  139. &LFSObject{
  140. RepoID: 2,
  141. OID: "ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f",
  142. Size: 100,
  143. Storage: lfsutil.StorageLocal,
  144. CreatedAt: time.Unix(1588568886, 0).UTC(),
  145. },
  146. &LoginSource{
  147. Type: auth.PAM,
  148. Name: "My PAM",
  149. IsActived: true,
  150. Provider: pam.NewProvider(&pam.Config{
  151. ServiceName: "PAM service",
  152. }),
  153. CreatedUnix: 1588568886,
  154. UpdatedUnix: 1588572486, // 1 hour later
  155. },
  156. &LoginSource{
  157. Type: auth.GitHub,
  158. Name: "GitHub.com",
  159. IsActived: true,
  160. Provider: github.NewProvider(&github.Config{
  161. APIEndpoint: "https://api.github.com",
  162. }),
  163. CreatedUnix: 1588568886,
  164. },
  165. }
  166. for _, val := range vals {
  167. err := db.Create(val).Error
  168. require.NoError(t, err)
  169. }
  170. }
  171. func dumpTables(t *testing.T, db *gorm.DB) {
  172. ctx := context.Background()
  173. for _, table := range Tables {
  174. tableName := getTableType(table)
  175. var buf bytes.Buffer
  176. err := dumpTable(ctx, db, table, &buf)
  177. if err != nil {
  178. t.Fatalf("%s: %v", tableName, err)
  179. }
  180. golden := filepath.Join("testdata", "backup", tableName+".golden.json")
  181. testutil.AssertGolden(t, golden, testutil.Update("TestDumpAndImport"), buf.String())
  182. }
  183. }
  184. func importTables(t *testing.T, db *gorm.DB) {
  185. ctx := context.Background()
  186. for _, table := range Tables {
  187. tableName := getTableType(table)
  188. err := func() error {
  189. golden := filepath.Join("testdata", "backup", tableName+".golden.json")
  190. f, err := os.Open(golden)
  191. if err != nil {
  192. return errors.Wrap(err, "open table file")
  193. }
  194. defer func() { _ = f.Close() }()
  195. return importTable(ctx, db, table, f)
  196. }()
  197. if err != nil {
  198. t.Fatalf("%s: %v", tableName, err)
  199. }
  200. }
  201. }