github.com/diptanu/nomad@v0.5.7-0.20170516172507-d72e86cbe3d9/command/agent/keyring_test.go (about) 1 package agent 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "testing" 9 ) 10 11 func TestAgent_LoadKeyrings(t *testing.T) { 12 key := "tbLJg26ZJyJ9pK3qhc9jig==" 13 14 // Should be no configured keyring file by default 15 dir1, agent1 := makeAgent(t, nil) 16 defer os.RemoveAll(dir1) 17 defer agent1.Shutdown() 18 19 c := agent1.server.GetConfig() 20 if c.SerfConfig.KeyringFile != "" { 21 t.Fatalf("bad: %#v", c.SerfConfig.KeyringFile) 22 } 23 if c.SerfConfig.MemberlistConfig.Keyring != nil { 24 t.Fatalf("keyring should not be loaded") 25 } 26 27 // Server should auto-load LAN and WAN keyring files 28 dir2, agent2 := makeAgent(t, func(c *Config) { 29 file := filepath.Join(c.DataDir, serfKeyring) 30 if err := initKeyring(file, key); err != nil { 31 t.Fatalf("err: %s", err) 32 } 33 }) 34 defer os.RemoveAll(dir2) 35 defer agent2.Shutdown() 36 37 c = agent2.server.GetConfig() 38 if c.SerfConfig.KeyringFile == "" { 39 t.Fatalf("should have keyring file") 40 } 41 if c.SerfConfig.MemberlistConfig.Keyring == nil { 42 t.Fatalf("keyring should be loaded") 43 } 44 } 45 46 func TestAgent_InitKeyring(t *testing.T) { 47 key1 := "tbLJg26ZJyJ9pK3qhc9jig==" 48 key2 := "4leC33rgtXKIVUr9Nr0snQ==" 49 expected := fmt.Sprintf(`["%s"]`, key1) 50 51 dir, err := ioutil.TempDir("", "nomad") 52 if err != nil { 53 t.Fatalf("err: %s", err) 54 } 55 defer os.RemoveAll(dir) 56 57 file := filepath.Join(dir, "keyring") 58 59 // First initialize the keyring 60 if err := initKeyring(file, key1); err != nil { 61 t.Fatalf("err: %s", err) 62 } 63 64 content, err := ioutil.ReadFile(file) 65 if err != nil { 66 t.Fatalf("err: %s", err) 67 } 68 if string(content) != expected { 69 t.Fatalf("bad: %s", content) 70 } 71 72 // Try initializing again with a different key 73 if err := initKeyring(file, key2); err != nil { 74 t.Fatalf("err: %s", err) 75 } 76 77 // Content should still be the same 78 content, err = ioutil.ReadFile(file) 79 if err != nil { 80 t.Fatalf("err: %s", err) 81 } 82 if string(content) != expected { 83 t.Fatalf("bad: %s", content) 84 } 85 }