Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add short git sha #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions pkg/deployments/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package deployments
import (
"fmt"
"path"
"strings"

"github.com/Yelp/paasta-tools-go/pkg/config"
)
Expand Down Expand Up @@ -117,6 +118,30 @@ func (provider *DefaultImageProvider) getImageForDeployGroup(deploymentGroup str
return deployment.DockerImage, nil
}

func GetPaastaGitShaFromDockerURL(dockerUrl string) (string, error) {
image := strings.Split(dockerUrl, "/")
if len(image) != 2 {
return "", fmt.Errorf(
"Failed to extract paasta git sha from url: %s",
dockerUrl,
)
}
gitShaStrings := strings.Split(image[1], "-")
if len(gitShaStrings) != 2 {
return "", fmt.Errorf(
"Failed to extract paasta git sha from url: %s",
dockerUrl,
)
}
if len(gitShaStrings[1]) < 8 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why 8? i think 7 was long time default for git.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but actually am I off by one anyway? shouldn't it be 9?

return "", fmt.Errorf(
"%s doesn't look like a git sha, not long enough",
gitShaStrings[1],
)
}
return "git" + gitShaStrings[1][:8], nil
}

// DeploymentAnnotations returns a map of annotations for the relevant service
// deployment group
func DeploymentAnnotations(
Expand Down
74 changes: 58 additions & 16 deletions pkg/deployments/deployments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,6 @@ func (fakereader FakeRegistryReader) Read(content interface{}) error {
return nil
}

type StaticImageProvider struct {
DockerRegistry string
Image string
}

func NewStaticImageProvider(dockerRegistry, image string) *StaticImageProvider {
return &StaticImageProvider{
DockerRegistry: dockerRegistry,
Image: image,
}
}

func (provider StaticImageProvider) DockerImageURLForService(serviceName, deploymentGroup string) (string, error) {
return fmt.Sprintf("%s/%s", provider.DockerRegistry, provider.Image), nil
}

func TestDefaultProviderGetDeployment(test *testing.T) {
fakeDeployments := Deployments{
V2: V2DeploymentsConfig{
Expand Down Expand Up @@ -102,6 +86,64 @@ func TestMakeControlGroup(test *testing.T) {
}
}

func TestDockerImageURLForDeployGroup(test *testing.T) {
fakeDeployments := Deployments{
V2: V2DeploymentsConfig{
Deployments: map[string]V2DeploymentGroup{
"dev.every": V2DeploymentGroup{
DockerImage: "busybox:latest",
GitSHA: "03d6f783c99695af0e716588abb9ba83ac957be2",
},
"test.every": V2DeploymentGroup{
DockerImage: "ubuntu:latest",
GitSHA: "f3d6f783c99695af0e716588abb9ba83ac957be3",
},
},
},
}
registry := DockerRegistry{
Registry: "fakeregistry.yelp.com",
}
imageReader := &FakeDeploymentsReader{data: fakeDeployments}
registryReader := &FakeRegistryReader{registry: registry}
imageProvider := DefaultImageProvider{
RegistryURLReader: registryReader,
ImageReader: imageReader,
}
actual, err := imageProvider.DockerImageURLForDeployGroup("dev.every")
if err != nil {
test.Errorf("Failed to imageProvider.DockerImageURLForDeployGroup")
}
expected := "fakeregistry.yelp.com/busybox:latest"
if actual != expected {
test.Errorf("Expected '%+v', got '%+v'", expected, actual)
}
}

func TestGetPaastaGitShaFromDockerURL(test *testing.T) {
expected := "gitabc12345"
actual, err := GetPaastaGitShaFromDockerURL("docker.thing/paasta-abc12345abcabcabacbacbacb")
if err != nil {
test.Errorf("Failed to GetPaastaGitShaFromDockerURL: %s", err)
}
if actual != expected {
test.Errorf("Expected '%+v', got '%+v'", expected, actual)
}

actual, err = GetPaastaGitShaFromDockerURL("docker.thing")
if err == nil {
test.Errorf("Expected failure for invalid docker URL")
}
actual, err = GetPaastaGitShaFromDockerURL("docker.thing/abc12345abcabcabacbacbacb")
if err == nil {
test.Errorf("Expected failure for invalid docker URL")
}
actual, err = GetPaastaGitShaFromDockerURL("docker.thing/paasta-abc12")
if err == nil {
test.Errorf("Expected failure for invalid docker URL")
}
}

func TestDeploymentAnnotationsForControlGroup(test *testing.T) {
fakeDeployments := &Deployments{
V2: V2DeploymentsConfig{
Expand Down