github.com/s-matyukevich/consul@v1.4.5/command/keyring/keyring_test.go (about) 1 package keyring 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/hashicorp/consul/agent" 8 "github.com/mitchellh/cli" 9 ) 10 11 func TestKeyringCommand_noTabs(t *testing.T) { 12 t.Parallel() 13 if strings.ContainsRune(New(nil).Help(), '\t') { 14 t.Fatal("help has tabs") 15 } 16 } 17 18 func TestKeyringCommand(t *testing.T) { 19 t.Parallel() 20 key1 := "HS5lJ+XuTlYKWaeGYyG+/A==" 21 key2 := "kZyFABeAmc64UMTrm9XuKA==" 22 23 // Begin with a single key 24 a1 := agent.NewTestAgent(t, t.Name(), ` 25 encrypt = "`+key1+`" 26 `) 27 defer a1.Shutdown() 28 29 // The LAN and WAN keyrings were initialized with key1 30 out := listKeys(t, a1.HTTPAddr()) 31 if !strings.Contains(out, "dc1 (LAN):\n "+key1) { 32 t.Fatalf("bad: %#v", out) 33 } 34 if !strings.Contains(out, "WAN:\n "+key1) { 35 t.Fatalf("bad: %#v", out) 36 } 37 if strings.Contains(out, key2) { 38 t.Fatalf("bad: %#v", out) 39 } 40 41 // Install the second key onto the keyring 42 installKey(t, a1.HTTPAddr(), key2) 43 44 // Both keys should be present 45 out = listKeys(t, a1.HTTPAddr()) 46 for _, key := range []string{key1, key2} { 47 if !strings.Contains(out, key) { 48 t.Fatalf("bad: %#v", out) 49 } 50 } 51 52 // Rotate to key2, remove key1 53 useKey(t, a1.HTTPAddr(), key2) 54 removeKey(t, a1.HTTPAddr(), key1) 55 56 // Only key2 is present now 57 out = listKeys(t, a1.HTTPAddr()) 58 if !strings.Contains(out, "dc1 (LAN):\n "+key2) { 59 t.Fatalf("bad: %#v", out) 60 } 61 if !strings.Contains(out, "WAN:\n "+key2) { 62 t.Fatalf("bad: %#v", out) 63 } 64 if strings.Contains(out, key1) { 65 t.Fatalf("bad: %#v", out) 66 } 67 } 68 69 func TestKeyringCommand_help(t *testing.T) { 70 t.Parallel() 71 ui := cli.NewMockUi() 72 c := New(ui) 73 code := c.Run(nil) 74 if code != 1 { 75 t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String()) 76 } 77 78 // Test that we didn't actually try to dial the RPC server. 79 if !strings.Contains(ui.ErrorWriter.String(), "Usage:") { 80 t.Fatalf("bad: %#v", ui.ErrorWriter.String()) 81 } 82 } 83 84 func TestKeyringCommand_failedConnection(t *testing.T) { 85 t.Parallel() 86 ui := cli.NewMockUi() 87 c := New(ui) 88 args := []string{"-list", "-http-addr=127.0.0.1:0"} 89 code := c.Run(args) 90 if code != 1 { 91 t.Fatalf("bad: %d, %#v", code, ui.ErrorWriter.String()) 92 } 93 if !strings.Contains(ui.ErrorWriter.String(), "dial") { 94 t.Fatalf("bad: %#v", ui.OutputWriter.String()) 95 } 96 } 97 98 func TestKeyringCommand_invalidRelayFactor(t *testing.T) { 99 t.Parallel() 100 ui := cli.NewMockUi() 101 c := New(ui) 102 103 args := []string{"-list", "-relay-factor=6"} 104 code := c.Run(args) 105 if code != 1 { 106 t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String()) 107 } 108 } 109 110 func listKeys(t *testing.T, addr string) string { 111 ui := cli.NewMockUi() 112 c := New(ui) 113 114 args := []string{"-list", "-http-addr=" + addr} 115 code := c.Run(args) 116 if code != 0 { 117 t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String()) 118 } 119 120 return ui.OutputWriter.String() 121 } 122 123 func installKey(t *testing.T, addr string, key string) { 124 ui := cli.NewMockUi() 125 c := New(ui) 126 127 args := []string{"-install=" + key, "-http-addr=" + addr} 128 code := c.Run(args) 129 if code != 0 { 130 t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String()) 131 } 132 } 133 134 func useKey(t *testing.T, addr string, key string) { 135 ui := cli.NewMockUi() 136 c := New(ui) 137 138 args := []string{"-use=" + key, "-http-addr=" + addr} 139 code := c.Run(args) 140 if code != 0 { 141 t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String()) 142 } 143 } 144 145 func removeKey(t *testing.T, addr string, key string) { 146 ui := cli.NewMockUi() 147 c := New(ui) 148 149 args := []string{"-remove=" + key, "-http-addr=" + addr} 150 code := c.Run(args) 151 if code != 0 { 152 t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String()) 153 } 154 }