Fixing indents, adding repo_name option and additional tests

This commit is contained in:
David Dyball 2014-08-08 09:15:32 +01:00
parent 8ac2ca6f42
commit 39695c45bd
2 changed files with 126 additions and 88 deletions

View file

@ -16,7 +16,7 @@ type Docker struct {
// Connection information for the docker server that will build the image
DockerServer string `yaml:"docker_server"`
DockerServerPort int `yaml:"docker_port"`
DockerServerPort int `yaml:"docker_port"`
// The Docker client version to download. This must match the docker version on the server
DockerVersion string `yaml:"docker_version"`
@ -25,20 +25,23 @@ type Docker struct {
RegistryHost string `yaml:"registry_host"`
RegistryProtocol string `yaml:"registry_protocol"`
RegistryPort int `yaml:"registry_port"`
RegistryLogin bool `yaml:"registry_login"`
RegistryLogin bool `yaml:"registry_login"`
RegistryLoginUri string `yaml:"registry_login_uri"`
// Allow setting Repo + Image names for delivery
// NOTE: RepoName is not compatible with private Registries
RepoName string `yaml:"repo_name"`
ImageName string `yaml:"image_name"`
// Authentication credentials for index.docker.io
Username string `yaml:"username"`
Password string `yaml:"password"`
Email string `yaml:"email"`
Email string `yaml:"email"`
// Keep the build on the Docker host after pushing?
KeepBuild bool `yaml:"keep_build"`
// Do we want to override "latest" automatically with this build?
PushLatest bool `yaml:"push_latest"`
// Do we want to override "latest" automatically with this build?
PushLatest bool `yaml:"push_latest"`
Branch string `yaml:"branch,omitempty"`
Tag string `yaml:"custom_tag"`
@ -52,12 +55,21 @@ type Docker struct {
func (d *Docker) Write(f *buildfile.Buildfile, r *repo.Repo) {
if len(d.DockerServer) == 0 || d.DockerServerPort == 0 || len(d.DockerVersion) == 0 ||
len(d.ImageName) == 0 {
f.WriteCmdSilent(`echo "Docker Plugin: Missing argument(s)"`)
f.WriteCmdSilent(`echo -e "Docker Plugin: Missing argument(s)"\n\n`)
if len(d.DockerServer) == 0 { f.WriteCmdSilent(`echo -e "\tdocker_server not defined in yaml`) }
if d.DockerServerPort == 0 { f.WriteCmdSilent(`echo -e "\tdocker_port not defined in yaml`) }
if len(d.DockerVersion) == 0 { f.WriteCmdSilent(`echo -e "\tdocker_version not defined in yaml`) }
if len(d.ImageName) == 0 { f.WriteCmdSilent(`echo -e "\timage_name not defined in yaml`) }
return
}
// Ensure correct apt-get has the https method-driver as per (http://askubuntu.com/questions/165676/)
f.WriteCmd("sudo apt-get install apt-transport-https")
if len(d.RepoName) > 0 && len(d.RegistryHost) > 0 {
f.WriteCmdSilent(`echo -e "Docker Plugin: Invalid Arguments Specified\n\n cannot combine repo_name and registry_host\n\t(It's not possible to host sub-repo's on private registries)\n"`)
return
}
// Ensure correct apt-get has the https method-driver as per (http://askubuntu.com/questions/165676/)
f.WriteCmd("sudo apt-get install apt-transport-https")
// Install Docker on the container
f.WriteCmd("sudo sh -c \"echo deb https://get.docker.io/ubuntu docker main\\ > " +
@ -72,12 +84,16 @@ func (d *Docker) Write(f *buildfile.Buildfile, r *repo.Repo) {
// Construct Image BaseName
// e.g. "docker.mycompany.com/myimage" for private registries
// "myuser/myimage" for index.docker.io
// "myuser/myimage" for index.docker.io
imageBaseName := ""
if len(d.RegistryHost) > 0 {
imageBaseName = fmt.Sprintf("%s/%s",d.RegistryHost,d.ImageName)
} else {
imageBaseName = fmt.Sprintf("%s/%s",d.Username,d.ImageName)
if len(d.RepoName) > 0 {
imageBaseName = fmt.Sprintf("%s/%s",d.RepoName,d.ImageName)
} else {
imageBaseName = fmt.Sprintf("%s/%s",d.Username,d.ImageName)
}
}
registryLoginEndpoint := ""
@ -102,9 +118,6 @@ func (d *Docker) Write(f *buildfile.Buildfile, r *repo.Repo) {
}
}
//splitRepoName := strings.Split(r.Name, "/")
//dockerRepo := d.ImageName + "/" + splitRepoName[len(splitRepoName)-1]
dockerPath := "."
if len(d.Dockerfile) != 0 {
dockerPath = fmt.Sprintf("- < %s", d.Dockerfile)
@ -131,11 +144,11 @@ func (d *Docker) Write(f *buildfile.Buildfile, r *repo.Repo) {
dockerServerUrl, d.Username, d.Password, d.Email))
}
// Are we overriding the "latest" tag?
if d.PushLatest {
// Are we overriding the "latest" tag?
if d.PushLatest {
f.WriteCmd(fmt.Sprintf("docker -H %s tag %s:%s %s:latest",
dockerServerUrl, imageBaseName, imageTag, imageBaseName))
}
}
f.WriteCmd(fmt.Sprintf("docker -H %s push %s", dockerServerUrl, imageBaseName))

View file

@ -1,29 +1,52 @@
package publish
import (
"strings"
"testing"
"strings"
"testing"
"gopkg.in/v1/yaml"
"github.com/drone/drone/pkg/build/buildfile"
"github.com/drone/drone/pkg/build/repo"
"gopkg.in/v1/yaml"
"github.com/drone/drone/pkg/build/buildfile"
"github.com/drone/drone/pkg/build/repo"
)
type PublishToDrone struct {
Publish *Publish `yaml:"publish,omitempty"`
Publish *Publish `yaml:"publish,omitempty"`
}
func setUpWithDrone(input string) (string, error) {
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
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
}
// Private Registry + RepoName (invalid config)
var privateRegistryRepoNameYaml = `
publish:
docker:
docker_server: server
docker_port: 1000
docker_version: 1.0
registry_host: server
registry_login: false
repo_name: company
image_name: image
`
func TestPrivateRegistryRepoName(t *testing.T) {
response, err := setUpWithDrone(privateRegistryRepoNameYaml)
t.Log(privateRegistryRepoNameYaml)
if err != nil {
t.Fatalf("Can't unmarshal script: %s\n\n", err.Error())
}
if !strings.Contains(response, "Docker Plugin: Invalid Arguments Specified") {
t.Fatalf("registry_host + repo_name should produce an invalid config error, it didn't")
}
}
// Private Registry Test (no auth)
var privateRegistryNoAuthYaml = `
@ -39,12 +62,13 @@ publish:
`
func TestPrivateRegistryNoAuth(t *testing.T) {
response, err := setUpWithDrone(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")
}
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")
}
}
// Private Registry Test (with auth)
@ -66,18 +90,18 @@ publish:
`
func TestPrivateRegistryAuth(t *testing.T) {
response, err := setUpWithDrone(privateRegistryAuthYaml)
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")
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")
t.Fatalf("Response: " + response + " doesn't contain private registry login\n\n")
}
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")
}
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")
}
}
// Override "latest" Test
@ -95,16 +119,16 @@ publish:
`
func TestOverrideLatestTag(t *testing.T) {
response, err := setUpWithDrone(overrideLatestTagYaml)
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")
}
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")
}
}
// Keep builds Test
@ -122,7 +146,7 @@ publish:
`
func TestKeepBuilds(t *testing.T) {
response, err := setUpWithDrone(keepBuildsYaml)
t.Log(keepBuildsYaml)
t.Log(keepBuildsYaml)
if err != nil {
t.Fatalf("Can't unmarshal script: %s\n\n", err.Error())
}
@ -146,8 +170,8 @@ publish:
`
func TestCustomTag(t *testing.T) {
response, err := setUpWithDrone(customTagYaml)
t.Log(customTagYaml)
if err != nil {
t.Log(customTagYaml)
if err != nil {
t.Fatalf("Can't unmarshal script: %s\n", err.Error())
}
if strings.Contains(response, "$(git rev-parse --short HEAD)") {
@ -168,14 +192,14 @@ publish:
`
func TestMissingFields(t *testing.T) {
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, "echo \"Docker Plugin: Missing argument(s)\"") {
t.Fatalf("Response: " + response + " didn't contain missing arguments warning\n\n")
}
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")
}
}
var validYaml = `
@ -194,28 +218,28 @@ publish:
`
func TestValidYaml(t *testing.T) {
response, err := setUpWithDrone(validYaml)
t.Log(validYaml)
if err != nil {
t.Fatalf("Can't unmarshal script: %s\n\n", err.Error())
}
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")
}
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")
}
}
var withoutDockerFileYaml = `
@ -232,6 +256,7 @@ publish:
func TestWithoutDockerFile(t *testing.T) {
response, err := setUpWithDrone(withoutDockerFileYaml)
t.Log(withoutDockerFileYaml)
if err != nil {
t.Fatalf("Can't unmarshal script: %s\n\n", err.Error())
}