Kompose will read input from stdin

Resolves issue #870

for example,

```
$ cat docker-compose.yaml | kompose convert -f -
INFO Kubernetes file "frontend-service.yaml" created
INFO Kubernetes file "redis-master-service.yaml" created
INFO Kubernetes file "redis-slave-service.yaml" created
INFO Kubernetes file "frontend-deployment.yaml" created
INFO Kubernetes file "redis-master-deployment.yaml" created
INFO Kubernetes file "redis-slave-deployment.yaml" created
```

Added integration test for the same.
`
This commit is contained in:
Suraj Narwade
2018-03-07 12:49:21 +05:30
parent 908e3f1b59
commit 6d4c8f9b78
8 changed files with 144 additions and 9 deletions
+1
View File
@@ -0,0 +1 @@
mode: atomic
+30
View File
@@ -0,0 +1,30 @@
package cmd
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"testing"
)
var ProjectPath = "$GOPATH/src/github.com/kubernetes/kompose/"
var BinaryLocation = os.ExpandEnv(ProjectPath + "kompose")
func Test_stdin(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
kjson := `{"version": "2","services": {"redis": {"image": "redis:3.0","ports": ["6379"]}}}`
cmdStr := fmt.Sprintf("%s convert --stdout -j -f - <<EOF\n%s\nEOF\n", BinaryLocation, kjson)
subproc := exec.Command("/bin/sh", "-c", cmdStr)
output, err := subproc.Output()
if err != nil {
fmt.Println("error", err)
}
g, err := ioutil.ReadFile("/tmp/output-k8s.json")
if !bytes.Equal(output, g) {
t.Errorf("Test Failed")
}
}
+7
View File
@@ -663,5 +663,12 @@ cmd="kompose convert --provider=openshift --stdout -j -f $KOMPOSE_ROOT/examples/
sed -e "s;%VERSION%;$version;g" -e "s;%CMD%;$cmd;g" $KOMPOSE_ROOT/script/test/fixtures/examples/output-gitlab-os.json > /tmp/output-os.json
convert::expect_success_and_warning "kompose convert --provider=openshift --stdout -j -f $KOMPOSE_ROOT/examples/docker-gitlab.yaml" "/tmp/output-os.json"
# Testing stdin feature
cmd="$KOMPOSE_ROOT/kompose convert --stdout -j -f -"
sed -e "s;%VERSION%;$version;g" -e "s;%CMD%;$cmd;g" $KOMPOSE_ROOT/script/test/fixtures/stdin/output.json > /tmp/output-k8s.json
echo -e "\n"
go test -v github.com/kubernetes/kompose/script/test/cmd
rm /tmp/output-k8s.json /tmp/output-os.json
exit $EXIT_STATUS
+80
View File
@@ -0,0 +1,80 @@
{
"kind": "List",
"apiVersion": "v1",
"metadata": {},
"items": [
{
"kind": "Service",
"apiVersion": "v1",
"metadata": {
"name": "redis",
"creationTimestamp": null,
"labels": {
"io.kompose.service": "redis"
},
"annotations": {
"kompose.cmd": "%CMD%",
"kompose.version": "%VERSION%"
}
},
"spec": {
"ports": [
{
"name": "6379",
"port": 6379,
"targetPort": 6379
}
],
"selector": {
"io.kompose.service": "redis"
}
},
"status": {
"loadBalancer": {}
}
},
{
"kind": "Deployment",
"apiVersion": "extensions/v1beta1",
"metadata": {
"name": "redis",
"creationTimestamp": null,
"labels": {
"io.kompose.service": "redis"
},
"annotations": {
"kompose.cmd": "%CMD%",
"kompose.version": "%VERSION%"
}
},
"spec": {
"replicas": 1,
"template": {
"metadata": {
"creationTimestamp": null,
"labels": {
"io.kompose.service": "redis"
}
},
"spec": {
"containers": [
{
"name": "redis",
"image": "redis:3.0",
"ports": [
{
"containerPort": 6379
}
],
"resources": {}
}
],
"restartPolicy": "Always"
}
},
"strategy": {}
},
"status": {}
}
]
}
+7
View File
@@ -117,6 +117,13 @@ test_k8s() {
./kompose down -f $f --controller=replicationcontroller
done
echo -e "\nTesting stdin to kompose\n"
echo -e "\n${RED}cat examples/docker-compose.yaml | ./kompose up -f -${NC}\n"
cat examples/docker-compose.yaml | ./kompose up -f -
sleep 2 # Sleep for k8s to catch up to deployment
echo -e "\n${RED}cat examples/docker-compose.yaml | ./kompose down -f - ${NC}\n"
cat examples/docker-compose.yaml | ./kompose down -f -
}
if [[ $1 == "start" ]]; then