Skip to content

Commit

Permalink
backup return backupfile's path && fix
Browse files Browse the repository at this point in the history
fix
  • Loading branch information
wrfly committed Aug 7, 2017
1 parent 6aac61f commit 62ae115
Show file tree
Hide file tree
Showing 14 changed files with 167 additions and 176 deletions.
2 changes: 2 additions & 0 deletions cluster/calcium/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func (c *calcium) Backup(id, srcPath string) (*types.BackupMessage, error) {
file, err := os.Create(backupFile)
if err != nil {
log.Errorf("Error during create backup file %s: %v", backupFile, err)
return nil, err
}
defer file.Close()

Expand All @@ -73,5 +74,6 @@ func (c *calcium) Backup(id, srcPath string) (*types.BackupMessage, error) {
return &types.BackupMessage{
Status: "ok",
Size: stat.Size,
Path: backupFile,
}, nil
}
21 changes: 11 additions & 10 deletions cluster/calcium/backup_test.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
package calcium

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestBackup(t *testing.T) {
initMockConfig()

srcPath := "/tmp"
_, err = mockc.Backup(mockID, srcPath)
assert.Error(t, err)
assert.Equal(t, err.Error(), "BackupDir not set")

config.BackupDir = "/tmp"
mockc, err = New(config)
if err != nil {
t.Error(err)
return
}
assert.NoError(t, err)

mockc.SetStore(mockStore)
ch, err := mockc.Backup(mockID, srcPath)
if err != nil {
t.Error(err)
return
}
assert.Equal(t, ch.Status, "ok")
msg, err := mockc.Backup(mockID, srcPath)
assert.NoError(t, err)

assert.Equal(t, msg.Status, "ok")
t.Log("Backup to ", msg.Path)

os.RemoveAll(msg.Path)
}
29 changes: 14 additions & 15 deletions cluster/calcium/create_container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,9 @@ func TestCreateContainerWithMemPrior(t *testing.T) {
}

// Create Container with memory prior
testlogF("Create containers with memory prior")
t.Log("Create containers with memory prior")
createCh, err := mockc.createContainerWithMemoryPrior(specs, opts)
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)
ids := []string{}
for msg := range createCh {
assert.True(t, msg.Success)
Expand All @@ -78,11 +76,9 @@ func TestCreateContainerWithMemPrior(t *testing.T) {
mockStore.On("GetContainers", ids).Return(&cs, nil)

// Remove Container
testlogF("Remove containers")
t.Log("Remove containers")
removeCh, err := mockc.RemoveContainer(ids)
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)
for msg := range removeCh {
fmt.Printf("ID: %s, Message: %s\n", msg.ContainerID, msg.Message)
assert.True(t, msg.Success)
Expand All @@ -93,9 +89,14 @@ func TestClean(t *testing.T) {
initMockConfig()

// delete pod
if err := mockc.store.DeletePod("dev_pod", true); err != nil {
t.Fatal(err)
}
err := mockc.store.DeletePod(podname, false)
assert.Error(t, err)
assert.Contains(t, err.Error(), "still has nodes, delete the nodes first")

// force delete
err = mockc.store.DeletePod(podname, true)
assert.NoError(t, err)

}

func TestCreateContainerWithCPUPrior(t *testing.T) {
Expand Down Expand Up @@ -131,11 +132,9 @@ func TestCreateContainerWithCPUPrior(t *testing.T) {
})).Return(nil)

// Create Container with memory prior
testlogF("Create containers with memory prior")
t.Log("Create containers with memory prior")
createCh, err := mockc.createContainerWithCPUPrior(specs, opts)
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)
ids := []string{}
for msg := range createCh {
assert.True(t, msg.Success)
Expand Down
8 changes: 6 additions & 2 deletions cluster/calcium/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ func mockDockerDoer(r *http.Request) (*http.Response, error) {
case fmt.Sprintf("/containers/%s/stop", containerID):
testlogF("stop container %s", containerID)
b = []byte("body")
case fmt.Sprintf("/containers/%s/archive", containerID):
case fmt.Sprintf("/containers/%s/archive", containerID): // docker cp
query := r.URL.Query()
path := query.Get("path")
testlogF("mock docker cp to %s", path)
headercontent, err := json.Marshal(types.ContainerPathStat{
Name: "name",
Mode: 0700,
Expand Down Expand Up @@ -332,10 +335,11 @@ func initMockConfig() {

// GetContainer
rContainer := &coretypes.Container{
ID: mockID,
Engine: clnt,
Podname: podname,
Nodename: nodename,
Name: "hello_hi_123",
}
mockStore.On("GetContainer", mockStringType).Return(rContainer, nil)
mockStore.On("GetContainer", mockID).Return(rContainer, nil)
}
7 changes: 3 additions & 4 deletions cluster/calcium/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package calcium

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestListNetworks(t *testing.T) {
initMockConfig()

networks, err := mockc.ListNetworks(podname)
if err != nil {
t.Error(err)
return
}
assert.NoError(t, err)
for _, network := range networks {
t.Log(network.Name)
}
Expand Down
18 changes: 14 additions & 4 deletions cluster/calcium/remove_container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,31 @@ package calcium
import (
"testing"

"gitlab.ricebook.net/platform/core/types"

"github.com/docker/docker/client"
"github.com/stretchr/testify/assert"
)

func TestRemoveContainer(t *testing.T) {
initMockConfig()

ids := []string{}
clnt, _ := client.NewClient("http://127.0.0.1", "v1.29", mockDockerHTTPClient(), nil)
for i := 0; i < 5; i++ {
ids = append(ids, mockContainerID())
}
ch, err := mockc.RemoveContainer(ids)
if err != nil {
t.Error(err)
return
for _, id := range ids {
c := types.Container{
ID: id,
Engine: clnt,
}
mockStore.On("GetContainer", id).Return(&c, nil)
}

ch, err := mockc.RemoveContainer(ids)
assert.NoError(t, err)

for c := range ch {
assert.True(t, c.Success)
}
Expand Down
6 changes: 2 additions & 4 deletions cluster/calcium/remove_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ func TestRemoveImage(t *testing.T) {

images := []string{image}
ch, err := mockc.RemoveImage(podname, nodename, images)
if err != nil {
t.Error(err)
return
}
assert.NoError(t, err)

for c := range ch {
assert.True(t, c.Success)
}
Expand Down
Loading

0 comments on commit 62ae115

Please sign in to comment.