diff --git a/.gitignore b/.gitignore index b25c15b..127db4a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ *~ +*.pickle +*.line diff --git a/cmd/bucky-pickle-relay/main_test.go b/cmd/bucky-pickle-relay/main_test.go new file mode 100644 index 0000000..a644438 --- /dev/null +++ b/cmd/bucky-pickle-relay/main_test.go @@ -0,0 +1,73 @@ +package main + +import ( + "fmt" + "io/ioutil" + "os" + "os/exec" + "strings" + "testing" +) + +func readfile(s string) ([]byte, error) { + f, err := os.Open(s) + if err != nil { + return nil, err + } + + blob, err := ioutil.ReadAll(f) + f.Close() + return blob, err +} + +func TestUnMarshal(t *testing.T) { + cmd := exec.Command("testdata/gentests.py") + err := cmd.Run() + if err != nil { + t.Fatal(err) + } + + files, err := ioutil.ReadDir(".") + if err != nil { + t.Fatal(err) + } + + testcases := 0 + for _, file := range files { + if !strings.HasSuffix(file.Name(), ".pickle") { + continue + } + + testcases++ + base := file.Name()[:len(file.Name())-7] + t.Logf("Testing %s", base) + pickle, err := readfile(fmt.Sprintf("%s.pickle", base)) + if err != nil { + t.Error(err) + continue + } + blob, err := readfile(fmt.Sprintf("%s.line", base)) + if err != nil { + t.Error(err) + continue + } + lines := strings.Split(string(blob), "\n") + + // Actually run our code + decode := decodePickle(pickle) + + // Validation + for i, s := range decode { + if s != strings.TrimSpace(lines[i]) { + t.Errorf("Graphite line strings don't match: %s != %s", + s, strings.TrimSpace(lines[i])) + } + } + os.Remove(fmt.Sprintf("%s.pickle", base)) + os.Remove(fmt.Sprintf("%s.line", base)) + } + + if testcases == 0 { + t.Fatal("No testcases generated by testdata/gentests.py") + } +} diff --git a/cmd/bucky-pickle-relay/testdata/gentests.py b/cmd/bucky-pickle-relay/testdata/gentests.py new file mode 100755 index 0000000..b10c921 --- /dev/null +++ b/cmd/bucky-pickle-relay/testdata/gentests.py @@ -0,0 +1,66 @@ +#!/usr/bin/python + +import pickle +import time + +from types import FloatType + +def ts(): + return time.time() + +data = { + "invalid.000": { "foo": "bar" }, + "invalid.001": [ "string" ], + "invalid.002": [ [ 5, ( ts(), 42 ) ] ], + + "test.001" : [ [ "test.001", [ ts(), 42 ] ], + [ "test.002", [ ts(), 43 ] ], + [ "test.003", ( ts(), 44 ) ], + ( "test.004", ( ts(), 45 ) ), + ], + + "test.002" : [ [ "test.001", [ str(ts()), 42 ] ], + [ "test.002", [ ts(), "43" ] ], + [ "test.003", ( str(ts()), "44" ) ], + ( "test.004", ( str(ts()), "3.14159265358979323846264338327950288419716939937510" ) ), + ], + + "test.003" : [ [ "test.001", [ int(ts()), 42 ] ], + [ "test.002", [ int(ts()), 43 ] ], + [ "test.003", ( int(ts()), 44 ) ], + ( "test.004", ( int(ts()), 45 ) ), + ], + + "test.004" : [ [ "test.001", [ ts(), 42.3456 ] ], + [ "test.002", [ ts(), 3.14159265358979323846 ] ], + [ "test.003", ( ts(), 2.71828 ) ], + ( "test.004", ( ts(), -9.7 ) ), + ], + + "test.005" : [ [ "test.001", [ ts(), (1<<64) - 1 ] ], + [ "test.002", [ ts(), (1<<64) + 0 ] ], + [ "test.003", ( ts(), (1<<64) + 1 ) ], + ( "test.004", ( ts(), (1<<64) + 2 ) ), + ], + +} + +if __name__ == "__main__": + for key in data.keys(): + with open("%s.pickle" % key, 'w') as fd: + pickle.dump(data[key], fd) + with open("%s.line" % key, 'w') as fd: + if type(data[key]) != type([]) and type(data[key]) != type(()): + continue + for l in data[key]: + try: + if type(l[1][0]) == FloatType: + l[1][0] = "%.12f" % l[1][0] + if type(l[1][1]) == FloatType: + l[1][1] = "%.12f" % l[1][1] + if type(l[0]) == type(""): + s = "%s %s %s\n" % (l[0], l[1][1], l[1][0]) + fd.write(s) + except: + pass +