Skip to content

Commit

Permalink
Add the bats testging for apps
Browse files Browse the repository at this point in the history
  • Loading branch information
hwchiu committed Aug 21, 2018
1 parent deeb7e9 commit 004290e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 19 deletions.
10 changes: 9 additions & 1 deletion src/server/handler_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ func createAppHandler(ctx *web.Context) {
}

session := sp.Mongo.NewSession()
session.C(entity.DeploymentCollectionName).EnsureIndex(mgo.Index{
Key: []string{"name"},
Unique: true,
})
session.C(entity.ServiceCollectionName).EnsureIndex(mgo.Index{
Key: []string{"name"},
Unique: true,
})
defer session.Close()

// Check whether this name has been used
Expand Down Expand Up @@ -59,7 +67,7 @@ func createAppHandler(ctx *web.Context) {
p.Service.Namespace = p.Deployment.Namespace
if err := service.CreateService(sp, &p.Service); err != nil {
if errors.IsAlreadyExists(err) {
response.Conflict(req.Request, resp.ResponseWriter, fmt.Errorf("Service Name: %s already existed", p.Service))
response.Conflict(req.Request, resp.ResponseWriter, fmt.Errorf("Service Name: %s already existed", p.Service.Name))
} else {
response.InternalServerError(req.Request, resp.ResponseWriter, err)
}
Expand Down
4 changes: 2 additions & 2 deletions tests/05-app/app.info
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"deployment": {
"name": "@DEPLOYMENTNAME@",
"name": "@APPNAME@",
"labels":{},
"envVars":{
"myip":"1.2.3.4"
Expand All @@ -23,7 +23,7 @@
"replicas":2
},
"service":{
"name": "@SERVICENAME@",
"name": "@APPNAME@",
"namespace":"default",
"type":"ClusterIP",
"selector":{},
Expand Down
4 changes: 1 addition & 3 deletions tests/05-app/init.bash
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
if [ -z "$appName" ]; then
export name=$(date | md5sum | cut -b 1-19)
export appName="test-app-$name"
export svcName="test-app-svc-$name"

rm -rf app.json
cp app.info app.json
sed -i "s/@DEPLOYMENTNAME@/${appName}/" app.json
sed -i "s/@SERVICENAME@/${svcName}/" app.json
sed -i "s/@APPNAME@/${appName}/" app.json
fi
40 changes: 27 additions & 13 deletions tests/05-app/script.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,53 @@

load init

@test "Create Deployment" {
@test "Create App" {
http -v --check-status 127.0.0.1:7890/v1/apps < app.json
[ $? = 0 ]
#Wait the Deployment
#jsonpath="{.status.phase}"
NEXT_WAIT_TIME=0
WAIT_LIMIT=40
until kubectl get apps ${deploymentName} -o jsonpath="{.status.readyReplicas}" | grep "2" || [ $NEXT_WAIT_TIME -eq $WAIT_LIMIT ]; do
until kubectl get deployment ${appName} -o jsonpath="{.status.readyReplicas}" | grep "2" || [ $NEXT_WAIT_TIME -eq $WAIT_LIMIT ]; do
sleep 2
kubectl get apps ${deploymentName}
kubectl get deployment ${appName}
NEXT_WAIT_TIME=$((NEXT_WAIT_TIME+ 1))
done
[ $NEXT_WAIT_TIME != $WAIT_LIMIT ]
}

@test "List Service" {
run bash -c "http http://127.0.0.1:7890/v1/services 2>/dev/null | jq -r '.[] | select(.name == \"${appName}\").name'"
[ "$output" = "${appName}" ]
[ $status = 0 ]
}

@test "List Deployment" {
run bash -c "http http://127.0.0.1:7890/v1/apps/ 2>/dev/null | jq -r '.[] | select(.name == \"${deploymentName}\").name'"
[ "$output" = "${deploymentName}" ]
run bash -c "http http://127.0.0.1:7890/v1/deployments 2>/dev/null | jq -r '.[] | select(.name == \"${appName}\").name'"
[ "$output" = "${appName}" ]
[ $status = 0 ]
}

@test "Check Deployment Attribute" {
run kubectl get apps ${deploymentName} -o jsonpath='{.spec.template.spec.hostNetwork}'
@test "Check Deployment Label" {
run kubectl get deployment ${appName} -o jsonpath="{.spec.template.metadata.labels.app}"
[ "$output" = "${appName}" ]
[ $status = 0 ]
[ "$output" = "true" ]
}

@test "Check Deployment Env" {
kubectl get apps ${deploymentName} -o jsonpath='{.spec.template.spec.containers[0].env}' | grep "myip"
[ $? = 0 ]
@test "Check Service Selector" {
run kubectl get svc ${appName} -o jsonpath="{.spec.selector.app}"
[ "$output" = "${appName}" ]
[ $status = 0 ]
}

@test "Delete Deployment" {
run bash -c 'http http://127.0.0.1:7890/v1/apps/ 2>/dev/null | jq -r ".[0].id"'
run http DELETE http://127.0.0.1:7890/v1/apps/${output} 2>/dev/null
run bash -c "http http://127.0.0.1:7890/v1/deployments 2>/dev/null | jq -r '.[] | select(.name == \"${appName}\").id'"
run http DELETE http://127.0.0.1:7890/v1/deployments/${output} 2>/dev/null
[ $status = 0 ]
}

@test "Delete Services" {
run bash -c "http http://127.0.0.1:7890/v1/services 2>/dev/null | jq -r '.[] | select(.name == \"${appName}\").id'"
run http DELETE http://127.0.0.1:7890/v1/services/${output} 2>/dev/null
[ $status = 0 ]
}

0 comments on commit 004290e

Please sign in to comment.