Skip to content

Commit

Permalink
add etcdv3 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CMGS committed Nov 16, 2018
1 parent e19a63b commit 4c9d79c
Show file tree
Hide file tree
Showing 17 changed files with 1,030 additions and 304 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@ mock: deps
mockery -dir vendor/github.com/docker/docker/client -name APIClient

cloc:
cloc --exclude-dir=vendor,3rdmocks --not-match-f=test .
cloc --exclude-dir=vendor,3rdmocks,mocks --not-match-f=test .

unit-test:
go test --race -cover ./utils/... ./types/... ./store/etcdv3/... ./scheduler/complex/...
9 changes: 5 additions & 4 deletions source/mocks/Source.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

93 changes: 93 additions & 0 deletions store/etcdv3/container_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package etcdv3

import (
"context"
"encoding/json"
"fmt"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"

"github.com/projecteru2/core/types"
)

func TestContainer(t *testing.T) {
etcd := InitCluster(t)
defer AfterTest(t, etcd)
m := NewMercury(t, etcd.RandClient())
ctx := context.Background()
ID := "1234567812345678123456781234567812345678123456781234567812345678"
name := "test_app_1"
appname := "test"
entrypoint := "app"
nodename := "n1"
podname := "test"
container := &types.Container{
Name: name,
ID: ID,
Nodename: nodename,
Podname: podname,
}
node := &types.Node{
Name: nodename,
Podname: podname,
Endpoint: "tcp://127.0.0.1:2376",
}
bytes, err := json.Marshal(container)
assert.NoError(t, err)
nodeBytes, err := json.Marshal(node)
assert.NoError(t, err)
_, err = m.AddPod(ctx, podname, "CPU", "")
assert.NoError(t, err)
_, err = m.Create(ctx, fmt.Sprintf(nodeInfoKey, podname, nodename), string(nodeBytes))
assert.NoError(t, err)
_, err = m.Create(ctx, fmt.Sprintf(nodePodKey, nodename), podname)
assert.NoError(t, err)
// Add
assert.NoError(t, m.AddContainer(ctx, container))
r, err := m.GetOne(ctx, fmt.Sprintf(containerInfoKey, container.ID))
assert.NoError(t, err)
assert.Equal(t, r.Value, bytes)
r, err = m.GetOne(ctx, fmt.Sprintf(nodeContainersKey, container.Nodename, container.ID))
assert.NoError(t, err)
assert.Equal(t, r.Value, bytes)
r, err = m.GetOne(ctx, filepath.Join(containerDeployPrefix, appname, entrypoint, container.Nodename, container.ID))
assert.NoError(t, err)
assert.Equal(t, string(r.Value), "")
// RemoveFail
container.ID = "a"
assert.Error(t, m.RemoveContainer(ctx, container))
// GetFail
_, err = m.GetContainer(ctx, "a")
assert.Error(t, err)
// GetContainers
container.ID = ID
containers, err := m.GetContainers(ctx, []string{ID})
assert.NoError(t, err)
assert.Equal(t, len(containers), 1)
assert.Equal(t, containers[0].Name, name)
// Deployed
assert.NoError(t, m.ContainerDeployed(ctx, ID, appname, entrypoint, nodename, ""))
assert.Error(t, m.ContainerDeployed(ctx, ID, appname, entrypoint, "n2", ""))
// ListContainers
containers, err = m.ListContainers(ctx, appname, entrypoint, "")
assert.Equal(t, len(containers), 1)
assert.Equal(t, containers[0].Name, name)
// ListNodeContainers
containers, err = m.ListNodeContainers(ctx, nodename)
assert.Equal(t, len(containers), 1)
assert.Equal(t, containers[0].Name, name)
containers, err = m.ListNodeContainers(ctx, "n2")
assert.Equal(t, len(containers), 0)
// WatchDeployStatus
ctx2, cancel := context.WithCancel(ctx)
defer cancel()
ch := m.WatchDeployStatus(ctx2, appname, entrypoint, "")
go func() {
for s := range ch {
assert.Equal(t, s.Appname, appname)
}
}()
assert.NoError(t, m.RemoveContainer(ctx, container))
}
42 changes: 42 additions & 0 deletions store/etcdv3/deploy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package etcdv3

import (
"context"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"

"github.com/projecteru2/core/types"
)

