2014-06-20 22:47:32 +00:00
|
|
|
package publish
|
|
|
|
|
|
|
|
import (
|
2014-08-08 08:15:32 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2014-06-20 22:47:32 +00:00
|
|
|
|
2014-08-08 08:15:32 +00:00
|
|
|
"gopkg.in/v1/yaml"
|
|
|
|
"github.com/drone/drone/pkg/build/buildfile"
|
|
|
|
"github.com/drone/drone/pkg/build/repo"
|
2014-06-20 22:47:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type PublishToDrone struct {
|
2014-08-08 08:15:32 +00:00
|
|
|
Publish *Publish `yaml:"publish,omitempty"`
|
2014-06-20 22:47:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func setUpWithDrone(input string) (string, error) {
|
2014-08-08 08:15:32 +00:00
|
|
|
var buildStruct PublishToDrone
|
|
|
|
err := yaml.Unmarshal([]byte(input), &buildStruct)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
bf := buildfile.New()
|
|
|
|
buildStruct.Publish.Write(bf, &repo.Repo{Name: "name"})
|
|
|
|
return bf.String(), err
|
2014-06-20 22:47:32 +00:00
|
|
|
}
|
|
|
|
|
2014-08-07 22:50:51 +00:00
|
|
|
// Private Registry Test (no auth)
|
|
|
|
var privateRegistryNoAuthYaml = `
|
|
|
|
publish:
|
|
|
|
docker:
|
|
|
|
dockerfile: file_path
|
|
|
|
docker_server: server
|
|
|
|
docker_port: 1000
|
|
|
|
docker_version: 1.0
|
|
|
|
registry_login: false
|
2014-08-09 01:16:40 +00:00
|
|
|
image_name: registry/image
|
2014-08-07 22:50:51 +00:00
|
|
|
`
|
|
|
|
func TestPrivateRegistryNoAuth(t *testing.T) {
|
|
|
|
response, err := setUpWithDrone(privateRegistryNoAuthYaml)
|
2014-08-08 08:15:32 +00:00
|
|
|
t.Log(privateRegistryNoAuthYaml)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Can't unmarshal script: %s\n\n", err.Error())
|
|
|
|
}
|
|
|
|
if !strings.Contains(response, "docker -H server:1000 build -t registry/image:$(git rev-parse --short HEAD)") {
|
|
|
|
t.Fatalf("Response: " + response + " doesn't contain registry in image-names: expected registry/image\n\n")
|
|
|
|
}
|
2014-08-07 22:50:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Private Registry Test (with auth)
|
|
|
|
var privateRegistryAuthYaml = `
|
|
|
|
publish:
|
|
|
|
docker:
|
|
|
|
dockerfile: file_path
|
|
|
|
docker_server: server
|
|
|
|
docker_port: 1000
|
|
|
|
docker_version: 1.0
|
2014-08-09 01:16:40 +00:00
|
|
|
registry_login_url: https://registry:8000/v1/
|
2014-08-07 22:50:51 +00:00
|
|
|
registry_login: true
|
|
|
|
username: username
|
|
|
|
password: password
|
|
|
|
email: email@example.com
|
2014-08-09 01:16:40 +00:00
|
|
|
image_name: registry/image
|
2014-08-07 22:50:51 +00:00
|
|
|
`
|
|
|
|
func TestPrivateRegistryAuth(t *testing.T) {
|
|
|
|
response, err := setUpWithDrone(privateRegistryAuthYaml)
|
2014-08-08 08:15:32 +00:00
|
|
|
t.Log(privateRegistryAuthYaml)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Can't unmarshal script: %s\n\n", err.Error())
|
|
|
|
}
|
|
|
|
if !strings.Contains(response, "docker -H server:1000 login -u username -p password -e email@example.com https://registry:8000/v1/") {
|
|
|
|
t.Log("\n\n\n\ndocker -H server:1000 login -u username -p xxxxxxxx -e email@example.com https://registry:8000/v1/\n\n\n\n")
|
2014-08-07 22:50:51 +00:00
|
|
|
t.Fatalf("Response: " + response + " doesn't contain private registry login\n\n")
|
|
|
|
}
|
2014-08-08 08:15:32 +00:00
|
|
|
if !strings.Contains(response, "docker -H server:1000 build -t registry/image:$(git rev-parse --short HEAD) .") {
|
|
|
|
t.Log("docker -H server:1000 build -t registry/image:$(git rev-parse --short HEAD) .")
|
|
|
|
t.Fatalf("Response: " + response + " doesn't contain registry in image-names\n\n")
|
|
|
|
}
|
2014-08-07 22:50:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Override "latest" Test
|
|
|
|
var overrideLatestTagYaml = `
|
|
|
|
publish:
|
|
|
|
docker:
|
|
|
|
docker_server: server
|
|
|
|
docker_port: 1000
|
|
|
|
docker_version: 1.0
|
|
|
|
username: username
|
|
|
|
password: password
|
|
|
|
email: email@example.com
|
2014-08-09 01:16:40 +00:00
|
|
|
image_name: username/image
|
2014-08-07 22:50:51 +00:00
|
|
|
push_latest: true
|
|
|
|
`
|
|
|
|
func TestOverrideLatestTag(t *testing.T) {
|
|
|
|
response, err := setUpWithDrone(overrideLatestTagYaml)
|
2014-08-08 08:15:32 +00:00
|
|
|
t.Log(overrideLatestTagYaml)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Can't unmarshal script: %s\n\n", err.Error())
|
|
|
|
}
|
|
|
|
if !strings.Contains(response, "docker -H server:1000 build -t username/image:$(git rev-parse --short HEAD) .") {
|
|
|
|
t.Fatalf("Response: " + response + " doesn't contain the git-ref tagged image\n\n")
|
|
|
|
}
|
|
|
|
if !strings.Contains(response, "docker -H server:1000 tag username/image:$(git rev-parse --short HEAD) username/image:latest") {
|
|
|
|
t.Fatalf("Response: " + response + " doesn't contain 'latest' tag command\n\n")
|
|
|
|
}
|
2014-08-07 22:50:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Keep builds Test
|
|
|
|
var keepBuildsYaml = `
|
|
|
|
publish:
|
|
|
|
docker:
|
|
|
|
docker_server: server
|
|
|
|
docker_port: 1000
|
|
|
|
docker_version: 1.0
|
|
|
|
keep_build: true
|
|
|
|
username: username
|
|
|
|
password: password
|
|
|
|
email: email@example.com
|
|
|
|
image_name: image
|
|
|
|
`
|
|
|
|
func TestKeepBuilds(t *testing.T) {
|
|
|
|
response, err := setUpWithDrone(keepBuildsYaml)
|
2014-08-08 08:15:32 +00:00
|
|
|
t.Log(keepBuildsYaml)
|
2014-08-07 22:50:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Can't unmarshal script: %s\n\n", err.Error())
|
|
|
|
}
|
|
|
|
if strings.Contains(response, "docker -H server:1000 rmi") {
|
|
|
|
t.Fatalf("Response: " + response + " incorrectly instructs the docker server to remove the builds when it shouldn't\n\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Custom Tag test
|
|
|
|
var customTagYaml = `
|
|
|
|
publish:
|
|
|
|
docker:
|
|
|
|
docker_server: server
|
|
|
|
docker_port: 1000
|
|
|
|
docker_version: 1.0
|
|
|
|
custom_tag: release-0.1
|
|
|
|
username: username
|
|
|
|
password: password
|
|
|
|
email: email@example.com
|
2014-08-09 01:16:40 +00:00
|
|
|
image_name: username/image
|
2014-08-07 22:50:51 +00:00
|
|
|
`
|
|
|
|
func TestCustomTag(t *testing.T) {
|
|
|
|
response, err := setUpWithDrone(customTagYaml)
|
2014-08-08 08:15:32 +00:00
|
|
|
t.Log(customTagYaml)
|
|
|
|
if err != nil {
|
2014-08-07 22:50:51 +00:00
|
|
|
t.Fatalf("Can't unmarshal script: %s\n", err.Error())
|
|
|
|
}
|
|
|
|
if strings.Contains(response, "$(git rev-parse --short HEAD)") {
|
|
|
|
t.Fatalf("Response: " + response + " is tagging images from git-refs when it should use a custom tag\n\n")
|
|
|
|
}
|
|
|
|
if !strings.Contains(response, "docker -H server:1000 build -t username/image:release-0.1") {
|
|
|
|
t.Fatalf("Response: " + response + " isn't tagging images using our custom tag\n\n")
|
|
|
|
}
|
|
|
|
if !strings.Contains(response, "docker -H server:1000 push username/image"){
|
|
|
|
t.Fatalf("Response: " + response + " doesn't push the custom tagged image\n\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-20 22:47:32 +00:00
|
|
|
var missingFieldsYaml = `
|
|
|
|
publish:
|
|
|
|
docker:
|
|
|
|
dockerfile: file
|
|
|
|
`
|
|
|
|
|
|
|
|
func TestMissingFields(t *testing.T) {
|
2014-08-08 08:15:32 +00:00
|
|
|
response, err := setUpWithDrone(missingFieldsYaml)
|
|
|
|
t.Log(missingFieldsYaml)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Can't unmarshal script: %s\n\n", err.Error())
|
|
|
|
}
|
|
|
|
if !strings.Contains(response, "Missing argument(s)") {
|
|
|
|
t.Fatalf("Response: " + response + " didn't contain missing arguments warning\n\n")
|
|
|
|
}
|
2014-06-20 22:47:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var validYaml = `
|
|
|
|
publish:
|
|
|
|
docker:
|
|
|
|
docker_file: file_path
|
|
|
|
docker_server: server
|
|
|
|
docker_port: 1000
|
|
|
|
docker_version: 1.0
|
|
|
|
username: user
|
|
|
|
password: password
|
|
|
|
email: email
|
2014-08-09 01:16:40 +00:00
|
|
|
image_name: user/image
|
2014-08-07 22:50:51 +00:00
|
|
|
push_latest: true
|
2014-08-09 01:16:40 +00:00
|
|
|
registry_login: true
|
2014-06-20 22:47:32 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
func TestValidYaml(t *testing.T) {
|
2014-08-08 08:15:32 +00:00
|
|
|
response, err := setUpWithDrone(validYaml)
|
|
|
|
t.Log(validYaml)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Can't unmarshal script: %s\n\n", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(response, "docker -H server:1000 tag user/image:$(git rev-parse --short HEAD) user/image:latest") {
|
|
|
|
t.Fatalf("Response: " + response + " doesn't contain tag command for latest\n\n")
|
|
|
|
}
|
|
|
|
if !strings.Contains(response, "docker -H server:1000 build -t user/image:$(git rev-parse --short HEAD) - <") {
|
|
|
|
t.Fatalf("Response: " + response + "doesn't contain build command for commit hash\n\n")
|
|
|
|
}
|
|
|
|
if !strings.Contains(response, "docker -H server:1000 login -u user -p password -e email") {
|
|
|
|
t.Fatalf("Response: " + response + " doesn't contain login command\n\n")
|
|
|
|
}
|
|
|
|
if !strings.Contains(response, "docker -H server:1000 push user/image") {
|
|
|
|
t.Fatalf("Response: " + response + " doesn't contain push command\n\n")
|
|
|
|
}
|
|
|
|
if !strings.Contains(response, "docker -H server:1000 rmi user/image:" +
|
|
|
|
"$(git rev-parse --short HEAD)") {
|
|
|
|
t.Fatalf("Response: " + response + " doesn't contain remove image command\n\n")
|
|
|
|
}
|
2014-06-20 22:47:32 +00:00
|
|
|
}
|
2014-06-27 22:22:45 +00:00
|
|
|
|
|
|
|
var withoutDockerFileYaml = `
|
|
|
|
publish:
|
|
|
|
docker:
|
|
|
|
docker_server: server
|
|
|
|
docker_port: 1000
|
|
|
|
docker_version: 1.0
|
2014-08-09 01:16:40 +00:00
|
|
|
image_name: user/image
|
2014-06-27 22:22:45 +00:00
|
|
|
username: user
|
|
|
|
password: password
|
|
|
|
email: email
|
|
|
|
`
|
|
|
|
|
|
|
|
func TestWithoutDockerFile(t *testing.T) {
|
|
|
|
response, err := setUpWithDrone(withoutDockerFileYaml)
|
2014-08-08 08:15:32 +00:00
|
|
|
t.Log(withoutDockerFileYaml)
|
2014-06-27 22:22:45 +00:00
|
|
|
if err != nil {
|
2014-08-07 22:50:51 +00:00
|
|
|
t.Fatalf("Can't unmarshal script: %s\n\n", err.Error())
|
2014-06-27 22:22:45 +00:00
|
|
|
}
|
|
|
|
|
2014-08-07 22:50:51 +00:00
|
|
|
if !strings.Contains(response, "docker -H server:1000 build -t user/image:$(git rev-parse --short HEAD) .") {
|
|
|
|
t.Fatalf("Response: " + response + " doesn't contain build command\n\n")
|
2014-06-27 22:22:45 +00:00
|
|
|
}
|
|
|
|
}
|