diff --git a/cncd/pipeline/pipec/exec_test.go b/cncd/pipeline/pipec/exec_test.go deleted file mode 100644 index 1c8db673c..000000000 --- a/cncd/pipeline/pipec/exec_test.go +++ /dev/null @@ -1,83 +0,0 @@ -// +build integration - -package main - -import ( - "context" - "os" - "testing" - "time" - - "github.com/laszlocph/drone-oss-08/cncd/pipeline/pipeline" - "github.com/laszlocph/drone-oss-08/cncd/pipeline/pipeline/backend/docker" - "github.com/laszlocph/drone-oss-08/cncd/pipeline/pipeline/backend/kubernetes" - "github.com/laszlocph/drone-oss-08/cncd/pipeline/pipeline/interrupt" -) - -func TestExec(t *testing.T) { - reader, err := os.Open("../samples/sample_1/pipeline.json") - if err != nil { - t.Errorf("Could not read pipeline %f", err) - } - defer reader.Close() - - config, err := pipeline.Parse(reader) - if err != nil { - t.Errorf("Could not parse pipeline %f", err) - } - - engine, err := docker.NewEnv() - if err != nil { - t.Errorf("Could not start Docker engine %f", err) - } - - ctx, cancel := context.WithTimeout(context.Background(), time.Hour) - defer cancel() - ctx = interrupt.WithContext(ctx) - - err = pipeline.New(config, - pipeline.WithContext(ctx), - pipeline.WithLogger(defaultLogger), - pipeline.WithTracer(defaultTracer), - pipeline.WithEngine(engine), - ).Run() - - if err != nil { - t.Errorf("Pipeline exited with error %v", err) - } -} - -func TestKubeExec(t *testing.T) { - // reader, err := os.Open("../samples/sample_1/pipeline.json") - reader, err := os.Open("../samples/sample_7_redis/pipeline.json") - if err != nil { - t.Errorf("Could not read pipeline %f", err) - } - defer reader.Close() - - config, err := pipeline.Parse(reader) - if err != nil { - t.Errorf("Could not parse pipeline %f", err) - } - - // os.Setenv("KUBECONFIG", "/etc/rancher/k3s/k3s.yaml") - engine, err := kubernetes.New("default", "example-nfs", "100Mi") - if err != nil { - t.Errorf("Could not start Kubernetes engine %f", err) - } - - ctx, cancel := context.WithTimeout(context.Background(), time.Hour) - defer cancel() - ctx = interrupt.WithContext(ctx) - - err = pipeline.New(config, - pipeline.WithContext(ctx), - pipeline.WithLogger(defaultLogger), - pipeline.WithTracer(defaultTracer), - pipeline.WithEngine(engine), - ).Run() - - if err != nil { - t.Errorf("Pipeline exited with error %v", err) - } -} diff --git a/cncd/pipeline/pipec/pipec b/cncd/pipeline/pipec/pipec new file mode 100755 index 000000000..181f9f55b Binary files /dev/null and b/cncd/pipeline/pipec/pipec differ diff --git a/cncd/pipeline/pipeline/fixtures/01_single_step/.env b/cncd/pipeline/pipeline/fixtures/01_single_step/.env new file mode 100644 index 000000000..50dfaf1b8 --- /dev/null +++ b/cncd/pipeline/pipeline/fixtures/01_single_step/.env @@ -0,0 +1,17 @@ +CI_REPO=drone/envsubst +CI_REPO_NAME=drone/envsubst +CI_REPO_LINK=https://github.com/drone/envsubst +CI_REPO_REMOTE=https://github.com/drone/envsubst.git +CI_REPO_PRIVATE=false +CI_BUILD_NUMBER=6 +CI_BUILD_CREATED=1486119586 +CI_BUILD_STARTED=1486119585 +CI_BUILD_EVENT=push +CI_BUILD_LINK= +CI_COMMIT_SHA=d0876d3176965f9552a611cbd56e24a9264355e6 +CI_COMMIT_REF=refs/heads/master +CI_COMMIT_REFSPEC= +CI_COMMIT_BRANCH=master +CI_COMMIT_MESSAGE="added a few more test cases for escaping behavior" +CI_COMMIT_AUTHOR=bradrydzewski +CI_COMMIT_AUTHOR_NAME=bradrydzewski diff --git a/cncd/pipeline/pipeline/fixtures/01_single_step/compile b/cncd/pipeline/pipeline/fixtures/01_single_step/compile new file mode 100755 index 000000000..1640bb90d --- /dev/null +++ b/cncd/pipeline/pipeline/fixtures/01_single_step/compile @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +script_dir="$( cd "$(dirname "$0")" ; pwd -P )" + +cd $script_dir/../../../pipec/ +go build + +cd $script_dir +../../../pipec/pipec compile diff --git a/cncd/pipeline/pipeline/fixtures/01_single_step/pipeline.json b/cncd/pipeline/pipeline/fixtures/01_single_step/pipeline.json new file mode 100644 index 000000000..0f1781e76 --- /dev/null +++ b/cncd/pipeline/pipeline/fixtures/01_single_step/pipeline.json @@ -0,0 +1,168 @@ +{ + "pipeline": [ + { + "name": "pipeline_clone", + "alias": "clone", + "steps": [ + { + "name": "pipeline_clone", + "alias": "clone", + "image": "plugins/git:latest", + "working_dir": "/go/src/github.com/drone/envsubst", + "environment": { + "CI": "drone", + "CI_BUILD_CREATED": "1486119586", + "CI_BUILD_EVENT": "push", + "CI_BUILD_NUMBER": "6", + "CI_BUILD_STARTED": "1486119585", + "CI_COMMIT_AUTHOR": "bradrydzewski", + "CI_COMMIT_AUTHOR_NAME": "bradrydzewski", + "CI_COMMIT_BRANCH": "master", + "CI_COMMIT_MESSAGE": "added a few more test cases for escaping behavior", + "CI_COMMIT_REF": "refs/heads/master", + "CI_COMMIT_SHA": "d0876d3176965f9552a611cbd56e24a9264355e6", + "CI_REMOTE_URL": "https://github.com/drone/envsubst.git", + "CI_REPO": "drone/envsubst", + "CI_REPO_LINK": "https://github.com/drone/envsubst", + "CI_REPO_NAME": "drone/envsubst", + "CI_REPO_REMOTE": "https://github.com/drone/envsubst.git", + "CI_SYSTEM": "pipec", + "CI_SYSTEM_ARCH": "linux/amd64", + "CI_SYSTEM_LINK": "https://github.com/cncd/pipec", + "CI_SYSTEM_NAME": "pipec", + "CI_WORKSPACE": "/go/src/github.com/drone/envsubst", + "DRONE": "true", + "DRONE_ARCH": "linux/amd64", + "DRONE_BRANCH": "master", + "DRONE_BUILD_CREATED": "1486119586", + "DRONE_BUILD_EVENT": "push", + "DRONE_BUILD_LINK": "https://github.com/cncd/pipec/drone/envsubst/6", + "DRONE_BUILD_NUMBER": "6", + "DRONE_BUILD_STARTED": "1486119585", + "DRONE_COMMIT": "d0876d3176965f9552a611cbd56e24a9264355e6", + "DRONE_COMMIT_AUTHOR": "bradrydzewski", + "DRONE_COMMIT_BRANCH": "master", + "DRONE_COMMIT_MESSAGE": "added a few more test cases for escaping behavior", + "DRONE_COMMIT_REF": "refs/heads/master", + "DRONE_COMMIT_SHA": "d0876d3176965f9552a611cbd56e24a9264355e6", + "DRONE_JOB_STARTED": "1486119585", + "DRONE_REMOTE_URL": "https://github.com/drone/envsubst.git", + "DRONE_REPO": "drone/envsubst", + "DRONE_REPO_LINK": "https://github.com/drone/envsubst", + "DRONE_REPO_NAME": "envsubst", + "DRONE_REPO_OWNER": "drone", + "DRONE_REPO_SCM": "git", + "DRONE_WORKSPACE": "/go/src/github.com/drone/envsubst", + "PLUGIN_DEPTH": "0" + }, + "volumes": [ + "pipeline_default:/go" + ], + "networks": [ + { + "name": "pipeline_default", + "aliases": [ + "clone" + ] + } + ], + "on_success": true, + "auth_config": {} + } + ] + }, + { + "name": "pipeline_stage_0", + "alias": "build", + "steps": [ + { + "name": "pipeline_step_0", + "alias": "build", + "image": "golang:1.11", + "working_dir": "/go/src/github.com/drone/envsubst", + "environment": { + "CI": "drone", + "CI_BUILD_CREATED": "1486119586", + "CI_BUILD_EVENT": "push", + "CI_BUILD_NUMBER": "6", + "CI_BUILD_STARTED": "1486119585", + "CI_COMMIT_AUTHOR": "bradrydzewski", + "CI_COMMIT_AUTHOR_NAME": "bradrydzewski", + "CI_COMMIT_BRANCH": "master", + "CI_COMMIT_MESSAGE": "added a few more test cases for escaping behavior", + "CI_COMMIT_REF": "refs/heads/master", + "CI_COMMIT_SHA": "d0876d3176965f9552a611cbd56e24a9264355e6", + "CI_REMOTE_URL": "https://github.com/drone/envsubst.git", + "CI_REPO": "drone/envsubst", + "CI_REPO_LINK": "https://github.com/drone/envsubst", + "CI_REPO_NAME": "drone/envsubst", + "CI_REPO_REMOTE": "https://github.com/drone/envsubst.git", + "CI_SCRIPT": "CmlmIFsgLW4gIiRDSV9ORVRSQ19NQUNISU5FIiBdOyB0aGVuCmNhdCA8PEVPRiA+ICRIT01FLy5uZXRyYwptYWNoaW5lICRDSV9ORVRSQ19NQUNISU5FCmxvZ2luICRDSV9ORVRSQ19VU0VSTkFNRQpwYXNzd29yZCAkQ0lfTkVUUkNfUEFTU1dPUkQKRU9GCmNobW9kIDA2MDAgJEhPTUUvLm5ldHJjCmZpCnVuc2V0IENJX05FVFJDX1VTRVJOQU1FCnVuc2V0IENJX05FVFJDX1BBU1NXT1JECnVuc2V0IENJX1NDUklQVAp1bnNldCBEUk9ORV9ORVRSQ19VU0VSTkFNRQp1bnNldCBEUk9ORV9ORVRSQ19QQVNTV09SRAoKZWNobyArICJnbyBnZXQgLXQgLi8uLi4iCmdvIGdldCAtdCAuLy4uLgoKZWNobyArICJnbyBidWlsZCIKZ28gYnVpbGQKCmVjaG8gKyAiZ28gdGVzdCAtdiIKZ28gdGVzdCAtdgoK", + "CI_SYSTEM": "pipec", + "CI_SYSTEM_ARCH": "linux/amd64", + "CI_SYSTEM_LINK": "https://github.com/cncd/pipec", + "CI_SYSTEM_NAME": "pipec", + "CI_WORKSPACE": "/go/src/github.com/drone/envsubst", + "DRONE": "true", + "DRONE_ARCH": "linux/amd64", + "DRONE_BRANCH": "master", + "DRONE_BUILD_CREATED": "1486119586", + "DRONE_BUILD_EVENT": "push", + "DRONE_BUILD_LINK": "https://github.com/cncd/pipec/drone/envsubst/6", + "DRONE_BUILD_NUMBER": "6", + "DRONE_BUILD_STARTED": "1486119585", + "DRONE_COMMIT": "d0876d3176965f9552a611cbd56e24a9264355e6", + "DRONE_COMMIT_AUTHOR": "bradrydzewski", + "DRONE_COMMIT_BRANCH": "master", + "DRONE_COMMIT_MESSAGE": "added a few more test cases for escaping behavior", + "DRONE_COMMIT_REF": "refs/heads/master", + "DRONE_COMMIT_SHA": "d0876d3176965f9552a611cbd56e24a9264355e6", + "DRONE_JOB_STARTED": "1486119585", + "DRONE_REMOTE_URL": "https://github.com/drone/envsubst.git", + "DRONE_REPO": "drone/envsubst", + "DRONE_REPO_LINK": "https://github.com/drone/envsubst", + "DRONE_REPO_NAME": "envsubst", + "DRONE_REPO_OWNER": "drone", + "DRONE_REPO_SCM": "git", + "DRONE_WORKSPACE": "/go/src/github.com/drone/envsubst", + "HOME": "/root", + "SHELL": "/bin/sh" + }, + "entrypoint": [ + "/bin/sh", + "-c" + ], + "command": [ + "echo $CI_SCRIPT | base64 -d | /bin/sh -e" + ], + "volumes": [ + "pipeline_default:/go" + ], + "networks": [ + { + "name": "pipeline_default", + "aliases": [ + "build" + ] + } + ], + "on_success": true, + "auth_config": {} + } + ] + } + ], + "networks": [ + { + "name": "pipeline_default", + "driver": "bridge" + } + ], + "volumes": [ + { + "name": "pipeline_default", + "driver": "local" + } + ], + "secrets": null +} \ No newline at end of file diff --git a/cncd/pipeline/pipeline/fixtures/01_single_step/pipeline.yml b/cncd/pipeline/pipeline/fixtures/01_single_step/pipeline.yml new file mode 100644 index 000000000..d22ff6e45 --- /dev/null +++ b/cncd/pipeline/pipeline/fixtures/01_single_step/pipeline.yml @@ -0,0 +1,11 @@ +workspace: + base: /go + path: src/github.com/drone/envsubst + +pipeline: + build: + image: golang:1.11 + commands: + - go get -t ./... + - go build + - go test -v diff --git a/cncd/pipeline/pipeline/fixtures/02_services/.env b/cncd/pipeline/pipeline/fixtures/02_services/.env new file mode 100644 index 000000000..50dfaf1b8 --- /dev/null +++ b/cncd/pipeline/pipeline/fixtures/02_services/.env @@ -0,0 +1,17 @@ +CI_REPO=drone/envsubst +CI_REPO_NAME=drone/envsubst +CI_REPO_LINK=https://github.com/drone/envsubst +CI_REPO_REMOTE=https://github.com/drone/envsubst.git +CI_REPO_PRIVATE=false +CI_BUILD_NUMBER=6 +CI_BUILD_CREATED=1486119586 +CI_BUILD_STARTED=1486119585 +CI_BUILD_EVENT=push +CI_BUILD_LINK= +CI_COMMIT_SHA=d0876d3176965f9552a611cbd56e24a9264355e6 +CI_COMMIT_REF=refs/heads/master +CI_COMMIT_REFSPEC= +CI_COMMIT_BRANCH=master +CI_COMMIT_MESSAGE="added a few more test cases for escaping behavior" +CI_COMMIT_AUTHOR=bradrydzewski +CI_COMMIT_AUTHOR_NAME=bradrydzewski diff --git a/cncd/pipeline/pipeline/fixtures/02_services/compile b/cncd/pipeline/pipeline/fixtures/02_services/compile new file mode 100755 index 000000000..1640bb90d --- /dev/null +++ b/cncd/pipeline/pipeline/fixtures/02_services/compile @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +script_dir="$( cd "$(dirname "$0")" ; pwd -P )" + +cd $script_dir/../../../pipec/ +go build + +cd $script_dir +../../../pipec/pipec compile diff --git a/cncd/pipeline/pipeline/fixtures/02_services/pipeline.json b/cncd/pipeline/pipeline/fixtures/02_services/pipeline.json new file mode 100644 index 000000000..36579cacd --- /dev/null +++ b/cncd/pipeline/pipeline/fixtures/02_services/pipeline.json @@ -0,0 +1,302 @@ +{ + "pipeline": [ + { + "name": "pipeline_clone", + "alias": "clone", + "steps": [ + { + "name": "pipeline_clone", + "alias": "clone", + "image": "plugins/git:latest", + "working_dir": "/pipeline/src", + "environment": { + "CI": "drone", + "CI_BUILD_CREATED": "1486119586", + "CI_BUILD_EVENT": "push", + "CI_BUILD_NUMBER": "6", + "CI_BUILD_STARTED": "1486119585", + "CI_COMMIT_AUTHOR": "bradrydzewski", + "CI_COMMIT_AUTHOR_NAME": "bradrydzewski", + "CI_COMMIT_BRANCH": "master", + "CI_COMMIT_MESSAGE": "added a few more test cases for escaping behavior", + "CI_COMMIT_REF": "refs/heads/master", + "CI_COMMIT_SHA": "d0876d3176965f9552a611cbd56e24a9264355e6", + "CI_REMOTE_URL": "https://github.com/drone/envsubst.git", + "CI_REPO": "drone/envsubst", + "CI_REPO_LINK": "https://github.com/drone/envsubst", + "CI_REPO_NAME": "drone/envsubst", + "CI_REPO_REMOTE": "https://github.com/drone/envsubst.git", + "CI_SYSTEM": "pipec", + "CI_SYSTEM_ARCH": "linux/amd64", + "CI_SYSTEM_LINK": "https://github.com/cncd/pipec", + "CI_SYSTEM_NAME": "pipec", + "CI_WORKSPACE": "/pipeline/src", + "DRONE": "true", + "DRONE_ARCH": "linux/amd64", + "DRONE_BRANCH": "master", + "DRONE_BUILD_CREATED": "1486119586", + "DRONE_BUILD_EVENT": "push", + "DRONE_BUILD_LINK": "https://github.com/cncd/pipec/drone/envsubst/6", + "DRONE_BUILD_NUMBER": "6", + "DRONE_BUILD_STARTED": "1486119585", + "DRONE_COMMIT": "d0876d3176965f9552a611cbd56e24a9264355e6", + "DRONE_COMMIT_AUTHOR": "bradrydzewski", + "DRONE_COMMIT_BRANCH": "master", + "DRONE_COMMIT_MESSAGE": "added a few more test cases for escaping behavior", + "DRONE_COMMIT_REF": "refs/heads/master", + "DRONE_COMMIT_SHA": "d0876d3176965f9552a611cbd56e24a9264355e6", + "DRONE_JOB_STARTED": "1486119585", + "DRONE_REMOTE_URL": "https://github.com/drone/envsubst.git", + "DRONE_REPO": "drone/envsubst", + "DRONE_REPO_LINK": "https://github.com/drone/envsubst", + "DRONE_REPO_NAME": "envsubst", + "DRONE_REPO_OWNER": "drone", + "DRONE_REPO_SCM": "git", + "DRONE_WORKSPACE": "/pipeline/src", + "PLUGIN_DEPTH": "0" + }, + "volumes": [ + "pipeline_default:/pipeline" + ], + "networks": [ + { + "name": "pipeline_default", + "aliases": [ + "clone" + ] + } + ], + "on_success": true, + "auth_config": {} + } + ] + }, + { + "name": "pipeline_services", + "alias": "services", + "steps": [ + { + "name": "pipeline_services_0", + "alias": "redis1", + "image": "redis:3.0", + "detach": true, + "environment": { + "CI": "drone", + "CI_BUILD_CREATED": "1486119586", + "CI_BUILD_EVENT": "push", + "CI_BUILD_NUMBER": "6", + "CI_BUILD_STARTED": "1486119585", + "CI_COMMIT_AUTHOR": "bradrydzewski", + "CI_COMMIT_AUTHOR_NAME": "bradrydzewski", + "CI_COMMIT_BRANCH": "master", + "CI_COMMIT_MESSAGE": "added a few more test cases for escaping behavior", + "CI_COMMIT_REF": "refs/heads/master", + "CI_COMMIT_SHA": "d0876d3176965f9552a611cbd56e24a9264355e6", + "CI_REMOTE_URL": "https://github.com/drone/envsubst.git", + "CI_REPO": "drone/envsubst", + "CI_REPO_LINK": "https://github.com/drone/envsubst", + "CI_REPO_NAME": "drone/envsubst", + "CI_REPO_REMOTE": "https://github.com/drone/envsubst.git", + "CI_SYSTEM": "pipec", + "CI_SYSTEM_ARCH": "linux/amd64", + "CI_SYSTEM_LINK": "https://github.com/cncd/pipec", + "CI_SYSTEM_NAME": "pipec", + "CI_WORKSPACE": "/pipeline/src", + "DRONE": "true", + "DRONE_ARCH": "linux/amd64", + "DRONE_BRANCH": "master", + "DRONE_BUILD_CREATED": "1486119586", + "DRONE_BUILD_EVENT": "push", + "DRONE_BUILD_LINK": "https://github.com/cncd/pipec/drone/envsubst/6", + "DRONE_BUILD_NUMBER": "6", + "DRONE_BUILD_STARTED": "1486119585", + "DRONE_COMMIT": "d0876d3176965f9552a611cbd56e24a9264355e6", + "DRONE_COMMIT_AUTHOR": "bradrydzewski", + "DRONE_COMMIT_BRANCH": "master", + "DRONE_COMMIT_MESSAGE": "added a few more test cases for escaping behavior", + "DRONE_COMMIT_REF": "refs/heads/master", + "DRONE_COMMIT_SHA": "d0876d3176965f9552a611cbd56e24a9264355e6", + "DRONE_JOB_STARTED": "1486119585", + "DRONE_REMOTE_URL": "https://github.com/drone/envsubst.git", + "DRONE_REPO": "drone/envsubst", + "DRONE_REPO_LINK": "https://github.com/drone/envsubst", + "DRONE_REPO_NAME": "envsubst", + "DRONE_REPO_OWNER": "drone", + "DRONE_REPO_SCM": "git", + "DRONE_WORKSPACE": "/pipeline/src" + }, + "volumes": [ + "pipeline_default:/pipeline" + ], + "networks": [ + { + "name": "pipeline_default", + "aliases": [ + "redis1" + ] + } + ], + "on_success": true, + "auth_config": {} + }, + { + "name": "pipeline_services_1", + "alias": "redis2", + "image": "redis:3.0", + "detach": true, + "environment": { + "CI": "drone", + "CI_BUILD_CREATED": "1486119586", + "CI_BUILD_EVENT": "push", + "CI_BUILD_NUMBER": "6", + "CI_BUILD_STARTED": "1486119585", + "CI_COMMIT_AUTHOR": "bradrydzewski", + "CI_COMMIT_AUTHOR_NAME": "bradrydzewski", + "CI_COMMIT_BRANCH": "master", + "CI_COMMIT_MESSAGE": "added a few more test cases for escaping behavior", + "CI_COMMIT_REF": "refs/heads/master", + "CI_COMMIT_SHA": "d0876d3176965f9552a611cbd56e24a9264355e6", + "CI_REMOTE_URL": "https://github.com/drone/envsubst.git", + "CI_REPO": "drone/envsubst", + "CI_REPO_LINK": "https://github.com/drone/envsubst", + "CI_REPO_NAME": "drone/envsubst", + "CI_REPO_REMOTE": "https://github.com/drone/envsubst.git", + "CI_SYSTEM": "pipec", + "CI_SYSTEM_ARCH": "linux/amd64", + "CI_SYSTEM_LINK": "https://github.com/cncd/pipec", + "CI_SYSTEM_NAME": "pipec", + "CI_WORKSPACE": "/pipeline/src", + "DRONE": "true", + "DRONE_ARCH": "linux/amd64", + "DRONE_BRANCH": "master", + "DRONE_BUILD_CREATED": "1486119586", + "DRONE_BUILD_EVENT": "push", + "DRONE_BUILD_LINK": "https://github.com/cncd/pipec/drone/envsubst/6", + "DRONE_BUILD_NUMBER": "6", + "DRONE_BUILD_STARTED": "1486119585", + "DRONE_COMMIT": "d0876d3176965f9552a611cbd56e24a9264355e6", + "DRONE_COMMIT_AUTHOR": "bradrydzewski", + "DRONE_COMMIT_BRANCH": "master", + "DRONE_COMMIT_MESSAGE": "added a few more test cases for escaping behavior", + "DRONE_COMMIT_REF": "refs/heads/master", + "DRONE_COMMIT_SHA": "d0876d3176965f9552a611cbd56e24a9264355e6", + "DRONE_JOB_STARTED": "1486119585", + "DRONE_REMOTE_URL": "https://github.com/drone/envsubst.git", + "DRONE_REPO": "drone/envsubst", + "DRONE_REPO_LINK": "https://github.com/drone/envsubst", + "DRONE_REPO_NAME": "envsubst", + "DRONE_REPO_OWNER": "drone", + "DRONE_REPO_SCM": "git", + "DRONE_WORKSPACE": "/pipeline/src" + }, + "volumes": [ + "pipeline_default:/pipeline" + ], + "networks": [ + { + "name": "pipeline_default", + "aliases": [ + "redis2" + ] + } + ], + "on_success": true, + "auth_config": {} + } + ] + }, + { + "name": "pipeline_stage_0", + "alias": "build", + "steps": [ + { + "name": "pipeline_step_0", + "alias": "build", + "image": "redis:3.0", + "working_dir": "/pipeline/src", + "environment": { + "CI": "drone", + "CI_BUILD_CREATED": "1486119586", + "CI_BUILD_EVENT": "push", + "CI_BUILD_NUMBER": "6", + "CI_BUILD_STARTED": "1486119585", + "CI_COMMIT_AUTHOR": "bradrydzewski", + "CI_COMMIT_AUTHOR_NAME": "bradrydzewski", + "CI_COMMIT_BRANCH": "master", + "CI_COMMIT_MESSAGE": "added a few more test cases for escaping behavior", + "CI_COMMIT_REF": "refs/heads/master", + "CI_COMMIT_SHA": "d0876d3176965f9552a611cbd56e24a9264355e6", + "CI_REMOTE_URL": "https://github.com/drone/envsubst.git", + "CI_REPO": "drone/envsubst", + "CI_REPO_LINK": "https://github.com/drone/envsubst", + "CI_REPO_NAME": "drone/envsubst", + "CI_REPO_REMOTE": "https://github.com/drone/envsubst.git", + "CI_SCRIPT": "CmlmIFsgLW4gIiRDSV9ORVRSQ19NQUNISU5FIiBdOyB0aGVuCmNhdCA8PEVPRiA+ICRIT01FLy5uZXRyYwptYWNoaW5lICRDSV9ORVRSQ19NQUNISU5FCmxvZ2luICRDSV9ORVRSQ19VU0VSTkFNRQpwYXNzd29yZCAkQ0lfTkVUUkNfUEFTU1dPUkQKRU9GCmNobW9kIDA2MDAgJEhPTUUvLm5ldHJjCmZpCnVuc2V0IENJX05FVFJDX1VTRVJOQU1FCnVuc2V0IENJX05FVFJDX1BBU1NXT1JECnVuc2V0IENJX1NDUklQVAp1bnNldCBEUk9ORV9ORVRSQ19VU0VSTkFNRQp1bnNldCBEUk9ORV9ORVRSQ19QQVNTV09SRAoKZWNobyArICJzbGVlcCAxIgpzbGVlcCAxCgplY2hvICsgInJlZGlzLWNsaSAtaCByZWRpczEgcGluZyIKcmVkaXMtY2xpIC1oIHJlZGlzMSBwaW5nCgplY2hvICsgInJlZGlzLWNsaSAtaCByZWRpczIgcGluZyIKcmVkaXMtY2xpIC1oIHJlZGlzMiBwaW5nCgplY2hvICsgInJlZGlzLWNsaSAtaCByZWRpczEgc2V0IEhFTExPIGhlbGxvIgpyZWRpcy1jbGkgLWggcmVkaXMxIHNldCBIRUxMTyBoZWxsbwoKZWNobyArICJyZWRpcy1jbGkgLWggcmVkaXMyIHNldCBIRUxMTyBob2xhIgpyZWRpcy1jbGkgLWggcmVkaXMyIHNldCBIRUxMTyBob2xhCgplY2hvICsgInJlZGlzLWNsaSAtaCByZWRpczEgZ2V0IEhFTExPIgpyZWRpcy1jbGkgLWggcmVkaXMxIGdldCBIRUxMTwoKZWNobyArICJyZWRpcy1jbGkgLWggcmVkaXMyIGdldCBIRUxMTyIKcmVkaXMtY2xpIC1oIHJlZGlzMiBnZXQgSEVMTE8KCg==", + "CI_SYSTEM": "pipec", + "CI_SYSTEM_ARCH": "linux/amd64", + "CI_SYSTEM_LINK": "https://github.com/cncd/pipec", + "CI_SYSTEM_NAME": "pipec", + "CI_WORKSPACE": "/pipeline/src", + "DRONE": "true", + "DRONE_ARCH": "linux/amd64", + "DRONE_BRANCH": "master", + "DRONE_BUILD_CREATED": "1486119586", + "DRONE_BUILD_EVENT": "push", + "DRONE_BUILD_LINK": "https://github.com/cncd/pipec/drone/envsubst/6", + "DRONE_BUILD_NUMBER": "6", + "DRONE_BUILD_STARTED": "1486119585", + "DRONE_COMMIT": "d0876d3176965f9552a611cbd56e24a9264355e6", + "DRONE_COMMIT_AUTHOR": "bradrydzewski", + "DRONE_COMMIT_BRANCH": "master", + "DRONE_COMMIT_MESSAGE": "added a few more test cases for escaping behavior", + "DRONE_COMMIT_REF": "refs/heads/master", + "DRONE_COMMIT_SHA": "d0876d3176965f9552a611cbd56e24a9264355e6", + "DRONE_JOB_STARTED": "1486119585", + "DRONE_REMOTE_URL": "https://github.com/drone/envsubst.git", + "DRONE_REPO": "drone/envsubst", + "DRONE_REPO_LINK": "https://github.com/drone/envsubst", + "DRONE_REPO_NAME": "envsubst", + "DRONE_REPO_OWNER": "drone", + "DRONE_REPO_SCM": "git", + "DRONE_WORKSPACE": "/pipeline/src", + "HOME": "/root", + "SHELL": "/bin/sh" + }, + "entrypoint": [ + "/bin/sh", + "-c" + ], + "command": [ + "echo $CI_SCRIPT | base64 -d | /bin/sh -e" + ], + "volumes": [ + "pipeline_default:/pipeline" + ], + "networks": [ + { + "name": "pipeline_default", + "aliases": [ + "build" + ] + } + ], + "on_success": true, + "auth_config": {} + } + ] + } + ], + "networks": [ + { + "name": "pipeline_default", + "driver": "bridge" + } + ], + "volumes": [ + { + "name": "pipeline_default", + "driver": "local" + } + ], + "secrets": null +} \ No newline at end of file diff --git a/cncd/pipeline/pipeline/fixtures/02_services/pipeline.yml b/cncd/pipeline/pipeline/fixtures/02_services/pipeline.yml new file mode 100644 index 000000000..ae8b73bd0 --- /dev/null +++ b/cncd/pipeline/pipeline/fixtures/02_services/pipeline.yml @@ -0,0 +1,17 @@ +pipeline: + build: + image: redis:3.0 + commands: + - sleep 1 # redis needs a second to init + - redis-cli -h redis1 ping + - redis-cli -h redis2 ping + - redis-cli -h redis1 set HELLO hello + - redis-cli -h redis2 set HELLO hola + - redis-cli -h redis1 get HELLO + - redis-cli -h redis2 get HELLO + +services: + redis1: + image: redis:3.0 + redis2: + image: redis:3.0 diff --git a/cncd/pipeline/pipeline/pipeline_test.go b/cncd/pipeline/pipeline/pipeline_test.go new file mode 100644 index 000000000..8d031d11f --- /dev/null +++ b/cncd/pipeline/pipeline/pipeline_test.go @@ -0,0 +1,112 @@ +// +build integration + +package pipeline + +import ( + "context" + "fmt" + "io" + "os" + "strconv" + "testing" + "time" + + "github.com/laszlocph/drone-oss-08/cncd/pipeline/pipeline/backend" + "github.com/laszlocph/drone-oss-08/cncd/pipeline/pipeline/backend/docker" + "github.com/laszlocph/drone-oss-08/cncd/pipeline/pipeline/backend/kubernetes" + "github.com/laszlocph/drone-oss-08/cncd/pipeline/pipeline/interrupt" + "github.com/laszlocph/drone-oss-08/cncd/pipeline/pipeline/multipart" +) + +func Test_Docker_01_single_step(t *testing.T) { + run(t, "fixtures/01_single_step/pipeline.json", true) +} + +func Test_Kubernetes_01_single_step(t *testing.T) { + run(t, "fixtures/01_single_step/pipeline.json", false) +} + +func Test_Docker_02_services(t *testing.T) { + run(t, "fixtures/02_services/pipeline.json", true) +} + +func Test_Kubernetes_02_services(t *testing.T) { + run(t, "fixtures/02_services/pipeline.json", false) +} + +func run(t *testing.T, fixture string, dockerEngine bool) { + reader, err := os.Open(fixture) + if err != nil { + t.Errorf("Could not read pipeline %f", err) + } + defer reader.Close() + + config, err := Parse(reader) + if err != nil { + t.Errorf("Could not parse pipeline %f", err) + } + + var defaultTracer = TraceFunc(func(state *State) error { + if state.Process.Exited { + if state.Process.ExitCode != 0 { + t.Errorf("proc %q exited with status %d\n", state.Pipeline.Step.Name, state.Process.ExitCode) + } else { + fmt.Printf("proc %q exited with status %d\n", state.Pipeline.Step.Name, state.Process.ExitCode) + } + } else { + fmt.Printf("proc %q started\n", state.Pipeline.Step.Name) + state.Pipeline.Step.Environment["CI_BUILD_STATUS"] = "success" + state.Pipeline.Step.Environment["CI_BUILD_FINISHED"] = strconv.FormatInt(time.Now().Unix(), 10) + if state.Pipeline.Error != nil { + state.Pipeline.Step.Environment["CI_BUILD_STATUS"] = "failure" + } + } + return nil + }) + + ctx, cancel := context.WithTimeout(context.Background(), time.Hour) + defer cancel() + ctx = interrupt.WithContext(ctx) + + if dockerEngine { + dockerEngine, err := docker.NewEnv() + if err != nil { + t.Errorf("Could not start Docker engine %f", err) + } + err = New(config, + WithContext(ctx), + WithLogger(defaultLogger), + WithTracer(defaultTracer), + WithEngine(dockerEngine), + ).Run() + + if err != nil { + t.Errorf("Pipeline exited with error %v", err) + } + } else { + // os.Setenv("KUBECONFIG", "/etc/rancher/k3s/k3s.yaml") + kubernetesEngine, err := kubernetes.New("default", "example-nfs", "100Mi") + if err != nil { + t.Errorf("Could not start Kubernetes engine %f", err) + } + err = New(config, + WithContext(ctx), + WithLogger(defaultLogger), + WithTracer(defaultTracer), + WithEngine(kubernetesEngine), + ).Run() + + if err != nil { + t.Errorf("Pipeline exited with error %v", err) + } + } +} + +var defaultLogger = LogFunc(func(proc *backend.Step, rc multipart.Reader) error { + part, err := rc.NextPart() + if err != nil { + return err + } + io.Copy(os.Stderr, part) + return nil +})