backup_test.go 3.2 KB

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