cp_test.go 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. /*
  2. Copyright 2014 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 cp
  14. import (
  15. "archive/tar"
  16. "bytes"
  17. "fmt"
  18. "io"
  19. "io/ioutil"
  20. "net/http"
  21. "os"
  22. "path"
  23. "path/filepath"
  24. "reflect"
  25. "strings"
  26. "testing"
  27. "github.com/stretchr/testify/assert"
  28. "github.com/stretchr/testify/require"
  29. v1 "k8s.io/api/core/v1"
  30. "k8s.io/apimachinery/pkg/api/errors"
  31. "k8s.io/apimachinery/pkg/runtime"
  32. "k8s.io/apimachinery/pkg/runtime/schema"
  33. "k8s.io/cli-runtime/pkg/genericclioptions"
  34. "k8s.io/client-go/rest/fake"
  35. kexec "k8s.io/kubernetes/pkg/kubectl/cmd/exec"
  36. cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
  37. "k8s.io/kubernetes/pkg/kubectl/scheme"
  38. )
  39. type FileType int
  40. const (
  41. RegularFile FileType = 0
  42. SymLink FileType = 1
  43. RegexFile FileType = 2
  44. )
  45. func TestExtractFileSpec(t *testing.T) {
  46. tests := []struct {
  47. spec string
  48. expectedPod string
  49. expectedNamespace string
  50. expectedFile string
  51. expectErr bool
  52. }{
  53. {
  54. spec: "namespace/pod:/some/file",
  55. expectedPod: "pod",
  56. expectedNamespace: "namespace",
  57. expectedFile: "/some/file",
  58. },
  59. {
  60. spec: "pod:/some/file",
  61. expectedPod: "pod",
  62. expectedFile: "/some/file",
  63. },
  64. {
  65. spec: "/some/file",
  66. expectedFile: "/some/file",
  67. },
  68. {
  69. spec: ":file:not:exist:in:local:filesystem",
  70. expectErr: true,
  71. },
  72. {
  73. spec: "namespace/pod/invalid:/some/file",
  74. expectErr: true,
  75. },
  76. {
  77. spec: "pod:/some/filenamewith:in",
  78. expectedPod: "pod",
  79. expectedFile: "/some/filenamewith:in",
  80. },
  81. }
  82. for _, test := range tests {
  83. spec, err := extractFileSpec(test.spec)
  84. if test.expectErr && err == nil {
  85. t.Errorf("unexpected non-error")
  86. continue
  87. }
  88. if err != nil && !test.expectErr {
  89. t.Errorf("unexpected error: %v", err)
  90. continue
  91. }
  92. if spec.PodName != test.expectedPod {
  93. t.Errorf("expected: %s, saw: %s", test.expectedPod, spec.PodName)
  94. }
  95. if spec.PodNamespace != test.expectedNamespace {
  96. t.Errorf("expected: %s, saw: %s", test.expectedNamespace, spec.PodNamespace)
  97. }
  98. if spec.File != test.expectedFile {
  99. t.Errorf("expected: %s, saw: %s", test.expectedFile, spec.File)
  100. }
  101. }
  102. }
  103. func TestGetPrefix(t *testing.T) {
  104. tests := []struct {
  105. input string
  106. expected string
  107. }{
  108. {
  109. input: "/foo/bar",
  110. expected: "foo/bar",
  111. },
  112. {
  113. input: "foo/bar",
  114. expected: "foo/bar",
  115. },
  116. }
  117. for _, test := range tests {
  118. out := getPrefix(test.input)
  119. if out != test.expected {
  120. t.Errorf("expected: %s, saw: %s", test.expected, out)
  121. }
  122. }
  123. }
  124. func TestStripPathShortcuts(t *testing.T) {
  125. tests := []struct {
  126. name string
  127. input string
  128. expected string
  129. }{
  130. {
  131. name: "test single path shortcut prefix",
  132. input: "../foo/bar",
  133. expected: "foo/bar",
  134. },
  135. {
  136. name: "test multiple path shortcuts",
  137. input: "../../foo/bar",
  138. expected: "foo/bar",
  139. },
  140. {
  141. name: "test multiple path shortcuts with absolute path",
  142. input: "/tmp/one/two/../../foo/bar",
  143. expected: "tmp/foo/bar",
  144. },
  145. {
  146. name: "test multiple path shortcuts with no named directory",
  147. input: "../../",
  148. expected: "",
  149. },
  150. {
  151. name: "test multiple path shortcuts with no named directory and no trailing slash",
  152. input: "../..",
  153. expected: "",
  154. },
  155. {
  156. name: "test multiple path shortcuts with absolute path and filename containing leading dots",
  157. input: "/tmp/one/two/../../foo/..bar",
  158. expected: "tmp/foo/..bar",
  159. },
  160. {
  161. name: "test multiple path shortcuts with no named directory and filename containing leading dots",
  162. input: "../...foo",
  163. expected: "...foo",
  164. },
  165. {
  166. name: "test filename containing leading dots",
  167. input: "...foo",
  168. expected: "...foo",
  169. },
  170. {
  171. name: "test root directory",
  172. input: "/",
  173. expected: "",
  174. },
  175. }
  176. for _, test := range tests {
  177. out := stripPathShortcuts(test.input)
  178. if out != test.expected {
  179. t.Errorf("expected: %s, saw: %s", test.expected, out)
  180. }
  181. }
  182. }
  183. func TestIsDestRelative(t *testing.T) {
  184. tests := []struct {
  185. base string
  186. dest string
  187. relative bool
  188. }{
  189. {
  190. base: "/dir",
  191. dest: "/dir/../link",
  192. relative: false,
  193. },
  194. {
  195. base: "/dir",
  196. dest: "/dir/../../link",
  197. relative: false,
  198. },
  199. {
  200. base: "/dir",
  201. dest: "/link",
  202. relative: false,
  203. },
  204. {
  205. base: "/dir",
  206. dest: "/dir/link",
  207. relative: true,
  208. },
  209. {
  210. base: "/dir",
  211. dest: "/dir/int/../link",
  212. relative: true,
  213. },
  214. {
  215. base: "dir",
  216. dest: "dir/link",
  217. relative: true,
  218. },
  219. {
  220. base: "dir",
  221. dest: "dir/int/../link",
  222. relative: true,
  223. },
  224. {
  225. base: "dir",
  226. dest: "dir/../../link",
  227. relative: false,
  228. },
  229. }
  230. for i, test := range tests {
  231. t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
  232. if test.relative != isDestRelative(test.base, test.dest) {
  233. t.Errorf("unexpected result for: base %q, dest %q", test.base, test.dest)
  234. }
  235. })
  236. }
  237. }
  238. func checkErr(t *testing.T, err error) {
  239. if err != nil {
  240. t.Errorf("unexpected error: %v", err)
  241. t.FailNow()
  242. }
  243. }
  244. func TestTarUntar(t *testing.T) {
  245. dir, err := ioutil.TempDir("", "input")
  246. checkErr(t, err)
  247. dir2, err := ioutil.TempDir("", "output")
  248. checkErr(t, err)
  249. dir3, err := ioutil.TempDir("", "dir")
  250. checkErr(t, err)
  251. dir = dir + "/"
  252. defer func() {
  253. os.RemoveAll(dir)
  254. os.RemoveAll(dir2)
  255. os.RemoveAll(dir3)
  256. }()
  257. files := []struct {
  258. name string
  259. nameList []string
  260. data string
  261. omitted bool
  262. fileType FileType
  263. }{
  264. {
  265. name: "foo",
  266. data: "foobarbaz",
  267. fileType: RegularFile,
  268. },
  269. {
  270. name: "dir/blah",
  271. data: "bazblahfoo",
  272. fileType: RegularFile,
  273. },
  274. {
  275. name: "some/other/directory/",
  276. data: "with more data here",
  277. fileType: RegularFile,
  278. },
  279. {
  280. name: "blah",
  281. data: "same file name different data",
  282. fileType: RegularFile,
  283. },
  284. {
  285. name: "gakki",
  286. data: "tmp/gakki",
  287. fileType: SymLink,
  288. },
  289. {
  290. name: "relative_to_dest",
  291. data: path.Join(dir2, "foo"),
  292. fileType: SymLink,
  293. },
  294. {
  295. name: "tricky_relative",
  296. data: path.Join(dir3, "xyz"),
  297. fileType: SymLink,
  298. },
  299. {
  300. name: "absolute_path",
  301. data: "/tmp/gakki",
  302. fileType: SymLink,
  303. },
  304. {
  305. name: "blah*",
  306. nameList: []string{"blah1", "blah2"},
  307. data: "regexp file name",
  308. fileType: RegexFile,
  309. },
  310. }
  311. for _, file := range files {
  312. filepath := path.Join(dir, file.name)
  313. if err := os.MkdirAll(path.Dir(filepath), 0755); err != nil {
  314. t.Fatalf("unexpected error: %v", err)
  315. }
  316. if file.fileType == RegularFile {
  317. createTmpFile(t, filepath, file.data)
  318. } else if file.fileType == SymLink {
  319. err := os.Symlink(file.data, filepath)
  320. if err != nil {
  321. t.Fatalf("unexpected error: %v", err)
  322. }
  323. } else if file.fileType == RegexFile {
  324. for _, fileName := range file.nameList {
  325. createTmpFile(t, path.Join(dir, fileName), file.data)
  326. }
  327. } else {
  328. t.Fatalf("unexpected file type: %v", file)
  329. }
  330. }
  331. opts := NewCopyOptions(genericclioptions.NewTestIOStreamsDiscard())
  332. writer := &bytes.Buffer{}
  333. if err := makeTar(dir, dir, writer); err != nil {
  334. t.Fatalf("unexpected error: %v", err)
  335. }
  336. reader := bytes.NewBuffer(writer.Bytes())
  337. if err := opts.untarAll(reader, dir2, ""); err != nil {
  338. t.Fatalf("unexpected error: %v", err)
  339. }
  340. for _, file := range files {
  341. absPath := filepath.Join(dir2, strings.TrimPrefix(dir, os.TempDir()))
  342. filePath := filepath.Join(absPath, file.name)
  343. if file.fileType == RegularFile {
  344. cmpFileData(t, filePath, file.data)
  345. } else if file.fileType == SymLink {
  346. dest, err := os.Readlink(filePath)
  347. if file.omitted {
  348. if err != nil && strings.Contains(err.Error(), "no such file or directory") {
  349. continue
  350. }
  351. t.Fatalf("expected to omit symlink for %s", filePath)
  352. }
  353. if err != nil {
  354. t.Fatalf("unexpected error: %v", err)
  355. }
  356. if file.data != dest {
  357. t.Fatalf("expected: %s, saw: %s", file.data, dest)
  358. }
  359. } else if file.fileType == RegexFile {
  360. for _, fileName := range file.nameList {
  361. cmpFileData(t, path.Join(dir, fileName), file.data)
  362. }
  363. } else {
  364. t.Fatalf("unexpected file type: %v", file)
  365. }
  366. }
  367. }
  368. func TestTarUntarWrongPrefix(t *testing.T) {
  369. dir, err := ioutil.TempDir("", "input")
  370. checkErr(t, err)
  371. dir2, err := ioutil.TempDir("", "output")
  372. checkErr(t, err)
  373. dir = dir + "/"
  374. defer func() {
  375. os.RemoveAll(dir)
  376. os.RemoveAll(dir2)
  377. }()
  378. filepath := path.Join(dir, "foo")
  379. if err := os.MkdirAll(path.Dir(filepath), 0755); err != nil {
  380. t.Fatalf("unexpected error: %v", err)
  381. }
  382. createTmpFile(t, filepath, "sample data")
  383. opts := NewCopyOptions(genericclioptions.NewTestIOStreamsDiscard())
  384. writer := &bytes.Buffer{}
  385. if err := makeTar(dir, dir, writer); err != nil {
  386. t.Fatalf("unexpected error: %v", err)
  387. }
  388. reader := bytes.NewBuffer(writer.Bytes())
  389. err = opts.untarAll(reader, dir2, "verylongprefix-showing-the-tar-was-tempered-with")
  390. if err == nil || !strings.Contains(err.Error(), "tar contents corrupted") {
  391. t.Fatalf("unexpected error: %v", err)
  392. }
  393. }
  394. func TestTarDestinationName(t *testing.T) {
  395. dir, err := ioutil.TempDir(os.TempDir(), "input")
  396. dir2, err2 := ioutil.TempDir(os.TempDir(), "output")
  397. if err != nil || err2 != nil {
  398. t.Errorf("unexpected error: %v | %v", err, err2)
  399. t.FailNow()
  400. }
  401. defer func() {
  402. if err := os.RemoveAll(dir); err != nil {
  403. t.Errorf("Unexpected error cleaning up: %v", err)
  404. }
  405. if err := os.RemoveAll(dir2); err != nil {
  406. t.Errorf("Unexpected error cleaning up: %v", err)
  407. }
  408. }()
  409. files := []struct {
  410. name string
  411. data string
  412. }{
  413. {
  414. name: "foo",
  415. data: "foobarbaz",
  416. },
  417. {
  418. name: "dir/blah",
  419. data: "bazblahfoo",
  420. },
  421. {
  422. name: "some/other/directory",
  423. data: "with more data here",
  424. },
  425. {
  426. name: "blah",
  427. data: "same file name different data",
  428. },
  429. }
  430. // ensure files exist on disk
  431. for _, file := range files {
  432. filepath := path.Join(dir, file.name)
  433. if err := os.MkdirAll(path.Dir(filepath), 0755); err != nil {
  434. t.Errorf("unexpected error: %v", err)
  435. t.FailNow()
  436. }
  437. createTmpFile(t, filepath, file.data)
  438. }
  439. reader, writer := io.Pipe()
  440. go func() {
  441. if err := makeTar(dir, dir2, writer); err != nil {
  442. t.Errorf("unexpected error: %v", err)
  443. }
  444. }()
  445. tarReader := tar.NewReader(reader)
  446. for {
  447. hdr, err := tarReader.Next()
  448. if err == io.EOF {
  449. break
  450. } else if err != nil {
  451. t.Errorf("unexpected error: %v", err)
  452. t.FailNow()
  453. }
  454. if !strings.HasPrefix(hdr.Name, path.Base(dir2)) {
  455. t.Errorf("expected %q as destination filename prefix, saw: %q", path.Base(dir2), hdr.Name)
  456. }
  457. }
  458. }
  459. func TestBadTar(t *testing.T) {
  460. dir, err := ioutil.TempDir(os.TempDir(), "dest")
  461. if err != nil {
  462. t.Errorf("unexpected error: %v ", err)
  463. t.FailNow()
  464. }
  465. defer os.RemoveAll(dir)
  466. // More or less cribbed from https://golang.org/pkg/archive/tar/#example__minimal
  467. var buf bytes.Buffer
  468. tw := tar.NewWriter(&buf)
  469. var files = []struct {
  470. name string
  471. body string
  472. }{
  473. {"/prefix/foo/bar/../../home/bburns/names.txt", "Down and back"},
  474. }
  475. for _, file := range files {
  476. hdr := &tar.Header{
  477. Name: file.name,
  478. Mode: 0600,
  479. Size: int64(len(file.body)),
  480. }
  481. if err := tw.WriteHeader(hdr); err != nil {
  482. t.Errorf("unexpected error: %v ", err)
  483. t.FailNow()
  484. }
  485. if _, err := tw.Write([]byte(file.body)); err != nil {
  486. t.Errorf("unexpected error: %v ", err)
  487. t.FailNow()
  488. }
  489. }
  490. if err := tw.Close(); err != nil {
  491. t.Errorf("unexpected error: %v ", err)
  492. t.FailNow()
  493. }
  494. opts := NewCopyOptions(genericclioptions.NewTestIOStreamsDiscard())
  495. if err := opts.untarAll(&buf, dir, "/prefix"); err != nil {
  496. t.Errorf("unexpected error: %v ", err)
  497. t.FailNow()
  498. }
  499. for _, file := range files {
  500. _, err := os.Stat(path.Join(dir, path.Clean(file.name[len("/prefix"):])))
  501. if err != nil {
  502. t.Errorf("Error finding file: %v", err)
  503. }
  504. }
  505. }
  506. // clean prevents path traversals by stripping them out.
  507. // This is adapted from https://golang.org/src/net/http/fs.go#L74
  508. func clean(fileName string) string {
  509. return path.Clean(string(os.PathSeparator) + fileName)
  510. }
  511. func TestClean(t *testing.T) {
  512. tests := []struct {
  513. input string
  514. cleaned string
  515. }{
  516. {
  517. "../../../tmp/foo",
  518. "/tmp/foo",
  519. },
  520. {
  521. "/../../../tmp/foo",
  522. "/tmp/foo",
  523. },
  524. }
  525. for _, test := range tests {
  526. out := clean(test.input)
  527. if out != test.cleaned {
  528. t.Errorf("Expected: %s, saw %s", test.cleaned, out)
  529. }
  530. }
  531. }
  532. func TestCopyToPod(t *testing.T) {
  533. tf := cmdtesting.NewTestFactory().WithNamespace("test")
  534. ns := scheme.Codecs
  535. codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
  536. tf.Client = &fake.RESTClient{
  537. GroupVersion: schema.GroupVersion{Group: "", Version: "v1"},
  538. NegotiatedSerializer: ns,
  539. Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
  540. responsePod := &v1.Pod{}
  541. return &http.Response{StatusCode: http.StatusNotFound, Header: cmdtesting.DefaultHeader(), Body: ioutil.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(codec, responsePod))))}, nil
  542. }),
  543. }
  544. tf.ClientConfigVal = cmdtesting.DefaultClientConfig()
  545. ioStreams, _, _, _ := genericclioptions.NewTestIOStreams()
  546. cmd := NewCmdCp(tf, ioStreams)
  547. srcFile, err := ioutil.TempDir("", "test")
  548. if err != nil {
  549. t.Errorf("unexpected error: %v", err)
  550. t.FailNow()
  551. }
  552. defer os.RemoveAll(srcFile)
  553. tests := map[string]struct {
  554. dest string
  555. expectedErr bool
  556. }{
  557. "copy to directory": {
  558. dest: "/tmp/",
  559. expectedErr: false,
  560. },
  561. "copy to root": {
  562. dest: "/",
  563. expectedErr: false,
  564. },
  565. "copy to empty file name": {
  566. dest: "",
  567. expectedErr: true,
  568. },
  569. }
  570. for name, test := range tests {
  571. opts := NewCopyOptions(ioStreams)
  572. src := fileSpec{
  573. File: srcFile,
  574. }
  575. dest := fileSpec{
  576. PodNamespace: "pod-ns",
  577. PodName: "pod-name",
  578. File: test.dest,
  579. }
  580. opts.Complete(tf, cmd)
  581. t.Run(name, func(t *testing.T) {
  582. err = opts.copyToPod(src, dest, &kexec.ExecOptions{})
  583. //If error is NotFound error , it indicates that the
  584. //request has been sent correctly.
  585. //Treat this as no error.
  586. if test.expectedErr && errors.IsNotFound(err) {
  587. t.Errorf("expected error but didn't get one")
  588. }
  589. if !test.expectedErr && !errors.IsNotFound(err) {
  590. t.Errorf("unexpected error: %v", err)
  591. }
  592. })
  593. }
  594. }
  595. func TestCopyToPodNoPreserve(t *testing.T) {
  596. tf := cmdtesting.NewTestFactory().WithNamespace("test")
  597. ns := scheme.Codecs
  598. codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
  599. tf.Client = &fake.RESTClient{
  600. GroupVersion: schema.GroupVersion{Group: "", Version: "v1"},
  601. NegotiatedSerializer: ns,
  602. Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
  603. responsePod := &v1.Pod{}
  604. return &http.Response{StatusCode: http.StatusNotFound, Header: cmdtesting.DefaultHeader(), Body: ioutil.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(codec, responsePod))))}, nil
  605. }),
  606. }
  607. tf.ClientConfigVal = cmdtesting.DefaultClientConfig()
  608. ioStreams, _, _, _ := genericclioptions.NewTestIOStreams()
  609. cmd := NewCmdCp(tf, ioStreams)
  610. srcFile, err := ioutil.TempDir("", "test")
  611. if err != nil {
  612. t.Errorf("unexpected error: %v", err)
  613. t.FailNow()
  614. }
  615. defer os.RemoveAll(srcFile)
  616. tests := map[string]struct {
  617. expectedCmd []string
  618. nopreserve bool
  619. }{
  620. "copy to pod no preserve user and permissions": {
  621. expectedCmd: []string{"tar", "--no-same-permissions", "--no-same-owner", "-xf", "-", "-C", "."},
  622. nopreserve: true,
  623. },
  624. "copy to pod preserve user and permissions": {
  625. expectedCmd: []string{"tar", "-xf", "-", "-C", "."},
  626. nopreserve: false,
  627. },
  628. }
  629. opts := NewCopyOptions(ioStreams)
  630. src := fileSpec{
  631. File: srcFile,
  632. }
  633. dest := fileSpec{
  634. PodNamespace: "pod-ns",
  635. PodName: "pod-name",
  636. File: "foo",
  637. }
  638. opts.Complete(tf, cmd)
  639. for name, test := range tests {
  640. t.Run(name, func(t *testing.T) {
  641. options := &kexec.ExecOptions{}
  642. opts.NoPreserve = test.nopreserve
  643. err = opts.copyToPod(src, dest, options)
  644. if !(reflect.DeepEqual(test.expectedCmd, options.Command)) {
  645. t.Errorf("expected cmd: %v, got: %v", test.expectedCmd, options.Command)
  646. }
  647. })
  648. }
  649. }
  650. func TestValidate(t *testing.T) {
  651. tests := []struct {
  652. name string
  653. args []string
  654. expectedErr bool
  655. }{
  656. {
  657. name: "Validate Succeed",
  658. args: []string{"1", "2"},
  659. expectedErr: false,
  660. },
  661. {
  662. name: "Validate Fail",
  663. args: []string{"1", "2", "3"},
  664. expectedErr: true,
  665. },
  666. }
  667. tf := cmdtesting.NewTestFactory()
  668. ioStreams, _, _, _ := genericclioptions.NewTestIOStreams()
  669. opts := NewCopyOptions(ioStreams)
  670. cmd := NewCmdCp(tf, ioStreams)
  671. for _, test := range tests {
  672. t.Run(test.name, func(t *testing.T) {
  673. err := opts.Validate(cmd, test.args)
  674. if (err != nil) != test.expectedErr {
  675. t.Errorf("expected error: %v, saw: %v, error: %v", test.expectedErr, err != nil, err)
  676. }
  677. })
  678. }
  679. }
  680. type testFile struct {
  681. path string
  682. linkTarget string // For link types
  683. expected string // Expect to find the file here (or not, if empty)
  684. }
  685. func TestUntar(t *testing.T) {
  686. testdir, err := ioutil.TempDir("", "test-untar")
  687. require.NoError(t, err)
  688. defer os.RemoveAll(testdir)
  689. t.Logf("Test base: %s", testdir)
  690. basedir := filepath.Join(testdir, "base")
  691. files := []testFile{{
  692. // Absolute file within dest
  693. path: filepath.Join(basedir, "abs"),
  694. expected: filepath.Join(basedir, basedir, "abs"),
  695. }, { // Absolute file outside dest
  696. path: filepath.Join(testdir, "abs-out"),
  697. expected: filepath.Join(basedir, testdir, "abs-out"),
  698. }, { // Absolute nested file within dest
  699. path: filepath.Join(basedir, "nested/nest-abs"),
  700. expected: filepath.Join(basedir, basedir, "nested/nest-abs"),
  701. }, { // Absolute nested file outside dest
  702. path: filepath.Join(basedir, "nested/../../nest-abs-out"),
  703. expected: filepath.Join(basedir, testdir, "nest-abs-out"),
  704. }, { // Relative file inside dest
  705. path: "relative",
  706. expected: filepath.Join(basedir, "relative"),
  707. }, { // Relative file outside dest
  708. path: "../unrelative",
  709. expected: "",
  710. }, { // Nested relative file inside dest
  711. path: "nested/nest-rel",
  712. expected: filepath.Join(basedir, "nested/nest-rel"),
  713. }, { // Nested relative file outside dest
  714. path: "nested/../../nest-unrelative",
  715. expected: "",
  716. }}
  717. mkExpectation := func(expected, suffix string) string {
  718. if expected == "" {
  719. return ""
  720. }
  721. return expected + suffix
  722. }
  723. links := []testFile{}
  724. for _, f := range files {
  725. links = append(links, testFile{
  726. path: f.path + "-innerlink",
  727. linkTarget: "link-target",
  728. expected: mkExpectation(f.expected, "-innerlink"),
  729. }, testFile{
  730. path: f.path + "-innerlink-abs",
  731. linkTarget: filepath.Join(basedir, "link-target"),
  732. expected: mkExpectation(f.expected, "-innerlink-abs"),
  733. }, testFile{
  734. path: f.path + "-backlink",
  735. linkTarget: filepath.Join("..", "link-target"),
  736. expected: mkExpectation(f.expected, "-backlink"),
  737. }, testFile{
  738. path: f.path + "-outerlink-abs",
  739. linkTarget: filepath.Join(testdir, "link-target"),
  740. expected: mkExpectation(f.expected, "-outerlink-abs"),
  741. })
  742. if f.expected != "" {
  743. // outerlink is the number of backticks to escape to testdir
  744. outerlink, _ := filepath.Rel(f.expected, testdir)
  745. links = append(links, testFile{
  746. path: f.path + "-outerlink",
  747. linkTarget: filepath.Join(outerlink, "link-target"),
  748. expected: mkExpectation(f.expected, "-outerlink"),
  749. })
  750. }
  751. }
  752. files = append(files, links...)
  753. // Test back-tick escaping through a symlink.
  754. files = append(files,
  755. testFile{
  756. path: "nested/again/back-link",
  757. linkTarget: "../../nested",
  758. expected: filepath.Join(basedir, "nested/again/back-link"),
  759. },
  760. testFile{
  761. path: "nested/again/back-link/../../../back-link-file",
  762. expected: filepath.Join(basedir, "back-link-file"),
  763. })
  764. // Test chaining back-tick symlinks.
  765. files = append(files,
  766. testFile{
  767. path: "nested/back-link-first",
  768. linkTarget: "../",
  769. expected: filepath.Join(basedir, "nested/back-link-first"),
  770. },
  771. testFile{
  772. path: "nested/back-link-second",
  773. linkTarget: "back-link-first/..",
  774. expected: filepath.Join(basedir, "nested/back-link-second"),
  775. })
  776. files = append(files,
  777. testFile{ // Relative directory path with terminating /
  778. path: "direct/dir/",
  779. expected: "",
  780. })
  781. buf := makeTestTar(t, files)
  782. // Capture warnings to stderr for debugging.
  783. output := (*testWriter)(t)
  784. opts := NewCopyOptions(genericclioptions.IOStreams{In: &bytes.Buffer{}, Out: output, ErrOut: output})
  785. require.NoError(t, opts.untarAll(buf, filepath.Join(basedir), ""))
  786. expectations := map[string]bool{}
  787. for _, f := range files {
  788. if f.expected != "" {
  789. expectations[f.expected] = false
  790. }
  791. }
  792. filepath.Walk(testdir, func(path string, info os.FileInfo, err error) error {
  793. if err != nil {
  794. return err
  795. }
  796. if info.IsDir() {
  797. return nil // Ignore directories.
  798. }
  799. if _, ok := expectations[path]; !ok {
  800. t.Errorf("Unexpected file at %s", path)
  801. } else {
  802. expectations[path] = true
  803. }
  804. return nil
  805. })
  806. for path, found := range expectations {
  807. if !found {
  808. t.Errorf("Missing expected file %s", path)
  809. }
  810. }
  811. }
  812. func TestUntar_NestedSymlinks(t *testing.T) {
  813. testdir, err := ioutil.TempDir("", "test-untar-nested")
  814. require.NoError(t, err)
  815. defer os.RemoveAll(testdir)
  816. t.Logf("Test base: %s", testdir)
  817. basedir := filepath.Join(testdir, "base")
  818. // Test chaining back-tick symlinks.
  819. backLinkFirst := testFile{
  820. path: "nested/back-link-first",
  821. linkTarget: "../",
  822. expected: filepath.Join(basedir, "nested/back-link-first"),
  823. }
  824. files := []testFile{backLinkFirst, {
  825. path: "nested/back-link-first/back-link-second",
  826. linkTarget: "../",
  827. expected: "",
  828. }}
  829. buf := makeTestTar(t, files)
  830. // Capture warnings to stderr for debugging.
  831. output := (*testWriter)(t)
  832. opts := NewCopyOptions(genericclioptions.IOStreams{In: &bytes.Buffer{}, Out: output, ErrOut: output})
  833. // Expect untarAll to fail. The second link will trigger a directory to be created at
  834. // "nested/back-link-first", which should trigger a file exists error when the back-link-first
  835. // symlink is created.
  836. expectedErr := os.LinkError{
  837. Op: "symlink",
  838. Old: backLinkFirst.linkTarget,
  839. New: backLinkFirst.expected,
  840. Err: fmt.Errorf("file exists")}
  841. actualErr := opts.untarAll(buf, filepath.Join(basedir), "")
  842. assert.EqualError(t, actualErr, expectedErr.Error())
  843. }
  844. func makeTestTar(t *testing.T, files []testFile) *bytes.Buffer {
  845. buf := &bytes.Buffer{}
  846. tw := tar.NewWriter(buf)
  847. for _, f := range files {
  848. if f.linkTarget == "" {
  849. hdr := &tar.Header{
  850. Name: f.path,
  851. Mode: 0666,
  852. Size: int64(len(f.path)),
  853. }
  854. require.NoError(t, tw.WriteHeader(hdr), f.path)
  855. if !strings.HasSuffix(f.path, "/") {
  856. _, err := tw.Write([]byte(f.path))
  857. require.NoError(t, err, f.path)
  858. }
  859. } else {
  860. hdr := &tar.Header{
  861. Name: f.path,
  862. Mode: int64(0777 | os.ModeSymlink),
  863. Typeflag: tar.TypeSymlink,
  864. Linkname: f.linkTarget,
  865. }
  866. require.NoError(t, tw.WriteHeader(hdr), f.path)
  867. }
  868. }
  869. tw.Close()
  870. return buf
  871. }
  872. func TestUntar_SingleFile(t *testing.T) {
  873. testdir, err := ioutil.TempDir("", "test-untar")
  874. require.NoError(t, err)
  875. defer os.RemoveAll(testdir)
  876. dest := filepath.Join(testdir, "target")
  877. buf := &bytes.Buffer{}
  878. tw := tar.NewWriter(buf)
  879. const (
  880. srcName = "source"
  881. content = "file contents"
  882. )
  883. hdr := &tar.Header{
  884. Name: srcName,
  885. Mode: 0666,
  886. Size: int64(len(content)),
  887. }
  888. require.NoError(t, tw.WriteHeader(hdr))
  889. _, err = tw.Write([]byte(content))
  890. require.NoError(t, err)
  891. tw.Close()
  892. // Capture warnings to stderr for debugging.
  893. output := (*testWriter)(t)
  894. opts := NewCopyOptions(genericclioptions.IOStreams{In: &bytes.Buffer{}, Out: output, ErrOut: output})
  895. require.NoError(t, opts.untarAll(buf, filepath.Join(dest), srcName))
  896. cmpFileData(t, dest, content)
  897. }
  898. func createTmpFile(t *testing.T, filepath, data string) {
  899. f, err := os.Create(filepath)
  900. if err != nil {
  901. t.Fatalf("unexpected error: %v", err)
  902. }
  903. defer f.Close()
  904. if _, err := io.Copy(f, bytes.NewBuffer([]byte(data))); err != nil {
  905. t.Fatalf("unexpected error: %v", err)
  906. }
  907. if err := f.Close(); err != nil {
  908. t.Fatal(err)
  909. }
  910. }
  911. func cmpFileData(t *testing.T, filePath, data string) {
  912. actual, err := ioutil.ReadFile(filePath)
  913. require.NoError(t, err)
  914. assert.EqualValues(t, data, actual)
  915. }
  916. type testWriter testing.T
  917. func (t *testWriter) Write(p []byte) (n int, err error) {
  918. t.Logf(string(p))
  919. return len(p), nil
  920. }