Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

yolo to stage #367

Merged
merged 1 commit into from
Sep 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/filecache.c
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ static void get_fresh_fd(filecache_t *cache,
// but on the close (dav_flush/release), the PUT fails and the file never makes it to the server.
// On opening again, the server will deliver this unexpected 404. Changes for forensic-haven
// should prevent these errors in the future (2013-08-29)
if (inject_error(filecache_error_fresh404)) response_code = 400;
if (inject_error(filecache_error_fresh400)) response_code = 400;
if (response_code == 304) {
// This should never happen with a well-behaved server.
if (pdata == NULL || inject_error(filecache_error_freshcurl2)) {
Expand Down
6 changes: 3 additions & 3 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,9 @@ static void curl_error_capture_test(void) {
// Sleep 11 seconds between injections
sleep(11);

// flop between filecache_error_fresh404 and no_error
// flop between filecache_error_fresh400 and no_error

if (tdx == no_error) tdx = filecache_error_fresh404;
if (tdx == no_error) tdx = filecache_error_fresh400;
else tdx = no_error;

log_print(LOG_NOTICE, SECTION_UTIL_DEFAULT, "fce: %d Uninjecting %d; injecting %d", inject_error_count, fdx, tdx);
Expand Down Expand Up @@ -253,7 +253,7 @@ static void filecache_forensic_haven_test(void) {
{filecache_error_freshflock2, "filecache_error_freshflock2"},
{filecache_error_freshsession, "filecache_error_freshsession"},
{filecache_error_freshcurl1, "filecache_error_freshcurl1"},
{filecache_error_fresh404, "filecache_error_fresh404"},
{filecache_error_fresh400, "filecache_error_fresh400"},
{filecache_error_freshcurl2, "filecache_error_freshcurl2"},
{filecache_error_freshopen2, "filecache_error_freshopen2"},
{filecache_error_freshpdata, "filecache_error_freshpdata"},
Expand Down
2 changes: 1 addition & 1 deletion src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void *inject_error_mechanism(void *ptr);
#define filecache_error_freshcurl2 35
#define filecache_error_freshopen2 36
#define filecache_error_freshpdata 37
#define filecache_error_fresh404 38
#define filecache_error_fresh400 38
#define filecache_error_opencalloc 39
#define filecache_error_readsdata 40
#define filecache_error_readread 41
Expand Down
11 changes: 11 additions & 0 deletions tests/server-tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
These tests are designed to provide functionality similar to unit tests.
However, they are designed to run directly against a working fileserver.
As such, depending on the requirements of your file server, they require explicit parameters.
Edit the file:
test-server-config.json
to include a binding id, site id, server path, server port, and env.
Then run:
go test
If you run:
go test -v
it will also display certain log messages in addition to errors.
90 changes: 43 additions & 47 deletions tests/server-tests/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package testserver
import (
"crypto/rand"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -33,11 +32,10 @@ type TestOutput struct {
content string
}

func newrequest(file string, method string, body io.Reader) (*http.Request, error) {
func newrequest(file string, method string, body io.Reader, t *testing.T) (*http.Request, error) {
req, err := http.NewRequest(method, file, body)
if err != nil {
// handle err
fmt.Errorf("Error on NewRequest; exiting...")
t.Errorf("Error on NewRequest; exiting...")
return nil, err
}
req.Header.Add("Log-To-Journal", "true")
Expand All @@ -52,9 +50,9 @@ func randstring(len int) string {
return hex.EncodeToString(randBytes)
}

func collectResults(testOutput TestOutput) {
func collectResults(testOutput TestOutput, t *testing.T) {
// TODO figure out how to collect results
fmt.Printf("%v\n", testOutput.result)
t.Logf("%v\n", testOutput.result)
}

func testMethod(t *testing.T, testInput TestInput) error {
Expand All @@ -64,49 +62,48 @@ func testMethod(t *testing.T, testInput TestInput) error {
testOutput TestOutput
)

fmt.Printf("testMethod: %s %d %s\n", testInput.method, testInput.expectedStatusCode, testInput.path)
t.Logf("testMethod: %s %d %s", testInput.method, testInput.expectedStatusCode, testInput.path)
if testInput.method == "PUT" { // only PUT sends content?
req, err = newrequest(testInput.path, testInput.method, strings.NewReader(testInput.content))
req, err = newrequest(testInput.path, testInput.method, strings.NewReader(testInput.content), t)
} else {
req, err = newrequest(testInput.path, testInput.method, nil)
req, err = newrequest(testInput.path, testInput.method, nil, t)
}
if err != nil {
// handle error
t.Errorf("testMethod: %s, Error: %v", testInput.method, err)
return err
}

for _, pair := range testInput.headers {
req.Header.Add(pair.key, pair.value)
fmt.Printf("testMethod: Headers: method: %s; path: %s; header: %v\n", testInput.method, testInput.path, req.Header)
t.Logf("testMethod: Headers: method: %s; path: %s; header: %v", testInput.method, testInput.path, req.Header)
}

defer collectResults(testOutput)
defer collectResults(testOutput, t)
resp, err := testInput.client.Do(req)
if err != nil {
// handle err
t.Errorf("Error on client.Do; method: %s; path: %s; exiting...\n", testInput.method, testInput.path)
t.Errorf("testMethod: Error on client.Do; method: %s; path: %s; exiting...\n", testInput.method, testInput.path)
testOutput.err = err
testOutput.result = ""
return err
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if resp.StatusCode != testInput.expectedStatusCode {
t.Errorf("testMethod: %s; path: %s; Error, expected Status %d, got %v",
testInput.method, testInput.path, testInput.expectedStatusCode, resp.Status)
t.Errorf("testMethod: %s; path: %s; Error, expected Status %d, got %v; body: %v",
testInput.method, testInput.path, testInput.expectedStatusCode, resp.Status, string(body))
}

if testInput.method == "GET" { // Are there other methods which get content. We could genericize
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
// handle error
t.Errorf("testMethod: Error on ioutil.ReadAll: %v", err)
return err
}

if testInput.expectedStatusCode != 404 &&
len(testInput.content) > 0 &&
string(body) != testInput.content {
t.Errorf("testMethod: Error, expected content %v, got %v", testInput.content, resp.Body)
t.Errorf("testMethod: Error, expected content %v, got %v", testInput.content, string(body))
}
}

Expand All @@ -126,17 +123,18 @@ func TestPaths(t *testing.T) {
var testOutput TestOutput
var paths []PathResult

fmt.Println("TestPaths")
t.Log("TestPaths")

testInput.client = getClient()
testInput.client = getClient(t)

paths = make([]PathResult, 7)
paths[0].path = getServerPath()
paths[0].statusCode = 200
paths[1].path = paths[0].path + "sites/"
paths[1].statusCode = 405
paths[2].path = paths[1].path + getSiteId() + "/"
paths[2].statusCode = 404
// Response might be different if there is a trailing slash after site id
paths[2].path = paths[1].path + getSiteId()
paths[2].statusCode = 405
paths[3].path = paths[2].path + "/environments/"
paths[3].statusCode = 405
paths[4].path = paths[3].path + "self/"
Expand All @@ -149,14 +147,12 @@ func TestPaths(t *testing.T) {
testInput.content = "" // content is ignored
testInput.method = "GET"

// for _, method := range []string{"HEAL", "TEST", "LOCK", "PROPPATCH", "OPTIONS"} {
// Temporarily remove HEAL, it takes a long time
for _, entry := range paths {
testInput.path = entry.path
testInput.expectedStatusCode = entry.statusCode
err := testMethod(t, testInput)
if err != nil {
// handle error
t.Errorf("TestPaths: Error on path %v: %v", testInput.path, err)
}
testOutput.result = ""
}
Expand All @@ -166,9 +162,9 @@ func TestAuxiliaryOps(t *testing.T) {
var testInput TestInput
var testOutput TestOutput

fmt.Println("TestAuxiliaryFileOps")
t.Log("TestAuxiliaryFileOps")

testInput.client = getClient()
testInput.client = getClient(t)
filepath := getFilesPath()

dirname := filepath + randstring(8) + "/"
Expand All @@ -183,7 +179,7 @@ func TestAuxiliaryOps(t *testing.T) {
testInput.method = method
err := testMethod(t, testInput)
if err != nil {
// handle error
t.Errorf("TestAuxiliaryOps: Error on method %s on path %v: %v", method, testInput.path, err)
}
testOutput.result = ""
}
Expand All @@ -195,9 +191,9 @@ func TestBasicFileOps(t *testing.T) {
pair HeaderPair
)

fmt.Println("TestBasicFileOps")
t.Log("TestBasicFileOps")

testInput.client = getClient()
testInput.client = getClient(t)
filepath := getFilesPath()

dirname := filepath + randstring(8) + "/"
Expand All @@ -211,7 +207,7 @@ func TestBasicFileOps(t *testing.T) {
err := testMethod(t, testInput)
testInput.expectedStatusCode = 200 // Reset to default
if err != nil {
// handle error
t.Errorf("TestBasicFileOps: Error on method %s on path %v: %v", testInput.method, testInput.path, err)
}

// Add filename to dirname which was used in MKCOL
Expand All @@ -221,7 +217,7 @@ func TestBasicFileOps(t *testing.T) {
testInput.expectedStatusCode = 201
err = testMethod(t, testInput)
if err != nil {
// handle error
t.Errorf("TestBasicFileOps: Error on method %s on path %v: %v", testInput.method, testInput.path, err)
}

// Sort of emulating the way propfind works in fusedav-valhalla world
Expand All @@ -237,15 +233,15 @@ func TestBasicFileOps(t *testing.T) {

err = testMethod(t, testInput)
if err != nil {
// handle error
t.Errorf("TestBasicFileOps: Error on method %s on path %v: %v", testInput.method, testInput.path, err)
}
// PROPFIND on directory
testInput.path = dirname
testInput.method = "PROPFIND"
testInput.expectedStatusCode = 207
err = testMethod(t, testInput)
if err != nil {
// handle error
t.Errorf("TestBasicFileOps: Error on method %s on path %v: %v", testInput.method, testInput.path, err)
}

// PROPFIND on file
Expand All @@ -254,7 +250,7 @@ func TestBasicFileOps(t *testing.T) {
testInput.expectedStatusCode = 207
err = testMethod(t, testInput)
if err != nil {
// handle error
t.Errorf("TestBasicFileOps: Error on method %s on path %v: %v", testInput.method, testInput.path, err)
}

// Clear the headers
Expand All @@ -264,7 +260,7 @@ func TestBasicFileOps(t *testing.T) {
testInput.expectedStatusCode = 200
err = testMethod(t, testInput)
if err != nil {
// handle error
t.Errorf("TestBasicFileOps: Error on method %s on path %v: %v", testInput.method, testInput.path, err)
}

// COPY
Expand All @@ -274,7 +270,7 @@ func TestBasicFileOps(t *testing.T) {
testInput.expectedStatusCode = 200
err = testMethod(t, testInput)
if err != nil {
// handle error
t.Errorf("TestBasicFileOps: Error on method %s on path %v: %v", testInput.method, testInput.path, err)
}

// Sort of cheating. We should use the content we get from GET
Expand All @@ -286,15 +282,15 @@ func TestBasicFileOps(t *testing.T) {
testInput.expectedStatusCode = 201
err = testMethod(t, testInput)
if err != nil {
// handle error
t.Errorf("TestBasicFileOps: Error on method %s on path %v: %v", testInput.method, testInput.path, err)
}

// Get the tofile as part of copy to verify
testInput.method = "GET"
testInput.expectedStatusCode = 200
err = testMethod(t, testInput)
if err != nil {
// handle error
t.Errorf("TestBasicFileOps: Error on method %s on path %v: %v", testInput.method, testInput.path, err)
}
// e COPY

Expand All @@ -309,7 +305,7 @@ func TestBasicFileOps(t *testing.T) {
testInput.expectedStatusCode = 204
err = testMethod(t, testInput)
if err != nil {
// handle error
t.Errorf("TestBasicFileOps: Error on method %s on path %v: %v", testInput.method, testInput.path, err)
}

// Clear the headers
Expand All @@ -321,7 +317,7 @@ func TestBasicFileOps(t *testing.T) {
testInput.expectedStatusCode = 200
err = testMethod(t, testInput)
if err != nil {
// handle error
t.Errorf("TestBasicFileOps: Error on method %s on path %v: %v", testInput.method, testInput.path, err)
}

// Get the original file. It should get a 404
Expand All @@ -330,7 +326,7 @@ func TestBasicFileOps(t *testing.T) {
testInput.expectedStatusCode = 404
err = testMethod(t, testInput)
if err != nil {
// handle error
t.Errorf("TestBasicFileOps: Error on method %s on path %v: %v", testInput.method, testInput.path, err)
}
// e MOVE

Expand All @@ -341,15 +337,15 @@ func TestBasicFileOps(t *testing.T) {
testInput.expectedStatusCode = 204
err = testMethod(t, testInput)
if err != nil {
// handle error
t.Errorf("TestBasicFileOps: Error on method %s on path %v: %v", testInput.method, testInput.path, err)
}

testInput.path = newfile
testInput.method = "DELETE"
testInput.expectedStatusCode = 204
err = testMethod(t, testInput)
if err != nil {
// handle error
t.Errorf("TestBasicFileOps: Error on method %s on path %v: %v", testInput.method, testInput.path, err)
}

testInput.path = filename
Expand All @@ -358,14 +354,14 @@ func TestBasicFileOps(t *testing.T) {
testInput.expectedStatusCode = 204
err = testMethod(t, testInput)
if err != nil {
// handle error
t.Errorf("TestBasicFileOps: Error on method %s on path %v: %v", testInput.method, testInput.path, err)
}

testInput.path = dirname
testInput.method = "DELETE"
testInput.expectedStatusCode = 204
err = testMethod(t, testInput)
if err != nil {
// handle error
t.Errorf("TestBasicFileOps: Error on method %s on path %v: %v", testInput.method, testInput.path, err)
}
}
8 changes: 8 additions & 0 deletions tests/server-tests/base-test-server-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"BindingId": "<bid>",
"SiteId": "<sid>",
"ServerPath": "<path>",
"ServerPort": "<port>",
"Env": "<env>"
}

Loading