forked from LaconicNetwork/kompose
Merge pull request #871 from surajnarwade/stdin
Kompose will read input from stdin
This commit is contained in:
commit
5a374742cf
@ -24,6 +24,7 @@ install:
|
|||||||
- script/test/cmd/fix_detached_head.sh
|
- script/test/cmd/fix_detached_head.sh
|
||||||
|
|
||||||
script:
|
script:
|
||||||
|
- make bin
|
||||||
- make test
|
- make test
|
||||||
# Only cross-compile once
|
# Only cross-compile once
|
||||||
- if [ "$CROSS_COMPILE" == "yes" ]; then make cross; fi
|
- if [ "$CROSS_COMPILE" == "yes" ]; then make cross; fi
|
||||||
|
|||||||
10
Makefile
10
Makefile
@ -44,16 +44,16 @@ clean:
|
|||||||
|
|
||||||
.PHONY: test-unit
|
.PHONY: test-unit
|
||||||
test-unit:
|
test-unit:
|
||||||
go test $(BUILD_FLAGS) -race -cover -v $(PKGS)
|
go test -short $(BUILD_FLAGS) -race -cover -v $(PKGS)
|
||||||
|
|
||||||
# Run unit tests and collect coverage
|
# Run unit tests and collect coverage
|
||||||
.PHONY: test-unit-cover
|
.PHONY: test-unit-cover
|
||||||
test-unit-cover:
|
test-unit-cover:
|
||||||
# First install packages that are dependencies of the test.
|
# First install packages that are dependencies of the test.
|
||||||
go test -i -race -cover $(PKGS)
|
go test -short -i -race -cover $(PKGS)
|
||||||
# go test doesn't support collecting coverage across multiple packages,
|
# go test doesn't support colleting coverage across multiple packages,
|
||||||
# generate go test commands using go list and run go test for every package separately
|
# generate go test commands using go list and run go test for every package separately
|
||||||
go list -f '"go test -race -cover -v -coverprofile={{.Dir}}/.coverprofile {{.ImportPath}}"' github.com/kubernetes/kompose/... | grep -v "vendor" | xargs -L 1 -P4 sh -c
|
go list -f '"go test -short -race -cover -v -coverprofile={{.Dir}}/.coverprofile {{.ImportPath}}"' github.com/kubernetes/kompose/... | grep -v "vendor" | xargs -L 1 -P4 sh -c
|
||||||
|
|
||||||
|
|
||||||
# run openshift up/down tests
|
# run openshift up/down tests
|
||||||
@ -94,7 +94,7 @@ check-vendor:
|
|||||||
|
|
||||||
# Run all tests
|
# Run all tests
|
||||||
.PHONY: test
|
.PHONY: test
|
||||||
test: test-dep check-vendor validate test-unit-cover install test-cmd
|
test: bin test-dep check-vendor validate test-unit-cover install test-cmd
|
||||||
|
|
||||||
# Install all the required test-dependencies before executing tests (only valid when running `make test`)
|
# Install all the required test-dependencies before executing tests (only valid when running `make test`)
|
||||||
.PHONY: test-dep
|
.PHONY: test-dep
|
||||||
|
|||||||
@ -24,6 +24,9 @@ import (
|
|||||||
|
|
||||||
yaml "gopkg.in/yaml.v2"
|
yaml "gopkg.in/yaml.v2"
|
||||||
|
|
||||||
|
"bufio"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/docker/libcompose/project"
|
"github.com/docker/libcompose/project"
|
||||||
"github.com/fatih/structs"
|
"github.com/fatih/structs"
|
||||||
"github.com/kubernetes/kompose/pkg/kobject"
|
"github.com/kubernetes/kompose/pkg/kobject"
|
||||||
@ -197,10 +200,16 @@ func getVersionFromFile(file string) (string, error) {
|
|||||||
Version string `json:"version"` // This affects YAML as well
|
Version string `json:"version"` // This affects YAML as well
|
||||||
}
|
}
|
||||||
var version ComposeVersion
|
var version ComposeVersion
|
||||||
|
var loadedFile []byte
|
||||||
loadedFile, err := ioutil.ReadFile(file)
|
var err error
|
||||||
if err != nil {
|
if file == "-" {
|
||||||
return "", err
|
data := bufio.NewScanner(os.Stdin)
|
||||||
|
loadedFile = data.Bytes()
|
||||||
|
} else {
|
||||||
|
loadedFile, err = ioutil.ReadFile(file)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = yaml.Unmarshal(loadedFile, &version)
|
err = yaml.Unmarshal(loadedFile, &version)
|
||||||
|
|||||||
1
script/test/cmd/.coverprofile
Normal file
1
script/test/cmd/.coverprofile
Normal file
@ -0,0 +1 @@
|
|||||||
|
mode: atomic
|
||||||
30
script/test/cmd/cmd_test.go
Normal file
30
script/test/cmd/cmd_test.go
Normal 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")
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -758,5 +758,12 @@ cmd="kompose --provider=openshift convert --stdout -j -f $KOMPOSE_ROOT/script/te
|
|||||||
sed -e "s;%VERSION%;$version;g" -e "s;%CMD%;$cmd;g" $KOMPOSE_ROOT/script/test/fixtures/controller/output-os-controller-v3-template.json > /tmp/output-os.json
|
sed -e "s;%VERSION%;$version;g" -e "s;%CMD%;$cmd;g" $KOMPOSE_ROOT/script/test/fixtures/controller/output-os-controller-v3-template.json > /tmp/output-os.json
|
||||||
convert::expect_success "$cmd" "/tmp/output-os.json"
|
convert::expect_success "$cmd" "/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
|
rm /tmp/output-k8s.json /tmp/output-os.json
|
||||||
exit $EXIT_STATUS
|
exit $EXIT_STATUS
|
||||||
|
|||||||
80
script/test/fixtures/stdin/output.json
vendored
Normal file
80
script/test/fixtures/stdin/output.json
vendored
Normal 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": {}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@ -117,6 +117,13 @@ test_k8s() {
|
|||||||
./kompose down -f $f --controller=replicationcontroller
|
./kompose down -f $f --controller=replicationcontroller
|
||||||
|
|
||||||
done
|
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
|
if [[ $1 == "start" ]]; then
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user