k3s/pkg/kubectl/cmd/run_test.go

333 lines
8.4 KiB
Go
Raw Normal View History

2015-08-04 19:54:17 +00:00
/*
Copyright 2014 The Kubernetes Authors All rights reserved.
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 (
2015-10-20 18:27:26 +00:00
"bytes"
"fmt"
"io/ioutil"
"net/http"
2015-09-14 12:18:42 +00:00
"os"
"reflect"
2015-08-04 19:54:17 +00:00
"testing"
"github.com/spf13/cobra"
"k8s.io/kubernetes/pkg/api"
2015-10-20 18:27:26 +00:00
"k8s.io/kubernetes/pkg/api/testapi"
2016-02-12 18:58:43 +00:00
"k8s.io/kubernetes/pkg/client/restclient"
2015-10-20 18:27:26 +00:00
"k8s.io/kubernetes/pkg/client/unversioned/fake"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util/intstr"
2015-08-04 19:54:17 +00:00
)
func TestGetRestartPolicy(t *testing.T) {
tests := []struct {
input string
interactive bool
expected api.RestartPolicy
expectErr bool
}{
{
input: "",
expected: api.RestartPolicyAlways,
},
{
input: "",
interactive: true,
expected: api.RestartPolicyOnFailure,
},
{
input: string(api.RestartPolicyAlways),
interactive: true,
expected: api.RestartPolicyAlways,
},
{
input: string(api.RestartPolicyNever),
interactive: true,
expected: api.RestartPolicyNever,
},
{
input: string(api.RestartPolicyAlways),
expected: api.RestartPolicyAlways,
},
{
input: string(api.RestartPolicyNever),
expected: api.RestartPolicyNever,
},
{
input: "foo",
expectErr: true,
},
}
for _, test := range tests {
cmd := &cobra.Command{}
cmd.Flags().String("restart", "", "dummy restart flag")
cmd.Flags().Lookup("restart").Value.Set(test.input)
policy, err := getRestartPolicy(cmd, test.interactive)
if test.expectErr && err == nil {
t.Error("unexpected non-error")
}
if !test.expectErr && err != nil {
t.Errorf("unexpected error: %v", err)
}
if !test.expectErr && policy != test.expected {
t.Errorf("expected: %s, saw: %s (%s:%v)", test.expected, policy, test.input, test.interactive)
}
}
}
func TestGetEnv(t *testing.T) {
test := struct {
input []string
expected []string
}{
input: []string{"a=b", "c=d"},
expected: []string{"a=b", "c=d"},
}
cmd := &cobra.Command{}
cmd.Flags().StringSlice("env", test.input, "")
envStrings := cmdutil.GetFlagStringSlice(cmd, "env")
if len(envStrings) != 2 || !reflect.DeepEqual(envStrings, test.expected) {
t.Errorf("expected: %s, saw: %s", test.expected, envStrings)
}
}
2015-10-20 18:27:26 +00:00
2015-09-14 12:18:42 +00:00
func TestRunArgsFollowDashRules(t *testing.T) {
_, _, rc := testData()
tests := []struct {
args []string
argsLenAtDash int
expectError bool
name string
}{
{
args: []string{},
argsLenAtDash: -1,
expectError: true,
name: "empty",
},
{
args: []string{"foo"},
argsLenAtDash: -1,
expectError: false,
name: "no cmd",
},
{
args: []string{"foo", "sleep"},
argsLenAtDash: -1,
expectError: false,
name: "cmd no dash",
},
{
args: []string{"foo", "sleep"},
argsLenAtDash: 1,
expectError: false,
name: "cmd has dash",
},
{
args: []string{"foo", "sleep"},
argsLenAtDash: 0,
expectError: true,
name: "no name",
},
}
for _, test := range tests {
f, tf, codec := NewAPIFactory()
tf.Client = &fake.RESTClient{
Codec: codec,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
2016-05-11 07:50:47 +00:00
return &http.Response{StatusCode: 201, Header: defaultHeader(), Body: objBody(codec, &rc.Items[0])}, nil
2015-09-14 12:18:42 +00:00
}),
}
tf.Namespace = "test"
2016-02-12 18:58:43 +00:00
tf.ClientConfig = &restclient.Config{}
2015-09-14 12:18:42 +00:00
cmd := NewCmdRun(f, os.Stdin, os.Stdout, os.Stderr)
cmd.Flags().Set("image", "nginx")
cmd.Flags().Set("generator", "run/v1")
2015-09-14 12:18:42 +00:00
err := Run(f, os.Stdin, os.Stdout, os.Stderr, cmd, test.args, test.argsLenAtDash)
if test.expectError && err == nil {
t.Errorf("unexpected non-error (%s)", test.name)
}
if !test.expectError && err != nil {
t.Errorf("unexpected error: %v (%s)", err, test.name)
}
}
}
2015-10-20 18:27:26 +00:00
func TestGenerateService(t *testing.T) {
tests := []struct {
port string
args []string
serviceGenerator string
params map[string]interface{}
expectErr bool
name string
service api.Service
expectPOST bool
}{
{
port: "80",
args: []string{"foo"},
serviceGenerator: "service/v2",
params: map[string]interface{}{
"name": "foo",
},
expectErr: false,
name: "basic",
service: api.Service{
ObjectMeta: api.ObjectMeta{
Name: "foo",
},
Spec: api.ServiceSpec{
Ports: []api.ServicePort{
{
Port: 80,
Protocol: "TCP",
TargetPort: intstr.FromInt(80),
2015-10-20 18:27:26 +00:00
},
},
Selector: map[string]string{
"run": "foo",
},
Type: api.ServiceTypeClusterIP,
SessionAffinity: api.ServiceAffinityNone,
},
},
expectPOST: true,
},
{
port: "80",
args: []string{"foo"},
serviceGenerator: "service/v2",
params: map[string]interface{}{
"name": "foo",
"labels": "app=bar",
},
expectErr: false,
name: "custom labels",
service: api.Service{
ObjectMeta: api.ObjectMeta{
Name: "foo",
Labels: map[string]string{"app": "bar"},
},
Spec: api.ServiceSpec{
Ports: []api.ServicePort{
{
Port: 80,
Protocol: "TCP",
TargetPort: intstr.FromInt(80),
2015-10-20 18:27:26 +00:00
},
},
Selector: map[string]string{
"app": "bar",
},
Type: api.ServiceTypeClusterIP,
SessionAffinity: api.ServiceAffinityNone,
},
},
expectPOST: true,
},
{
expectErr: true,
name: "missing port",
expectPOST: false,
},
{
port: "80",
args: []string{"foo"},
serviceGenerator: "service/v2",
params: map[string]interface{}{
"name": "foo",
},
expectErr: false,
name: "dry-run",
expectPOST: false,
},
}
for _, test := range tests {
sawPOST := false
f, tf, codec := NewAPIFactory()
2016-02-12 18:58:43 +00:00
tf.ClientConfig = &restclient.Config{ContentConfig: restclient.ContentConfig{GroupVersion: testapi.Default.GroupVersion()}}
2015-10-20 18:27:26 +00:00
tf.Client = &fake.RESTClient{
Codec: codec,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
2015-10-20 18:27:26 +00:00
switch p, m := req.URL.Path, req.Method; {
case test.expectPOST && m == "POST" && p == "/namespaces/namespace/services":
sawPOST = true
body := objBody(codec, &test.service)
data, err := ioutil.ReadAll(req.Body)
if err != nil {
t.Errorf("unexpected error: %v", err)
t.FailNow()
}
defer req.Body.Close()
svc := &api.Service{}
if err := runtime.DecodeInto(codec, data, svc); err != nil {
2015-10-20 18:27:26 +00:00
t.Errorf("unexpected error: %v", err)
t.FailNow()
}
// Copy things that are defaulted by the system
test.service.Annotations = svc.Annotations
if !reflect.DeepEqual(&test.service, svc) {
t.Errorf("expected:\n%v\nsaw:\n%v\n", &test.service, svc)
}
2016-05-11 07:50:47 +00:00
return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: body}, nil
2015-10-20 18:27:26 +00:00
default:
// Ensures no GET is performed when deleting by name
t.Errorf("%s: unexpected request: %s %#v\n%#v", test.name, req.Method, req.URL, req)
return nil, fmt.Errorf("unexpected request")
}
}),
}
cmd := &cobra.Command{}
cmd.Flags().String("output", "", "")
cmd.Flags().Bool(cmdutil.ApplyAnnotationsFlag, false, "")
2016-01-22 18:33:23 +00:00
cmd.Flags().Bool("record", false, "Record current kubectl command in the resource annotation.")
2016-03-10 01:27:19 +00:00
cmdutil.AddInclude3rdPartyFlags(cmd)
2015-10-20 18:27:26 +00:00
addRunFlags(cmd)
if !test.expectPOST {
cmd.Flags().Set("dry-run", "true")
}
if len(test.port) > 0 {
cmd.Flags().Set("port", test.port)
test.params["port"] = test.port
}
buff := &bytes.Buffer{}
err := generateService(f, cmd, test.args, test.serviceGenerator, test.params, "namespace", buff)
if test.expectErr {
if err == nil {
t.Error("unexpected non-error")
}
continue
}
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if test.expectPOST != sawPOST {
t.Errorf("expectPost: %v, sawPost: %v", test.expectPOST, sawPOST)
2015-10-20 18:27:26 +00:00
}
}
}