@@ -5,15 +5,28 @@ package sysinfo
55import (
66 "context"
77 "errors"
8+ "os"
9+ "os/exec"
10+ "path/filepath"
11+ "strings"
812 "testing"
913 "time"
1014
15+ _ "github.com/gogf/gf/contrib/drivers/sqlite/v2"
16+ "github.com/gogf/gf/v2/database/gdb"
17+ "github.com/gogf/gf/v2/frame/g"
18+
1119 "lina-core/internal/service/cachecoord"
1220)
1321
1422const (
1523 // testRuntimeConfigDomain is the sysinfo test projection domain.
1624 testRuntimeConfigDomain cachecoord.Domain = "runtime-config"
25+ // sqliteSysInfoChildEnv marks the isolated child process that owns
26+ // GoFrame's global SQLite database configuration.
27+ sqliteSysInfoChildEnv = "LINA_SQLITE_SYSINFO_CHILD"
28+ // sqliteSysInfoDBEnv stores the temporary SQLite path for the child test.
29+ sqliteSysInfoDBEnv = "LINA_SQLITE_SYSINFO_DB"
1730)
1831
1932// fakeCacheCoordService provides deterministic cachecoord snapshots for
@@ -119,3 +132,60 @@ func TestLoadCacheCoordinationToleratesSnapshotFailure(t *testing.T) {
119132 t .Fatalf ("expected empty diagnostics after snapshot failure, got %#v" , items )
120133 }
121134}
135+
136+ // TestGetDbVersionSupportsSQLite verifies sysinfo does not use MySQL-only
137+ // VERSION() diagnostics against SQLite databases.
138+ func TestGetDbVersionSupportsSQLite (t * testing.T ) {
139+ if os .Getenv (sqliteSysInfoChildEnv ) == "1" {
140+ t .Skip ("parent test only launches the isolated SQLite child process" )
141+ }
142+
143+ dbPath := filepath .Join (t .TempDir (), "sysinfo.db" )
144+ cmd := exec .Command (os .Args [0 ], "-test.run=^TestGetDbVersionSupportsSQLiteChild$" , "-test.count=1" , "-test.v" )
145+ cmd .Env = append (os .Environ (),
146+ sqliteSysInfoChildEnv + "=1" ,
147+ sqliteSysInfoDBEnv + "=" + dbPath ,
148+ )
149+ output , err := cmd .CombinedOutput ()
150+ if err != nil {
151+ t .Fatalf ("SQLite sysinfo child test failed: %v\n %s" , err , string (output ))
152+ }
153+ }
154+
155+ // TestGetDbVersionSupportsSQLiteChild runs the actual SQLite sysinfo
156+ // diagnostic check in an isolated process because GoFrame database config is
157+ // global.
158+ func TestGetDbVersionSupportsSQLiteChild (t * testing.T ) {
159+ if os .Getenv (sqliteSysInfoChildEnv ) != "1" {
160+ t .Skip ("SQLite sysinfo child test is executed by TestGetDbVersionSupportsSQLite" )
161+ }
162+
163+ ctx := context .Background ()
164+ dbPath := os .Getenv (sqliteSysInfoDBEnv )
165+ if dbPath == "" {
166+ t .Fatalf ("%s must be set" , sqliteSysInfoDBEnv )
167+ }
168+ link := "sqlite::@file(" + dbPath + ")"
169+ if err := gdb .SetConfig (gdb.Config {
170+ gdb .DefaultGroupName : gdb.ConfigGroup {{Link : link }},
171+ }); err != nil {
172+ t .Fatalf ("configure SQLite sysinfo database failed: %v" , err )
173+ }
174+ t .Cleanup (func () {
175+ if closeErr := g .DB ().Close (ctx ); closeErr != nil {
176+ t .Errorf ("close SQLite sysinfo database failed: %v" , closeErr )
177+ }
178+ })
179+
180+ service := & serviceImpl {}
181+ version , err := service .getDbVersion (ctx )
182+ if err != nil {
183+ t .Fatalf ("get SQLite sysinfo database version failed: %v" , err )
184+ }
185+ if ! strings .HasPrefix (version , "SQLite " ) {
186+ t .Fatalf ("expected SQLite version label, got %q" , version )
187+ }
188+ if strings .TrimSpace (strings .TrimPrefix (version , "SQLite " )) == "" {
189+ t .Fatalf ("expected SQLite version number to be non-empty, got %q" , version )
190+ }
191+ }
0 commit comments