2015-04-23 23:27:19 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors.
|
2015-04-23 23:27:19 +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 (
|
|
|
|
"testing"
|
2016-10-18 22:53:26 +00:00
|
|
|
|
|
|
|
cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
|
2018-04-24 04:40:35 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
|
2015-04-23 23:27:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestValidateArgs(t *testing.T) {
|
2018-02-22 14:52:10 +00:00
|
|
|
f := cmdtesting.NewTestFactory()
|
2018-03-08 22:23:55 +00:00
|
|
|
defer f.Cleanup()
|
2015-04-23 23:27:19 +00:00
|
|
|
|
|
|
|
tests := []struct {
|
2017-05-11 12:09:12 +00:00
|
|
|
testName string
|
2015-04-23 23:27:19 +00:00
|
|
|
flags map[string]string
|
2015-08-14 18:46:43 +00:00
|
|
|
filenames []string
|
2015-04-23 23:27:19 +00:00
|
|
|
args []string
|
|
|
|
expectErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
testName: "nothing",
|
2017-05-11 12:09:12 +00:00
|
|
|
expectErr: true,
|
2015-04-23 23:27:19 +00:00
|
|
|
},
|
|
|
|
{
|
2017-05-11 12:09:12 +00:00
|
|
|
testName: "no file, no image",
|
2015-04-23 23:27:19 +00:00
|
|
|
flags: map[string]string{},
|
|
|
|
args: []string{"foo"},
|
|
|
|
expectErr: true,
|
|
|
|
},
|
|
|
|
{
|
2017-05-11 12:09:12 +00:00
|
|
|
testName: "valid file example",
|
2015-08-14 18:46:43 +00:00
|
|
|
filenames: []string{"bar.yaml"},
|
|
|
|
args: []string{"foo"},
|
2015-04-23 23:27:19 +00:00
|
|
|
},
|
|
|
|
{
|
2017-05-11 12:09:12 +00:00
|
|
|
testName: "missing second image name",
|
2015-04-23 23:27:19 +00:00
|
|
|
flags: map[string]string{
|
|
|
|
"image": "foo:v2",
|
|
|
|
},
|
2017-05-11 12:09:12 +00:00
|
|
|
args: []string{"foo"},
|
2015-04-23 23:27:19 +00:00
|
|
|
},
|
|
|
|
{
|
2017-05-11 12:09:12 +00:00
|
|
|
testName: "valid image example",
|
2015-04-23 23:27:19 +00:00
|
|
|
flags: map[string]string{
|
|
|
|
"image": "foo:v2",
|
|
|
|
},
|
2017-05-11 12:09:12 +00:00
|
|
|
args: []string{"foo", "foo-v2"},
|
2015-04-23 23:27:19 +00:00
|
|
|
},
|
|
|
|
{
|
2017-05-11 12:09:12 +00:00
|
|
|
testName: "both filename and image example",
|
2015-04-23 23:27:19 +00:00
|
|
|
flags: map[string]string{
|
2015-08-14 18:46:43 +00:00
|
|
|
"image": "foo:v2",
|
2015-04-23 23:27:19 +00:00
|
|
|
},
|
2015-08-14 18:46:43 +00:00
|
|
|
filenames: []string{"bar.yaml"},
|
2015-04-23 23:27:19 +00:00
|
|
|
args: []string{"foo", "foo-v2"},
|
|
|
|
expectErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
2018-04-24 04:40:35 +00:00
|
|
|
cmd := NewCmdRollingUpdate(f, genericclioptions.NewTestIOStreamsDiscard())
|
2015-04-23 23:27:19 +00:00
|
|
|
|
|
|
|
if test.flags != nil {
|
|
|
|
for key, val := range test.flags {
|
|
|
|
cmd.Flags().Set(key, val)
|
|
|
|
}
|
|
|
|
}
|
2015-09-25 02:55:03 +00:00
|
|
|
err := validateArguments(cmd, test.filenames, test.args)
|
2015-04-23 23:27:19 +00:00
|
|
|
if err != nil && !test.expectErr {
|
2017-05-11 12:09:12 +00:00
|
|
|
t.Errorf("%s: unexpected error: %v", test.testName, err)
|
2015-04-23 23:27:19 +00:00
|
|
|
}
|
|
|
|
if err == nil && test.expectErr {
|
2017-05-11 12:09:12 +00:00
|
|
|
t.Errorf("%s: unexpected non-error", test.testName)
|
2015-04-23 23:27:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|