Skip to content

Commit

Permalink
test: add unit test for storage module
Browse files Browse the repository at this point in the history
Add unit-test for storage module.

Signed-off-by: Rudy Zhang <rudyflyzhang@gmail.com>
  • Loading branch information
rudyfly committed Aug 14, 2018
1 parent e0111ac commit 9217717
Show file tree
Hide file tree
Showing 3 changed files with 289 additions and 14 deletions.
14 changes: 0 additions & 14 deletions storage/volume/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"sync"

"github.com/alibaba/pouch/plugins"
"github.com/alibaba/pouch/storage/volume/types"

"github.com/pkg/errors"
)

Expand Down Expand Up @@ -253,18 +251,6 @@ func AllDriversName() []string {
return names
}

// ListDriverOption return backend driver's options by name.
func ListDriverOption(name string) map[string]types.Option {
dv, err := Get(name)
if err != nil {
return nil
}
if opt, ok := dv.(Opt); ok {
return opt.Options()
}
return nil
}

// Alias is used to add driver name's alias into exist driver.
func Alias(name, alias string) error {
matched, err := regexp.MatchString(driverNameRegexp, alias)
Expand Down
156 changes: 156 additions & 0 deletions storage/volume/driver/driver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package driver

import (
"testing"
)

func TestRegister(t *testing.T) {
testDriverName := "testdriver"
fake1Driver := NewFakeDriver(testDriverName)

err := Register(fake1Driver)
if err != nil {
t.Errorf("failed to register fake1 driver")
}

d, err := Get(testDriverName)
if err != nil {
t.Errorf("failed to get fake1 driver, err: %v", err)
}
if d == nil {
t.Errorf("failed to get fake1 driver is nil")
}

if d.Name(Contexts()) != testDriverName {
t.Errorf("error driver name with testdriver")
}

success := Unregister(testDriverName)
if success == false {
t.Errorf("failed to unregister testdriver")
}

d, err = Get(testDriverName)
if err == nil || d != nil {
t.Errorf("failed to unregister testdriver, get driver: %s", d.Name(Contexts()))
}
}

func TestGetAll(t *testing.T) {
for _, name := range []string{"testdriver1", "testdriver2", "testdriver3"} {
driver := NewFakeDriver(name)
err := Register(driver)
if err != nil {
t.Errorf("failed to register driver: %s", name)
}
}

names := AllDriversName()
if len(names) != 3 {
t.Errorf("failed to get all drivers, number is %d", len(names))
}

for _, n := range names {
if n != "testdriver1" && n != "testdriver2" && n != "testdriver3" {
t.Errorf("failed to get all driver, name %s is unknown", n)
}
}
}

func TestAlias(t *testing.T) {
for _, name := range []string{"testdriver1", "testdriver2"} {
driver := NewFakeDriver(name)
err := Register(driver)
if err != nil {
t.Errorf("failed to register driver: %s", name)
}
}

err := Alias("testdriver1", "testdriver111")
if err != nil {
t.Errorf("failed to alias driver")
}

d, err := Get("testdriver111")
if err != nil {
t.Errorf("failed to get alias volume driver.")
}
if d == nil {
t.Errorf("failed to get alias volume driver")
}

if d.Name(Contexts()) != "testdriver1" {
t.Errorf("failed to get volume name: %s", d.Name(Contexts()))
}
}

func TestVolumeStoreMode_Valid(t *testing.T) {
tests := []struct {
Mode VolumeStoreMode
expect bool
}{
{
LocalStore,
true,
},
{
RemoteStore,
true,
},
{
CreateDeleteInCentral,
false,
},
{
UseLocalMetaStore,
false,
},
{
LocalStore | RemoteStore,
false,
},
{
LocalStore | CreateDeleteInCentral,
false,
},
{
LocalStore | UseLocalMetaStore,
true,
},
{
RemoteStore | CreateDeleteInCentral,
true,
},
{
RemoteStore | UseLocalMetaStore,
false,
},
{
CreateDeleteInCentral | UseLocalMetaStore,
false,
},
{
LocalStore | RemoteStore | CreateDeleteInCentral,
false,
},
{
LocalStore | RemoteStore | UseLocalMetaStore,
false,
},
{
RemoteStore | CreateDeleteInCentral | UseLocalMetaStore,
false,
},
{
LocalStore | RemoteStore | CreateDeleteInCentral | UseLocalMetaStore,
false,
},
}

for index, tt := range tests {
if tt.Mode.Valid() != tt.expect {
t.Errorf("failed to test valid VolumeStoreMode, index: %d, mode: %v, expect: %v",
index+1, tt.Mode, tt.expect)
}
}
}
133 changes: 133 additions & 0 deletions storage/volume/driver/proxy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package driver

import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"

"github.com/alibaba/pouch/plugins"
)

