summaryrefslogtreecommitdiff
path: root/caddytest/caddytest.go
diff options
context:
space:
mode:
authorMohammed Al Sahaf <msaa1990@gmail.com>2021-02-05 18:36:52 +0300
committerGitHub <noreply@github.com>2021-02-05 15:36:52 +0000
commit0aefa7b0478f3a16f33d386b9de0167ed8cf7e2a (patch)
treebc27426e9c8357c3ef0b5eb2fbf3b13cb90cc5f3 /caddytest/caddytest.go
parent8c291298c9241cd2d130f273b25f806687ce5e87 (diff)
ci: deflake integration tests (#3966)
* ci: deflake integration tests by pulling Caddy for the running config until new config is loaded
Diffstat (limited to 'caddytest/caddytest.go')
-rw-r--r--caddytest/caddytest.go74
1 files changed, 55 insertions, 19 deletions
diff --git a/caddytest/caddytest.go b/caddytest/caddytest.go
index b3896e7..094e45c 100644
--- a/caddytest/caddytest.go
+++ b/caddytest/caddytest.go
@@ -14,6 +14,7 @@ import (
"net/http/cookiejar"
"os"
"path"
+ "reflect"
"regexp"
"runtime"
"strings"
@@ -98,6 +99,10 @@ func (tc *Tester) InitServer(rawConfig string, configType string) {
tc.t.Logf("failed to load config: %s", err)
tc.t.Fail()
}
+ if err := tc.ensureConfigRunning(rawConfig, configType); err != nil {
+ tc.t.Logf("failed ensurng config is running: %s", err)
+ tc.t.Fail()
+ }
}
// 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 {
return nil
}
-var hasValidated bool
-var arePrerequisitesValid bool
+func (tc *Tester) ensureConfigRunning(rawConfig string, configType string) error {
+ expectedBytes := []byte(prependCaddyFilePath(rawConfig))
+ if configType != "json" {
+ adapter := caddyconfig.GetAdapter(configType)
+ if adapter == nil {
+ return fmt.Errorf("adapter of config type is missing: %s", configType)
+ }
+ expectedBytes, _, _ = adapter.Adapt([]byte(rawConfig), nil)
+ }
-func validateTestPrerequisites() error {
+ var expected interface{}
+ err := json.Unmarshal(expectedBytes, &expected)
+ if err != nil {
+ return err
+ }
+
+ client := &http.Client{
+ Timeout: Default.LoadRequestTimeout,
+ }
- if hasValidated {
- if !arePrerequisitesValid {
- return errors.New("caddy integration prerequisites failed. see first error")
+ fetchConfig := func(client *http.Client) interface{} {
+ resp, err := client.Get(fmt.Sprintf("http://localhost:%d/config/", Default.AdminPort))
+ if err != nil {
+ return nil
}
- return nil
+ defer resp.Body.Close()
+ actualBytes, err := ioutil.ReadAll(resp.Body)
+ if err != nil {
+ return nil
+ }
+ var actual interface{}
+ err = json.Unmarshal(actualBytes, &actual)
+ if err != nil {
+ return nil
+ }
+ return actual
+ }
+
+ for retries := 4; retries > 0; retries-- {
+ if reflect.DeepEqual(expected, fetchConfig(client)) {
+ return nil
+ }
+ time.Sleep(10 * time.Millisecond)
}
+ tc.t.Errorf("POSTed configuration isn't active")
+ return errors.New("EnsureConfigRunning: POSTed configuration isn't active")
+}
- hasValidated = true
- arePrerequisitesValid = false
+// validateTestPrerequisites ensures the certificates are available in the
+// designated path and Caddy sub-process is running.
+func validateTestPrerequisites() error {
// check certificates are found
for _, certName := range Default.Certifcates {
@@ -200,20 +242,14 @@ func validateTestPrerequisites() error {
caddycmd.Main()
}()
- // wait for caddy to start
- retries := 4
- for ; retries > 0 && isCaddyAdminRunning() != nil; retries-- {
+ // wait for caddy to start serving the initial config
+ for retries := 4; retries > 0 && isCaddyAdminRunning() != nil; retries-- {
time.Sleep(10 * time.Millisecond)
}
}
- // assert that caddy is running
- if err := isCaddyAdminRunning(); err != nil {
- return err
- }
-
- arePrerequisitesValid = true
- return nil
+ // one more time to return the error
+ return isCaddyAdminRunning()
}
func isCaddyAdminRunning() error {