1// Copyright 2016 The etcd Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package e2e
16
17import "testing"
18
19func TestCtlV3UserAdd(t *testing.T)          { testCtl(t, userAddTest) }
20func TestCtlV3UserAddNoTLS(t *testing.T)     { testCtl(t, userAddTest, withCfg(configNoTLS)) }
21func TestCtlV3UserAddClientTLS(t *testing.T) { testCtl(t, userAddTest, withCfg(configClientTLS)) }
22func TestCtlV3UserAddPeerTLS(t *testing.T)   { testCtl(t, userAddTest, withCfg(configPeerTLS)) }
23func TestCtlV3UserAddTimeout(t *testing.T)   { testCtl(t, userAddTest, withDialTimeout(0)) }
24
25func TestCtlV3UserDelete(t *testing.T) { testCtl(t, userDelTest) }
26func TestCtlV3UserPasswd(t *testing.T) { testCtl(t, userPasswdTest) }
27
28type userCmdDesc struct {
29	args        []string
30	expectedStr string
31	stdIn       []string
32}
33
34func userAddTest(cx ctlCtx) {
35	cmdSet := []userCmdDesc{
36		// Adds a user name.
37		{
38			args:        []string{"add", "username", "--interactive=false"},
39			expectedStr: "User username created",
40			stdIn:       []string{"password"},
41		},
42		// Adds a user name using the usertest:password syntax.
43		{
44			args:        []string{"add", "usertest:password"},
45			expectedStr: "User usertest created",
46			stdIn:       []string{},
47		},
48		// Tries to add a user with empty username.
49		{
50			args:        []string{"add", ":password"},
51			expectedStr: "empty user name is not allowed.",
52			stdIn:       []string{},
53		},
54		// Tries to add a user name that already exists.
55		{
56			args:        []string{"add", "username", "--interactive=false"},
57			expectedStr: "user name already exists",
58			stdIn:       []string{"password"},
59		},
60	}
61
62	for i, cmd := range cmdSet {
63		if err := ctlV3User(cx, cmd.args, cmd.expectedStr, cmd.stdIn); err != nil {
64			if cx.dialTimeout > 0 && !isGRPCTimedout(err) {
65				cx.t.Fatalf("userAddTest #%d: ctlV3User error (%v)", i, err)
66			}
67		}
68	}
69}
70
71func userDelTest(cx ctlCtx) {
72	cmdSet := []userCmdDesc{
73		// Adds a user name.
74		{
75			args:        []string{"add", "username", "--interactive=false"},
76			expectedStr: "User username created",
77			stdIn:       []string{"password"},
78		},
79		// Deletes the user name just added.
80		{
81			args:        []string{"delete", "username"},
82			expectedStr: "User username deleted",
83		},
84		// Deletes a user name that is not present.
85		{
86			args:        []string{"delete", "username"},
87			expectedStr: "user name not found",
88		},
89	}
90
91	for i, cmd := range cmdSet {
92		if err := ctlV3User(cx, cmd.args, cmd.expectedStr, cmd.stdIn); err != nil {
93			cx.t.Fatalf("userDelTest #%d: ctlV3User error (%v)", i, err)
94		}
95	}
96}
97
98func userPasswdTest(cx ctlCtx) {
99	cmdSet := []userCmdDesc{
100		// Adds a user name.
101		{
102			args:        []string{"add", "username", "--interactive=false"},
103			expectedStr: "User username created",
104			stdIn:       []string{"password"},
105		},
106		// Changes the password.
107		{
108			args:        []string{"passwd", "username", "--interactive=false"},
109			expectedStr: "Password updated",
110			stdIn:       []string{"password1"},
111		},
112	}
113
114	for i, cmd := range cmdSet {
115		if err := ctlV3User(cx, cmd.args, cmd.expectedStr, cmd.stdIn); err != nil {
116			cx.t.Fatalf("userPasswdTest #%d: ctlV3User error (%v)", i, err)
117		}
118	}
119}
120
121func ctlV3User(cx ctlCtx, args []string, expStr string, stdIn []string) error {
122	cmdArgs := append(cx.PrefixArgs(), "user")
123	cmdArgs = append(cmdArgs, args...)
124
125	proc, err := spawnCmd(cmdArgs)
126	if err != nil {
127		return err
128	}
129
130	// Send 'stdIn' strings as input.
131	for _, s := range stdIn {
132		if err = proc.Send(s + "\r"); err != nil {
133			return err
134		}
135	}
136
137	_, err = proc.Expect(expStr)
138	return err
139}
140