@@ -13,63 +13,124 @@ import (
1313 "go.viam.com/utils"
1414)
1515
16+ // Mimics an old server's response to the restart_status HTTP endpoint.
17+ type oldRestartStatusResponse struct {
18+ RestartAllowed bool `json:"restart_allowed"`
19+ }
20+
21+ // Ensures that checkRestartProperty works correctly for restart_allowed and
22+ // does_not_handle_needs_restart against a fake viamserver instance (HTTP server).
1623func TestCheckRestartProperty (t * testing.T ) {
1724 logger := logging .NewTestLogger (t )
1825 ctx := context .Background ()
1926
20- expectedRestartStatusResponse := RestartStatusResponse {
21- RestartAllowed : false ,
22- DoesNotHandleNeedsRestart : true ,
23- }
24-
25- // Set up an HTTP server that reports the expected RestartStatusResponse at the address
26- // http://localhost:8080/restart_status.
27- mux := http .NewServeMux ()
28- mux .HandleFunc ("/restart_status" , func (w http.ResponseWriter , _ * http.Request ) {
29- w .Header ().Set ("Content-Type" , "application/json" )
30- test .That (t , json .NewEncoder (w ).Encode (expectedRestartStatusResponse ), test .ShouldBeNil )
31- })
32- // Use NewPossiblySecureHTTPServer to mimic RDK's behavior.
33- httpServer , err := utils .NewPossiblySecureHTTPServer (mux , utils.HTTPServerOptions {
34- Secure : false ,
35- Addr : "localhost:8080" ,
36- })
37- test .That (t , err , test .ShouldBeNil )
38- ln , err := net .Listen ("tcp" , "localhost:8080" )
39- test .That (t , err , test .ShouldBeNil )
40- var wg sync.WaitGroup
41- wg .Add (1 )
42- go func () {
43- defer wg .Done ()
44- err := httpServer .Serve (ln )
45- if err != nil {
46- println (err .Error ())
47- }
48- }()
49- t .Cleanup (func () {
50- test .That (t , httpServer .Shutdown (ctx ), test .ShouldBeNil )
51- wg .Wait ()
52- })
53-
54- // Create a new viamserver object with mostly empty fields except for checkURL and
55- // checkURLAlt.
27+ targetAddr := "localhost:8080"
5628 s := & viamServer {
57- logger : logger ,
58- checkURL : "http://localhost:8080" ,
29+ logger : logger ,
30+ // checkURL will normally be the .cloud address of the machine; use localhost instead
31+ // here.
32+ checkURL : "http://" + targetAddr ,
33+ // checkURLAlt is always 127.0.0.1:[bind-port] in agent code.
5934 checkURLAlt : "http://127.0.0.1:8080" ,
6035 }
6136
62- // Run checkRestartProperty on the viamserver object and ensure that expected values are
63- // returned with no errors.
64- s .mu .Lock ()
65- restartAllowed , err := s .checkRestartProperty (ctx , RestartPropertyRestartAllowed )
66- s .mu .Unlock ()
67- test .That (t , err , test .ShouldBeNil )
68- test .That (t , restartAllowed , test .ShouldEqual , expectedRestartStatusResponse .RestartAllowed )
37+ falseVal := false
38+ trueVal := true
39+ testCases := []struct {
40+ name string
41+ expectedRestartAllowed bool
42+ // Can be unset (mimic old server), false, and true.
43+ expectedDoesNotHandleNeedsRestart * bool
44+ }{
45+ {
46+ "restart_allowed=false;does_not_handle_needs_restart=unset" ,
47+ false ,
48+ nil ,
49+ },
50+ {
51+ "restart_allowed=false;does_not_handle_needs_restart=false" ,
52+ false ,
53+ & falseVal ,
54+ },
55+ {
56+ "restart_allowed=false;does_not_handle_needs_restart=true" ,
57+ false ,
58+ & trueVal ,
59+ },
60+ {
61+ "restart_allowed=true;does_not_handle_needs_restart=unset" ,
62+ true ,
63+ nil ,
64+ },
65+ {
66+ "restart_allowed=true;does_not_handle_needs_restart=false" ,
67+ true ,
68+ nil ,
69+ },
70+ {
71+ "restart_allowed=true;does_not_handle_needs_restart=true" ,
72+ true ,
73+ & trueVal ,
74+ },
75+ }
76+
77+ for _ , tc := range testCases {
78+ t .Run (tc .name , func (t * testing.T ) {
79+ var expectedRestartStatusResponse any
80+ if tc .expectedDoesNotHandleNeedsRestart != nil {
81+ expectedRestartStatusResponse = RestartStatusResponse {
82+ RestartAllowed : tc .expectedRestartAllowed ,
83+ DoesNotHandleNeedsRestart : * tc .expectedDoesNotHandleNeedsRestart ,
84+ }
85+ } else {
86+ expectedRestartStatusResponse = oldRestartStatusResponse {
87+ RestartAllowed : tc .expectedRestartAllowed ,
88+ }
89+ }
90+
91+ mux := http .NewServeMux ()
92+ mux .HandleFunc ("/restart_status" , func (w http.ResponseWriter , _ * http.Request ) {
93+ w .Header ().Set ("Content-Type" , "application/json" )
94+ test .That (t , json .NewEncoder (w ).Encode (expectedRestartStatusResponse ), test .ShouldBeNil )
95+ })
96+ // Use NewPossiblySecureHTTPServer to mimic RDK's behavior.
97+ httpServer , err := utils .NewPossiblySecureHTTPServer (mux , utils.HTTPServerOptions {
98+ Secure : false ,
99+ Addr : targetAddr ,
100+ })
101+ test .That (t , err , test .ShouldBeNil )
102+ ln , err := net .Listen ("tcp" , targetAddr )
103+ test .That (t , err , test .ShouldBeNil )
104+ var wg sync.WaitGroup
105+ wg .Add (1 )
106+ go func () {
107+ defer wg .Done ()
108+ err := httpServer .Serve (ln )
109+ // Should be "server closed" due to Shutdown below.
110+ test .That (t , err , test .ShouldBeError , http .ErrServerClosed )
111+ }()
112+ t .Cleanup (func () {
113+ test .That (t , httpServer .Shutdown (ctx ), test .ShouldBeNil )
114+ wg .Wait ()
115+ })
69116
70- s .mu .Lock ()
71- doesNotHandle , err := s .checkRestartProperty (ctx , RestartPropertyDoesNotHandleNeedsRestart )
72- s .mu .Unlock ()
73- test .That (t , err , test .ShouldBeNil )
74- test .That (t , doesNotHandle , test .ShouldEqual , expectedRestartStatusResponse .DoesNotHandleNeedsRestart )
117+ s .mu .Lock ()
118+ restartAllowed , err := s .checkRestartProperty (ctx , RestartPropertyRestartAllowed )
119+ s .mu .Unlock ()
120+ test .That (t , err , test .ShouldBeNil )
121+ test .That (t , restartAllowed , test .ShouldEqual , tc .expectedRestartAllowed )
122+
123+ s .mu .Lock ()
124+ doesNotHandleNeedsRestart , err := s .checkRestartProperty (ctx , RestartPropertyDoesNotHandleNeedsRestart )
125+ s .mu .Unlock ()
126+ test .That (t , err , test .ShouldBeNil )
127+ // does_not_handle_restart should be false if explicitly false or unset in the test
128+ // case.
129+ var expectedDoesNotHandleNeedsRestart bool
130+ if tc .expectedDoesNotHandleNeedsRestart != nil {
131+ expectedDoesNotHandleNeedsRestart = * tc .expectedDoesNotHandleNeedsRestart
132+ }
133+ test .That (t , doesNotHandleNeedsRestart , test .ShouldEqual , expectedDoesNotHandleNeedsRestart )
134+ })
135+ }
75136}
0 commit comments