Skip to content

Commit

Permalink
add test cases
Browse files Browse the repository at this point in the history
Signed-off-by: Ce Gao <ce.gao@outlook.com>
  • Loading branch information
gaocegege committed Nov 16, 2016
1 parent 19ca183 commit c0464e6
Show file tree
Hide file tree
Showing 15 changed files with 381 additions and 81 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
- ./tests/run-e2e.sh
- overalls -project=github.com/caicloud/cyclone -covermode=count -debug -ignore=Godeps,vendor,tests,.git,scripts,node_modules,docs,api

after_success:
- $HOME/gopath/bin/goveralls -service=travis-ci -coverprofile=overalls.coverprofile
8 changes: 6 additions & 2 deletions api/rest/worker_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ import (

"github.com/caicloud/cyclone/api"
"github.com/caicloud/cyclone/pkg/log"
"github.com/caicloud/cyclone/pkg/osutil"
"github.com/caicloud/cyclone/store"
"github.com/emicklei/go-restful"
docker_client "github.com/fsouza/go-dockerclient"
)

const WORKER_NODE_DOCKER_VERSION = "1.10.1"
// WORKER_NODE_DOCKER_VERSION defines the required docker version in worker node.
const WORKER_NODE_DOCKER_VERSION = "WORKER_NODE_DOCKER_VERSION"

// createSystemWorkerNode creates a system worker node.
//
Expand Down Expand Up @@ -108,6 +110,8 @@ func createSystemWorkerNode(request *restful.Request, response *restful.Response

// IsValidDockerHost checks if the docker host is valid.
func IsValidDockerHost(dockerHost string) (bool, error) {
requiredDockerVersion := osutil.GetStringEnv(WORKER_NODE_DOCKER_VERSION, "1.10.1")

client, err := docker_client.NewClient(dockerHost)
if err != nil {
return false, err
Expand All @@ -127,7 +131,7 @@ func IsValidDockerHost(dockerHost string) (bool, error) {
for _, env := range *envs {
if strings.HasPrefix(env, "Version") {
log.Infof("The docker version of adding worker node is %s", strings.Split(env, "=")[1])
if strings.Split(env, "=")[1] == WORKER_NODE_DOCKER_VERSION {
if strings.Split(env, "=")[1] == requiredDockerVersion {
bVaildDockerVersion = true
}
break
Expand Down
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
84 changes: 42 additions & 42 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,51 @@ 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
return dm.Client
}

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

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

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

// GetAuthConfig gets the auth config of docker manager.
func (dm *Manager) GetAuthConfig() *AuthConfig {
return dm.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 +143,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 +160,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 +185,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 Down Expand Up @@ -234,12 +234,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 +257,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 +292,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 +318,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 +351,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 +386,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 +398,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
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 c0464e6

Please sign in to comment.