backup_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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/lfsutil"
  20. "gogs.io/gogs/internal/testutil"
  21. )
  22. func TestDumpAndImport(t *testing.T) {
  23. if testing.Short() {
  24. t.Skip()
  25. }
  26. t.Parallel()
  27. if len(Tables) != 4 {
  28. t.Fatalf("New table has added (want 4 got %d), please add new tests for the table and update this check", len(Tables))
  29. }
  30. db := initTestDB(t, "dumpAndImport", Tables...)
  31. setupDBToDump(t, db)
  32. dumpTables(t, db)
  33. importTables(t, db)
  34. // Dump and assert golden again to make sure data aren't changed.
  35. dumpTables(t, db)
  36. }
  37. func setupDBToDump(t *testing.T, db *gorm.DB) {
  38. vals := []interface{}{
  39. &Access{
  40. ID: 1,
  41. UserID: 1,
  42. RepoID: 11,
  43. Mode: AccessModeRead,
  44. },
  45. &Access{
  46. ID: 2,
  47. UserID: 2,
  48. RepoID: 22,
  49. Mode: AccessModeWrite,
  50. },
  51. &AccessToken{
  52. UserID: 1,
  53. Name: "test1",
  54. Sha1: cryptoutil.SHA1("2910d03d-c0b5-4f71-bad5-c4086e4efae3"),
  55. SHA256: cryptoutil.SHA256(cryptoutil.SHA1("2910d03d-c0b5-4f71-bad5-c4086e4efae3")),
  56. CreatedUnix: 1588568886,
  57. UpdatedUnix: 1588572486, // 1 hour later
  58. },
  59. &AccessToken{
  60. UserID: 1,
  61. Name: "test2",
  62. Sha1: cryptoutil.SHA1("84117e17-7e67-4024-bd04-1c23e6e809d4"),
  63. SHA256: cryptoutil.SHA256(cryptoutil.SHA1("84117e17-7e67-4024-bd04-1c23e6e809d4")),
  64. CreatedUnix: 1588568886,
  65. },
  66. &AccessToken{
  67. UserID: 2,
  68. Name: "test1",
  69. Sha1: cryptoutil.SHA1("da2775ce-73dd-47ba-b9d2-bbcc346585c4"),
  70. SHA256: cryptoutil.SHA256(cryptoutil.SHA1("da2775ce-73dd-47ba-b9d2-bbcc346585c4")),
  71. CreatedUnix: 1588568886,
  72. },
  73. &AccessToken{
  74. UserID: 2,
  75. Name: "test2",
  76. Sha1: cryptoutil.SHA256(cryptoutil.SHA1("1b2dccd1-a262-470f-bb8c-7fc73192e9bb"))[:40],
  77. SHA256: cryptoutil.SHA256(cryptoutil.SHA1("1b2dccd1-a262-470f-bb8c-7fc73192e9bb")),
  78. CreatedUnix: 1588568886,
  79. },
  80. &LFSObject{
  81. RepoID: 1,
  82. OID: "ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f",
  83. Size: 100,
  84. Storage: lfsutil.StorageLocal,
  85. CreatedAt: time.Unix(1588568886, 0).UTC(),
  86. },
  87. &LFSObject{
  88. RepoID: 2,
  89. OID: "ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f",
  90. Size: 100,
  91. Storage: lfsutil.StorageLocal,
  92. CreatedAt: time.Unix(1588568886, 0).UTC(),
  93. },
  94. &LoginSource{
  95. Type: auth.PAM,
  96. Name: "My PAM",
  97. IsActived: true,
  98. Provider: pam.NewProvider(&pam.Config{
  99. ServiceName: "PAM service",
  100. }),
  101. CreatedUnix: 1588568886,
  102. UpdatedUnix: 1588572486, // 1 hour later
  103. },
  104. &LoginSource{
  105. Type: auth.GitHub,
  106. Name: "GitHub.com",
  107. IsActived: true,
  108. Provider: github.NewProvider(&github.Config{
  109. APIEndpoint: "https://api.github.com",
  110. }),
  111. CreatedUnix: 1588568886,
  112. },
  113. }
  114. for _, val := range vals {
  115. err := db.Create(val).Error
  116. require.NoError(t, err)
  117. }
  118. }
  119. func dumpTables(t *testing.T, db *gorm.DB) {
  120. ctx := context.Background()
  121. for _, table := range Tables {
  122. tableName := getTableType(table)
  123. var buf bytes.Buffer
  124. err := dumpTable(ctx, db, table, &buf)
  125. if err != nil {
  126. t.Fatalf("%s: %v", tableName, err)
  127. }
  128. golden := filepath.Join("testdata", "backup", tableName+".golden.json")
  129. testutil.AssertGolden(t, golden, testutil.Update("TestDumpAndImport"), buf.String())
  130. }
  131. }
  132. func importTables(t *testing.T, db *gorm.DB) {
  133. ctx := context.Background()
  134. for _, table := range Tables {
  135. tableName := getTableType(table)
  136. err := func() error {
  137. golden := filepath.Join("testdata", "backup", tableName+".golden.json")
  138. f, err := os.Open(golden)
  139. if err != nil {
  140. return errors.Wrap(err, "open table file")
  141. }
  142. defer func() { _ = f.Close() }()
  143. return importTable(ctx, db, table, f)
  144. }()
  145. if err != nil {
  146. t.Fatalf("%s: %v", tableName, err)
  147. }
  148. }
  149. }