MCPcopy
hub / github.com/kubernetes/kubectl / TestCopyToPod

Function TestCopyToPod

pkg/cmd/cp/cp_test.go:611–680  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

609}
610
611func TestCopyToPod(t *testing.T) {
612 tf := cmdtesting.NewTestFactory().WithNamespace("test")
613 ns := scheme.Codecs.WithoutConversion()
614 codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
615
616 tf.Client = &fake.RESTClient{
617 GroupVersion: schema.GroupVersion{Group: "", Version: "v1"},
618 NegotiatedSerializer: ns,
619 Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
620 responsePod := &v1.Pod{}
621 return &http.Response{StatusCode: http.StatusNotFound, Header: cmdtesting.DefaultHeader(), Body: io.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(codec, responsePod))))}, nil
622 }),
623 }
624
625 tf.ClientConfigVal = cmdtesting.DefaultClientConfig()
626 ioStreams, _, _, _ := genericiooptions.NewTestIOStreams()
627
628 cmd := NewCmdCp(tf, ioStreams)
629
630 srcFile, err := os.MkdirTemp("", "test")
631 if err != nil {
632 t.Errorf("unexpected error: %v", err)
633 t.FailNow()
634 }
635 defer os.RemoveAll(srcFile)
636
637 tests := map[string]struct {
638 src string
639 dest string
640 expectedErr bool
641 }{
642 "copy to directory": {
643 src: srcFile,
644 dest: "/tmp/",
645 expectedErr: false,
646 },
647 "copy to root": {
648 src: srcFile,
649 dest: "/",
650 expectedErr: false,
651 },
652 "copy to empty file name": {
653 src: srcFile,
654 dest: "",
655 expectedErr: true,
656 },
657 "copy unexisting file": {
658 src: filepath.Join(srcFile, "nope"),
659 dest: "/tmp",
660 expectedErr: true,
661 },
662 }
663
664 for name, test := range tests {
665 opts := NewCopyOptions(ioStreams)
666 opts.Complete(tf, cmd, []string{test.src, fmt.Sprintf("pod-ns/pod-name:%s", test.dest)})
667 t.Run(name, func(t *testing.T) {
668 err = opts.Run()

Callers

nothing calls this directly

Calls 6

CompleteMethod · 0.95
RunMethod · 0.95
NewCmdCpFunction · 0.85
NewCopyOptionsFunction · 0.85
WithNamespaceMethod · 0.80
JoinMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…