Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
keimoon committed Feb 27, 2018
1 parent 75a4d3b commit c1b22d2
Show file tree
Hide file tree
Showing 14 changed files with 198 additions and 15 deletions.
14 changes: 7 additions & 7 deletions buildtree/buildtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,15 +222,15 @@ func (t *BuildTree) cyclicCheck(node *buildNode) error {
current := node
for {
if nodes.Exists(current.name) {
return stacktrace.NewError("Cyclic dependency found for %q", current.name)
return stacktrace.Propagate(ErrCyclicDependency{current.name}, "Cyclic dependency found for %q", current.name)
}
nodes.Add(current.name)
if current.depend == "" {
return nil
}
parent, ok := t.allNodes[current.depend]
if !ok {
return stacktrace.NewError("Dependency for %q not found: %q", node.name, node.depend)
return stacktrace.Propagate(ErrDependencyMissing{node.name, node.depend}, "Dependency for %q not found: %q", node.name, node.depend)
}
current = parent
}
Expand All @@ -246,11 +246,11 @@ func (t *BuildTree) assertDockerfile(node *buildNode) error {
return err
}
if !utils.CompareDockerName(node.depend, imageInfo.FullName) {
return stacktrace.NewError("Mismatch dependency for %q: %q in config but got %q in dockerfile", node.name, node.depend, imageInfo.FullName)
return stacktrace.Propagate(ErrMismatchDependencyImage{node.name, node.depend, imageInfo.FullName}, "Mismatch dependency for %q: %q in config but got %q in dockerfile", node.name, node.depend, imageInfo.FullName)
}
parentTag := t.allNodes[node.depend].tag
if parentTag != imageInfo.Tag {
return stacktrace.NewError("Mismatch dependency image tag for %q (parent is %q): %q in config but got %q in dockerfile", node.name, node.depend, parentTag, imageInfo.Tag)
return stacktrace.Propagate(ErrMismatchDependencyTag{node.name, node.depend, parentTag, imageInfo.Tag}, "Mismatch dependency image tag for %q (parent is %q): %q in config but got %q in dockerfile", node.name, node.depend, parentTag, imageInfo.Tag)
}
return nil
}
Expand All @@ -266,7 +266,7 @@ func (t *BuildTree) dirtyCheck(node *buildNode, parentIsDirty, parentIsForced bo
}
credential := t.credentials[imageInfo.RegistryName]
if credential == nil {
return stacktrace.NewError("Cannot find credential for %s", imageInfo.RegistryName)
return stacktrace.Propagate(ErrMissingCredential{imageInfo.RegistryName}, "Cannot find credential for %s", imageInfo.RegistryName)
}
tagExists, err := utils.DockerCheckTagExists(imageInfo.ShortName, node.tag, &utils.DockerCredential{
Registry: credential.Registry,
Expand All @@ -278,13 +278,13 @@ func (t *BuildTree) dirtyCheck(node *buildNode, parentIsDirty, parentIsForced bo
}
if t.isProvided(node) {
if !tagExists {
return stacktrace.NewError("Cannot find tag %q for provided image %q", node.tag, node.name)
return stacktrace.Propagate(ErrMissingTag{node.tag, node.name}, "Cannot find tag %q for provided image %q", node.tag, node.name)
}
node.dirty = false
} else if parentIsDirty {
node.dirty = true
if tagExists {
return stacktrace.NewError("Image needs to be updated but still using old tag: %s", node.name)
return stacktrace.Propagate(ErrImageTagOutdated{node.name}, "Image needs to be updated but still using old tag: %q", node.name)
}
} else {
node.dirty = !tagExists
Expand Down
69 changes: 64 additions & 5 deletions buildtree/buildtree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ import (
"sort"
"testing"

"github.com/palantir/stacktrace"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

type BuildTreeTestSuite struct {
suite.Suite
resourceFolder string
}

func (s *BuildTreeTestSuite) SetupTest() {
s.resourceFolder = "../test-resources"
}

func (s *BuildTreeTestSuite) TestReadConfigfile() {
Expand Down Expand Up @@ -74,7 +80,7 @@ func (s *BuildTreeTestSuite) TestBuildTreeHappyPath() {
s.T().Log("Skipping test happy path")
return
}
rootFolder := "../test-resources/happy-path"
rootFolder := filepath.Join(s.resourceFolder, "happy-path")
buildTree, err := ReadBuildTreeFromFile(filepath.Join(rootFolder, "doriath.yml"), map[string]string{})
require.Nil(s.T(), err, "build tree must be readable")
err = buildTree.Prepare()
Expand Down Expand Up @@ -118,7 +124,7 @@ func (s *BuildTreeTestSuite) TestBuildTreeHappyPath() {
expectedChildNode2 := &buildNodeForTestData{
buildRoot: filepath.Join(rootFolder, "child2"),
name: "library/nginx",
tag: "should-not-exists",
tag: "should-not-exist",
depend: "library/ubuntu",
children: []string{"library/redis"},
dirty: true,
Expand All @@ -127,7 +133,7 @@ func (s *BuildTreeTestSuite) TestBuildTreeHappyPath() {
expectedGrandChildNode2 := &buildNodeForTestData{
buildRoot: filepath.Join(rootFolder, "grandchild2"),
name: "library/redis",
tag: "should-not-exists",
tag: "should-not-exist",
depend: "library/nginx",
children: []string{},
dirty: true,
Expand Down Expand Up @@ -155,9 +161,62 @@ func (s *BuildTreeTestSuite) TestBuildTreeHappyPath() {
require.Equal(s.T(), expectedGrandChildNode3, s.convertNodeToTestData(buildTree.allNodes["library/mariadb"]))
}

func (s *BuildTreeTestSuite) TestCyclicCheck() {
rootFolder := filepath.Join(s.resourceFolder, "cyclic-check")
buildTree, err := ReadBuildTreeFromFile(filepath.Join(rootFolder, "doriath.yml"), map[string]string{})
require.Nil(s.T(), err, "build tree must be readable")
err = buildTree.Prepare()
_, ok := stacktrace.RootCause(err).(ErrCyclicDependency)
require.True(s.T(), ok)
}

func (s *BuildTreeTestSuite) TestMismatchImage() {
rootFolder := filepath.Join(s.resourceFolder, "mismatch-image")
buildTree, err := ReadBuildTreeFromFile(filepath.Join(rootFolder, "doriath.yml"), map[string]string{})
require.Nil(s.T(), err, "build tree must be readable")
err = buildTree.Prepare()
_, ok := stacktrace.RootCause(err).(ErrMismatchDependencyImage)
require.True(s.T(), ok)
}

func (s *BuildTreeTestSuite) TestMismatchTag() {
rootFolder := filepath.Join(s.resourceFolder, "mismatch-tag")
buildTree, err := ReadBuildTreeFromFile(filepath.Join(rootFolder, "doriath.yml"), map[string]string{})
require.Nil(s.T(), err, "build tree must be readable")
err = buildTree.Prepare()
_, ok := stacktrace.RootCause(err).(ErrMismatchDependencyTag)
require.True(s.T(), ok)
}

func (s *BuildTreeTestSuite) TestMissingProvidedImage() {
if !s.checkIntegTestEnable() {
s.T().Log("Skipping test missing provided image")
return
}
rootFolder := filepath.Join(s.resourceFolder, "missing-provided-image")
buildTree, err := ReadBuildTreeFromFile(filepath.Join(rootFolder, "doriath.yml"), map[string]string{})
require.Nil(s.T(), err, "build tree must be readable")
err = buildTree.Prepare()
_, ok := stacktrace.RootCause(err).(ErrMissingTag)
require.True(s.T(), ok)
}

func (s *BuildTreeTestSuite) TestOutdateTag() {
if !s.checkIntegTestEnable() {
s.T().Log("Skipping test outdate tag")
return
}
rootFolder := filepath.Join(s.resourceFolder, "outdate-tag")
buildTree, err := ReadBuildTreeFromFile(filepath.Join(rootFolder, "doriath.yml"), map[string]string{})
require.Nil(s.T(), err, "build tree must be readable")
err = buildTree.Prepare()
_, ok := stacktrace.RootCause(err).(ErrImageTagOutdated)
require.True(s.T(), ok)
}

func (s *BuildTreeTestSuite) checkIntegTestEnable() bool {
integTestEnv := os.Getenv("INTEG_TEST_ENABLE")
if integTestEnv == "1" || integTestEnv == "true" {
dockerhubTestEnv := os.Getenv("DOCKERHUB_TEST_ENABLE")
if dockerhubTestEnv == "1" || dockerhubTestEnv == "true" {
if os.Getenv("DOCKERHUB_USERNAME") == "" {
require.Fail(s.T(), "DOCKERHUB_USERNAME was not defined")
}
Expand Down
66 changes: 66 additions & 0 deletions buildtree/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package buildtree

import "fmt"

type ErrCyclicDependency struct {
Name string
}

func (e ErrCyclicDependency) Error() string {
return fmt.Sprintf("cyclic dependency found for %q", e.Name)
}

type ErrDependencyMissing struct {
Name string
Depend string
}

func (e ErrDependencyMissing) Error() string {
return fmt.Sprintf("dependency for %q not found: %q", e.Name, e.Depend)
}

type ErrMismatchDependencyImage struct {
Name string
Depend string
ActualFullname string
}

func (e ErrMismatchDependencyImage) Error() string {
return fmt.Sprintf("mismatch dependency for %q: %q in config but got %q in dockerfile", e.Name, e.Depend, e.ActualFullname)
}

type ErrMismatchDependencyTag struct {
Name string
Depend string
ExpectedTag string
ActualTag string
}

func (e ErrMismatchDependencyTag) Error() string {
return fmt.Sprintf("mismatch dependency image tag for %q (parent is %q): %q in config but got %q in dockerfile", e.Name, e.Depend, e.ExpectedTag, e.ActualTag)
}

type ErrMissingCredential struct {
RegistryName string
}

func (e ErrMissingCredential) Error() string {
return fmt.Sprintf("missing credential for %q", e.RegistryName)
}

type ErrMissingTag struct {
Tag string
Name string
}

func (e ErrMissingTag) Error() string {
return fmt.Sprintf("cannot find tag %q for provided image %q", e.Tag, e.Name)
}

type ErrImageTagOutdated struct {
Name string
}

func (e ErrImageTagOutdated) Error() string {
return fmt.Sprintf("image needs to be updated but still using old tag: %q", e.Name)
}
14 changes: 14 additions & 0 deletions test-resources/cyclic-check/doriath.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
root_dir: .
build:
- name: node1
tag: 1.0
from: ./node1
depend: node3
- name: node2
tag: 1.0
from: ./node2
depend: node1
- name: node3
tag: 1.0
from: ./node3
depend: node2
4 changes: 2 additions & 2 deletions test-resources/happy-path/doriath.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ build:
from: ./grandchild1
depend: alpine
- name: nginx
tag: should-not-exists
tag: should-not-exist
from: ./child2
depend: ubuntu
- name: redis
tag: should-not-exists
tag: should-not-exist
from: ./grandchild2
depend: nginx
- name: postgres
Expand Down
2 changes: 1 addition & 1 deletion test-resources/happy-path/grandchild2/Dockerfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
FROM nginx:should-not-exists
FROM nginx:should-not-exist
9 changes: 9 additions & 0 deletions test-resources/mismatch-image/doriath.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root_dir: .
build:
- name: ubuntu
tag: 16.04
from: provided
- name: node1
tag: 1.0
from: node1
depend: ubuntu
1 change: 1 addition & 0 deletions test-resources/mismatch-image/node1/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM alpine:edge
9 changes: 9 additions & 0 deletions test-resources/mismatch-tag/doriath.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root_dir: .
build:
- name: ubuntu
tag: 16.04
from: provided
- name: node1
tag: 1.0
from: ./node1
depend: ubuntu
1 change: 1 addition & 0 deletions test-resources/mismatch-tag/node1/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM ubuntu:14.04
9 changes: 9 additions & 0 deletions test-resources/missing-provided-image/doriath.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root_dir: .
build:
- name: ubuntu
tag: should-not-exist
from: provided
credentials:
- name: dockerhub
username: ${DOCKERHUB_USERNAME}
password: ${DOCKERHUB_PASSWORD}
1 change: 1 addition & 0 deletions test-resources/outdate-tag/alpine/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM ubuntu:should-not-exist
13 changes: 13 additions & 0 deletions test-resources/outdate-tag/doriath.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
root_dir: .
build:
- name: ubuntu
tag: should-not-exist
from: ./ubuntu
- name: alpine
tag: edge
from: ./alpine
depend: ubuntu
credentials:
- name: dockerhub
username: ${DOCKERHUB_USERNAME}
password: ${DOCKERHUB_PASSWORD}
1 change: 1 addition & 0 deletions test-resources/outdate-tag/ubuntu/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM debian:8

0 comments on commit c1b22d2

Please sign in to comment.