2015-01-14 15:11:31 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
2015-01-14 15:11:31 +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 kubectl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2015-01-14 15:11:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGenerate(t *testing.T) {
|
|
|
|
tests := []struct {
|
2015-08-12 05:48:00 +00:00
|
|
|
params map[string]interface{}
|
2015-01-14 15:11:31 +00:00
|
|
|
expected *api.ReplicationController
|
|
|
|
expectErr bool
|
|
|
|
}{
|
|
|
|
{
|
2015-08-12 05:48:00 +00:00
|
|
|
params: map[string]interface{}{
|
2015-01-14 15:11:31 +00:00
|
|
|
"name": "foo",
|
|
|
|
"image": "someimage",
|
|
|
|
"replicas": "1",
|
2015-02-11 21:20:51 +00:00
|
|
|
"port": "-1",
|
2015-01-14 15:11:31 +00:00
|
|
|
},
|
|
|
|
expected: &api.ReplicationController{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
2015-05-21 20:53:10 +00:00
|
|
|
Labels: map[string]string{"run": "foo"},
|
2015-01-14 15:11:31 +00:00
|
|
|
},
|
|
|
|
Spec: api.ReplicationControllerSpec{
|
|
|
|
Replicas: 1,
|
2015-05-21 20:53:10 +00:00
|
|
|
Selector: map[string]string{"run": "foo"},
|
2015-01-14 15:11:31 +00:00
|
|
|
Template: &api.PodTemplateSpec{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
2015-05-21 20:53:10 +00:00
|
|
|
Labels: map[string]string{"run": "foo"},
|
2015-01-14 15:11:31 +00:00
|
|
|
},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
Image: "someimage",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-01-31 04:51:40 +00:00
|
|
|
{
|
2015-08-12 05:48:00 +00:00
|
|
|
params: map[string]interface{}{
|
|
|
|
"name": "foo",
|
|
|
|
"image": "someimage",
|
|
|
|
"replicas": "1",
|
|
|
|
"port": "-1",
|
|
|
|
"args": []string{"bar", "baz", "blah"},
|
|
|
|
},
|
|
|
|
expected: &api.ReplicationController{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Labels: map[string]string{"run": "foo"},
|
|
|
|
},
|
|
|
|
Spec: api.ReplicationControllerSpec{
|
|
|
|
Replicas: 1,
|
|
|
|
Selector: map[string]string{"run": "foo"},
|
|
|
|
Template: &api.PodTemplateSpec{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Labels: map[string]string{"run": "foo"},
|
|
|
|
},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
Image: "someimage",
|
|
|
|
Args: []string{"bar", "baz", "blah"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
params: map[string]interface{}{
|
|
|
|
"name": "foo",
|
|
|
|
"image": "someimage",
|
|
|
|
"replicas": "1",
|
|
|
|
"port": "-1",
|
|
|
|
"args": []string{"bar", "baz", "blah"},
|
|
|
|
"command": "true",
|
|
|
|
},
|
|
|
|
expected: &api.ReplicationController{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Labels: map[string]string{"run": "foo"},
|
|
|
|
},
|
|
|
|
Spec: api.ReplicationControllerSpec{
|
|
|
|
Replicas: 1,
|
|
|
|
Selector: map[string]string{"run": "foo"},
|
|
|
|
Template: &api.PodTemplateSpec{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Labels: map[string]string{"run": "foo"},
|
|
|
|
},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
Image: "someimage",
|
|
|
|
Command: []string{"bar", "baz", "blah"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
params: map[string]interface{}{
|
2015-01-31 04:51:40 +00:00
|
|
|
"name": "foo",
|
|
|
|
"image": "someimage",
|
|
|
|
"replicas": "1",
|
|
|
|
"port": "80",
|
|
|
|
},
|
|
|
|
expected: &api.ReplicationController{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
2015-05-21 20:53:10 +00:00
|
|
|
Labels: map[string]string{"run": "foo"},
|
2015-01-31 04:51:40 +00:00
|
|
|
},
|
|
|
|
Spec: api.ReplicationControllerSpec{
|
|
|
|
Replicas: 1,
|
2015-05-21 20:53:10 +00:00
|
|
|
Selector: map[string]string{"run": "foo"},
|
2015-01-31 04:51:40 +00:00
|
|
|
Template: &api.PodTemplateSpec{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
2015-05-21 20:53:10 +00:00
|
|
|
Labels: map[string]string{"run": "foo"},
|
2015-01-31 04:51:40 +00:00
|
|
|
},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
Image: "someimage",
|
2015-02-23 22:25:56 +00:00
|
|
|
Ports: []api.ContainerPort{
|
2015-01-31 04:51:40 +00:00
|
|
|
{
|
|
|
|
ContainerPort: 80,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-04-29 23:54:23 +00:00
|
|
|
{
|
2015-08-12 05:48:00 +00:00
|
|
|
params: map[string]interface{}{
|
2015-04-29 23:54:23 +00:00
|
|
|
"name": "foo",
|
|
|
|
"image": "someimage",
|
|
|
|
"replicas": "1",
|
|
|
|
"port": "80",
|
|
|
|
"hostport": "80",
|
|
|
|
},
|
|
|
|
expected: &api.ReplicationController{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
2015-05-21 20:53:10 +00:00
|
|
|
Labels: map[string]string{"run": "foo"},
|
2015-04-29 23:54:23 +00:00
|
|
|
},
|
|
|
|
Spec: api.ReplicationControllerSpec{
|
|
|
|
Replicas: 1,
|
2015-05-21 20:53:10 +00:00
|
|
|
Selector: map[string]string{"run": "foo"},
|
2015-04-29 23:54:23 +00:00
|
|
|
Template: &api.PodTemplateSpec{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
2015-05-21 20:53:10 +00:00
|
|
|
Labels: map[string]string{"run": "foo"},
|
2015-04-29 23:54:23 +00:00
|
|
|
},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
Image: "someimage",
|
|
|
|
Ports: []api.ContainerPort{
|
|
|
|
{
|
|
|
|
ContainerPort: 80,
|
|
|
|
HostPort: 80,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2015-08-12 05:48:00 +00:00
|
|
|
params: map[string]interface{}{
|
2015-04-29 23:54:23 +00:00
|
|
|
"name": "foo",
|
|
|
|
"image": "someimage",
|
|
|
|
"replicas": "1",
|
|
|
|
"hostport": "80",
|
|
|
|
},
|
|
|
|
expected: nil,
|
|
|
|
expectErr: true,
|
|
|
|
},
|
2015-01-14 15:11:31 +00:00
|
|
|
{
|
2015-08-12 05:48:00 +00:00
|
|
|
params: map[string]interface{}{
|
2015-01-14 15:11:31 +00:00
|
|
|
"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)
|
|
|
|
}
|
2015-04-29 23:54:23 +00:00
|
|
|
if test.expectErr && err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2015-01-31 04:51:40 +00:00
|
|
|
if !reflect.DeepEqual(obj.(*api.ReplicationController).Spec.Template, test.expected.Spec.Template) {
|
|
|
|
t.Errorf("\nexpected:\n%#v\nsaw:\n%#v", test.expected.Spec.Template, obj.(*api.ReplicationController).Spec.Template)
|
2015-01-14 15:11:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-04 19:54:17 +00:00
|
|
|
|
|
|
|
func TestGeneratePod(t *testing.T) {
|
|
|
|
tests := []struct {
|
2015-08-12 05:48:00 +00:00
|
|
|
params map[string]interface{}
|
2015-08-04 19:54:17 +00:00
|
|
|
expected *api.Pod
|
|
|
|
expectErr bool
|
|
|
|
}{
|
|
|
|
{
|
2015-08-12 05:48:00 +00:00
|
|
|
params: map[string]interface{}{
|
2015-08-04 19:54:17 +00:00
|
|
|
"name": "foo",
|
|
|
|
"image": "someimage",
|
|
|
|
"port": "-1",
|
|
|
|
},
|
|
|
|
expected: &api.Pod{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
Image: "someimage",
|
|
|
|
ImagePullPolicy: api.PullIfNotPresent,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
DNSPolicy: api.DNSClusterFirst,
|
|
|
|
RestartPolicy: api.RestartPolicyAlways,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2015-08-12 05:48:00 +00:00
|
|
|
params: map[string]interface{}{
|
2015-08-04 19:54:17 +00:00
|
|
|
"name": "foo",
|
|
|
|
"image": "someimage",
|
|
|
|
"port": "80",
|
|
|
|
},
|
|
|
|
expected: &api.Pod{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
Image: "someimage",
|
|
|
|
ImagePullPolicy: api.PullIfNotPresent,
|
|
|
|
Ports: []api.ContainerPort{
|
|
|
|
{
|
|
|
|
ContainerPort: 80,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
DNSPolicy: api.DNSClusterFirst,
|
|
|
|
RestartPolicy: api.RestartPolicyAlways,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2015-08-12 05:48:00 +00:00
|
|
|
params: map[string]interface{}{
|
2015-08-04 19:54:17 +00:00
|
|
|
"name": "foo",
|
|
|
|
"image": "someimage",
|
|
|
|
"port": "80",
|
|
|
|
"hostport": "80",
|
|
|
|
},
|
|
|
|
expected: &api.Pod{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
Image: "someimage",
|
|
|
|
ImagePullPolicy: api.PullIfNotPresent,
|
|
|
|
Ports: []api.ContainerPort{
|
|
|
|
{
|
|
|
|
ContainerPort: 80,
|
|
|
|
HostPort: 80,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
DNSPolicy: api.DNSClusterFirst,
|
|
|
|
RestartPolicy: api.RestartPolicyAlways,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2015-08-12 05:48:00 +00:00
|
|
|
params: map[string]interface{}{
|
2015-08-04 19:54:17 +00:00
|
|
|
"name": "foo",
|
|
|
|
"image": "someimage",
|
|
|
|
"hostport": "80",
|
|
|
|
},
|
|
|
|
expected: nil,
|
|
|
|
expectErr: true,
|
|
|
|
},
|
|
|
|
{
|
2015-08-12 05:48:00 +00:00
|
|
|
params: map[string]interface{}{
|
2015-08-04 19:54:17 +00:00
|
|
|
"name": "foo",
|
|
|
|
"image": "someimage",
|
|
|
|
"replicas": "1",
|
|
|
|
"labels": "foo=bar,baz=blah",
|
|
|
|
},
|
|
|
|
expected: &api.Pod{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Labels: map[string]string{"foo": "bar", "baz": "blah"},
|
|
|
|
},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
Image: "someimage",
|
|
|
|
ImagePullPolicy: api.PullIfNotPresent,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
DNSPolicy: api.DNSClusterFirst,
|
|
|
|
RestartPolicy: api.RestartPolicyAlways,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
generator := BasicPod{}
|
|
|
|
for _, test := range tests {
|
|
|
|
obj, err := generator.Generate(test.params)
|
|
|
|
if !test.expectErr && err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
if test.expectErr && err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(obj.(*api.Pod), test.expected) {
|
|
|
|
t.Errorf("\nexpected:\n%#v\nsaw:\n%#v", test.expected, obj.(*api.Pod))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|