github.com/lmars/docker@v1.6.0-rc2/volumes/repository_test.go (about)

     1  package volumes
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/docker/docker/daemon/graphdriver"
    10  	_ "github.com/docker/docker/daemon/graphdriver/vfs"
    11  )
    12  
    13  func TestRepositoryFindOrCreate(t *testing.T) {
    14  	root, err := ioutil.TempDir(os.TempDir(), "volumes")
    15  	if err != nil {
    16  		t.Fatal(err)
    17  	}
    18  	defer os.RemoveAll(root)
    19  	repo, err := newRepo(root)
    20  	if err != nil {
    21  		t.Fatal(err)
    22  	}
    23  
    24  	// no path
    25  	v, err := repo.FindOrCreateVolume("", true)
    26  	if err != nil {
    27  		t.Fatal(err)
    28  	}
    29  
    30  	// FIXME: volumes are heavily dependent on the vfs driver, but this should not be so!
    31  	expected := filepath.Join(root, "repo-graph", "vfs", "dir", v.ID)
    32  	if v.Path != expected {
    33  		t.Fatalf("expected new path to be created in %s, got %s", expected, v.Path)
    34  	}
    35  
    36  	// with a non-existant path
    37  	dir := filepath.Join(root, "doesntexist")
    38  	v, err = repo.FindOrCreateVolume(dir, true)
    39  	if err != nil {
    40  		t.Fatal(err)
    41  	}
    42  
    43  	if v.Path != dir {
    44  		t.Fatalf("expected new path to be created in %s, got %s", dir, v.Path)
    45  	}
    46  
    47  	if _, err := os.Stat(v.Path); err != nil {
    48  		t.Fatal(err)
    49  	}
    50  
    51  	// with a pre-existing path
    52  	// can just use the same path from above since it now exists
    53  	v, err = repo.FindOrCreateVolume(dir, true)
    54  	if err != nil {
    55  		t.Fatal(err)
    56  	}
    57  	if v.Path != dir {
    58  		t.Fatalf("expected new path to be created in %s, got %s", dir, v.Path)
    59  	}
    60  
    61  }
    62  
    63  func TestRepositoryGet(t *testing.T) {
    64  	root, err := ioutil.TempDir(os.TempDir(), "volumes")
    65  	if err != nil {
    66  		t.Fatal(err)
    67  	}
    68  	defer os.RemoveAll(root)
    69  	repo, err := newRepo(root)
    70  	if err != nil {
    71  		t.Fatal(err)
    72  	}
    73  
    74  	v, err := repo.FindOrCreateVolume("", true)
    75  	if err != nil {
    76  		t.Fatal(err)
    77  	}
    78  
    79  	v2 := repo.Get(v.Path)
    80  	if v2 == nil {
    81  		t.Fatalf("expected to find volume but didn't")
    82  	}
    83  	if v2 != v {
    84  		t.Fatalf("expected get to return same volume")
    85  	}
    86  }
    87  
    88  func TestRepositoryDelete(t *testing.T) {
    89  	root, err := ioutil.TempDir(os.TempDir(), "volumes")
    90  	if err != nil {
    91  		t.Fatal(err)
    92  	}
    93  	defer os.RemoveAll(root)
    94  	repo, err := newRepo(root)
    95  	if err != nil {
    96  		t.Fatal(err)
    97  	}
    98  
    99  	// with a normal volume
   100  	v, err := repo.FindOrCreateVolume("", true)
   101  	if err != nil {
   102  		t.Fatal(err)
   103  	}
   104  
   105  	if err := repo.Delete(v.Path); err != nil {
   106  		t.Fatal(err)
   107  	}
   108  
   109  	if v := repo.Get(v.Path); v != nil {
   110  		t.Fatalf("expected volume to not exist")
   111  	}
   112  
   113  	if _, err := os.Stat(v.Path); err == nil {
   114  		t.Fatalf("expected volume files to be removed")
   115  	}
   116  
   117  	// with a bind mount
   118  	dir := filepath.Join(root, "test")
   119  	v, err = repo.FindOrCreateVolume(dir, true)
   120  	if err != nil {
   121  		t.Fatal(err)
   122  	}
   123  
   124  	if err := repo.Delete(v.Path); err != nil {
   125  		t.Fatal(err)
   126  	}
   127  
   128  	if v := repo.Get(v.Path); v != nil {
   129  		t.Fatalf("expected volume to not exist")
   130  	}
   131  
   132  	if _, err := os.Stat(v.Path); err != nil && os.IsNotExist(err) {
   133  		t.Fatalf("expected bind volume data to persist after destroying volume")
   134  	}
   135  
   136  	// with container refs
   137  	dir = filepath.Join(root, "test")
   138  	v, err = repo.FindOrCreateVolume(dir, true)
   139  	if err != nil {
   140  		t.Fatal(err)
   141  	}
   142  	v.AddContainer("1234")
   143  
   144  	if err := repo.Delete(v.Path); err == nil {
   145  		t.Fatalf("expected volume delete to fail due to container refs")
   146  	}
   147  
   148  	v.RemoveContainer("1234")
   149  	if err := repo.Delete(v.Path); err != nil {
   150  		t.Fatal(err)
   151  	}
   152  
   153  }
   154  
   155  func newRepo(root string) (*Repository, error) {
   156  	configPath := filepath.Join(root, "repo-config")
   157  	graphDir := filepath.Join(root, "repo-graph")
   158  
   159  	driver, err := graphdriver.GetDriver("vfs", graphDir, []string{})
   160  	if err != nil {
   161  		return nil, err
   162  	}
   163  	return NewRepository(configPath, driver)
   164  }