Skip to content

Commit

Permalink
Unit tests for bucky-pickle-relay
Browse files Browse the repository at this point in the history
This daemon is a bit valuable in my infrastructure, let's make sure
changes don't affect its output.
  • Loading branch information
jjneely committed Jun 21, 2017
1 parent 15618fb commit b850dd1
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -1 +1,3 @@
*~
*.pickle
*.line
73 changes: 73 additions & 0 deletions 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")
}
}
66 changes: 66 additions & 0 deletions 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

0 comments on commit b850dd1

Please sign in to comment.