github.com/djenriquez/nomad-1@v0.8.1/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 t.Parallel() 13 key := "tbLJg26ZJyJ9pK3qhc9jig==" 14 15 // Should be no configured keyring file by default 16 agent1 := NewTestAgent(t, t.Name(), nil) 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 WAN keyring files 28 agent2 := &TestAgent{ 29 T: t, 30 Name: t.Name() + "2", 31 Key: key, 32 } 33 agent2.Start() 34 defer agent2.Shutdown() 35 36 c = agent2.server.GetConfig() 37 if c.SerfConfig.KeyringFile == "" { 38 t.Fatalf("should have keyring file") 39 } 40 if c.SerfConfig.MemberlistConfig.Keyring == nil { 41 t.Fatalf("keyring should be loaded") 42 } 43 } 44 45 func TestAgent_InitKeyring(t *testing.T) { 46 t.Parallel() 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 }