-
Notifications
You must be signed in to change notification settings - Fork 950
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add unit test for storage module
Add unit-test for storage module. Signed-off-by: Rudy Zhang <rudyflyzhang@gmail.com>
- Loading branch information
Showing
3 changed files
with
289 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |