mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-03 19:58:17 +00:00 
			
		
		
		
	Merge pull request #3471 from brendandburns/shell
Add tests for run command and generate.
This commit is contained in:
		@@ -56,7 +56,7 @@ Examples:
 | 
				
			|||||||
				usageError(cmd, fmt.Sprintf("Generator: %s not found.", generator))
 | 
									usageError(cmd, fmt.Sprintf("Generator: %s not found.", generator))
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			names := generator.ParamNames()
 | 
								names := generator.ParamNames()
 | 
				
			||||||
			params, err := kubectl.MakeParams(cmd, names)
 | 
								params := kubectl.MakeParams(cmd, names)
 | 
				
			||||||
			params["name"] = args[0]
 | 
								params["name"] = args[0]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			err = kubectl.ValidateParams(names, params)
 | 
								err = kubectl.ValidateParams(names, params)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -58,7 +58,7 @@ func ValidateParams(paramSpec []GeneratorParam, params map[string]string) error
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// MakeParams is a utility that creates generator parameters from a command line
 | 
					// MakeParams is a utility that creates generator parameters from a command line
 | 
				
			||||||
func MakeParams(cmd *cobra.Command, params []GeneratorParam) (map[string]string, error) {
 | 
					func MakeParams(cmd *cobra.Command, params []GeneratorParam) map[string]string {
 | 
				
			||||||
	result := map[string]string{}
 | 
						result := map[string]string{}
 | 
				
			||||||
	for ix := range params {
 | 
						for ix := range params {
 | 
				
			||||||
		f := cmd.Flags().Lookup(params[ix].Name)
 | 
							f := cmd.Flags().Lookup(params[ix].Name)
 | 
				
			||||||
@@ -66,5 +66,5 @@ func MakeParams(cmd *cobra.Command, params []GeneratorParam) (map[string]string,
 | 
				
			|||||||
			result[params[ix].Name] = f.Value.String()
 | 
								result[params[ix].Name] = f.Value.String()
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return result, nil
 | 
						return result
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										114
									
								
								pkg/kubectl/generate_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										114
									
								
								pkg/kubectl/generate_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,114 @@
 | 
				
			|||||||
 | 
					/*
 | 
				
			||||||
 | 
					Copyright 2014 Google Inc. 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 kubectl
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"reflect"
 | 
				
			||||||
 | 
						"testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/spf13/cobra"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestValidateParams(t *testing.T) {
 | 
				
			||||||
 | 
						tests := []struct {
 | 
				
			||||||
 | 
							paramSpec []GeneratorParam
 | 
				
			||||||
 | 
							params    map[string]string
 | 
				
			||||||
 | 
							valid     bool
 | 
				
			||||||
 | 
						}{
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								paramSpec: []GeneratorParam{},
 | 
				
			||||||
 | 
								params:    map[string]string{},
 | 
				
			||||||
 | 
								valid:     true,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								paramSpec: []GeneratorParam{
 | 
				
			||||||
 | 
									{Name: "foo"},
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								params: map[string]string{},
 | 
				
			||||||
 | 
								valid:  true,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								paramSpec: []GeneratorParam{
 | 
				
			||||||
 | 
									{Name: "foo", Required: true},
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								params: map[string]string{
 | 
				
			||||||
 | 
									"foo": "bar",
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								valid: true,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								paramSpec: []GeneratorParam{
 | 
				
			||||||
 | 
									{Name: "foo", Required: true},
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								params: map[string]string{
 | 
				
			||||||
 | 
									"baz": "blah",
 | 
				
			||||||
 | 
									"foo": "bar",
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								valid: true,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								paramSpec: []GeneratorParam{
 | 
				
			||||||
 | 
									{Name: "foo", Required: true},
 | 
				
			||||||
 | 
									{Name: "baz", Required: true},
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								params: map[string]string{
 | 
				
			||||||
 | 
									"baz": "blah",
 | 
				
			||||||
 | 
									"foo": "bar",
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								valid: true,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								paramSpec: []GeneratorParam{
 | 
				
			||||||
 | 
									{Name: "foo", Required: true},
 | 
				
			||||||
 | 
									{Name: "baz", Required: true},
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								params: map[string]string{
 | 
				
			||||||
 | 
									"foo": "bar",
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								valid: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						for _, test := range tests {
 | 
				
			||||||
 | 
							err := ValidateParams(test.paramSpec, test.params)
 | 
				
			||||||
 | 
							if test.valid && err != nil {
 | 
				
			||||||
 | 
								t.Errorf("unexpected error: %v", err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if !test.valid && err == nil {
 | 
				
			||||||
 | 
								t.Errorf("unexpected non-error")
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestMakeParams(t *testing.T) {
 | 
				
			||||||
 | 
						cmd := &cobra.Command{}
 | 
				
			||||||
 | 
						cmd.Flags().String("foo", "bar", "")
 | 
				
			||||||
 | 
						cmd.Flags().String("baz", "", "")
 | 
				
			||||||
 | 
						cmd.Flags().Set("baz", "blah")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						paramSpec := []GeneratorParam{
 | 
				
			||||||
 | 
							{Name: "foo", Required: true},
 | 
				
			||||||
 | 
							{Name: "baz", Required: true},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						expected := map[string]string{
 | 
				
			||||||
 | 
							"foo": "bar",
 | 
				
			||||||
 | 
							"baz": "blah",
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						params := MakeParams(cmd, paramSpec)
 | 
				
			||||||
 | 
						if !reflect.DeepEqual(params, expected) {
 | 
				
			||||||
 | 
							t.Errorf("\nexpected:\n%v\nsaw:\n%v", expected, params)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										104
									
								
								pkg/kubectl/run_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										104
									
								
								pkg/kubectl/run_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,104 @@
 | 
				
			|||||||
 | 
					/*
 | 
				
			||||||
 | 
					Copyright 2014 Google Inc. 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 kubectl
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"reflect"
 | 
				
			||||||
 | 
						"testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestGenerate(t *testing.T) {
 | 
				
			||||||
 | 
						tests := []struct {
 | 
				
			||||||
 | 
							params    map[string]string
 | 
				
			||||||
 | 
							expected  *api.ReplicationController
 | 
				
			||||||
 | 
							expectErr bool
 | 
				
			||||||
 | 
						}{
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								params: map[string]string{
 | 
				
			||||||
 | 
									"name":     "foo",
 | 
				
			||||||
 | 
									"image":    "someimage",
 | 
				
			||||||
 | 
									"replicas": "1",
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								expected: &api.ReplicationController{
 | 
				
			||||||
 | 
									ObjectMeta: api.ObjectMeta{
 | 
				
			||||||
 | 
										Name:   "foo",
 | 
				
			||||||
 | 
										Labels: map[string]string{"run-container": "foo"},
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
									Spec: api.ReplicationControllerSpec{
 | 
				
			||||||
 | 
										Replicas: 1,
 | 
				
			||||||
 | 
										Selector: map[string]string{"run-container": "foo"},
 | 
				
			||||||
 | 
										Template: &api.PodTemplateSpec{
 | 
				
			||||||
 | 
											ObjectMeta: api.ObjectMeta{
 | 
				
			||||||
 | 
												Labels: map[string]string{"run-container": "foo"},
 | 
				
			||||||
 | 
											},
 | 
				
			||||||
 | 
											Spec: api.PodSpec{
 | 
				
			||||||
 | 
												Containers: []api.Container{
 | 
				
			||||||
 | 
													{
 | 
				
			||||||
 | 
														Name:  "foo",
 | 
				
			||||||
 | 
														Image: "someimage",
 | 
				
			||||||
 | 
													},
 | 
				
			||||||
 | 
												},
 | 
				
			||||||
 | 
											},
 | 
				
			||||||
 | 
										},
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								params: map[string]string{
 | 
				
			||||||
 | 
									"name":     "foo",
 | 
				
			||||||
 | 
									"image":    "someimage",
 | 
				
			||||||
 | 
									"replicas": "1",
 | 
				
			||||||
 | 
									"labels":   "foo=bar,baz=blah",
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								expected: &api.ReplicationController{
 | 
				
			||||||
 | 
									ObjectMeta: api.ObjectMeta{
 | 
				
			||||||
 | 
										Name:   "foo",
 | 
				
			||||||
 | 
										Labels: map[string]string{"foo": "bar", "baz": "blah"},
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
									Spec: api.ReplicationControllerSpec{
 | 
				
			||||||
 | 
										Replicas: 1,
 | 
				
			||||||
 | 
										Selector: map[string]string{"foo": "bar", "baz": "blah"},
 | 
				
			||||||
 | 
										Template: &api.PodTemplateSpec{
 | 
				
			||||||
 | 
											ObjectMeta: api.ObjectMeta{
 | 
				
			||||||
 | 
												Labels: map[string]string{"foo": "bar", "baz": "blah"},
 | 
				
			||||||
 | 
											},
 | 
				
			||||||
 | 
											Spec: api.PodSpec{
 | 
				
			||||||
 | 
												Containers: []api.Container{
 | 
				
			||||||
 | 
													{
 | 
				
			||||||
 | 
														Name:  "foo",
 | 
				
			||||||
 | 
														Image: "someimage",
 | 
				
			||||||
 | 
													},
 | 
				
			||||||
 | 
												},
 | 
				
			||||||
 | 
											},
 | 
				
			||||||
 | 
										},
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						generator := BasicReplicationController{}
 | 
				
			||||||
 | 
						for _, test := range tests {
 | 
				
			||||||
 | 
							obj, err := generator.Generate(test.params)
 | 
				
			||||||
 | 
							if !test.expectErr && err != nil {
 | 
				
			||||||
 | 
								t.Errorf("unexpected error: %v", err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if !reflect.DeepEqual(obj, test.expected) {
 | 
				
			||||||
 | 
								t.Errorf("\nexpected:\n%v\nsaw:\n%v", test.expected, obj)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user