blob: 4d313dcc9c3db43cd02d9e1fb563b1f9c22afed5 [file] [log] [blame]
package impl_test
import (
"io"
"io/ioutil"
"os"
"path"
"testing"
"veyron.io/veyron/veyron/services/mgmt/node/impl"
"veyron.io/veyron/veyron2/services/mgmt/node"
)
// TestAssociationPersistance verifies correct operation of association
// persistance code.
func TestAssociationPersistance(t *testing.T) {
td, err := ioutil.TempDir("", "nmtest")
if err != nil {
t.Fatalf("TempDir failed: %v", err)
}
defer os.RemoveAll(td)
nbsa1, err := impl.NewBlessingSystemAssociationStore(td)
if err != nil {
t.Fatalf("NewBlessingSystemAssociationStore failed: %v", err)
}
// Insert an association.
err = nbsa1.AssociateSystemAccountForBlessings([]string{"alice", "bob"}, "alice_account")
if err != nil {
t.Fatalf("AssociateSystemAccount failed: %v", err)
}
got1, err := nbsa1.AllBlessingSystemAssociations()
if err != nil {
t.Fatalf("AllBlessingSystemAssociations failed: %v", err)
}
compareAssociations(t, got1, []node.Association{
{
"alice",
"alice_account",
},
{
"bob",
"alice_account",
},
})
nbsa2, err := impl.NewBlessingSystemAssociationStore(td)
if err != nil {
t.Fatalf("NewBlessingSystemAssociationStore failed: %v", err)
}
got2, err := nbsa2.AllBlessingSystemAssociations()
if err != nil {
t.Fatalf("AllBlessingSystemAssociations failed: %v", err)
}
compareAssociations(t, got1, got2)
sysacc, have := nbsa2.SystemAccountForBlessings([]string{"bob"})
if expected := true; have != expected {
t.Fatalf("SystemAccountForBlessings failed. got %v, expected %v", have, expected)
}
if expected := "alice_account"; sysacc != expected {
t.Fatalf("SystemAccountForBlessings failed. got %v, expected %v", sysacc, expected)
}
sysacc, have = nbsa2.SystemAccountForBlessings([]string{"doug"})
if expected := false; have != expected {
t.Fatalf("SystemAccountForBlessings failed. got %v, expected %v", have, expected)
}
if expected := ""; sysacc != expected {
t.Fatalf("SystemAccountForBlessings failed. got %v, expected %v", sysacc, expected)
}
// Remove "bob".
err = nbsa1.DisassociateSystemAccountForBlessings([]string{"bob"})
if err != nil {
t.Fatalf("DisassociateSystemAccountForBlessings failed: %v", err)
}
// Verify that "bob" has been removed.
got1, err = nbsa1.AllBlessingSystemAssociations()
if err != nil {
t.Fatalf("AllBlessingSystemAssociations failed: %v", err)
}
compareAssociations(t, got1, []node.Association{
{
"alice",
"alice_account",
},
})
err = nbsa1.AssociateSystemAccountForBlessings([]string{"alice", "bob"}, "alice_other_account")
if err != nil {
t.Fatalf("AssociateSystemAccount failed: %v", err)
}
// Verify that "bob" and "alice" have new values.
got1, err = nbsa1.AllBlessingSystemAssociations()
if err != nil {
t.Fatalf("AllBlessingSystemAssociations failed: %v", err)
}
compareAssociations(t, got1, []node.Association{
{
"alice",
"alice_other_account",
},
{
"bob",
"alice_other_account",
},
})
// Make future serialization attempts fail.
if err := os.RemoveAll(td); err != nil {
t.Fatalf("os.RemoveAll: couldn't delete %s: %v", td, err)
}
err = nbsa1.AssociateSystemAccountForBlessings([]string{"doug"}, "alice_account")
if err == nil {
t.Fatalf("AssociateSystemAccount should have failed but didn't")
}
}
func TestAssociationPersistanceDetectsBadStartingConditions(t *testing.T) {
dir := "/i-am-hoping-that-there-is-no-such-directory"
nbsa1, err := impl.NewBlessingSystemAssociationStore(dir)
if nbsa1 != nil || err == nil {
t.Fatalf("bad root directory %s ought to have caused an error", dir)
}
// Create a NewBlessingSystemAssociationStore directory as a side-effect.
nbsa1, err = impl.NewBlessingSystemAssociationStore(os.TempDir())
defer os.RemoveAll(path.Join(os.TempDir(), "node-manager"))
if err != nil {
t.Fatalf("NewBlessingSystemAssociationStore failed: %v", err)
}
tpath := path.Join(os.TempDir(), "node-manager", "node-data", "associated.accounts")
f, err := os.Create(tpath)
if err != nil {
t.Fatalf("could not open backing file for setup: %v", err)
}
if _, err := io.WriteString(f, "bad-json\""); err != nil {
t.Fatalf("could not write to test file %s: %v", tpath, err)
}
f.Close()
nbsa1, err = impl.NewBlessingSystemAssociationStore(dir)
if nbsa1 != nil || err == nil {
t.Fatalf("invalid JSON ought to have caused an error")
}
// This test will fail if executed as root or if your system is configured oddly.
unreadableFile := "/dev/autofs"
nbsa1, err = impl.NewBlessingSystemAssociationStore(unreadableFile)
if nbsa1 != nil || err == nil {
t.Fatalf("unreadable file %s ought to have caused an error", unreadableFile)
}
}