Skip to content

Commit

Permalink
Change curls for Go http client, and check http status code
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Diaz authored and gdiazlo committed Feb 26, 2019
1 parent 14f46ae commit 45b8d80
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 60 deletions.
35 changes: 8 additions & 27 deletions tests/e2e/agents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"fmt"
"io/ioutil"
"net/http"
"os/exec"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -123,17 +122,10 @@ func TestAgents(t *testing.T) {
})

let("Tamper 1st event", func(t *testing.T) {
cmd := exec.Command("curl",
"-sS",
"-XDELETE",
"-H", fmt.Sprintf("Api-Key:%s", APIKey),
"-H", "Content-type: application/json",
QEDTamperURL,
"-d", fmt.Sprintf(`{"Digest": "%X"}`, hashing.NewSha256Hasher().Do(hashing.Digest(event))),
)

_, err := cmd.CombinedOutput()
assert.NoError(t, err, "Subprocess must not exit with status 1")
buff := strings.NewReader(fmt.Sprintf(`{"Digest": "%X"}`, hashing.NewSha256Hasher().Do(hashing.Digest(event))))
resp, err := doReq("DELETE", QEDTamperURL, APIKey, buff)
assert.NoError(t, err)
assert.Equal(t, resp.StatusCode, http.StatusOK, "Server should respond with http status code 200")
})

let("Check Auditor alerts", func(t *testing.T) {
Expand Down Expand Up @@ -165,21 +157,10 @@ func TestAgents(t *testing.T) {
})

let("Tamper 1st event", func(t *testing.T) {
cmd := exec.Command("curl",
"-sS",
"-XPATCH",
"-H", fmt.Sprintf("Api-Key: %s", APIKey),
"-H", "Content-type: application/json",
QEDTamperURL,
"-d", fmt.Sprintf(`{"Digest": "%X","Value": "%X"}`,
hasher.Do(hashing.Digest(event)),
hasher.Do(hashing.Digest(tampered)),
),
)

_, err := cmd.CombinedOutput()

assert.NoError(t, err, "Subprocess must not exit with status 1")
buff := strings.NewReader(fmt.Sprintf(`{"Digest": "%X","Value": "%X"}`, hasher.Do(hashing.Digest(event)), hasher.Do(hashing.Digest(tampered))))
resp, err := doReq("PATCH", QEDTamperURL, APIKey, buff)
assert.NoError(t, err)
assert.Equal(t, resp.StatusCode, http.StatusOK, "Server should respond with http status code 200")
})

let("Add 2nd event", func(t *testing.T) {
Expand Down
42 changes: 9 additions & 33 deletions tests/e2e/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
package e2e

import (
"fmt"
"os/exec"
"net/http"
"testing"
"time"

Expand All @@ -43,47 +42,24 @@ func TestStart(t *testing.T) {

scenario("Test availability of profiling server", func() {
let("Query to expected context", func(t *testing.T) {
cmd := exec.Command("curl",
"--fail",
"-sS",
"-XGET",
"-H", fmt.Sprintf("Api-Key:%s", APIKey),
"-H", "Content-type: application/json",
QEDProfilingURL,
)

_, err := cmd.CombinedOutput()
assert.NoError(t, err, "Subprocess must not exit with non-zero status")
resp, err := doReq("GET", QEDProfilingURL, APIKey, nil)
assert.NoError(t, err)
assert.Equal(t, resp.StatusCode, http.StatusOK, "Server should respond with http status code 200")
})

let("Query to unexpected context", func(t *testing.T) {
cmd := exec.Command("curl",
"--fail",
"-sS",
"-XGET",
"-H", fmt.Sprintf("Api-Key:%s", APIKey),
"-H", "Content-type: application/json",
QEDProfilingURL+"/xD",
)
resp, err := doReq("GET", QEDProfilingURL+"/xD", APIKey, nil)
assert.NoError(t, err)
assert.Equal(t, resp.StatusCode, http.StatusNotFound, "Server should respond with http status code 404")

_, err := cmd.CombinedOutput()
assert.Error(t, err, "Subprocess must exit with non-zero status")
})
})

scenario("Test availability of metrics server", func() {
let("Query metrics endpoint", func(t *testing.T) {
cmd := exec.Command("curl",
"--fail",
"-sS",
"-XGET",
"-H", fmt.Sprintf("Api-Key:%s", APIKey),
"-H", "Content-type: application/json",
QEDMetricsURL,
)

_, err := cmd.CombinedOutput()
resp, err := doReq("GET", QEDMetricsURL, APIKey, nil)
assert.NoError(t, err, "Subprocess must not exit with non-zero status")
assert.Equal(t, resp.StatusCode, http.StatusOK, "Server should respond with http status code 200")
})

})
Expand Down
24 changes: 24 additions & 0 deletions tests/e2e/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ package e2e

import (
"fmt"
"net/http"
"os"
"os/user"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -58,6 +60,28 @@ func delay(duration time.Duration) scope.TestF {
}
}

func doReq(method string, url, apiKey string, payload *strings.Reader) (*http.Response, error) {
var err error
if payload == nil {
payload = strings.NewReader("")
}
req, err := http.NewRequest(method, url, payload)
if err != nil {
return nil, err
}

req.Header.Set("Api-Key", apiKey)
req.Header.Set("Content-Type", "application/json")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return resp, err
}

func newAgent(id int, name string, role member.Type, p gossip.Processor, t *testing.T) *gossip.Agent {
agentConf := gossip.DefaultConfig()
agentConf.NodeName = fmt.Sprintf("%s%d", name, id)
Expand Down

0 comments on commit 45b8d80

Please sign in to comment.