func TestDeploy(t *testing.T) {
etcd := InitCluster(t)
defer AfterTest(t, etcd)
m := NewMercury(t, etcd.RandClient())
ctx := context.Background()
opts := &types.DeployOptions{
Name: "app",
Entrypoint: &types.Entrypoint{Name: "entry"},
ProcessIdent: "abc",
}
nodeInfo := types.NodeInfo{Name: "node"}

// no container deployed
nodesInfo, err := m.MakeDeployStatus(ctx, opts, []types.NodeInfo{nodeInfo})
assert.NoError(t, err)
assert.Equal(t, len(nodesInfo), 1)
assert.Equal(t, nodesInfo[0].Name, nodeInfo.Name)
// have containers
key := filepath.Join(containerDeployPrefix, opts.Name, opts.Entrypoint.Name, "node", "id1")
_, err = m.Put(ctx, key, "")
assert.NoError(t, err)
key = filepath.Join(containerDeployPrefix, opts.Name, opts.Entrypoint.Name, "node", "id2")
_, err = m.Put(ctx, key, "")
assert.NoError(t, err)
nodesInfo, err = m.MakeDeployStatus(ctx, opts, []types.NodeInfo{nodeInfo})
assert.NoError(t, err)
assert.Equal(t, len(nodesInfo), 1)
assert.Equal(t, nodesInfo[0].Name, nodeInfo.Name)
assert.Equal(t, nodesInfo[0].Count, 2)
}
4 changes: 2 additions & 2 deletions store/etcdv3/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ func dumpFromString(ca, cert, key *os.File, caStr, certStr, keyStr string) error
return nil
}

func makeRawClient(endpoint, apiversion string) (*engineapi.Client, error) {
func makeRawClient(endpoint, apiversion string) (engineapi.APIClient, error) {
return engineapi.NewClient(endpoint, apiversion, nil, nil)
}

// use endpoint, cert files path, and api version to create docker client
// we don't check whether this is connectable
func makeRawClientWithTLS(ca, cert, key *os.File, endpoint, apiversion string) (*engineapi.Client, error) {
func makeRawClientWithTLS(ca, cert, key *os.File, endpoint, apiversion string) (engineapi.APIClient, error) {
var cli *http.Client
options := tlsconfig.Options{
CAFile: ca.Name(),
Expand Down
70 changes: 70 additions & 0 deletions store/etcdv3/helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package etcdv3

import (
"bytes"
"io/ioutil"
"os"
"testing"

"github.com/projecteru2/core/types"
"github.com/projecteru2/core/utils"
"github.com/stretchr/testify/assert"
)

func TestDumpCerts(t *testing.T) {
buff := bytes.NewBuffer([]byte(""))
rc := ioutil.NopCloser(buff)
fname1, err := utils.TempFile(rc)
assert.NoError(t, err)
fname2, err := utils.TempFile(rc)
assert.NoError(t, err)
fname3, err := utils.TempFile(rc)
assert.NoError(t, err)

f1, err := os.Create(fname1)
assert.NoError(t, err)
f2, err := os.Create(fname2)
assert.NoError(t, err)
f3, err := os.Create(fname3)
assert.NoError(t, err)

err = dumpFromString(f1, f2, f3, "a", "b", "c")
assert.NoError(t, err)

d1, err := ioutil.ReadFile(fname1)
assert.NoError(t, err)
assert.Equal(t, string(d1), "a")
d2, err := ioutil.ReadFile(fname2)
assert.NoError(t, err)
assert.Equal(t, string(d2), "b")
d3, err := ioutil.ReadFile(fname3)
assert.NoError(t, err)
assert.Equal(t, string(d3), "c")

os.Remove(fname1)
os.Remove(fname2)
os.Remove(fname3)
}

func TestParseStatusKey(t *testing.T) {
key := "/deploy/appname/entry/node/id"
p1, p2, p3, p4 := parseStatusKey(key)
assert.Equal(t, p1, "appname")
assert.Equal(t, p2, "entry")
assert.Equal(t, p3, "node")
assert.Equal(t, p4, "id")
}

func TestSetCount(t *testing.T) {
nodesCount := map[string]int{
"n1": 1,
"n2": 2,
}
nodesInfo := []types.NodeInfo{
types.NodeInfo{Name: "n1"},
types.NodeInfo{Name: "n2"},
}
nodesInfo = setCount(nodesCount, nodesInfo)
assert.Equal(t, nodesInfo[0].Count, 1)
assert.Equal(t, nodesInfo[1].Count, 2)
}
2 changes: 1 addition & 1 deletion store/etcdv3/mercury.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,4 @@ func (m *Mercury) parseKey(key string) string {
return key
}

var _cache = &utils.Cache{Clients: make(map[string]*engineapi.Client)}
var _cache = &utils.Cache{Clients: make(map[string]engineapi.APIClient)}
3 changes: 2 additions & 1 deletion store/etcdv3/mercury_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ func NewMercury(t *testing.T, cliv3 *clientv3.Client) *Mercury {
Prefix: "/eru-test",
LockPrefix: "/eru-test-lock",
}
config.Docker.CertPath = "/tmp"
// config.Docker.CertPath = "/tmp"

return &Mercury{cliv3, config}
}

func TestMercury(t *testing.T) {
etcd := InitCluster(t)
defer AfterTest(t, etcd)
Expand Down
Loading

0 comments on commit 4c9d79c

Please sign in to comment.