version_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package backup
  2. import (
  3. "os"
  4. "path/filepath"
  5. "strings"
  6. "testing"
  7. "github.com/0xJacky/Nginx-UI/internal/version"
  8. "github.com/0xJacky/Nginx-UI/settings"
  9. "github.com/stretchr/testify/assert"
  10. cosysettings "github.com/uozi-tech/cosy/settings"
  11. )
  12. // TestBackupVersion verifies that the backup file contains correct version information
  13. func TestBackupVersion(t *testing.T) {
  14. // Make sure backup files are cleaned up at the start and end of the test
  15. cleanupBackupFiles()
  16. defer cleanupBackupFiles()
  17. // Create test configuration
  18. tempDir, err := os.MkdirTemp("", "nginx-ui-backup-version-test-*")
  19. assert.NoError(t, err)
  20. defer os.RemoveAll(tempDir)
  21. // Create config file
  22. configPath := filepath.Join(tempDir, "config.ini")
  23. testConfig := []byte("[app]\nName = Nginx UI Test\n")
  24. err = os.WriteFile(configPath, testConfig, 0644)
  25. assert.NoError(t, err)
  26. // Create database file
  27. dbName := settings.DatabaseSettings.GetName()
  28. dbFile := dbName + ".db"
  29. dbPath := filepath.Join(tempDir, dbFile)
  30. testDB := []byte("CREATE TABLE users (id INT, name TEXT);")
  31. err = os.WriteFile(dbPath, testDB, 0644)
  32. assert.NoError(t, err)
  33. // Create nginx directory
  34. nginxConfigDir := filepath.Join(tempDir, "nginx")
  35. err = os.MkdirAll(nginxConfigDir, 0755)
  36. assert.NoError(t, err)
  37. // Create nginx config
  38. testNginxContent := []byte("server {\n listen 80;\n server_name example.com;\n}\n")
  39. err = os.WriteFile(filepath.Join(nginxConfigDir, "nginx.conf"), testNginxContent, 0644)
  40. assert.NoError(t, err)
  41. // Setup test environment
  42. originalConfPath := cosysettings.ConfPath
  43. originalNginxConfigDir := settings.NginxSettings.ConfigDir
  44. cosysettings.ConfPath = configPath
  45. settings.NginxSettings.ConfigDir = nginxConfigDir
  46. // Restore original settings after test
  47. defer func() {
  48. cosysettings.ConfPath = originalConfPath
  49. settings.NginxSettings.ConfigDir = originalNginxConfigDir
  50. }()
  51. // Run backup
  52. result, err := Backup()
  53. assert.NoError(t, err)
  54. assert.NotEmpty(t, result.BackupContent)
  55. assert.NotEmpty(t, result.BackupName)
  56. assert.NotEmpty(t, result.AESKey)
  57. assert.NotEmpty(t, result.AESIv)
  58. // Save backup content to temporary file for restore testing
  59. backupFile := filepath.Join(tempDir, result.BackupName)
  60. err = os.WriteFile(backupFile, result.BackupContent, 0644)
  61. assert.NoError(t, err)
  62. // Decode AES key and IV
  63. key, err := DecodeFromBase64(result.AESKey)
  64. assert.NoError(t, err)
  65. iv, err := DecodeFromBase64(result.AESIv)
  66. assert.NoError(t, err)
  67. // Use the Restore function to extract and verify
  68. restoreDir, err := os.MkdirTemp("", "nginx-ui-restore-version-test-*")
  69. assert.NoError(t, err)
  70. defer os.RemoveAll(restoreDir)
  71. restoreResult, err := Restore(RestoreOptions{
  72. BackupPath: backupFile,
  73. AESKey: key,
  74. AESIv: iv,
  75. RestoreDir: restoreDir,
  76. VerifyHash: true,
  77. RestoreNginx: false,
  78. RestoreNginxUI: false,
  79. })
  80. assert.NoError(t, err)
  81. assert.True(t, restoreResult.HashMatch, "Hash should match")
  82. // Check hash_info.txt file
  83. hashInfoPath := filepath.Join(restoreDir, HashInfoFile)
  84. hashInfoContent, err := os.ReadFile(hashInfoPath)
  85. assert.NoError(t, err)
  86. // Verify version information
  87. versionInfo := version.GetVersionInfo()
  88. expectedVersion := versionInfo.Version
  89. // Check if hash_info.txt contains version info
  90. hashInfoStr := string(hashInfoContent)
  91. t.Logf("Hash info content: %s", hashInfoStr)
  92. assert.True(t, strings.Contains(hashInfoStr, "version: "), "Hash info should contain version field")
  93. // Parse hash_info.txt content
  94. info := parseHashInfo(hashInfoStr)
  95. assert.Equal(t, expectedVersion, info.Version, "Backup version should match current version")
  96. }