Skip to content

Commit

Permalink
Add unit-test for Volume Core's DetachVolume method
Browse files Browse the repository at this point in the history
  • Loading branch information
panzaiyu committed Jul 24, 2018
1 parent caabc86 commit 5c49537
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion storage/volume/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,54 @@ func TestAttachVolume(t *testing.T) {
// TODO
}

//Mock data for TestDetachVolume
func mockVolumeCore(root string) (*Core, error) {
cfg := Config{
VolumeMetaPath: path.Join(root, "volume.db"),
}
return NewCore(cfg)
}

func TestDetachVolume(t *testing.T) {
// TODO
volName1 := "vol2"
driverName1 := "fake_driver12"
volid1 := types.VolumeID{Name: volName1, Driver: driverName1}
extra1 := map[string]string{}

dir, err := ioutil.TempDir("", "TestDetachVolume")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)

//create volume core

core, err := mockVolumeCore(dir)
if err != nil {
t.Fatal(err)
}

driver.Register(driver.NewFakeDriver(driverName1))
defer driver.Unregister(driverName1)

core.CreateVolume(volid1)

//attach a volume and detach it
v1, err1 := core.AttachVolume(volid1, extra1)
if err1 != nil {
t.Fatalf("attach volume error: %v", err1)
}

if v1.Name != volName1 {
t.Fatalf("expect volume name is %s, but got %s", volName1, v1.Name)
}
if v1.Driver() != driverName1 {
t.Fatalf("expect volume driver is %s, but got %s", driverName1, v1.Driver())
}

//detach a null volume
_, err = core.DetachVolume(types.VolumeID{Name: "none", Driver: "none"}, nil)
if err == nil {
t.Fatal("expect get driver not found error, but err is nil")
}
}

0 comments on commit 5c49537

Please sign in to comment.