github.com/sl1pm4t/consul@v1.4.5-0.20190325224627-74c31c540f9c/command/acl/rules/translate_test.go (about) 1 package rules 2 3 import ( 4 "io" 5 "io/ioutil" 6 "os" 7 "strings" 8 "testing" 9 10 "github.com/hashicorp/consul/agent" 11 "github.com/hashicorp/consul/logger" 12 "github.com/hashicorp/consul/testrpc" 13 "github.com/hashicorp/consul/testutil" 14 "github.com/mitchellh/cli" 15 "github.com/stretchr/testify/assert" 16 ) 17 18 func TestRulesTranslateCommand_noTabs(t *testing.T) { 19 t.Parallel() 20 21 if strings.ContainsRune(New(cli.NewMockUi()).Help(), '\t') { 22 t.Fatal("help has tabs") 23 } 24 } 25 26 func TestRulesTranslateCommand(t *testing.T) { 27 t.Parallel() 28 assert := assert.New(t) 29 30 testDir := testutil.TempDir(t, "acl") 31 defer os.RemoveAll(testDir) 32 33 a := agent.NewTestAgent(t, t.Name(), ` 34 primary_datacenter = "dc1" 35 acl { 36 enabled = true 37 tokens { 38 master = "root" 39 } 40 }`) 41 42 a.Agent.LogWriter = logger.NewLogWriter(512) 43 44 defer a.Shutdown() 45 testrpc.WaitForLeader(t, a.RPC, "dc1") 46 stdinR, stdinW := io.Pipe() 47 48 ui := cli.NewMockUi() 49 cmd := New(ui) 50 cmd.testStdin = stdinR 51 52 rules := "service \"\" { policy = \"write\" }" 53 expected := "service_prefix \"\" {\n policy = \"write\"\n}" 54 55 // From a file 56 { 57 err := ioutil.WriteFile(testDir+"/rules.hcl", []byte(rules), 0644) 58 assert.NoError(err) 59 60 args := []string{ 61 "-http-addr=" + a.HTTPAddr(), 62 "-token=root", 63 "@" + testDir + "/rules.hcl", 64 } 65 66 code := cmd.Run(args) 67 assert.Equal(code, 0) 68 assert.Empty(ui.ErrorWriter.String()) 69 assert.Contains(ui.OutputWriter.String(), expected) 70 } 71 72 // From stdin 73 { 74 go func() { 75 stdinW.Write([]byte(rules)) 76 stdinW.Close() 77 }() 78 79 args := []string{ 80 "-http-addr=" + a.HTTPAddr(), 81 "-token=root", 82 "-", 83 } 84 85 code := cmd.Run(args) 86 assert.Equal(code, 0) 87 assert.Empty(ui.ErrorWriter.String()) 88 assert.Contains(ui.OutputWriter.String(), expected) 89 } 90 91 // From arg 92 { 93 args := []string{ 94 "-http-addr=" + a.HTTPAddr(), 95 "-token=root", 96 rules, 97 } 98 99 code := cmd.Run(args) 100 assert.Equal(code, 0) 101 assert.Empty(ui.ErrorWriter.String()) 102 assert.Contains(ui.OutputWriter.String(), expected) 103 } 104 }