Skip to content

Commit

Permalink
First real test
Browse files Browse the repository at this point in the history
- move the sanity test to the top for the compiler to see first
- add a basic identity test that ensures the identities were created

Signed-off-by: Erik Hollensbe <[email protected]>
  • Loading branch information
Erik Hollensbe committed Feb 4, 2021
1 parent 34c9592 commit 8a1c98d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
File renamed without changes.
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-checkpoint v0.5.0/go.mod h1:7nfLNL10NsxqO4iWuW6tWW0HjZuDrwkBuEQsVcpCOgg=
github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-cleanhttp v0.5.1 h1:dH3aiDG9Jvb5r5+bYHsikaOUIpcM0xvgMXVoDkXMzJM=
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 h1:1/D3zfFHttUKaCaGKZ/dR2roBXv0vKbSCnssIldfQdI=
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320/go.mod h1:EiZBMaudVLy8fmjf9Npq1dq9RalhveqZG5w/yz3mHWs=
Expand Down
60 changes: 59 additions & 1 deletion provision_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,64 @@
package main

import "testing"
import (
"testing"

"github.com/erikh/tftest"
)

// these are deliberately named to keep the code small. they do not add
// anything else. Hopefully we won't need this soon.
func h(m interface{}) map[string]interface{} {
return m.(map[string]interface{})
}

func a(m interface{}) []interface{} {
return m.([]interface{})
}

func s(m interface{}) string {
return m.(string)
}

func TestIdentity(t *testing.T) {
type identity struct {
name string
pubkey string
privkey string
}

tf := tftest.New(t)
tf.Apply("testdata/plans/basic-identity.tf")
resources := a(tf.State()["resources"])

ids := map[string]identity{}

for _, resource := range resources {
res := h(resource)
name := s(res["name"])

ids[name] = identity{
name: name,
pubkey: s(h(h(a(res["instances"])[0])["attributes"])["public_key"]),
privkey: s(h(h(a(res["instances"])[0])["attributes"])["private_key"]),
}
}

if len(ids) != 2 {
t.Fatalf("invalid count of identities created")
}

for _, name := range []string{"alice", "bob"} {
if _, ok := ids[name]; !ok {
t.Fatalf("%q was not present in state", name)
}

if ids[name].pubkey == "" {
t.Fatalf("%q: pubkey empty", name)
}

if ids[name].privkey == "" {
t.Fatalf("%q: pubkey empty", name)
}
}
}
2 changes: 2 additions & 0 deletions testdata/plans/basic-identity.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
resource "zerotier_identity" "alice" {}
resource "zerotier_identity" "bob" {}

0 comments on commit 8a1c98d

Please sign in to comment.