remotecommand_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package tests
  14. import (
  15. "bytes"
  16. "errors"
  17. "fmt"
  18. "io"
  19. "io/ioutil"
  20. "net/http"
  21. "net/http/httptest"
  22. "net/url"
  23. "strings"
  24. "testing"
  25. "time"
  26. "github.com/stretchr/testify/require"
  27. "k8s.io/apimachinery/pkg/runtime/schema"
  28. "k8s.io/apimachinery/pkg/types"
  29. "k8s.io/apimachinery/pkg/util/httpstream"
  30. remotecommandconsts "k8s.io/apimachinery/pkg/util/remotecommand"
  31. restclient "k8s.io/client-go/rest"
  32. remoteclient "k8s.io/client-go/tools/remotecommand"
  33. "k8s.io/client-go/transport/spdy"
  34. "k8s.io/kubernetes/pkg/api/legacyscheme"
  35. api "k8s.io/kubernetes/pkg/apis/core"
  36. "k8s.io/kubernetes/pkg/kubelet/server/remotecommand"
  37. )
  38. type fakeExecutor struct {
  39. t *testing.T
  40. testName string
  41. errorData string
  42. stdoutData string
  43. stderrData string
  44. expectStdin bool
  45. stdinReceived bytes.Buffer
  46. tty bool
  47. messageCount int
  48. command []string
  49. exec bool
  50. }
  51. func (ex *fakeExecutor) ExecInContainer(name string, uid types.UID, container string, cmd []string, in io.Reader, out, err io.WriteCloser, tty bool, resize <-chan remoteclient.TerminalSize, timeout time.Duration) error {
  52. return ex.run(name, uid, container, cmd, in, out, err, tty)
  53. }
  54. func (ex *fakeExecutor) AttachContainer(name string, uid types.UID, container string, in io.Reader, out, err io.WriteCloser, tty bool, resize <-chan remoteclient.TerminalSize) error {
  55. return ex.run(name, uid, container, nil, in, out, err, tty)
  56. }
  57. func (ex *fakeExecutor) run(name string, uid types.UID, container string, cmd []string, in io.Reader, out, err io.WriteCloser, tty bool) error {
  58. ex.command = cmd
  59. ex.tty = tty
  60. if e, a := "pod", name; e != a {
  61. ex.t.Errorf("%s: pod: expected %q, got %q", ex.testName, e, a)
  62. }
  63. if e, a := "uid", uid; e != string(a) {
  64. ex.t.Errorf("%s: uid: expected %q, got %q", ex.testName, e, a)
  65. }
  66. if ex.exec {
  67. if e, a := "ls /", strings.Join(ex.command, " "); e != a {
  68. ex.t.Errorf("%s: command: expected %q, got %q", ex.testName, e, a)
  69. }
  70. } else {
  71. if len(ex.command) > 0 {
  72. ex.t.Errorf("%s: command: expected nothing, got %v", ex.testName, ex.command)
  73. }
  74. }
  75. if len(ex.errorData) > 0 {
  76. return errors.New(ex.errorData)
  77. }
  78. if len(ex.stdoutData) > 0 {
  79. for i := 0; i < ex.messageCount; i++ {
  80. fmt.Fprint(out, ex.stdoutData)
  81. }
  82. }
  83. if len(ex.stderrData) > 0 {
  84. for i := 0; i < ex.messageCount; i++ {
  85. fmt.Fprint(err, ex.stderrData)
  86. }
  87. }
  88. if ex.expectStdin {
  89. io.Copy(&ex.stdinReceived, in)
  90. }
  91. return nil
  92. }
  93. func fakeServer(t *testing.T, requestReceived chan struct{}, testName string, exec bool, stdinData, stdoutData, stderrData, errorData string, tty bool, messageCount int, serverProtocols []string) http.HandlerFunc {
  94. return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  95. executor := &fakeExecutor{
  96. t: t,
  97. testName: testName,
  98. errorData: errorData,
  99. stdoutData: stdoutData,
  100. stderrData: stderrData,
  101. expectStdin: len(stdinData) > 0,
  102. tty: tty,
  103. messageCount: messageCount,
  104. exec: exec,
  105. }
  106. opts, err := remotecommand.NewOptions(req)
  107. require.NoError(t, err)
  108. if exec {
  109. cmd := req.URL.Query()[api.ExecCommandParam]
  110. remotecommand.ServeExec(w, req, executor, "pod", "uid", "container", cmd, opts, 0, 10*time.Second, serverProtocols)
  111. } else {
  112. remotecommand.ServeAttach(w, req, executor, "pod", "uid", "container", opts, 0, 10*time.Second, serverProtocols)
  113. }
  114. if e, a := strings.Repeat(stdinData, messageCount), executor.stdinReceived.String(); e != a {
  115. t.Errorf("%s: stdin: expected %q, got %q", testName, e, a)
  116. }
  117. close(requestReceived)
  118. })
  119. }
  120. func TestStream(t *testing.T) {
  121. testCases := []struct {
  122. TestName string
  123. Stdin string
  124. Stdout string
  125. Stderr string
  126. Error string
  127. Tty bool
  128. MessageCount int
  129. ClientProtocols []string
  130. ServerProtocols []string
  131. }{
  132. {
  133. TestName: "error",
  134. Error: "bail",
  135. Stdout: "a",
  136. ClientProtocols: []string{remotecommandconsts.StreamProtocolV2Name},
  137. ServerProtocols: []string{remotecommandconsts.StreamProtocolV2Name},
  138. },
  139. {
  140. TestName: "in/out/err",
  141. Stdin: "a",
  142. Stdout: "b",
  143. Stderr: "c",
  144. MessageCount: 100,
  145. ClientProtocols: []string{remotecommandconsts.StreamProtocolV2Name},
  146. ServerProtocols: []string{remotecommandconsts.StreamProtocolV2Name},
  147. },
  148. {
  149. TestName: "oversized stdin",
  150. Stdin: strings.Repeat("a", 20*1024*1024),
  151. Stdout: "b",
  152. Stderr: "",
  153. MessageCount: 1,
  154. ClientProtocols: []string{remotecommandconsts.StreamProtocolV2Name},
  155. ServerProtocols: []string{remotecommandconsts.StreamProtocolV2Name},
  156. },
  157. {
  158. TestName: "in/out/tty",
  159. Stdin: "a",
  160. Stdout: "b",
  161. Tty: true,
  162. MessageCount: 100,
  163. ClientProtocols: []string{remotecommandconsts.StreamProtocolV2Name},
  164. ServerProtocols: []string{remotecommandconsts.StreamProtocolV2Name},
  165. },
  166. {
  167. // 1.0 kubectl, 1.0 kubelet
  168. TestName: "unversioned client, unversioned server",
  169. Stdout: "b",
  170. Stderr: "c",
  171. MessageCount: 1,
  172. ClientProtocols: []string{},
  173. ServerProtocols: []string{},
  174. },
  175. {
  176. // 1.0 kubectl, 1.1+ kubelet
  177. TestName: "unversioned client, versioned server",
  178. Stdout: "b",
  179. Stderr: "c",
  180. MessageCount: 1,
  181. ClientProtocols: []string{},
  182. ServerProtocols: []string{remotecommandconsts.StreamProtocolV2Name, remotecommandconsts.StreamProtocolV1Name},
  183. },
  184. {
  185. // 1.1+ kubectl, 1.0 kubelet
  186. TestName: "versioned client, unversioned server",
  187. Stdout: "b",
  188. Stderr: "c",
  189. MessageCount: 1,
  190. ClientProtocols: []string{remotecommandconsts.StreamProtocolV2Name, remotecommandconsts.StreamProtocolV1Name},
  191. ServerProtocols: []string{},
  192. },
  193. }
  194. for _, testCase := range testCases {
  195. for _, exec := range []bool{true, false} {
  196. var name string
  197. if exec {
  198. name = testCase.TestName + " (exec)"
  199. } else {
  200. name = testCase.TestName + " (attach)"
  201. }
  202. var (
  203. streamIn io.Reader
  204. streamOut, streamErr io.Writer
  205. )
  206. localOut := &bytes.Buffer{}
  207. localErr := &bytes.Buffer{}
  208. requestReceived := make(chan struct{})
  209. server := httptest.NewServer(fakeServer(t, requestReceived, name, exec, testCase.Stdin, testCase.Stdout, testCase.Stderr, testCase.Error, testCase.Tty, testCase.MessageCount, testCase.ServerProtocols))
  210. url, _ := url.ParseRequestURI(server.URL)
  211. config := restclient.ContentConfig{
  212. GroupVersion: &schema.GroupVersion{Group: "x"},
  213. NegotiatedSerializer: legacyscheme.Codecs,
  214. }
  215. c, err := restclient.NewRESTClient(url, "", config, -1, -1, nil, nil)
  216. if err != nil {
  217. t.Fatalf("failed to create a client: %v", err)
  218. }
  219. req := c.Post().Resource("testing")
  220. if exec {
  221. req.Param("command", "ls")
  222. req.Param("command", "/")
  223. }
  224. if len(testCase.Stdin) > 0 {
  225. req.Param(api.ExecStdinParam, "1")
  226. streamIn = strings.NewReader(strings.Repeat(testCase.Stdin, testCase.MessageCount))
  227. }
  228. if len(testCase.Stdout) > 0 {
  229. req.Param(api.ExecStdoutParam, "1")
  230. streamOut = localOut
  231. }
  232. if testCase.Tty {
  233. req.Param(api.ExecTTYParam, "1")
  234. } else if len(testCase.Stderr) > 0 {
  235. req.Param(api.ExecStderrParam, "1")
  236. streamErr = localErr
  237. }
  238. conf := &restclient.Config{
  239. Host: server.URL,
  240. }
  241. transport, upgradeTransport, err := spdy.RoundTripperFor(conf)
  242. if err != nil {
  243. t.Errorf("%s: unexpected error: %v", name, err)
  244. continue
  245. }
  246. e, err := remoteclient.NewSPDYExecutorForProtocols(transport, upgradeTransport, "POST", req.URL(), testCase.ClientProtocols...)
  247. if err != nil {
  248. t.Errorf("%s: unexpected error: %v", name, err)
  249. continue
  250. }
  251. err = e.Stream(remoteclient.StreamOptions{
  252. Stdin: streamIn,
  253. Stdout: streamOut,
  254. Stderr: streamErr,
  255. Tty: testCase.Tty,
  256. })
  257. hasErr := err != nil
  258. if len(testCase.Error) > 0 {
  259. if !hasErr {
  260. t.Errorf("%s: expected an error", name)
  261. } else {
  262. if e, a := testCase.Error, err.Error(); !strings.Contains(a, e) {
  263. t.Errorf("%s: expected error stream read %q, got %q", name, e, a)
  264. }
  265. }
  266. server.Close()
  267. continue
  268. }
  269. if hasErr {
  270. t.Errorf("%s: unexpected error: %v", name, err)
  271. server.Close()
  272. continue
  273. }
  274. if len(testCase.Stdout) > 0 {
  275. if e, a := strings.Repeat(testCase.Stdout, testCase.MessageCount), localOut; e != a.String() {
  276. t.Errorf("%s: expected stdout data %q, got %q", name, e, a)
  277. }
  278. }
  279. if testCase.Stderr != "" {
  280. if e, a := strings.Repeat(testCase.Stderr, testCase.MessageCount), localErr; e != a.String() {
  281. t.Errorf("%s: expected stderr data %q, got %q", name, e, a)
  282. }
  283. }
  284. select {
  285. case <-requestReceived:
  286. case <-time.After(time.Minute):
  287. t.Errorf("%s: expected fakeServerInstance to receive request", name)
  288. }
  289. server.Close()
  290. }
  291. }
  292. }
  293. type fakeUpgrader struct {
  294. req *http.Request
  295. resp *http.Response
  296. conn httpstream.Connection
  297. err, connErr error
  298. checkResponse bool
  299. called bool
  300. t *testing.T
  301. }
  302. func (u *fakeUpgrader) RoundTrip(req *http.Request) (*http.Response, error) {
  303. u.called = true
  304. u.req = req
  305. return u.resp, u.err
  306. }
  307. func (u *fakeUpgrader) NewConnection(resp *http.Response) (httpstream.Connection, error) {
  308. if u.checkResponse && u.resp != resp {
  309. u.t.Errorf("response objects passed did not match: %#v", resp)
  310. }
  311. return u.conn, u.connErr
  312. }
  313. type fakeConnection struct {
  314. httpstream.Connection
  315. }
  316. // Dial is the common functionality between any stream based upgrader, regardless of protocol.
  317. // This method ensures that someone can use a generic stream executor without being dependent
  318. // on the core Kube client config behavior.
  319. func TestDial(t *testing.T) {
  320. upgrader := &fakeUpgrader{
  321. t: t,
  322. checkResponse: true,
  323. conn: &fakeConnection{},
  324. resp: &http.Response{
  325. StatusCode: http.StatusSwitchingProtocols,
  326. Body: ioutil.NopCloser(&bytes.Buffer{}),
  327. },
  328. }
  329. dialer := spdy.NewDialer(upgrader, &http.Client{Transport: upgrader}, "POST", &url.URL{Host: "something.com", Scheme: "https"})
  330. conn, protocol, err := dialer.Dial("protocol1")
  331. if err != nil {
  332. t.Fatal(err)
  333. }
  334. if conn != upgrader.conn {
  335. t.Errorf("unexpected connection: %#v", conn)
  336. }
  337. if !upgrader.called {
  338. t.Errorf("request not called")
  339. }
  340. _ = protocol
  341. }