Skip to content

Commit

Permalink
Merge pull request #52 from gaocegege/add-test-cases
Browse files Browse the repository at this point in the history
add test cases
  • Loading branch information
Superxi911 committed Nov 21, 2016
2 parents 662ece4 + 41ee996 commit d64bbfe
Show file tree
Hide file tree
Showing 18 changed files with 389 additions and 118 deletions.
24 changes: 22 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,45 @@
language: go

# sudo: required

# services:
# - docker

go:
- 1.6
- 1.7

go_import_path: github.com/caicloud/cyclone

before_install:
# env:
# global:
# - DOCKER_COMPOSE_VERSION=1.7.0
# - WORKER_NODE_DOCKER_VERSION=1.12.0

before_install:
# - sudo rm -f /usr/local/bin/docker-compose
# - curl -L https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-`uname -s`-`uname -m` > docker-compose
# - chmod +x docker-compose
# - sudo mv docker-compose /usr/local/bin
# install golang deps.
- go get github.com/tools/godep
- go get github.com/mattn/goveralls
- go get github.com/go-playground/overalls

install:
# verify docker.
# - docker version
# - docker-compose version
# - docker images
- godep go build .
- cd ./worker
- godep go build ./cyclone-worker.go
- cd - > /dev/null

script:
- ./tests/run-unit-test.sh
- overalls -project=github.com/caicloud/cyclone -covermode=count -debug -ignore=vendor,tests,.git,scripts,node_modules,docs
- overalls -project=github.com/caicloud/cyclone -covermode=count -debug -ignore=Godeps,vendor,tests,.git,scripts,node_modules,docs,api
# - ./tests/run-e2e.sh

after_success:
- $HOME/gopath/bin/goveralls -service=travis-ci -coverprofile=overalls.coverprofile
4 changes: 2 additions & 2 deletions docker-compose-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ zookeeper:

kafka:
image: wurstmeister/kafka:0.10.1.0
hostname: kafkahost
hostname: kafka
links:
- zookeeper:zk
volumes:
- /data/kafka_log:/data/kafka_log
environment:
- KAFKA_ADVERTISED_HOST_NAME=kafkahost
- KAFKA_ADVERTISED_HOST_NAME=0.0.0.0
- KAFKA_ADVERTISED_PORT=9092
- KAFKA_LOG_DIRS=/data/kafka_log
ports:
Expand Down
101 changes: 38 additions & 63 deletions docker/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ import (

// Manager manages all docker operations, like build, push, etc.
type Manager struct {
client *docker_client.Client
registry string
authConfig *AuthConfig
endPoint string
Client *docker_client.Client
Registry string
AuthConfig *AuthConfig
EndPoint string
}

// NewManager creates a new docker manager.
Expand All @@ -52,10 +52,10 @@ func NewManager(endpoint string, certPath string, registry api.RegistryCompose)
}

return &Manager{
client: client,
registry: registry.RegistryLocation,
authConfig: authConfig,
endPoint: endpoint,
Client: client,
Registry: registry.RegistryLocation,
AuthConfig: authConfig,
EndPoint: endpoint,
}, nil
}

Expand All @@ -75,51 +75,26 @@ func NewManager(endpoint string, certPath string, registry api.RegistryCompose)
}

return &Manager{
client: client,
registry: registry.RegistryLocation,
authConfig: authConfig,
endPoint: endpoint,
Client: client,
Registry: registry.RegistryLocation,
AuthConfig: authConfig,
EndPoint: endpoint,
}, nil
}

// GetDockerClient returns the docker client.
func (dm *Manager) GetDockerClient() *docker_client.Client {
return dm.client
}

// GetDockerAuthConfig returns the docker auth config.
func (dm *Manager) GetDockerAuthConfig() *AuthConfig {
return dm.authConfig
}

// GetDockerRegistry returns the docker registry.
func (dm *Manager) GetDockerRegistry() string {
return dm.registry
}

// GetDockerEndPoint returns the docker endpoint.
func (dm *Manager) GetDockerEndPoint() string {
return dm.endPoint
}

// GetAuthConfig gets the auth config of docker manager.
func (dm *Manager) GetAuthConfig() *AuthConfig {
return dm.authConfig
}

