2015-05-04 17:13:55 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors.
|
2015-05-04 17:13:55 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2015-09-27 00:00:39 +00:00
|
|
|
"net/url"
|
2016-08-04 06:31:23 +00:00
|
|
|
"os"
|
2015-05-04 17:13:55 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2017-10-28 01:31:42 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
2017-01-24 15:00:24 +00:00
|
|
|
"k8s.io/client-go/rest/fake"
|
2017-11-08 22:34:54 +00:00
|
|
|
api "k8s.io/kubernetes/pkg/apis/core"
|
2016-10-18 22:53:26 +00:00
|
|
|
cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
|
2015-05-04 17:13:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type fakePortForwarder struct {
|
2015-09-27 00:00:39 +00:00
|
|
|
method string
|
|
|
|
url *url.URL
|
|
|
|
pfErr error
|
2015-05-04 17:13:55 +00:00
|
|
|
}
|
|
|
|
|
2016-08-08 12:31:15 +00:00
|
|
|
func (f *fakePortForwarder) ForwardPorts(method string, url *url.URL, opts PortForwardOptions) error {
|
2015-09-27 00:00:39 +00:00
|
|
|
f.method = method
|
|
|
|
f.url = url
|
2015-05-04 17:13:55 +00:00
|
|
|
return f.pfErr
|
|
|
|
}
|
|
|
|
|
2016-10-14 07:10:45 +00:00
|
|
|
func testPortForward(t *testing.T, flags map[string]string, args []string) {
|
2017-10-28 01:31:42 +00:00
|
|
|
version := "v1"
|
2015-05-04 17:13:55 +00:00
|
|
|
|
2015-07-13 08:25:22 +00:00
|
|
|
tests := []struct {
|
2018-01-15 08:02:35 +00:00
|
|
|
name string
|
|
|
|
podPath, pfPath string
|
|
|
|
pod *api.Pod
|
|
|
|
pfErr bool
|
2015-07-13 08:25:22 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "pod portforward",
|
|
|
|
podPath: "/api/" + version + "/namespaces/test/pods/foo",
|
|
|
|
pfPath: "/api/" + version + "/namespaces/test/pods/foo/portforward",
|
|
|
|
pod: execPod(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "pod portforward error",
|
|
|
|
podPath: "/api/" + version + "/namespaces/test/pods/foo",
|
|
|
|
pfPath: "/api/" + version + "/namespaces/test/pods/foo/portforward",
|
|
|
|
pod: execPod(),
|
|
|
|
pfErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
2016-08-04 06:31:23 +00:00
|
|
|
var err error
|
2016-10-18 22:53:26 +00:00
|
|
|
f, tf, codec, ns := cmdtesting.NewAPIFactory()
|
2015-09-11 20:47:53 +00:00
|
|
|
tf.Client = &fake.RESTClient{
|
2017-10-28 01:31:42 +00:00
|
|
|
GroupVersion: schema.GroupVersion{Group: ""},
|
2016-05-04 10:24:16 +00:00
|
|
|
NegotiatedSerializer: ns,
|
2015-11-11 19:54:58 +00:00
|
|
|
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
|
2015-07-13 08:25:22 +00:00
|
|
|
switch p, m := req.URL.Path, req.Method; {
|
|
|
|
case p == test.podPath && m == "GET":
|
|
|
|
body := objBody(codec, test.pod)
|
2016-05-11 07:50:47 +00:00
|
|
|
return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: body}, nil
|
2015-07-13 08:25:22 +00:00
|
|
|
default:
|
|
|
|
// Ensures no GET is performed when deleting by name
|
|
|
|
t.Errorf("%s: unexpected request: %#v\n%#v", test.name, req.URL, req)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
tf.Namespace = "test"
|
2016-09-07 20:29:57 +00:00
|
|
|
tf.ClientConfig = defaultClientConfig()
|
2015-07-13 08:25:22 +00:00
|
|
|
ff := &fakePortForwarder{}
|
|
|
|
if test.pfErr {
|
|
|
|
ff.pfErr = fmt.Errorf("pf error")
|
|
|
|
}
|
2016-08-04 06:31:23 +00:00
|
|
|
|
|
|
|
opts := &PortForwardOptions{}
|
|
|
|
cmd := NewCmdPortForward(f, os.Stdout, os.Stderr)
|
|
|
|
cmd.Run = func(cmd *cobra.Command, args []string) {
|
2017-03-17 13:55:46 +00:00
|
|
|
if err = opts.Complete(f, cmd, args); err != nil {
|
2016-08-04 06:31:23 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
opts.PortForwarder = ff
|
|
|
|
if err = opts.Validate(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = opts.RunPortForward()
|
|
|
|
}
|
|
|
|
|
2016-10-14 07:10:45 +00:00
|
|
|
for name, value := range flags {
|
|
|
|
cmd.Flags().Set(name, value)
|
|
|
|
}
|
|
|
|
cmd.Run(cmd, args)
|
2015-07-13 08:25:22 +00:00
|
|
|
|
|
|
|
if test.pfErr && err != ff.pfErr {
|
2016-08-08 12:31:15 +00:00
|
|
|
t.Errorf("%s: Unexpected port-forward error: %v", test.name, err)
|
2015-07-13 08:25:22 +00:00
|
|
|
}
|
|
|
|
if !test.pfErr && err != nil {
|
|
|
|
t.Errorf("%s: Unexpected error: %v", test.name, err)
|
|
|
|
}
|
2015-09-27 00:00:39 +00:00
|
|
|
if test.pfErr {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if ff.url.Path != test.pfPath {
|
|
|
|
t.Errorf("%s: Did not get expected path for portforward request", test.name)
|
|
|
|
}
|
|
|
|
if ff.method != "POST" {
|
|
|
|
t.Errorf("%s: Did not get method for attach request: %s", test.name, ff.method)
|
|
|
|
}
|
2015-07-13 08:25:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-14 07:10:45 +00:00
|
|
|
func TestPortForward(t *testing.T) {
|
|
|
|
testPortForward(t, nil, []string{"foo", ":5000", ":1000"})
|
|
|
|
}
|
2016-08-04 06:31:23 +00:00
|
|
|
|
2016-10-14 07:10:45 +00:00
|
|
|
func TestPortForwardWithPFlag(t *testing.T) {
|
|
|
|
testPortForward(t, map[string]string{"pod": "foo"}, []string{":5000", ":1000"})
|
2015-05-04 17:13:55 +00:00
|
|
|
}
|