Skip to content

Commit

Permalink
[GRAPH] Fix #158, add triggering files, add tests
Browse files Browse the repository at this point in the history
Based on my research, the panic came from the call to graph.Walk
within graph.Copy.  With help from @BrianHicks, I was able to:

1. fix the bug with new, simpler logic for graph.Copy
2. those hcl files that triggered the race condition
3. add a blackbox test, which works with the hcl files
4. add a benchmark test case for testing parallel copies
  • Loading branch information
sehqlr committed Aug 19, 2016
1 parent a01bc81 commit 10f1733
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 12 deletions.
24 changes: 24 additions & 0 deletions blackbox/test_graph_dependencies.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash
set -eo pipefail

ROOT=$(pwd)

TMP=$(mktemp -d -t converge.graph_dependencies.XXXXXXXXXX)
function finish {
rm -rf $TMP
}
trap finish EXIT

pushd $TMP

for src in ${ROOT}/samples/dependencies/*.hcl; do
b=$(basename $src)
echo "starting repetative graph test for ${b}"
for i in `seq 1 5`; do
${ROOT}/converge graph -l "WARN" ${src} >/dev/null
done
echo "success: no errors generating dependencies for ${b}"
done
echo "success: no errors for any .hcl files"

popd
17 changes: 5 additions & 12 deletions graph/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,19 +422,12 @@ func (g *Graph) RootFirstTransform(ctx context.Context, cb TransformFunc) (*Grap
func (g *Graph) Copy() *Graph {
out := New()

err := g.Walk(
context.Background(),
func(id string, val interface{}) error {
out.Add(id, val)
for _, edge := range g.DownEdges(id) {
out.inner.Connect(edge)
}
for _, v := range g.Vertices() {
out.Add(v, g.Get(v))
}

return nil
},
)
if err != nil {
panic(err)
for _, e := range g.inner.Edges() {
out.inner.Connect(e)
}

return out
Expand Down
9 changes: 9 additions & 0 deletions graph/graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ func BenchmarkAddThenGet(b *testing.B) {
})
}

func BenchmarkCopyParallel(b *testing.B) {
g := graph.New()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
g.Copy()
}
})
}

func TestRemove(t *testing.T) {
// Remove should remove a vertex
t.Parallel()
Expand Down
28 changes: 28 additions & 0 deletions samples/dependencies/cert.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# cert
param "cn" {}
param "name" {}
param "ca_crt" {
default = "ca.crt"
}
param "ca_key" {
default = "ca.key"
}
task "directory" {
check = "test -d ssl"
apply = "mkdir ssl"
}
task "key" {
check = "test -f ssl/{{param `name`}}.key"
apply = "cd ssl; openssl genrsa -out {{param `name`}}.key 2048"
depends = ["task.directory"]
}
task "csr" {
check = "test -f ssl/{{param `name`}}.csr"
apply = "cd ssl; openssl req -new -key {{param `name`}}.key --subj \"/CN={{param `cn`}}\" -out {{param `name`}}.csr"
depends = ["task.key"]
}
task "crt" {
check = "test -f ssl/{{param `name`}}.crt"
apply = "cd ssl; openssl x509 -req -in {{param `name`}}.csr -CA {{param `ca_crt`}} -CAkey {{param `ca_key`}} -CAcreateserial -out {{param `name`}}.crt -days 10000"
depends = ["task.csr"]
}
39 changes: 39 additions & 0 deletions samples/dependencies/k8sCerts.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# create certificates for a Kubernetes cluster
param "master_ip" {}

task "directory" {
check = "test -d ssl"
apply = "mkdir ssl"
}

task "ca.key" {
check = "test -f ssl/ca.key"
apply = "cd ssl; openssl genrsa -out ca.key 2048"

depends = ["task.directory"]
}

task "ca.crt" {
check = "test -f ssl/ca.crt"
apply = "cd ssl; openssl req -x509 -new -nodes -key ca.key -subj \"/CN={{param `master_ip`}}\" -days 10000 -out ca.crt"

depends = ["task.ca.key"]
}

module "cert.hcl" "server" {
params {
cn = "{{param `master_ip`}}"
name = "server"
}

depends = ["task.ca.key", "task.ca.crt"]
}

module "cert.hcl" "kubelet" {
params {
cn = "{{param `master_ip`}}"
name = "kubelet"
}

depends = ["task.ca.key", "task.ca.crt"]
}
21 changes: 21 additions & 0 deletions samples/dependencies/simpleShell.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
param "working-directory" {
default = "/tmp/converge-testing"
}

param "test-file" {
default = "{{param `working-directory`}}/test-file"
}

task "directory" {
check = "[ -d \"{{param `working-directory`}}\" ] && echo present || (echo absent && exit 1)"
apply = "mkdir -p {{param `working-directory`}}"
}

task "touch-file" {
interpreter = "/bin/bash"
check_flags = ["-n"]

check = "cat {{param `test-file`}} | grep \"hello\""
apply = "echo hello > {{param `test-file`}}"
depends = ["task.directory"]
}

0 comments on commit 10f1733

Please sign in to comment.