Skip to content

Commit

Permalink
test: delete existing config at end of test run
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammed90 authored Jun 20, 2024
1 parent c2ccf86 commit bdb5225
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions caddytest/caddytest.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ func (tc *Tester) initServer(rawConfig string, configType string) error {
_ = json.Indent(&out, body, "", " ")
tc.t.Logf("----------- failed with config -----------\n%s", out.String())
}
tc.t.Log("deleting existing config in test cleanup")
deleteRequest, _ := http.NewRequest(http.MethodDelete, fmt.Sprintf("http://localhost:%d/config/", Default.AdminPort), nil)
res, err := tc.Client.Do(deleteRequest)
if err != nil {
tc.t.Logf("failed to delete existing config: %s", err)
}
if res != nil {
_, _ = io.Copy(io.Discard, res.Body)
res.Body.Close()
}
})

rawConfig = prependCaddyFilePath(rawConfig)
Expand Down Expand Up @@ -208,30 +218,34 @@ func (tc *Tester) ensureConfigRunning(rawConfig string, configType string) error
Timeout: Default.LoadRequestTimeout,
}

fetchConfig := func(client *http.Client) any {
fetchConfig := func(client *http.Client) (any, []byte) {
resp, err := client.Get(fmt.Sprintf("http://localhost:%d/config/", Default.AdminPort))
if err != nil {
return nil
return nil, nil
}
defer resp.Body.Close()
actualBytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil
return nil, actualBytes
}
var actual any
err = json.Unmarshal(actualBytes, &actual)
if err != nil {
return nil
return nil, actualBytes
}
return actual
return actual, actualBytes
}

var lastFetchedConf any
var lastFetchedConfBytes []byte
for retries := 10; retries > 0; retries-- {
if reflect.DeepEqual(expected, fetchConfig(client)) {
lastFetchedConf, lastFetchedConfBytes = fetchConfig(client)
if reflect.DeepEqual(expected, lastFetchedConf) {
return nil
}
time.Sleep(1 * time.Second)
}
tc.t.Logf("expected config: %s", expectedBytes)
tc.t.Logf("active config: %s", lastFetchedConfBytes)
tc.t.Errorf("POSTed configuration isn't active")
return errors.New("EnsureConfigRunning: POSTed configuration isn't active")
}
Expand Down

0 comments on commit bdb5225

Please sign in to comment.