// PullImage pulls an image by its name.
func (dm *Manager) PullImage(imageName string) error {
opts := docker_client.PullImageOptions{
Repository: imageName,
Registry: dm.registry,
Registry: dm.Registry,
}

authOpt := docker_client.AuthConfiguration{
Username: dm.authConfig.Username,
Password: dm.authConfig.Password,
Username: dm.AuthConfig.Username,
Password: dm.AuthConfig.Password,
}

err := dm.client.PullImage(opts, authOpt)
err := dm.Client.PullImage(opts, authOpt)
if err == nil {
log.InfoWithFields("Successfully pull docker image.", log.Fields{"image": imageName})
}
Expand All @@ -143,14 +118,14 @@ func (dm *Manager) BuildImage(event *api.Event) error {
// Use to pull cargo.caicloud.io/:username/:imagename:tag.
// TODO: we will consider more cases
authOpt := docker_client.AuthConfiguration{
Username: dm.authConfig.Username,
Password: dm.authConfig.Password,
Username: dm.AuthConfig.Username,
Password: dm.AuthConfig.Password,
}

authOpts := docker_client.AuthConfigurations{
Configs: make(map[string]docker_client.AuthConfiguration),
}
authOpts.Configs[dm.registry] = authOpt
authOpts.Configs[dm.Registry] = authOpt

opt := docker_client.BuildImageOptions{
Name: imageName,
Expand All @@ -160,7 +135,7 @@ func (dm *Manager) BuildImage(event *api.Event) error {
Memswap: -1,
OutputStream: event.Output,
}
err := dm.client.BuildImage(opt)
err := dm.Client.BuildImage(opt)
if err == nil {
log.InfoWithFields("Successfully built docker image.", log.Fields{"image": imageName})
}
Expand All @@ -185,11 +160,11 @@ func (dm *Manager) PushImage(event *api.Event) error {
}

authOpt := docker_client.AuthConfiguration{
Username: dm.authConfig.Username,
Password: dm.authConfig.Password,
Username: dm.AuthConfig.Username,
Password: dm.AuthConfig.Password,
}

err := dm.client.PushImage(opt, authOpt)
err := dm.Client.PushImage(opt, authOpt)
if err == nil {
log.InfoWithFields("Successfully pushed docker image.", log.Fields{"image": imageName})
}
Expand All @@ -214,7 +189,7 @@ func (dm *Manager) RunContainer(cco *docker_client.CreateContainerOptions) (stri
}

log.InfoWithFields("About to create the container.", log.Fields{"config": *cco})
client := dm.GetDockerClient()
client := dm.Client
container, err := client.CreateContainer(*cco)
if err != nil {
return "", err
Expand All @@ -234,12 +209,12 @@ func (dm *Manager) RunContainer(cco *docker_client.CreateContainerOptions) (stri

// StopContainer stops a container by given ID.
func (dm *Manager) StopContainer(ID string) error {
return dm.client.StopContainer(ID, 0)
return dm.Client.StopContainer(ID, 0)
}

// RemoveContainer removes a container by given ID.
func (dm *Manager) RemoveContainer(ID string) error {
return dm.client.RemoveContainer(docker_client.RemoveContainerOptions{
return dm.Client.RemoveContainer(docker_client.RemoveContainerOptions{
ID: ID,
RemoveVolumes: true,
Force: true,
Expand All @@ -257,21 +232,21 @@ func (dm *Manager) StopAndRemoveContainer(ID string) error {
// GetAuthOpts gets Auth options.
func (dm *Manager) GetAuthOpts() (authOpts docker_client.AuthConfigurations) {
authOpt := docker_client.AuthConfiguration{
Username: dm.authConfig.Username,
Password: dm.authConfig.Password,
Username: dm.AuthConfig.Username,
Password: dm.AuthConfig.Password,
}

authOpts = docker_client.AuthConfigurations{
Configs: make(map[string]docker_client.AuthConfiguration),
}
authOpts.Configs[dm.registry] = authOpt
authOpts.Configs[dm.Registry] = authOpt

return authOpts
}

// RemoveNetwork removes a network by given ID.
func (dm *Manager) RemoveNetwork(networkID string) error {
return dm.client.RemoveNetwork(networkID)
return dm.Client.RemoveNetwork(networkID)
}

// BuildImageSpecifyDockerfile builds docker image with params from event with
Expand All @@ -292,14 +267,14 @@ func (dm *Manager) BuildImageSpecifyDockerfile(event *api.Event,
// Use to pull cargo.caicloud.io/:username/:imagename:tag.
// TODO: we will consider more cases
authOpt := docker_client.AuthConfiguration{
Username: dm.authConfig.Username,
Password: dm.authConfig.Password,
Username: dm.AuthConfig.Username,
Password: dm.AuthConfig.Password,
}

authOpts := docker_client.AuthConfigurations{
Configs: make(map[string]docker_client.AuthConfiguration),
}
authOpts.Configs[dm.registry] = authOpt
authOpts.Configs[dm.Registry] = authOpt

if "" != dockerfilePath {
contextDir = contextDir + "/" + dockerfilePath
Expand All @@ -318,7 +293,7 @@ func (dm *Manager) BuildImageSpecifyDockerfile(event *api.Event,
Memswap: -1,
}
steplog.InsertStepLog(event, steplog.BuildImage, steplog.Start, nil)
err := dm.client.BuildImage(opt)
err := dm.Client.BuildImage(opt)
if err == nil {
steplog.InsertStepLog(event, steplog.BuildImage, steplog.Finish, nil)
log.InfoWithFields("Successfully built docker image.", log.Fields{"image": imageName})
Expand Down Expand Up @@ -351,7 +326,7 @@ func (dm *Manager) CleanUp(event *api.Event) error {

// RemoveImage removes an image by its name or ID.
func (dm *Manager) RemoveImage(name string) error {
return dm.client.RemoveImage(name)
return dm.Client.RemoveImage(name)
}

// parse parses the "FROM" in the repo's Dockerfile to check the images which the build images base on
Expand Down Expand Up @@ -386,7 +361,7 @@ func parse(despath string) ([]string, error) {

// IsImagePresent checks if given image exists.
func (dm *Manager) IsImagePresent(image string) (bool, error) {
_, err := dm.client.InspectImage(image)
_, err := dm.Client.InspectImage(image)
if err == nil {
return true, nil
}
Expand All @@ -398,10 +373,10 @@ func (dm *Manager) IsImagePresent(image string) (bool, error) {

// GetImageNameWithTag gets the image name with tag from registry, username, service name and version name.
func (dm *Manager) GetImageNameWithTag(username, serviceName, versionName string) string {
return fmt.Sprintf("%s/%s/%s:%s", dm.registry, strings.ToLower(username), strings.ToLower(serviceName), versionName)
return fmt.Sprintf("%s/%s/%s:%s", dm.Registry, strings.ToLower(username), strings.ToLower(serviceName), versionName)
}

// GetImageNameNoTag gets the image name without tag from registry, username, service name.
func (dm *Manager) GetImageNameNoTag(username, serviceName string) string {
return fmt.Sprintf("%s/%s/%s", dm.registry, strings.ToLower(username), strings.ToLower(serviceName))
return fmt.Sprintf("%s/%s/%s", dm.Registry, strings.ToLower(username), strings.ToLower(serviceName))
}
16 changes: 13 additions & 3 deletions pkg/pathutil/pathutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,21 @@ import (
"testing"
)

const name = "/var/cyclone/the-file-shouldn't-exist"
const (
illegalPath = "/var/cyclone/the-file-shouldn't-exist"
legalPath = "/var"
)

// TestEnsureParentDir tests the EnsureParentDir func.
func TestEnsureParentDir(t *testing.T) {
if err := EnsureParentDir(name, os.ModePerm); err == nil {
func TestEnsureParentDirWithError(t *testing.T) {
if err := EnsureParentDir(illegalPath, os.ModePerm); err == nil {
t.Error("Expected error to occur but it was nil")
}
}

// TestEnsureParentDir tests the EnsureParentDir func.
func TestEnsureParentDir(t *testing.T) {
if err := EnsureParentDir(legalPath, os.ModePerm); err != nil {
t.Error("Expected error to be nil")
}
}
1 change: 1 addition & 0 deletions scripts/local-up.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export REGISTRY_PASSWORD=${REGISTRY_PASSWORD:-""}
export WORK_REGISTRY_LOCATION=${WORK_REGISTRY_LOCATION:-"cargo.caicloud.io"}
export SUCCESSTEMPLATE=${SUCCESSTEMPLATE:-"./notify/provider/success.html"}
export ERRORTEMPLATE=${ERRORTEMPLATE:-"./notify/provider/error.html"}
export WORKER_NODE_DOCKER_VERSION=${WORKER_NODE_DOCKER_VERSION:-"1.10.1"}

# Static configs.
export REGISTRY_AUTH_LOG=${REGISTRY_AUTH_LOG}
Expand Down
10 changes: 5 additions & 5 deletions tests/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ func PushImageToLocalRegistry(dm *docker.Manager, image string) error {
return errors.New("Invalid image name.")
}
parts := strings.Split(image, ":")
return dm.GetDockerClient().PushImage(docker_client.PushImageOptions{
Name: fmt.Sprintf("%s/%s", dm.GetDockerRegistry(), parts[0]),
return dm.Client.PushImage(docker_client.PushImageOptions{
Name: fmt.Sprintf("%s/%s", dm.Registry, parts[0]),
Tag: parts[1],
Registry: dm.GetDockerRegistry(),
Registry: dm.Registry,
}, docker_client.AuthConfiguration{
Username: dm.GetDockerAuthConfig().Username,
Password: dm.GetDockerAuthConfig().Password,
Username: dm.AuthConfig.Username,
Password: dm.AuthConfig.Password,
})
}

Expand Down
2 changes: 2 additions & 0 deletions tests/run-e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ function run-local-up {
echo "Unable to find docker"
exit
fi
docker pull cargo.caicloud.io/circle/e2e-test-long-running-task
docker tag cargo.caicloud.io/circle/e2e-test-long-running-task localhost:5000/minimal-long-running-task
mkdir ${REGISTRY_DATA} ${REGISTRY_AUTH_LOG}

log "Mongo, Kafka, and the registry are all running in a docker container, cyclone running in local."
Expand Down

0 comments on commit d64bbfe

Please sign in to comment.