backup_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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) != 4 {
  29. t.Fatalf("New table has added (want 4 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. &LFSObject{
  82. RepoID: 1,
  83. OID: "ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f",
  84. Size: 100,
  85. Storage: lfsutil.StorageLocal,
  86. CreatedAt: time.Unix(1588568886, 0).UTC(),
  87. },
  88. &LFSObject{
  89. RepoID: 2,
  90. OID: "ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f",
  91. Size: 100,
  92. Storage: lfsutil.StorageLocal,
  93. CreatedAt: time.Unix(1588568886, 0).UTC(),
  94. },
  95. &LoginSource{
  96. Type: auth.PAM,
  97. Name: "My PAM",
  98. IsActived: true,
  99. Provider: pam.NewProvider(&pam.Config{
  100. ServiceName: "PAM service",
  101. }),
  102. CreatedUnix: 1588568886,
  103. UpdatedUnix: 1588572486, // 1 hour later
  104. },
  105. &LoginSource{
  106. Type: auth.GitHub,
  107. Name: "GitHub.com",
  108. IsActived: true,
  109. Provider: github.NewProvider(&github.Config{
  110. APIEndpoint: "https://api.github.com",
  111. }),
  112. CreatedUnix: 1588568886,
  113. },
  114. }
  115. for _, val := range vals {
  116. err := db.Create(val).Error
  117. require.NoError(t, err)
  118. }
  119. }
  120. func dumpTables(t *testing.T, db *gorm.DB) {
  121. ctx := context.Background()
  122. for _, table := range Tables {
  123. tableName := getTableType(table)
  124. var buf bytes.Buffer
  125. err := dumpTable(ctx, 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("TestDumpAndImport"), buf.String())
  131. }
  132. }
  133. func importTables(t *testing.T, db *gorm.DB) {
  134. ctx := context.Background()
  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(ctx, db, table, f)
  145. }()
  146. if err != nil {
  147. t.Fatalf("%s: %v", tableName, err)
  148. }
  149. }
  150. }