From 6a151c6267204e9d2e990e7bace170b6dd38c0ec Mon Sep 17 00:00:00 2001 From: Shubham Minglani Date: Tue, 27 Dec 2016 17:18:18 +0530 Subject: [PATCH] add stdin_open, tty support, add tests, fix #344 This adds supports for stdin_open: bool and tty: bool support for kubernetes and openshift providers in kompose. This maps to the template.Spec.Containers[0].Stdin and template.Spec.Containers[0].TTY in Kubernets world. Also, added tests. Fixes #344 --- pkg/kobject/kobject.go | 2 + pkg/loader/compose/compose.go | 4 +- pkg/transformer/kubernetes/k8sutils.go | 2 + pkg/transformer/kubernetes/kubernetes_test.go | 8 ++ pkg/transformer/openshift/openshift_test.go | 28 ++++ script/test/cmd/tests.sh | 16 +++ .../fixtures/stdin-true/docker-compose.yml | 7 + .../test/fixtures/stdin-true/output-k8s.json | 72 ++++++++++ .../test/fixtures/stdin-true/output-oc.json | 124 ++++++++++++++++++ .../test/fixtures/tty-true/docker-compose.yml | 7 + script/test/fixtures/tty-true/output-k8s.json | 72 ++++++++++ script/test/fixtures/tty-true/output-oc.json | 124 ++++++++++++++++++ 12 files changed, 464 insertions(+), 2 deletions(-) create mode 100644 script/test/fixtures/stdin-true/docker-compose.yml create mode 100644 script/test/fixtures/stdin-true/output-k8s.json create mode 100644 script/test/fixtures/stdin-true/output-oc.json create mode 100644 script/test/fixtures/tty-true/docker-compose.yml create mode 100644 script/test/fixtures/tty-true/output-k8s.json create mode 100644 script/test/fixtures/tty-true/output-oc.json diff --git a/pkg/kobject/kobject.go b/pkg/kobject/kobject.go index 93a720914..2c4f43400 100644 --- a/pkg/kobject/kobject.go +++ b/pkg/kobject/kobject.go @@ -69,6 +69,8 @@ type ServiceConfig struct { ServiceType string `compose:"kompose.service.type",bundle:""` Build string `compose:"build",bundle:""` ExposeService string `compose:"kompose.service.expose",bundle:""` + Stdin bool `compose:"stdin_open",bundle:""` + Tty bool `compose:"tty",bundle:""` } // EnvVar holds the environment variable struct of a container diff --git a/pkg/loader/compose/compose.go b/pkg/loader/compose/compose.go index ca927770f..d73de97a8 100644 --- a/pkg/loader/compose/compose.go +++ b/pkg/loader/compose/compose.go @@ -72,8 +72,6 @@ func checkUnsupportedKey(composeProject *project.Project) []string { "VolumeDriver": false, "Uts": false, "ReadOnly": false, - "StdinOpen": false, - "Tty": false, "Ulimits": false, "Dockerfile": false, "Net": false, @@ -320,6 +318,8 @@ func (c *Compose) LoadFile(file string) kobject.KomposeObject { serviceConfig.Restart = composeServiceConfig.Restart serviceConfig.User = composeServiceConfig.User serviceConfig.VolumesFrom = composeServiceConfig.VolumesFrom + serviceConfig.Stdin = composeServiceConfig.StdinOpen + serviceConfig.Tty = composeServiceConfig.Tty komposeObject.ServiceConfigs[name] = serviceConfig } diff --git a/pkg/transformer/kubernetes/k8sutils.go b/pkg/transformer/kubernetes/k8sutils.go index 8eed26160..eac71cbaa 100644 --- a/pkg/transformer/kubernetes/k8sutils.go +++ b/pkg/transformer/kubernetes/k8sutils.go @@ -324,6 +324,8 @@ func (k *Kubernetes) UpdateKubernetesObjects(name string, service kobject.Servic template.Spec.Containers[0].Args = service.Args template.Spec.Containers[0].WorkingDir = service.WorkingDir template.Spec.Containers[0].VolumeMounts = volumesMount + template.Spec.Containers[0].Stdin = service.Stdin + template.Spec.Containers[0].TTY = service.Tty template.Spec.Volumes = volumes securityContext := &api.SecurityContext{} diff --git a/pkg/transformer/kubernetes/kubernetes_test.go b/pkg/transformer/kubernetes/kubernetes_test.go index 7e8861ba0..3bde5ffba 100644 --- a/pkg/transformer/kubernetes/kubernetes_test.go +++ b/pkg/transformer/kubernetes/kubernetes_test.go @@ -53,6 +53,8 @@ func newServiceConfig() kobject.ServiceConfig { Privileged: true, Restart: "always", User: "user", // not supported + Stdin: true, + Tty: true, } } @@ -168,6 +170,12 @@ func checkPodTemplate(config kobject.ServiceConfig, template api.PodTemplateSpec if config.Privileged == privilegedNilOrFalse(template) { return fmt.Errorf("Found different template privileged: %#v vs. %#v", config.Privileged, template.Spec.Containers[0].SecurityContext) } + if config.Stdin != template.Spec.Containers[0].Stdin { + return fmt.Errorf("Found different values for stdin: %#v vs. %#v", config.Stdin, template.Spec.Containers[0].Stdin) + } + if config.Tty != template.Spec.Containers[0].TTY { + return fmt.Errorf("Found different values for TTY: %#v vs. %#v", config.Tty, template.Spec.Containers[0].TTY) + } return nil } diff --git a/pkg/transformer/openshift/openshift_test.go b/pkg/transformer/openshift/openshift_test.go index b4b24dd91..1772802b6 100644 --- a/pkg/transformer/openshift/openshift_test.go +++ b/pkg/transformer/openshift/openshift_test.go @@ -18,7 +18,9 @@ package openshift import ( "github.com/kubernetes-incubator/kompose/pkg/kobject" + deployapi "github.com/openshift/origin/pkg/deploy/api" "k8s.io/kubernetes/pkg/api" + "k8s.io/kubernetes/pkg/runtime" "testing" ) @@ -44,6 +46,32 @@ func newServiceConfig() kobject.ServiceConfig { Privileged: true, Restart: "always", User: "user", // not supported + Stdin: true, + Tty: true, + } +} + +func TestOpenShiftUpdateKubernetesObjects(t *testing.T) { + t.Log("Test case: Testing o.UpdateKubernetesObjects()") + var object []runtime.Object + o := OpenShift{} + serviceConfig := newServiceConfig() + + object = append(object, o.initDeploymentConfig("foobar", serviceConfig, 3)) + o.UpdateKubernetesObjects("foobar", serviceConfig, &object) + + for _, obj := range object { + switch tobj := obj.(type) { + case *deployapi.DeploymentConfig: + t.Log("> Testing if stdin is set correctly") + if tobj.Spec.Template.Spec.Containers[0].Stdin != serviceConfig.Stdin { + t.Errorf("Expected stdin to be %v, got %v instead", serviceConfig.Stdin, tobj.Spec.Template.Spec.Containers[0].Stdin) + } + t.Log("> Testing if TTY is set correctly") + if tobj.Spec.Template.Spec.Containers[0].TTY != serviceConfig.Tty { + t.Errorf("Expected TTY to be %v, got %v instead", serviceConfig.Tty, tobj.Spec.Template.Spec.Containers[0].TTY) + } + } } } diff --git a/script/test/cmd/tests.sh b/script/test/cmd/tests.sh index aac1a5932..ea9219a3e 100755 --- a/script/test/cmd/tests.sh +++ b/script/test/cmd/tests.sh @@ -119,6 +119,22 @@ convert::expect_success "kompose --file $KOMPOSE_ROOT/script/test/fixtures/keyon unset $(cat $KOMPOSE_ROOT/script/test/fixtures/keyonly-envs/envs | cut -d'=' -f1) +###### +# Test related to "stdin_open: true" in docker-compose +# kubernetes test +convert::expect_success "kompose -f $KOMPOSE_ROOT/script/test/fixtures/stdin-true/docker-compose.yml convert --stdout" "$KOMPOSE_ROOT/script/test/fixtures/stdin-true/output-k8s.json" +# openshift test +convert::expect_success "kompose --provider openshift -f $KOMPOSE_ROOT/script/test/fixtures/stdin-true/docker-compose.yml convert --stdout" "$KOMPOSE_ROOT/script/test/fixtures/stdin-true/output-oc.json" + + +###### +# Test related to "tty: true" in docker-compose +# kubernetes test +convert::expect_success "kompose -f $KOMPOSE_ROOT/script/test/fixtures/tty-true/docker-compose.yml convert --stdout" "$KOMPOSE_ROOT/script/test/fixtures/tty-true/output-k8s.json" +# openshift test +convert::expect_success "kompose --provider openshift -f $KOMPOSE_ROOT/script/test/fixtures/tty-true/docker-compose.yml convert --stdout" "$KOMPOSE_ROOT/script/test/fixtures/tty-true/output-oc.json" + + # Test related to kompose.expose.service label in docker compose file to ensure that services are exposed properly #kubernetes tests # when kompose.service.expose="True" diff --git a/script/test/fixtures/stdin-true/docker-compose.yml b/script/test/fixtures/stdin-true/docker-compose.yml new file mode 100644 index 000000000..615d096aa --- /dev/null +++ b/script/test/fixtures/stdin-true/docker-compose.yml @@ -0,0 +1,7 @@ +version: "2" +services: + client: + image: registry.centos.org/centos/centos:7 + ports: + - "1337" + stdin_open: true diff --git a/script/test/fixtures/stdin-true/output-k8s.json b/script/test/fixtures/stdin-true/output-k8s.json new file mode 100644 index 000000000..0ef19d3f4 --- /dev/null +++ b/script/test/fixtures/stdin-true/output-k8s.json @@ -0,0 +1,72 @@ +{ + "kind": "List", + "apiVersion": "v1", + "metadata": {}, + "items": [ + { + "kind": "Service", + "apiVersion": "v1", + "metadata": { + "name": "client", + "creationTimestamp": null, + "labels": { + "service": "client" + } + }, + "spec": { + "ports": [ + { + "name": "1337", + "protocol": "TCP", + "port": 1337, + "targetPort": 1337 + } + ], + "selector": { + "service": "client" + } + }, + "status": { + "loadBalancer": {} + } + }, + { + "kind": "Deployment", + "apiVersion": "extensions/v1beta1", + "metadata": { + "name": "client", + "creationTimestamp": null + }, + "spec": { + "replicas": 1, + "template": { + "metadata": { + "creationTimestamp": null, + "labels": { + "service": "client" + } + }, + "spec": { + "containers": [ + { + "name": "client", + "image": "registry.centos.org/centos/centos:7", + "ports": [ + { + "containerPort": 1337, + "protocol": "TCP" + } + ], + "resources": {}, + "stdin": true + } + ], + "restartPolicy": "Always" + } + }, + "strategy": {} + }, + "status": {} + } + ] +} diff --git a/script/test/fixtures/stdin-true/output-oc.json b/script/test/fixtures/stdin-true/output-oc.json new file mode 100644 index 000000000..375cc8412 --- /dev/null +++ b/script/test/fixtures/stdin-true/output-oc.json @@ -0,0 +1,124 @@ +{ + "kind": "List", + "apiVersion": "v1", + "metadata": {}, + "items": [ + { + "kind": "Service", + "apiVersion": "v1", + "metadata": { + "name": "client", + "creationTimestamp": null, + "labels": { + "service": "client" + } + }, + "spec": { + "ports": [ + { + "name": "1337", + "protocol": "TCP", + "port": 1337, + "targetPort": 1337 + } + ], + "selector": { + "service": "client" + } + }, + "status": { + "loadBalancer": {} + } + }, + { + "kind": "DeploymentConfig", + "apiVersion": "v1", + "metadata": { + "name": "client", + "creationTimestamp": null, + "labels": { + "service": "client" + } + }, + "spec": { + "strategy": { + "resources": {} + }, + "triggers": [ + { + "type": "ConfigChange" + }, + { + "type": "ImageChange", + "imageChangeParams": { + "automatic": true, + "containerNames": [ + "client" + ], + "from": { + "kind": "ImageStreamTag", + "name": "client:7" + } + } + } + ], + "replicas": 1, + "test": false, + "selector": { + "service": "client" + }, + "template": { + "metadata": { + "creationTimestamp": null, + "labels": { + "service": "client" + } + }, + "spec": { + "containers": [ + { + "name": "client", + "image": " ", + "ports": [ + { + "containerPort": 1337, + "protocol": "TCP" + } + ], + "resources": {}, + "stdin": true + } + ], + "restartPolicy": "Always" + } + } + }, + "status": {} + }, + { + "kind": "ImageStream", + "apiVersion": "v1", + "metadata": { + "name": "client", + "creationTimestamp": null + }, + "spec": { + "tags": [ + { + "name": "7", + "annotations": null, + "from": { + "kind": "DockerImage", + "name": "registry.centos.org/centos/centos:7" + }, + "generation": null, + "importPolicy": {} + } + ] + }, + "status": { + "dockerImageRepository": "" + } + } + ] +} diff --git a/script/test/fixtures/tty-true/docker-compose.yml b/script/test/fixtures/tty-true/docker-compose.yml new file mode 100644 index 000000000..5e994f134 --- /dev/null +++ b/script/test/fixtures/tty-true/docker-compose.yml @@ -0,0 +1,7 @@ +version: "2" +services: + client: + image: registry.centos.org/centos/centos:7 + ports: + - "1337" + tty: true diff --git a/script/test/fixtures/tty-true/output-k8s.json b/script/test/fixtures/tty-true/output-k8s.json new file mode 100644 index 000000000..906338d7e --- /dev/null +++ b/script/test/fixtures/tty-true/output-k8s.json @@ -0,0 +1,72 @@ +{ + "kind": "List", + "apiVersion": "v1", + "metadata": {}, + "items": [ + { + "kind": "Service", + "apiVersion": "v1", + "metadata": { + "name": "client", + "creationTimestamp": null, + "labels": { + "service": "client" + } + }, + "spec": { + "ports": [ + { + "name": "1337", + "protocol": "TCP", + "port": 1337, + "targetPort": 1337 + } + ], + "selector": { + "service": "client" + } + }, + "status": { + "loadBalancer": {} + } + }, + { + "kind": "Deployment", + "apiVersion": "extensions/v1beta1", + "metadata": { + "name": "client", + "creationTimestamp": null + }, + "spec": { + "replicas": 1, + "template": { + "metadata": { + "creationTimestamp": null, + "labels": { + "service": "client" + } + }, + "spec": { + "containers": [ + { + "name": "client", + "image": "registry.centos.org/centos/centos:7", + "ports": [ + { + "containerPort": 1337, + "protocol": "TCP" + } + ], + "resources": {}, + "tty": true + } + ], + "restartPolicy": "Always" + } + }, + "strategy": {} + }, + "status": {} + } + ] +} diff --git a/script/test/fixtures/tty-true/output-oc.json b/script/test/fixtures/tty-true/output-oc.json new file mode 100644 index 000000000..293cf05ee --- /dev/null +++ b/script/test/fixtures/tty-true/output-oc.json @@ -0,0 +1,124 @@ +{ + "kind": "List", + "apiVersion": "v1", + "metadata": {}, + "items": [ + { + "kind": "Service", + "apiVersion": "v1", + "metadata": { + "name": "client", + "creationTimestamp": null, + "labels": { + "service": "client" + } + }, + "spec": { + "ports": [ + { + "name": "1337", + "protocol": "TCP", + "port": 1337, + "targetPort": 1337 + } + ], + "selector": { + "service": "client" + } + }, + "status": { + "loadBalancer": {} + } + }, + { + "kind": "DeploymentConfig", + "apiVersion": "v1", + "metadata": { + "name": "client", + "creationTimestamp": null, + "labels": { + "service": "client" + } + }, + "spec": { + "strategy": { + "resources": {} + }, + "triggers": [ + { + "type": "ConfigChange" + }, + { + "type": "ImageChange", + "imageChangeParams": { + "automatic": true, + "containerNames": [ + "client" + ], + "from": { + "kind": "ImageStreamTag", + "name": "client:7" + } + } + } + ], + "replicas": 1, + "test": false, + "selector": { + "service": "client" + }, + "template": { + "metadata": { + "creationTimestamp": null, + "labels": { + "service": "client" + } + }, + "spec": { + "containers": [ + { + "name": "client", + "image": " ", + "ports": [ + { + "containerPort": 1337, + "protocol": "TCP" + } + ], + "resources": {}, + "tty": true + } + ], + "restartPolicy": "Always" + } + } + }, + "status": {} + }, + { + "kind": "ImageStream", + "apiVersion": "v1", + "metadata": { + "name": "client", + "creationTimestamp": null + }, + "spec": { + "tags": [ + { + "name": "7", + "annotations": null, + "from": { + "kind": "DockerImage", + "name": "registry.centos.org/centos/centos:7" + }, + "generation": null, + "importPolicy": {} + } + ] + }, + "status": { + "dockerImageRepository": "" + } + } + ] +}