12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250 |
- /*
- Copyright 2014 The Kubernetes Authors.
- 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 versioned
- import (
- "reflect"
- "testing"
- appsv1beta1 "k8s.io/api/apps/v1beta1"
- batchv1 "k8s.io/api/batch/v1"
- batchv1beta1 "k8s.io/api/batch/v1beta1"
- batchv2alpha1 "k8s.io/api/batch/v2alpha1"
- "k8s.io/api/core/v1"
- extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
- "k8s.io/apimachinery/pkg/api/resource"
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- )
- func TestGenerate(t *testing.T) {
- one := int32(1)
- tests := []struct {
- name string
- params map[string]interface{}
- expected *v1.ReplicationController
- expectErr bool
- }{
- {
- name: "test1",
- params: map[string]interface{}{
- "name": "foo",
- "image": "someimage",
- "image-pull-policy": "Always",
- "replicas": "1",
- "port": "",
- },
- expected: &v1.ReplicationController{
- ObjectMeta: metav1.ObjectMeta{
- Name: "foo",
- Labels: map[string]string{"run": "foo"},
- },
- Spec: v1.ReplicationControllerSpec{
- Replicas: &one,
- Selector: map[string]string{"run": "foo"},
- Template: &v1.PodTemplateSpec{
- ObjectMeta: metav1.ObjectMeta{
- Labels: map[string]string{"run": "foo"},
- },
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Name: "foo",
- Image: "someimage",
- ImagePullPolicy: v1.PullAlways,
- },
- },
- },
- },
- },
- },
- },
- {
- name: "test2",
- params: map[string]interface{}{
- "name": "foo",
- "image": "someimage",
- "replicas": "1",
- "port": "",
- "env": []string{"a=b", "c=d"},
- },
- expected: &v1.ReplicationController{
- ObjectMeta: metav1.ObjectMeta{
- Name: "foo",
- Labels: map[string]string{"run": "foo"},
- },
- Spec: v1.ReplicationControllerSpec{
- Replicas: &one,
- Selector: map[string]string{"run": "foo"},
- Template: &v1.PodTemplateSpec{
- ObjectMeta: metav1.ObjectMeta{
- Labels: map[string]string{"run": "foo"},
- },
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Name: "foo",
- Image: "someimage",
- Env: []v1.EnvVar{
- {
- Name: "a",
- Value: "b",
- },
- {
- Name: "c",
- Value: "d",
- },
- },
- },
- },
- },
- },
- },
- },
- },
- {
- name: "test3",
- params: map[string]interface{}{
- "name": "foo",
- "image": "someimage",
- "image-pull-policy": "Never",
- "replicas": "1",
- "port": "",
- "args": []string{"bar", "baz", "blah"},
- },
- expected: &v1.ReplicationController{
- ObjectMeta: metav1.ObjectMeta{
- Name: "foo",
- Labels: map[string]string{"run": "foo"},
- },
- Spec: v1.ReplicationControllerSpec{
- Replicas: &one,
- Selector: map[string]string{"run": "foo"},
- Template: &v1.PodTemplateSpec{
- ObjectMeta: metav1.ObjectMeta{
- Labels: map[string]string{"run": "foo"},
- },
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Name: "foo",
- Image: "someimage",
- ImagePullPolicy: v1.PullNever,
- Args: []string{"bar", "baz", "blah"},
- },
- },
- },
- },
- },
- },
- },
- {
- name: "test3",
- params: map[string]interface{}{
- "name": "foo",
- "image": "someimage",
- "replicas": "1",
- "port": "",
- "args": []string{"bar", "baz", "blah"},
- "command": "true",
- },
- expected: &v1.ReplicationController{
- ObjectMeta: metav1.ObjectMeta{
- Name: "foo",
- Labels: map[string]string{"run": "foo"},
- },
- Spec: v1.ReplicationControllerSpec{
- Replicas: &one,
- Selector: map[string]string{"run": "foo"},
- Template: &v1.PodTemplateSpec{
- ObjectMeta: metav1.ObjectMeta{
- Labels: map[string]string{"run": "foo"},
- },
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Name: "foo",
- Image: "someimage",
- Command: []string{"bar", "baz", "blah"},
- },
- },
- },
- },
- },
- },
- },
- {
- name: "test4",
- params: map[string]interface{}{
- "name": "foo",
- "image": "someimage",
- "replicas": "1",
- "port": "80",
- },
- expected: &v1.ReplicationController{
- ObjectMeta: metav1.ObjectMeta{
- Name: "foo",
- Labels: map[string]string{"run": "foo"},
- },
- Spec: v1.ReplicationControllerSpec{
- Replicas: &one,
- Selector: map[string]string{"run": "foo"},
- Template: &v1.PodTemplateSpec{
- ObjectMeta: metav1.ObjectMeta{
- Labels: map[string]string{"run": "foo"},
- },
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Name: "foo",
- Image: "someimage",
- Ports: []v1.ContainerPort{
- {
- ContainerPort: 80,
- },
- },
- },
- },
- },
- },
- },
- },
- },
- {
- name: "test5",
- params: map[string]interface{}{
- "name": "foo",
- "image": "someimage",
- "image-pull-policy": "IfNotPresent",
- "replicas": "1",
- "port": "80",
- "hostport": "80",
- },
- expected: &v1.ReplicationController{
- ObjectMeta: metav1.ObjectMeta{
- Name: "foo",
- Labels: map[string]string{"run": "foo"},
- },
- Spec: v1.ReplicationControllerSpec{
- Replicas: &one,
- Selector: map[string]string{"run": "foo"},
- Template: &v1.PodTemplateSpec{
- ObjectMeta: metav1.ObjectMeta{
- Labels: map[string]string{"run": "foo"},
- },
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Name: "foo",
- Image: "someimage",
- ImagePullPolicy: v1.PullIfNotPresent,
- Ports: []v1.ContainerPort{
- {
- ContainerPort: 80,
- HostPort: 80,
- },
- },
- },
- },
- },
- },
- },
- },
- },
- {
- name: "test6",
- params: map[string]interface{}{
- "name": "foo",
- "image": "someimage",
- "replicas": "1",
- "hostport": "80",
- },
- expected: nil,
- expectErr: true,
- },
- {
- name: "test7",
- params: map[string]interface{}{
- "name": "foo",
- "image": "someimage",
- "replicas": "1",
- "labels": "foo=bar,baz=blah",
- },
- expected: &v1.ReplicationController{
- ObjectMeta: metav1.ObjectMeta{
- Name: "foo",
- Labels: map[string]string{"foo": "bar", "baz": "blah"},
- },
- Spec: v1.ReplicationControllerSpec{
- Replicas: &one,
- Selector: map[string]string{"foo": "bar", "baz": "blah"},
- Template: &v1.PodTemplateSpec{
- ObjectMeta: metav1.ObjectMeta{
- Labels: map[string]string{"foo": "bar", "baz": "blah"},
- },
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Name: "foo",
- Image: "someimage",
- },
- },
- },
- },
- },
- },
- },
- {
- name: "test8",
- params: map[string]interface{}{
- "name": "foo",
- "image": "someimage",
- "replicas": "1",
- "hostport": "80",
- },
- expected: nil,
- expectErr: true,
- },
- {
- name: "test9",
- params: map[string]interface{}{
- "name": "foo",
- "image": "someimage",
- "replicas": "1",
- "labels": "foo=bar,baz=blah",
- "requests": "cpu100m,memory=100Mi",
- },
- expected: nil,
- expectErr: true,
- },
- {
- name: "test10",
- params: map[string]interface{}{
- "name": "foo",
- "image": "someimage",
- "replicas": "1",
- "labels": "foo=bar,baz=blah",
- "requests": "cpu=100m&memory=100Mi",
- },
- expected: nil,
- expectErr: true,
- },
- {
- name: "test11",
- params: map[string]interface{}{
- "name": "foo",
- "image": "someimage",
- "replicas": "1",
- "labels": "foo=bar,baz=blah",
- "requests": "cpu=",
- },
- expected: nil,
- expectErr: true,
- },
- {
- name: "test12",
- params: map[string]interface{}{
- "name": "foo",
- "image": "someimage",
- "replicas": "1",
- "labels": "foo=bar,baz=blah",
- "requests": "cpu=100m,memory=100Mi",
- "limits": "cpu=400m,memory=200Mi",
- },
- expected: &v1.ReplicationController{
- ObjectMeta: metav1.ObjectMeta{
- Name: "foo",
- Labels: map[string]string{"foo": "bar", "baz": "blah"},
- },
- Spec: v1.ReplicationControllerSpec{
- Replicas: &one,
- Selector: map[string]string{"foo": "bar", "baz": "blah"},
- Template: &v1.PodTemplateSpec{
- ObjectMeta: metav1.ObjectMeta{
- Labels: map[string]string{"foo": "bar", "baz": "blah"},
- },
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Name: "foo",
- Image: "someimage",
- Resources: v1.ResourceRequirements{
- Requests: v1.ResourceList{
- v1.ResourceCPU: resource.MustParse("100m"),
- v1.ResourceMemory: resource.MustParse("100Mi"),
- },
- Limits: v1.ResourceList{
- v1.ResourceCPU: resource.MustParse("400m"),
- v1.ResourceMemory: resource.MustParse("200Mi"),
- },
- },
- },
- },
- },
- },
- },
- },
- },
- }
- generator := BasicReplicationController{}
- for i, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- obj, err := generator.Generate(tt.params)
- t.Logf("%d: %#v", i, obj)
- if !tt.expectErr && err != nil {
- t.Errorf("unexpected error: %v", err)
- return
- }
- if tt.expectErr && err != nil {
- return
- }
- if !reflect.DeepEqual(obj.(*v1.ReplicationController).Spec.Template, tt.expected.Spec.Template) {
- t.Errorf("\nexpected:\n%#v\nsaw:\n%#v", tt.expected.Spec.Template, obj.(*v1.ReplicationController).Spec.Template)
- }
- })
- }
- }
- func TestGeneratePod(t *testing.T) {
- tests := []struct {
- name string
- params map[string]interface{}
- expected *v1.Pod
- expectErr bool
- }{
- {
- name: "test1",
- params: map[string]interface{}{
- "name": "foo",
- "image": "someimage",
- "port": "",
- },
- expected: &v1.Pod{
- ObjectMeta: metav1.ObjectMeta{
- Name: "foo",
- Labels: map[string]string{"run": "foo"},
- },
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Name: "foo",
- Image: "someimage",
- },
- },
- DNSPolicy: v1.DNSClusterFirst,
- RestartPolicy: v1.RestartPolicyAlways,
- },
- },
- },
- {
- name: "test2",
- params: map[string]interface{}{
- "name": "foo",
- "image": "someimage",
- "env": []string{"a", "c"},
- },
- expected: nil,
- expectErr: true,
- },
- {
- name: "test3",
- params: map[string]interface{}{
- "name": "foo",
- "image": "someimage",
- "image-pull-policy": "Always",
- "env": []string{"a=b", "c=d"},
- },
- expected: &v1.Pod{
- ObjectMeta: metav1.ObjectMeta{
- Name: "foo",
- Labels: map[string]string{"run": "foo"},
- },
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Name: "foo",
- Image: "someimage",
- ImagePullPolicy: v1.PullAlways,
- Env: []v1.EnvVar{
- {
- Name: "a",
- Value: "b",
- },
- {
- Name: "c",
- Value: "d",
- },
- },
- },
- },
- DNSPolicy: v1.DNSClusterFirst,
- RestartPolicy: v1.RestartPolicyAlways,
- },
- },
- },
- {
- name: "test4",
- params: map[string]interface{}{
- "name": "foo",
- "image": "someimage",
- "port": "80",
- },
- expected: &v1.Pod{
- ObjectMeta: metav1.ObjectMeta{
- Name: "foo",
- Labels: map[string]string{"run": "foo"},
- },
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Name: "foo",
- Image: "someimage",
- Ports: []v1.ContainerPort{
- {
- ContainerPort: 80,
- },
- },
- },
- },
- DNSPolicy: v1.DNSClusterFirst,
- RestartPolicy: v1.RestartPolicyAlways,
- },
- },
- },
- {
- name: "test5",
- params: map[string]interface{}{
- "name": "foo",
- "image": "someimage",
- "port": "80",
- "hostport": "80",
- },
- expected: &v1.Pod{
- ObjectMeta: metav1.ObjectMeta{
- Name: "foo",
- Labels: map[string]string{"run": "foo"},
- },
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Name: "foo",
- Image: "someimage",
- Ports: []v1.ContainerPort{
- {
- ContainerPort: 80,
- HostPort: 80,
- },
- },
- },
- },
- DNSPolicy: v1.DNSClusterFirst,
- RestartPolicy: v1.RestartPolicyAlways,
- },
- },
- },
- {
- name: "test6",
- params: map[string]interface{}{
- "name": "foo",
- "image": "someimage",
- "hostport": "80",
- },
- expected: nil,
- expectErr: true,
- },
- {
- name: "test7",
- params: map[string]interface{}{
- "name": "foo",
- "image": "someimage",
- "replicas": "1",
- "labels": "foo=bar,baz=blah",
- },
- expected: &v1.Pod{
- ObjectMeta: metav1.ObjectMeta{
- Name: "foo",
- Labels: map[string]string{"foo": "bar", "baz": "blah"},
- },
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Name: "foo",
- Image: "someimage",
- },
- },
- DNSPolicy: v1.DNSClusterFirst,
- RestartPolicy: v1.RestartPolicyAlways,
- },
- },
- },
- {
- name: "test8",
- params: map[string]interface{}{
- "name": "foo",
- "image": "someimage",
- "replicas": "1",
- "labels": "foo=bar,baz=blah",
- "stdin": "true",
- },
- expected: &v1.Pod{
- ObjectMeta: metav1.ObjectMeta{
- Name: "foo",
- Labels: map[string]string{"foo": "bar", "baz": "blah"},
- },
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Name: "foo",
- Image: "someimage",
- Stdin: true,
- StdinOnce: true,
- },
- },
- DNSPolicy: v1.DNSClusterFirst,
- RestartPolicy: v1.RestartPolicyAlways,
- },
- },
- },
- {
- name: "test9",
- params: map[string]interface{}{
- "name": "foo",
- "image": "someimage",
- "replicas": "1",
- "labels": "foo=bar,baz=blah",
- "stdin": "true",
- "leave-stdin-open": "true",
- },
- expected: &v1.Pod{
- ObjectMeta: metav1.ObjectMeta{
- Name: "foo",
- Labels: map[string]string{"foo": "bar", "baz": "blah"},
- },
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Name: "foo",
- Image: "someimage",
- Stdin: true,
- StdinOnce: false,
- },
- },
- DNSPolicy: v1.DNSClusterFirst,
- RestartPolicy: v1.RestartPolicyAlways,
- },
- },
- },
- }
- generator := BasicPod{}
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- obj, err := generator.Generate(tt.params)
- if !tt.expectErr && err != nil {
- t.Errorf("unexpected error: %v", err)
- }
- if tt.expectErr && err != nil {
- return
- }
- if !reflect.DeepEqual(obj.(*v1.Pod), tt.expected) {
- t.Errorf("\nexpected:\n%#v\nsaw:\n%#v", tt.expected, obj.(*v1.Pod))
- }
- })
- }
- }
- func TestGenerateDeployment(t *testing.T) {
- three := int32(3)
- tests := []struct {
- name string
- params map[string]interface{}
- expected *extensionsv1beta1.Deployment
- expectErr bool
- }{
- {
- name: "test1",
- params: map[string]interface{}{
- "labels": "foo=bar,baz=blah",
- "name": "foo",
- "replicas": "3",
- "image": "someimage",
- "image-pull-policy": "Always",
- "port": "80",
- "hostport": "80",
- "stdin": "true",
- "command": "true",
- "args": []string{"bar", "baz", "blah"},
- "env": []string{"a=b", "c=d"},
- "requests": "cpu=100m,memory=100Mi",
- "limits": "cpu=400m,memory=200Mi",
- },
- expected: &extensionsv1beta1.Deployment{
- ObjectMeta: metav1.ObjectMeta{
- Name: "foo",
- Labels: map[string]string{"foo": "bar", "baz": "blah"},
- },
- Spec: extensionsv1beta1.DeploymentSpec{
- Replicas: &three,
- Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar", "baz": "blah"}},
- Template: v1.PodTemplateSpec{
- ObjectMeta: metav1.ObjectMeta{
- Labels: map[string]string{"foo": "bar", "baz": "blah"},
- },
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Name: "foo",
- Image: "someimage",
- ImagePullPolicy: v1.PullAlways,
- Stdin: true,
- Ports: []v1.ContainerPort{
- {
- ContainerPort: 80,
- HostPort: 80,
- },
- },
- Command: []string{"bar", "baz", "blah"},
- Env: []v1.EnvVar{
- {
- Name: "a",
- Value: "b",
- },
- {
- Name: "c",
- Value: "d",
- },
- },
- Resources: v1.ResourceRequirements{
- Requests: v1.ResourceList{
- v1.ResourceCPU: resource.MustParse("100m"),
- v1.ResourceMemory: resource.MustParse("100Mi"),
- },
- Limits: v1.ResourceList{
- v1.ResourceCPU: resource.MustParse("400m"),
- v1.ResourceMemory: resource.MustParse("200Mi"),
- },
- },
- },
- },
- },
- },
- },
- },
- },
- }
- generator := DeploymentV1Beta1{}
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- obj, err := generator.Generate(tt.params)
- if !tt.expectErr && err != nil {
- t.Errorf("unexpected error: %v", err)
- }
- if tt.expectErr && err != nil {
- return
- }
- if !reflect.DeepEqual(obj.(*extensionsv1beta1.Deployment), tt.expected) {
- t.Errorf("\nexpected:\n%#v\nsaw:\n%#v", tt.expected, obj.(*extensionsv1beta1.Deployment))
- }
- })
- }
- }
- func TestGenerateAppsDeployment(t *testing.T) {
- three := int32(3)
- tests := []struct {
- name string
- params map[string]interface{}
- expected *appsv1beta1.Deployment
- expectErr bool
- }{
- {
- name: "test1",
- params: map[string]interface{}{
- "labels": "foo=bar,baz=blah",
- "name": "foo",
- "replicas": "3",
- "image": "someimage",
- "image-pull-policy": "Always",
- "port": "80",
- "hostport": "80",
- "stdin": "true",
- "command": "true",
- "args": []string{"bar", "baz", "blah"},
- "env": []string{"a=b", "c=d"},
- "requests": "cpu=100m,memory=100Mi",
- "limits": "cpu=400m,memory=200Mi",
- },
- expected: &appsv1beta1.Deployment{
- ObjectMeta: metav1.ObjectMeta{
- Name: "foo",
- Labels: map[string]string{"foo": "bar", "baz": "blah"},
- },
- Spec: appsv1beta1.DeploymentSpec{
- Replicas: &three,
- Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar", "baz": "blah"}},
- Template: v1.PodTemplateSpec{
- ObjectMeta: metav1.ObjectMeta{
- Labels: map[string]string{"foo": "bar", "baz": "blah"},
- },
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Name: "foo",
- Image: "someimage",
- ImagePullPolicy: v1.PullAlways,
- Stdin: true,
- Ports: []v1.ContainerPort{
- {
- ContainerPort: 80,
- HostPort: 80,
- },
- },
- Command: []string{"bar", "baz", "blah"},
- Env: []v1.EnvVar{
- {
- Name: "a",
- Value: "b",
- },
- {
- Name: "c",
- Value: "d",
- },
- },
- Resources: v1.ResourceRequirements{
- Requests: v1.ResourceList{
- v1.ResourceCPU: resource.MustParse("100m"),
- v1.ResourceMemory: resource.MustParse("100Mi"),
- },
- Limits: v1.ResourceList{
- v1.ResourceCPU: resource.MustParse("400m"),
- v1.ResourceMemory: resource.MustParse("200Mi"),
- },
- },
- },
- },
- },
- },
- },
- },
- },
- }
- generator := DeploymentAppsV1Beta1{}
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- obj, err := generator.Generate(tt.params)
- if !tt.expectErr && err != nil {
- t.Errorf("unexpected error: %v", err)
- }
- if tt.expectErr && err != nil {
- return
- }
- if !reflect.DeepEqual(obj.(*appsv1beta1.Deployment), tt.expected) {
- t.Errorf("\nexpected:\n%#v\nsaw:\n%#v", tt.expected, obj.(*appsv1beta1.Deployment))
- }
- })
- }
- }
- func TestGenerateJob(t *testing.T) {
- tests := []struct {
- name string
- params map[string]interface{}
- expected *batchv1.Job
- expectErr bool
- }{
- {
- name: "test1",
- params: map[string]interface{}{
- "labels": "foo=bar,baz=blah",
- "name": "foo",
- "image": "someimage",
- "port": "80",
- "hostport": "80",
- "stdin": "true",
- "leave-stdin-open": "true",
- "command": "true",
- "args": []string{"bar", "baz", "blah"},
- "env": []string{"a=b", "c=d"},
- "requests": "cpu=100m,memory=100Mi",
- "limits": "cpu=400m,memory=200Mi",
- "restart": "OnFailure",
- },
- expected: &batchv1.Job{
- ObjectMeta: metav1.ObjectMeta{
- Name: "foo",
- Labels: map[string]string{"foo": "bar", "baz": "blah"},
- },
- Spec: batchv1.JobSpec{
- Template: v1.PodTemplateSpec{
- ObjectMeta: metav1.ObjectMeta{
- Labels: map[string]string{"foo": "bar", "baz": "blah"},
- },
- Spec: v1.PodSpec{
- RestartPolicy: v1.RestartPolicyOnFailure,
- Containers: []v1.Container{
- {
- Name: "foo",
- Image: "someimage",
- Stdin: true,
- StdinOnce: false,
- Ports: []v1.ContainerPort{
- {
- ContainerPort: 80,
- HostPort: 80,
- },
- },
- Command: []string{"bar", "baz", "blah"},
- Env: []v1.EnvVar{
- {
- Name: "a",
- Value: "b",
- },
- {
- Name: "c",
- Value: "d",
- },
- },
- Resources: v1.ResourceRequirements{
- Requests: v1.ResourceList{
- v1.ResourceCPU: resource.MustParse("100m"),
- v1.ResourceMemory: resource.MustParse("100Mi"),
- },
- Limits: v1.ResourceList{
- v1.ResourceCPU: resource.MustParse("400m"),
- v1.ResourceMemory: resource.MustParse("200Mi"),
- },
- },
- },
- },
- },
- },
- },
- },
- },
- }
- generator := JobV1{}
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- obj, err := generator.Generate(tt.params)
- if !tt.expectErr && err != nil {
- t.Errorf("unexpected error: %v", err)
- }
- if tt.expectErr && err != nil {
- return
- }
- if !reflect.DeepEqual(obj.(*batchv1.Job), tt.expected) {
- t.Errorf("\nexpected:\n%#v\nsaw:\n%#v", tt.expected, obj.(*batchv1.Job))
- }
- })
- }
- }
- func TestGenerateCronJobAlpha(t *testing.T) {
- tests := []struct {
- name string
- params map[string]interface{}
- expected *batchv2alpha1.CronJob
- expectErr bool
- }{
- {
- name: "test1",
- params: map[string]interface{}{
- "labels": "foo=bar,baz=blah",
- "name": "foo",
- "image": "someimage",
- "port": "80",
- "hostport": "80",
- "stdin": "true",
- "leave-stdin-open": "true",
- "command": "true",
- "args": []string{"bar", "baz", "blah"},
- "env": []string{"a=b", "c=d"},
- "requests": "cpu=100m,memory=100Mi",
- "limits": "cpu=400m,memory=200Mi",
- "restart": "OnFailure",
- "schedule": "0/5 * * * ?",
- },
- expected: &batchv2alpha1.CronJob{
- ObjectMeta: metav1.ObjectMeta{
- Name: "foo",
- Labels: map[string]string{"foo": "bar", "baz": "blah"},
- },
- Spec: batchv2alpha1.CronJobSpec{
- Schedule: "0/5 * * * ?",
- ConcurrencyPolicy: batchv2alpha1.AllowConcurrent,
- JobTemplate: batchv2alpha1.JobTemplateSpec{
- Spec: batchv1.JobSpec{
- Template: v1.PodTemplateSpec{
- ObjectMeta: metav1.ObjectMeta{
- Labels: map[string]string{"foo": "bar", "baz": "blah"},
- },
- Spec: v1.PodSpec{
- RestartPolicy: v1.RestartPolicyOnFailure,
- Containers: []v1.Container{
- {
- Name: "foo",
- Image: "someimage",
- Stdin: true,
- StdinOnce: false,
- Ports: []v1.ContainerPort{
- {
- ContainerPort: 80,
- HostPort: 80,
- },
- },
- Command: []string{"bar", "baz", "blah"},
- Env: []v1.EnvVar{
- {
- Name: "a",
- Value: "b",
- },
- {
- Name: "c",
- Value: "d",
- },
- },
- Resources: v1.ResourceRequirements{
- Requests: v1.ResourceList{
- v1.ResourceCPU: resource.MustParse("100m"),
- v1.ResourceMemory: resource.MustParse("100Mi"),
- },
- Limits: v1.ResourceList{
- v1.ResourceCPU: resource.MustParse("400m"),
- v1.ResourceMemory: resource.MustParse("200Mi"),
- },
- },
- },
- },
- },
- },
- },
- },
- },
- },
- },
- }
- generator := CronJobV2Alpha1{}
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- obj, err := generator.Generate(tt.params)
- if !tt.expectErr && err != nil {
- t.Errorf("unexpected error: %v", err)
- }
- if tt.expectErr && err != nil {
- return
- }
- if !reflect.DeepEqual(obj.(*batchv2alpha1.CronJob), tt.expected) {
- t.Errorf("\nexpected:\n%#v\nsaw:\n%#v", tt.expected, obj.(*batchv2alpha1.CronJob))
- }
- })
- }
- }
- func TestGenerateCronJobBeta(t *testing.T) {
- tests := []struct {
- name string
- params map[string]interface{}
- expected *batchv1beta1.CronJob
- expectErr bool
- }{
- {
- name: "test1",
- params: map[string]interface{}{
- "labels": "foo=bar,baz=blah",
- "name": "foo",
- "image": "someimage",
- "port": "80",
- "hostport": "80",
- "stdin": "true",
- "leave-stdin-open": "true",
- "command": "true",
- "args": []string{"bar", "baz", "blah"},
- "env": []string{"a=b", "c=d"},
- "requests": "cpu=100m,memory=100Mi",
- "limits": "cpu=400m,memory=200Mi",
- "restart": "OnFailure",
- "schedule": "0/5 * * * ?",
- },
- expected: &batchv1beta1.CronJob{
- ObjectMeta: metav1.ObjectMeta{
- Name: "foo",
- Labels: map[string]string{"foo": "bar", "baz": "blah"},
- },
- Spec: batchv1beta1.CronJobSpec{
- Schedule: "0/5 * * * ?",
- ConcurrencyPolicy: batchv1beta1.AllowConcurrent,
- JobTemplate: batchv1beta1.JobTemplateSpec{
- Spec: batchv1.JobSpec{
- Template: v1.PodTemplateSpec{
- ObjectMeta: metav1.ObjectMeta{
- Labels: map[string]string{"foo": "bar", "baz": "blah"},
- },
- Spec: v1.PodSpec{
- RestartPolicy: v1.RestartPolicyOnFailure,
- Containers: []v1.Container{
- {
- Name: "foo",
- Image: "someimage",
- Stdin: true,
- StdinOnce: false,
- Ports: []v1.ContainerPort{
- {
- ContainerPort: 80,
- HostPort: 80,
- },
- },
- Command: []string{"bar", "baz", "blah"},
- Env: []v1.EnvVar{
- {
- Name: "a",
- Value: "b",
- },
- {
- Name: "c",
- Value: "d",
- },
- },
- Resources: v1.ResourceRequirements{
- Requests: v1.ResourceList{
- v1.ResourceCPU: resource.MustParse("100m"),
- v1.ResourceMemory: resource.MustParse("100Mi"),
- },
- Limits: v1.ResourceList{
- v1.ResourceCPU: resource.MustParse("400m"),
- v1.ResourceMemory: resource.MustParse("200Mi"),
- },
- },
- },
- },
- },
- },
- },
- },
- },
- },
- },
- }
- generator := CronJobV1Beta1{}
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- obj, err := generator.Generate(tt.params)
- if !tt.expectErr && err != nil {
- t.Errorf("unexpected error: %v", err)
- }
- if tt.expectErr && err != nil {
- return
- }
- if !reflect.DeepEqual(obj.(*batchv1beta1.CronJob), tt.expected) {
- t.Errorf("\nexpected:\n%#v\nsaw:\n%#v", tt.expected, obj.(*batchv1beta1.CronJob))
- }
- })
- }
- }
- func TestParseEnv(t *testing.T) {
- tests := []struct {
- name string
- envArray []string
- expected []v1.EnvVar
- expectErr bool
- test string
- }{
- {
- name: "test1",
- envArray: []string{
- "THIS_ENV=isOK",
- "this.dotted.env=isOKToo",
- "HAS_COMMAS=foo,bar",
- "HAS_EQUALS=jJnro54iUu75xNy==",
- },
- expected: []v1.EnvVar{
- {
- Name: "THIS_ENV",
- Value: "isOK",
- },
- {
- Name: "this.dotted.env",
- Value: "isOKToo",
- },
- {
- Name: "HAS_COMMAS",
- Value: "foo,bar",
- },
- {
- Name: "HAS_EQUALS",
- Value: "jJnro54iUu75xNy==",
- },
- },
- expectErr: false,
- test: "test case 1",
- },
- {
- name: "test2",
- envArray: []string{
- "WITH_OUT_EQUALS",
- },
- expected: []v1.EnvVar{},
- expectErr: true,
- test: "test case 2",
- },
- {
- name: "test3",
- envArray: []string{
- "WITH_OUT_VALUES=",
- },
- expected: []v1.EnvVar{
- {
- Name: "WITH_OUT_VALUES",
- Value: "",
- },
- },
- expectErr: false,
- test: "test case 3",
- },
- {
- name: "test4",
- envArray: []string{
- "=WITH_OUT_NAME",
- },
- expected: []v1.EnvVar{},
- expectErr: true,
- test: "test case 4",
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- envs, err := parseEnvs(tt.envArray)
- if !tt.expectErr && err != nil {
- t.Errorf("unexpected error: %v (%s)", err, tt.test)
- }
- if tt.expectErr && err != nil {
- return
- }
- if !reflect.DeepEqual(envs, tt.expected) {
- t.Errorf("\nexpected:\n%#v\nsaw:\n%#v (%s)", tt.expected, envs, tt.test)
- }
- })
- }
- }
|