sqlite_helper.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package e2e
  2. import (
  3. "database/sql"
  4. "encoding/json"
  5. "os"
  6. "path/filepath"
  7. "strconv"
  8. "testing"
  9. "time"
  10. "github.com/cline/cli/pkg/common"
  11. _ "github.com/glebarez/go-sqlite"
  12. "google.golang.org/grpc/health/grpc_health_v1"
  13. )
  14. // readInstancesFromSQLite reads instances directly from the SQLite database for testing
  15. func readInstancesFromSQLite(t *testing.T, clineDir string) []common.CoreInstanceInfo {
  16. t.Helper()
  17. dbPath := filepath.Join(clineDir, common.SETTINGS_SUBFOLDER, "locks.db")
  18. // Check if database exists
  19. if _, err := os.Stat(dbPath); os.IsNotExist(err) {
  20. return []common.CoreInstanceInfo{}
  21. }
  22. db, err := sql.Open("sqlite", dbPath)
  23. if err != nil {
  24. t.Logf("Warning: Failed to open SQLite database: %v", err)
  25. return []common.CoreInstanceInfo{}
  26. }
  27. defer db.Close()
  28. // Query instance locks
  29. query := common.SelectInstanceLockHoldersAscSQL
  30. rows, err := db.Query(query)
  31. if err != nil {
  32. t.Logf("Warning: Failed to query instance locks: %v", err)
  33. return []common.CoreInstanceInfo{}
  34. }
  35. defer rows.Close()
  36. var instances []common.CoreInstanceInfo
  37. for rows.Next() {
  38. var heldBy, lockTarget string
  39. var lockedAt int64
  40. err := rows.Scan(&heldBy, &lockTarget, &lockedAt)
  41. if err != nil {
  42. t.Logf("Warning: Failed to scan lock row: %v", err)
  43. continue
  44. }
  45. // Create InstanceInfo
  46. info := common.CoreInstanceInfo{
  47. Address: heldBy,
  48. HostServiceAddress: lockTarget,
  49. Status: grpc_health_v1.HealthCheckResponse_UNKNOWN, // Will be updated by health check
  50. LastSeen: time.Unix(lockedAt/1000, 0), // Convert from milliseconds
  51. }
  52. instances = append(instances, info)
  53. }
  54. return instances
  55. }
  56. // readDefaultInstanceFromSettings reads the default instance from the settings file
  57. func readDefaultInstanceFromSettings(t *testing.T, clineDir string) string {
  58. t.Helper()
  59. settingsPath := filepath.Join(clineDir, common.SETTINGS_SUBFOLDER, "settings", "cli-default-instance.json")
  60. data, err := os.ReadFile(settingsPath)
  61. if err != nil {
  62. if os.IsNotExist(err) {
  63. return ""
  64. }
  65. t.Logf("Warning: Failed to read default instance file: %v", err)
  66. return ""
  67. }
  68. var tmp struct {
  69. DefaultInstance string `json:"default_instance"`
  70. }
  71. if err := json.Unmarshal(data, &tmp); err != nil {
  72. t.Logf("Warning: Failed to parse default instance file: %v", err)
  73. return ""
  74. }
  75. return tmp.DefaultInstance
  76. }
  77. // insertRemoteInstanceIntoSQLite inserts a remote instance entry directly into SQLite for testing
  78. func insertRemoteInstanceIntoSQLite(t *testing.T, dbPath, address string, corePort, hostPort int) error {
  79. t.Helper()
  80. db, err := sql.Open("sqlite", dbPath)
  81. if err != nil {
  82. return err
  83. }
  84. defer db.Close()
  85. // Initialize database schema for testing
  86. createTableSQL := `
  87. CREATE TABLE IF NOT EXISTS locks (
  88. id INTEGER PRIMARY KEY,
  89. held_by TEXT NOT NULL,
  90. lock_type TEXT NOT NULL CHECK (lock_type IN ('file', 'instance', 'folder')),
  91. lock_target TEXT NOT NULL,
  92. locked_at INTEGER NOT NULL,
  93. UNIQUE(lock_type, lock_target)
  94. );
  95. `
  96. createIndexesSQL := `
  97. CREATE INDEX IF NOT EXISTS idx_locks_held_by ON locks(held_by);
  98. CREATE INDEX IF NOT EXISTS idx_locks_type ON locks(lock_type);
  99. CREATE INDEX IF NOT EXISTS idx_locks_target ON locks(lock_target);
  100. `
  101. if _, err := db.Exec(createTableSQL); err != nil {
  102. return err
  103. }
  104. if _, err := db.Exec(createIndexesSQL); err != nil {
  105. return err
  106. }
  107. // Insert the remote instance
  108. hostAddress := "remote.example.com:0"
  109. if hostPort != 0 {
  110. hostAddress = "remote.example.com:" + strconv.Itoa(hostPort)
  111. }
  112. insertSQL := `INSERT INTO locks (held_by, lock_type, lock_target, locked_at) VALUES (?, 'instance', ?, ?)`
  113. _, err = db.Exec(insertSQL, address, hostAddress, time.Now().Unix()*1000)
  114. return err
  115. }
  116. // verifyInstanceExistsInSQLite checks if an instance exists in the SQLite database
  117. func verifyInstanceExistsInSQLite(t *testing.T, dbPath, address string) bool {
  118. t.Helper()
  119. db, err := sql.Open("sqlite", dbPath)
  120. if err != nil {
  121. t.Logf("Failed to open database: %v", err)
  122. return false
  123. }
  124. defer db.Close()
  125. query := `SELECT COUNT(*) FROM locks WHERE held_by = ? AND lock_type = 'instance'`
  126. var count int
  127. err = db.QueryRow(query, address).Scan(&count)
  128. if err != nil {
  129. t.Logf("Failed to query database: %v", err)
  130. return false
  131. }
  132. return count > 0
  133. }