grim/convey

Parents c47dd1f8ad1d
Children 01bbb77efc2b
Finish up implement the detailed image settings for bitbucket pipelines. Fixes #114
--- a/bitbucket/loader.go Sat Jul 22 13:29:17 2017 -0500
+++ b/bitbucket/loader.go Sat Jul 22 14:02:29 2017 -0500
@@ -66,11 +66,43 @@
}
for idx, pipeline := range pipelines {
+ loginTask := ""
+ logoutTask := ""
+
image := defImage
if pipeline.Steps.Image.Name != "" {
image = pipeline.Steps.Image
+
+ // if the step has an image with a username, we need to add the tasks to login and logout
+ if image.Username != "" {
+ registry, _, _ := docker.ParseImage(image.Name)
+
+ // create and add the login task to the stage
+ loginTask = fmt.Sprintf("%s-%d-login", name, idx)
+ cfg.Tasks[loginTask] = &docker.Login{
+ Username: image.Username,
+ Password: image.Password,
+ Server: registry,
+ }
+
+ // create the logout task, but store the name so we can add it after the other tasks
+ logoutTask = fmt.Sprintf("%s-%d-logout", name, idx)
+ cfg.Tasks[logoutTask] = &docker.Logout{
+ Server: registry,
+ }
+ }
+ } else if image.Username != "" {
+ // if we're using the global image and it has a username, we need to add the tasks to the stage
+ loginTask = "login"
+ logoutTask = "logout"
}
+ // if we have a login task, add it to the tasks for this stage
+ if loginTask != "" {
+ plan.Stages[idx].Tasks = append(plan.Stages[idx].Tasks, loginTask)
+ }
+
+ // now figure out how to parse the script in the config
currentScript := []string{}
last := -1
@@ -114,6 +146,12 @@
name := fmt.Sprintf("%s-step-%d-%d", name, idx, last+1)
addScript(name, image, currentScript, plan, cfg)
}
+
+ // if we have a logout task, add it to the tasks for this stage
+ if logoutTask != "" {
+ plan.Stages[idx].Tasks = append(plan.Stages[idx].Tasks, logoutTask)
+ }
+
}
cfg.Plans[name] = plan
@@ -135,8 +173,6 @@
return nil, err
}
- defImage := pipeline.Image
-
cfg := &config.Config{
Tasks: map[string]tasks.Task{
"import": &docker.Import{
@@ -146,6 +182,23 @@
Plans: map[string]plans.Plan{},
}
+ // store the default image
+ defImage := pipeline.Image
+
+ // if the default image has a username, add a login/logout task
+ if defImage.Username != "" {
+ registry, _, _ := docker.ParseImage(defImage.Name)
+ cfg.Tasks["login"] = &docker.Login{
+ Username: defImage.Username,
+ Password: defImage.Password,
+ Server: registry,
+ }
+
+ cfg.Tasks["logout"] = &docker.Logout{
+ Server: registry,
+ }
+ }
+
// add the default pipelines
if len(pipeline.Pipelines.Default) > 0 {
addPipeline("default", defImage, pipeline.Pipelines.Default, cfg)
--- a/bitbucket/loader_test.go Sat Jul 22 13:29:17 2017 -0500
+++ b/bitbucket/loader_test.go Sat Jul 22 14:02:29 2017 -0500
@@ -86,6 +86,259 @@
Expect(cfg).To(Equal(expected))
}
+func (b *bitbucketSuite) TestLoaderComplexGlobalImage(t *testing.T) {
+ l := Loader{}
+
+ data := []byte(`image:
+ name: python:3
+pipelines:
+ default:
+ - step:
+ script:
+ - set -ex
+ - find . -type f -iname "*.pyc" -exec rm -f {} \; || true
+ - find . -type d -iname __pycache__ -exec rm -rf {} \; || true
+ - pip install -r dev-requirements.txt
+ - flake8
+ - PYTHONPATH=$(pwd) py.test --color=auto --cov=pipelines --cov-report=term-missing tests`)
+
+ cfg, err := l.Load("", "", data)
+
+ Expect(err).To(BeNil())
+
+ expected := &config.Config{
+ Tasks: map[string]tasks.Task{
+ "import": &docker.Import{
+ Files: []string{"."},
+ },
+ "default-step-0-0": &docker.Run{
+ Image: "python:3",
+ WorkDir: "/workspace",
+ Script: []string{
+ "set -ex",
+ "find . -type f -iname \"*.pyc\" -exec rm -f {} \\; || true",
+ "find . -type d -iname __pycache__ -exec rm -rf {} \\; || true",
+ "pip install -r dev-requirements.txt",
+ "flake8",
+ "PYTHONPATH=$(pwd) py.test --color=auto --cov=pipelines --cov-report=term-missing tests",
+ },
+ Shell: "/bin/sh",
+ },
+ },
+ Plans: map[string]plans.Plan{
+ "default": {
+ Stages: []stages.Stage{
+ stages.Stage{
+ Name: "stage-0",
+ Enabled: true,
+ Always: false,
+ Concurrent: false,
+ Environment: nil,
+ Tasks: []string{"import", "default-step-0-0"},
+ },
+ },
+ },
+ },
+ }
+
+ Expect(cfg).To(Equal(expected))
+}
+
+func (b *bitbucketSuite) TestLoaderComplexGlobalImageWithLogin(t *testing.T) {
+ l := Loader{}
+
+ data := []byte(`image:
+ name: registry.docker.io/python:3
+ username: foo
+ password: bar
+ email: email
+pipelines:
+ default:
+ - step:
+ script:
+ - set -ex
+ - find . -type f -iname "*.pyc" -exec rm -f {} \; || true
+ - find . -type d -iname __pycache__ -exec rm -rf {} \; || true
+ - pip install -r dev-requirements.txt
+ - flake8
+ - PYTHONPATH=$(pwd) py.test --color=auto --cov=pipelines --cov-report=term-missing tests`)
+
+ cfg, err := l.Load("", "", data)
+
+ Expect(err).To(BeNil())
+
+ expected := &config.Config{
+ Tasks: map[string]tasks.Task{
+ "import": &docker.Import{
+ Files: []string{"."},
+ },
+ "login": &docker.Login{
+ Username: "foo",
+ Password: "bar",
+ Server: "registry.docker.io",
+ },
+ "default-step-0-0": &docker.Run{
+ Image: "registry.docker.io/python:3",
+ WorkDir: "/workspace",
+ Script: []string{
+ "set -ex",
+ "find . -type f -iname \"*.pyc\" -exec rm -f {} \\; || true",
+ "find . -type d -iname __pycache__ -exec rm -rf {} \\; || true",
+ "pip install -r dev-requirements.txt",
+ "flake8",
+ "PYTHONPATH=$(pwd) py.test --color=auto --cov=pipelines --cov-report=term-missing tests",
+ },
+ Shell: "/bin/sh",
+ },
+ "logout": &docker.Logout{
+ Server: "registry.docker.io",
+ },
+ },
+ Plans: map[string]plans.Plan{
+ "default": {
+ Stages: []stages.Stage{
+ stages.Stage{
+ Name: "stage-0",
+ Enabled: true,
+ Always: false,
+ Concurrent: false,
+ Environment: nil,
+ Tasks: []string{"import", "login", "default-step-0-0", "logout"},
+ },
+ },
+ },
+ },
+ }
+
+ Expect(cfg).To(Equal(expected))
+}
+
+func (b *bitbucketSuite) TestLoaderComplexStepImage(t *testing.T) {
+ l := Loader{}
+
+ data := []byte(`pipelines:
+ default:
+ - step:
+ image:
+ name: python:3
+ script:
+ - set -ex
+ - find . -type f -iname "*.pyc" -exec rm -f {} \; || true
+ - find . -type d -iname __pycache__ -exec rm -rf {} \; || true
+ - pip install -r dev-requirements.txt
+ - flake8
+ - PYTHONPATH=$(pwd) py.test --color=auto --cov=pipelines --cov-report=term-missing tests`)
+
+ cfg, err := l.Load("", "", data)
+
+ Expect(err).To(BeNil())
+
+ expected := &config.Config{
+ Tasks: map[string]tasks.Task{
+ "import": &docker.Import{
+ Files: []string{"."},
+ },
+ "default-step-0-0": &docker.Run{
+ Image: "python:3",
+ WorkDir: "/workspace",
+ Script: []string{
+ "set -ex",
+ "find . -type f -iname \"*.pyc\" -exec rm -f {} \\; || true",
+ "find . -type d -iname __pycache__ -exec rm -rf {} \\; || true",
+ "pip install -r dev-requirements.txt",
+ "flake8",
+ "PYTHONPATH=$(pwd) py.test --color=auto --cov=pipelines --cov-report=term-missing tests",
+ },
+ Shell: "/bin/sh",
+ },
+ },
+ Plans: map[string]plans.Plan{
+ "default": {
+ Stages: []stages.Stage{
+ stages.Stage{
+ Name: "stage-0",
+ Enabled: true,
+ Always: false,
+ Concurrent: false,
+ Environment: nil,
+ Tasks: []string{"import", "default-step-0-0"},
+ },
+ },
+ },
+ },
+ }
+
+ Expect(cfg).To(Equal(expected))
+}
+
+func (b *bitbucketSuite) TestLoaderComplexStepImageWithLogin(t *testing.T) {
+ l := Loader{}
+
+ data := []byte(`pipelines:
+ default:
+ - step:
+ image:
+ name: registry.docker.io/python:3
+ username: user
+ password: pass
+ script:
+ - set -ex
+ - find . -type f -iname "*.pyc" -exec rm -f {} \; || true
+ - find . -type d -iname __pycache__ -exec rm -rf {} \; || true
+ - pip install -r dev-requirements.txt
+ - flake8
+ - PYTHONPATH=$(pwd) py.test --color=auto --cov=pipelines --cov-report=term-missing tests`)
+
+ cfg, err := l.Load("", "", data)
+
+ Expect(err).To(BeNil())
+
+ expected := &config.Config{
+ Tasks: map[string]tasks.Task{
+ "import": &docker.Import{
+ Files: []string{"."},
+ },
+ "default-0-login": &docker.Login{
+ Username: "user",
+ Password: "pass",
+ Server: "registry.docker.io",
+ },
+ "default-step-0-0": &docker.Run{
+ Image: "registry.docker.io/python:3",
+ WorkDir: "/workspace",
+ Script: []string{
+ "set -ex",
+ "find . -type f -iname \"*.pyc\" -exec rm -f {} \\; || true",
+ "find . -type d -iname __pycache__ -exec rm -rf {} \\; || true",
+ "pip install -r dev-requirements.txt",
+ "flake8",
+ "PYTHONPATH=$(pwd) py.test --color=auto --cov=pipelines --cov-report=term-missing tests",
+ },
+ Shell: "/bin/sh",
+ },
+ "default-0-logout": &docker.Logout{
+ Server: "registry.docker.io",
+ },
+ },
+ Plans: map[string]plans.Plan{
+ "default": {
+ Stages: []stages.Stage{
+ stages.Stage{
+ Name: "stage-0",
+ Enabled: true,
+ Always: false,
+ Concurrent: false,
+ Environment: nil,
+ Tasks: []string{"import", "default-0-login", "default-step-0-0", "default-0-logout"},
+ },
+ },
+ },
+ },
+ }
+
+ Expect(cfg).To(Equal(expected))
+}
+
func (b *bitbucketSuite) TestLoaderDockerMixed(t *testing.T) {
l := Loader{}