func TestRemoteDriverRequestError(t *testing.T) {
mux := http.NewServeMux()
server := httptest.NewServer(mux)
defer server.Close()

mux.HandleFunc(remoteVolumeCreateService, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
fmt.Fprintln(w, `{"Err": "Cannot create volume"}`)
})

mux.HandleFunc(remoteVolumeRemoveService, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
fmt.Fprintln(w, `{"Err": "Cannot remove volume"}`)
})

mux.HandleFunc(remoteVolumeMountService, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
fmt.Fprintln(w, `{"Err": "Cannot mount volume"}`)
})

mux.HandleFunc(remoteVolumeUnmountService, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
fmt.Fprintln(w, `{"Err": "Cannot unmount volume"}`)
})

mux.HandleFunc(remoteVolumePathService, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
fmt.Fprintln(w, `{"Err": "Unknown volume"}`)
})

mux.HandleFunc(remoteVolumeListService, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
fmt.Fprintln(w, `{"Err": "Cannot list volumes"}`)
})

mux.HandleFunc(remoteVolumeGetService, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
fmt.Fprintln(w, `{"Err": "Cannot get volume"}`)
})

mux.HandleFunc(remoteVolumeCapabilitiesService, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
http.Error(w, "error", 500)
})

u, _ := url.Parse(server.URL)
client, err := plugins.NewPluginClient("tcp://"+u.Host, &plugins.TLSConfig{InsecureSkipVerify: true})
if err != nil {
t.Fatal(err)
}

dp := &remoteDriverProxy{
client: client,
}

if err = dp.Create("volume", nil); err == nil {
t.Fatal("Expected error, was nil")
}

if !strings.Contains(err.Error(), "Cannot create volume") {
t.Fatalf("Unexpected error: %v\n", err)
}

_, err = dp.Mount("volume", "abc")
if err == nil {
t.Fatal("Expected error, was nil")
}

if !strings.Contains(err.Error(), "Cannot mount volume") {
t.Fatalf("Unexpected error: %v\n", err)
}

err = dp.Unmount("volume", "abc")
if err == nil {
t.Fatal("Expected error, was nil")
}

if !strings.Contains(err.Error(), "Cannot unmount volume") {
t.Fatalf("Unexpected error: %v\n", err)
}

err = dp.Remove("volume")
if err == nil {
t.Fatal("Expected error, was nil")
}

if !strings.Contains(err.Error(), "Cannot remove volume") {
t.Fatalf("Unexpected error: %v\n", err)
}

_, err = dp.Path("volume")
if err == nil {
t.Fatal("Expected error, was nil")
}

if !strings.Contains(err.Error(), "Unknown volume") {
t.Fatalf("Unexpected error: %v\n", err)
}

_, err = dp.List()
if err == nil {
t.Fatal("Expected error, was nil")
}
if !strings.Contains(err.Error(), "Cannot list volumes") {
t.Fatalf("Unexpected error: %v\n", err)
}

_, err = dp.Get("volume")
if err == nil {
t.Fatal("Expected error, was nil")
}
if !strings.Contains(err.Error(), "Cannot get volume") {
t.Fatalf("Unexpected error: %v\n", err)
}

_, err = dp.Capabilities()
if err == nil {
t.Fatal(err)
}
}

0 comments on commit 9217717

Please sign in to comment.