@marcel91200 / caddy Mirror · read-only / commits / 0aefa7b0
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 05:15 UTC

ci: deflake integration tests (#3966)

* ci: deflake integration tests by pulling Caddy for the running config until new config is loaded

Mohammed Al Sahaf committed Feb 5, 2021 at 18:36 UTC 0aefa7b0478f3a16f33d386b9de0167ed8cf7e2a
1 file changed +55 -19
caddytest/caddytest.go
+55 -19
@@ -14,6 +14,7 @@ import (
14 "net/http/cookiejar"
15 "os"
16 "path"
17 + "reflect"
18 "regexp"
19 "runtime"
20 "strings"
@@ -98,6 +99,10 @@ func (tc *Tester) InitServer(rawConfig string, configType string) {
99 tc.t.Logf("failed to load config: %s", err)
100 tc.t.Fail()
101 }
102 + if err := tc.ensureConfigRunning(rawConfig, configType); err != nil {
103 + tc.t.Logf("failed ensurng config is running: %s", err)
104 + tc.t.Fail()
105 + }
106 }
107
108 // InitServer this will configure the server with a configurion of a specific
@@ -171,20 +176,57 @@ func (tc *Tester) initServer(rawConfig string, configType string) error {
176 return nil
177 }
178
174 -var hasValidated bool
175 -var arePrerequisitesValid bool
179 +func (tc *Tester) ensureConfigRunning(rawConfig string, configType string) error {
180 + expectedBytes := []byte(prependCaddyFilePath(rawConfig))
181 + if configType != "json" {
182 + adapter := caddyconfig.GetAdapter(configType)
183 + if adapter == nil {
184 + return fmt.Errorf("adapter of config type is missing: %s", configType)
185 + }
186 + expectedBytes, _, _ = adapter.Adapt([]byte(rawConfig), nil)
187 + }
188
177 -func validateTestPrerequisites() error {
189 + var expected interface{}
190 + err := json.Unmarshal(expectedBytes, &expected)
191 + if err != nil {
192 + return err
193 + }
194 +
195 + client := &http.Client{
196 + Timeout: Default.LoadRequestTimeout,
197 + }
198
179 - if hasValidated {
180 - if !arePrerequisitesValid {
181 - return errors.New("caddy integration prerequisites failed. see first error")
199 + fetchConfig := func(client *http.Client) interface{} {
200 + resp, err := client.Get(fmt.Sprintf("http://localhost:%d/config/", Default.AdminPort))
201 + if err != nil {
202 + return nil
203 }
183 - return nil
204 + defer resp.Body.Close()
205 + actualBytes, err := ioutil.ReadAll(resp.Body)
206 + if err != nil {
207 + return nil
208 + }
209 + var actual interface{}
210 + err = json.Unmarshal(actualBytes, &actual)
211 + if err != nil {
212 + return nil
213 + }
214 + return actual
215 + }
216 +
217 + for retries := 4; retries > 0; retries-- {
218 + if reflect.DeepEqual(expected, fetchConfig(client)) {
219 + return nil
220 + }
221 + time.Sleep(10 * time.Millisecond)
222 }
223 + tc.t.Errorf("POSTed configuration isn't active")
224 + return errors.New("EnsureConfigRunning: POSTed configuration isn't active")
225 +}
226
186 - hasValidated = true
187 - arePrerequisitesValid = false
227 +// validateTestPrerequisites ensures the certificates are available in the
228 +// designated path and Caddy sub-process is running.
229 +func validateTestPrerequisites() error {
230
231 // check certificates are found
232 for _, certName := range Default.Certifcates {
@@ -200,20 +242,14 @@ func validateTestPrerequisites() error {
242 caddycmd.Main()
243 }()
244
203 - // wait for caddy to start
204 - retries := 4
205 - for ; retries > 0 && isCaddyAdminRunning() != nil; retries-- {
245 + // wait for caddy to start serving the initial config
246 + for retries := 4; retries > 0 && isCaddyAdminRunning() != nil; retries-- {
247 time.Sleep(10 * time.Millisecond)
248 }
249 }
250
210 - // assert that caddy is running
211 - if err := isCaddyAdminRunning(); err != nil {
212 - return err
213 - }
214 -
215 - arePrerequisitesValid = true
216 - return nil
251 + // one more time to return the error
252 + return isCaddyAdminRunning()
253 }
254
255 func isCaddyAdminRunning() error {