11package server
22
33import (
4+ "encoding/json"
45 "net/http"
56 "net/http/httptest"
7+ "os"
68 "path/filepath"
79 "strings"
810 "testing"
@@ -207,7 +209,7 @@ func TestRouter_UpdatingOptions(t *testing.T) {
207209 assert .Empty (t , body )
208210}
209211
210- func TestRouter_DeploymmentsWithErrorsDoNotUpdateService (t * testing.T ) {
212+ func TestRouter_DeploymentsWithErrorsDoNotUpdateService (t * testing.T ) {
211213 router := testRouter (t )
212214 _ , target := testBackend (t , "first" , http .StatusOK )
213215
@@ -220,21 +222,43 @@ func TestRouter_DeploymmentsWithErrorsDoNotUpdateService(t *testing.T) {
220222 serviceOptions := defaultServiceOptions
221223 serviceOptions .Hosts = []string {"example.com" }
222224
225+ assert .NoFileExists (t , router .statePath )
223226 require .NoError (t , router .DeployService ("service1" , []string {target }, defaultEmptyReaders , serviceOptions , defaultTargetOptions , DefaultDeployTimeout , DefaultDrainTimeout ))
224227 ensureServiceIsHealthy ()
228+ require .FileExists (t , router .statePath )
229+
230+ ensureStateWasNotSaved := func () {
231+ f , err := os .OpenFile (router .statePath , os .O_RDONLY , 0600 )
232+ require .NoError (t , err )
233+
234+ var services []* Service
235+ require .NoError (t , json .NewDecoder (f ).Decode (& services ), "if this test failed it means an invalid config prevents the system from booting" )
236+
237+ sm := NewServiceMap ()
238+ for _ , service := range services {
239+ sm .Set (service )
240+ }
241+ persistedOptions := sm .Get ("service1" ).options
242+ assert .Equal (t , serviceOptions .TLSPrivateKeyPath , persistedOptions .TLSPrivateKeyPath )
243+ assert .Equal (t , serviceOptions .TLSCertificatePath , persistedOptions .TLSCertificatePath )
244+ assert .Equal (t , serviceOptions .TLSEnabled , persistedOptions .TLSEnabled )
245+ assert .Equal (t , serviceOptions .ErrorPagePath , persistedOptions .ErrorPagePath )
246+ }
225247
226248 t .Run ("custom TLS that is not valid" , func (t * testing.T ) {
227- serviceOptions := ServiceOptions {TLSEnabled : true , TLSCertificatePath : "not valid" , TLSPrivateKeyPath : "not valid" }
228- require .Error (t , router .DeployService ("service1" , []string {target }, defaultEmptyReaders , serviceOptions , defaultTargetOptions , DefaultDeployTimeout , DefaultDrainTimeout ))
249+ newServiceOptions := ServiceOptions {TLSEnabled : true , TLSCertificatePath : "not valid" , TLSPrivateKeyPath : "not valid" }
250+ require .Error (t , router .DeployService ("service1" , []string {target }, defaultEmptyReaders , newServiceOptions , defaultTargetOptions , DefaultDeployTimeout , DefaultDrainTimeout ))
229251
230252 ensureServiceIsHealthy ()
253+ ensureStateWasNotSaved ()
231254 })
232255
233256 t .Run ("custom error pages that are not valid" , func (t * testing.T ) {
234- serviceOptions := ServiceOptions {ErrorPagePath : "not valid" }
235- require .Error (t , router .DeployService ("service1" , []string {target }, defaultEmptyReaders , serviceOptions , defaultTargetOptions , DefaultDeployTimeout , DefaultDrainTimeout ))
257+ newServiceOptions := ServiceOptions {ErrorPagePath : "not valid" }
258+ require .Error (t , router .DeployService ("service1" , []string {target }, defaultEmptyReaders , newServiceOptions , defaultTargetOptions , DefaultDeployTimeout , DefaultDrainTimeout ))
236259
237260 ensureServiceIsHealthy ()
261+ ensureStateWasNotSaved ()
238262 })
239263}
240264
0 commit comments