Skip to content

Commit

Permalink
Add unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
hwchiu committed Aug 21, 2018
1 parent 004290e commit dc2fa07
Showing 1 changed file with 133 additions and 0 deletions.
133 changes: 133 additions & 0 deletions src/server/handler_app_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package server

import (
"encoding/json"
"math/rand"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"

restful "github.com/emicklei/go-restful"
"github.com/linkernetworks/mongo"
"github.com/linkernetworks/vortex/src/config"
"github.com/linkernetworks/vortex/src/entity"
"github.com/linkernetworks/vortex/src/serviceprovider"
"github.com/moby/moby/pkg/namesgenerator"
"github.com/stretchr/testify/suite"
"gopkg.in/mgo.v2/bson"
)

func init() {
rand.Seed(time.Now().UnixNano())
}

type DeploymentTestSuite struct {
suite.Suite
sp *serviceprovider.Container
wc *restful.Container
session *mongo.Session
}

func (suite *DeploymentTestSuite) SetupSuite() {
cf := config.MustRead("../../config/testing.json")
sp := serviceprovider.NewForTesting(cf)

suite.sp = sp
//init session
suite.session = sp.Mongo.NewSession()
//init restful container
suite.wc = restful.NewContainer()
service := newAppService(suite.sp)
suite.wc.Add(service)
}

func (suite *DeploymentTestSuite) TearDownSuite() {}

func TestDeploymentSuite(t *testing.T) {
suite.Run(t, new(DeploymentTestSuite))
}

func (suite *DeploymentTestSuite) TestCreateDeployment() {
namespace := "default"
containers := []entity.Container{
{
Name: namesgenerator.GetRandomName(0),
Image: "busybox",
Command: []string{"sleep", "3600"},
},
}
tName := namesgenerator.GetRandomName(0)
deploy := entity.Deployment{
Name: tName,
Namespace: namespace,
Labels: map[string]string{},
EnvVars: map[string]string{},
Containers: containers,
Volumes: []entity.DeploymentVolume{},
Networks: []entity.DeploymentNetwork{},
Capability: true,
NetworkType: entity.DeploymentHostNetwork,
NodeAffinity: []string{},
Replicas: 1,
}

ports := []entity.ServicePort{
{
Name: namesgenerator.GetRandomName(0),
Port: int32(80),
TargetPort: 80,
NodePort: int32(30000),
},
}

serviceName := namesgenerator.GetRandomName(0)
service := entity.Service{
ID: bson.NewObjectId(),
Name: serviceName,
Namespace: "default",
Type: "NodePort",
Selector: selector,
Ports: ports,
}

app := entity.App{
Deployment: deployment,
Service: service,
}
bodyBytes, err := json.MarshalIndent(deploy, "", " ")
suite.NoError(err)

bodyReader := strings.NewReader(string(bodyBytes))
httpRequest, err := http.NewRequest("POST", "http://localhost:7890/v1/apps", bodyReader)
suite.NoError(err)

httpRequest.Header.Add("Content-Type", "application/json")
httpWriter := httptest.NewRecorder()
suite.wc.Dispatch(httpWriter, httpRequest)
assertResponseCode(suite.T(), http.StatusCreated, httpWriter)
defer suite.session.Remove(entity.DeploymentCollectionName, "name", app.Deployment.Name)
defer suite.session.Remove(entity.ServiceCollectionName, "name", app.Service.Name)

//load data to check
retDeployment := entity.Deployment{}
err = suite.session.FindOne(entity.DeploymentCollectionName, bson.M{"name": deploy.Name}, &retDeployment)
suite.NoError(err)
suite.NotEqual("", retDeployment.ID)
suite.Equal(deploy.Name, retDeployment.Name)
suite.Equal(len(deploy.Containers), len(retDeployment.Containers))

//We use the new write but empty input which will cause the readEntity Error
httpWriter = httptest.NewRecorder()
suite.wc.Dispatch(httpWriter, httpRequest)
assertResponseCode(suite.T(), http.StatusBadRequest, httpWriter)
//Create again and it should fail since the name exist
bodyReader = strings.NewReader(string(bodyBytes))
httpRequest, err = http.NewRequest("POST", "http://localhost:7890/v1/apps", bodyReader)
suite.NoError(err)
httpRequest.Header.Add("Content-Type", "application/json")
httpWriter = httptest.NewRecorder()
suite.wc.Dispatch(httpWriter, httpRequest)
assertResponseCode(suite.T(), http.StatusConflict, httpWriter)
}

0 comments on commit dc2fa07

Please sign in to comment.