@@ -16,23 +16,28 @@ import (
1616 "github.com/GyeongHoKim/onvif-simulator/internal/simulator"
1717)
1818
19- // chdirToTempConfig writes a minimal valid config into a temp dir and chdirs
20- // there so simulator.New can find it.
21- func chdirToTempConfig (t * testing.T ) (cleanup func ()) {
19+ // writeTempConfig writes a minimal valid config into a temp dir and returns
20+ // its absolute path. Callers pass the path to simulator.Options.ConfigPath
21+ // (the explicit override path is the public way for tests to point the
22+ // simulator at a fixture; chdir is no longer relied on).
23+ func writeTempConfig (t * testing.T ) (cfgPath string , cleanup func ()) {
2224 t .Helper ()
2325 dir := t .TempDir ()
2426 cfg := config.Config {
25- Version : 1 ,
27+ Version : config . CurrentVersion ,
2628 Device : config.DeviceConfig {
2729 UUID : "urn:uuid:00000000-0000-4000-8000-000000000001" ,
2830 Manufacturer : "Test" , Model : "SimCam" , Serial : "SN-1" ,
2931 Scopes : []string {"onvif://www.onvif.org/name/test" },
3032 },
31- Network : config.NetworkConfig {HTTPPort : 18081 },
33+ Network : config.NetworkConfig {HTTPPort : 18081 , RTSPPort : 0 },
3234 Media : config.MediaConfig {Profiles : []config.ProfileConfig {{
3335 Name : "main" , Token : "profile_main" ,
34- RTSP : "rtsp://127.0.0.1:8554/main" ,
35- Encoding : "H264" , Width : 1920 , Height : 1080 , FPS : 30 ,
36+ // RTSP retained as a passthrough fallback so this fixture covers
37+ // the back-compat path; a fixture variant with MediaFilePath is
38+ // exercised by the rtsp_test.go integration cases.
39+ MediaFilePath : "" ,
40+ Encoding : "H264" , Width : 1920 , Height : 1080 , FPS : 30 ,
3641 }}},
3742 Events : config.EventsConfig {
3843 Topics : []config.TopicConfig {
@@ -48,24 +53,18 @@ func chdirToTempConfig(t *testing.T) (cleanup func()) {
4853 if err != nil {
4954 t .Fatalf ("marshal: %v" , err )
5055 }
51- if writeErr := os .WriteFile (filepath .Join (dir , config .FileName ), data , 0o600 ); writeErr != nil {
56+ cfgPath = filepath .Join (dir , config .FileName )
57+ if writeErr := os .WriteFile (cfgPath , data , 0o600 ); writeErr != nil {
5258 t .Fatalf ("write: %v" , writeErr )
5359 }
54- prev , getErr := os .Getwd ()
55- if getErr != nil {
56- t .Fatalf ("getwd: %v" , getErr )
57- }
58- if chErr := os .Chdir (dir ); chErr != nil {
59- t .Fatalf ("chdir: %v" , chErr )
60- }
61- return func () { _ = os .Chdir (prev ) } //nolint:errcheck // best-effort restore.
60+ return cfgPath , func () { config .SetPath ("" ) }
6261}
6362
6463func TestControlServerEventsAndShutdown (t * testing.T ) {
65- cleanup := chdirToTempConfig (t )
64+ cfgPath , cleanup := writeTempConfig (t )
6665 defer cleanup ()
6766
68- sim , err := simulator .New (simulator.Options {EventBufferSize : 16 })
67+ sim , err := simulator .New (simulator.Options {EventBufferSize : 16 , ConfigPath : cfgPath })
6968 if err != nil {
7069 t .Fatalf ("simulator.New: %v" , err )
7170 }
@@ -118,10 +117,10 @@ func TestControlServerEventsAndShutdown(t *testing.T) {
118117}
119118
120119func TestControlServerRejectsGET (t * testing.T ) {
121- cleanup := chdirToTempConfig (t )
120+ cfgPath , cleanup := writeTempConfig (t )
122121 defer cleanup ()
123122
124- sim , err := simulator .New (simulator.Options {})
123+ sim , err := simulator .New (simulator.Options {ConfigPath : cfgPath })
125124 if err != nil {
126125 t .Fatalf ("simulator.New: %v" , err )
127126 }
@@ -151,10 +150,10 @@ func TestControlServerRejectsGET(t *testing.T) {
151150}
152151
153152func TestControlServerRejectsBadJSON (t * testing.T ) {
154- cleanup := chdirToTempConfig (t )
153+ cfgPath , cleanup := writeTempConfig (t )
155154 defer cleanup ()
156155
157- sim , err := simulator .New (simulator.Options {})
156+ sim , err := simulator .New (simulator.Options {ConfigPath : cfgPath })
158157 if err != nil {
159158 t .Fatalf ("simulator.New: %v" , err )
160159 }
0 commit comments