github.com/guilhermebr/docker@v1.4.2-0.20150428121140-67da055cebca/volumes/volume_test.go (about)

     1  package volumes
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/docker/docker/pkg/stringutils"
     8  )
     9  
    10  func TestContainers(t *testing.T) {
    11  	v := &Volume{containers: make(map[string]struct{})}
    12  	id := "1234"
    13  
    14  	v.AddContainer(id)
    15  
    16  	if v.Containers()[0] != id {
    17  		t.Fatalf("adding a container ref failed")
    18  	}
    19  
    20  	v.RemoveContainer(id)
    21  	if len(v.Containers()) != 0 {
    22  		t.Fatalf("removing container failed")
    23  	}
    24  }
    25  
    26  // os.Stat(v.Path) is returning ErrNotExist, initialize catch it and try to
    27  // mkdir v.Path but it dies and correctly returns the error
    28  func TestInitializeCannotMkdirOnNonExistentPath(t *testing.T) {
    29  	v := &Volume{Path: "nonexistentpath"}
    30  
    31  	err := v.initialize()
    32  	if err == nil {
    33  		t.Fatal("Expected not to initialize volume with a non existent path")
    34  	}
    35  
    36  	if !os.IsNotExist(err) {
    37  		t.Fatalf("Expected to get ErrNotExist error, got %s", err)
    38  	}
    39  }
    40  
    41  // os.Stat(v.Path) is NOT returning ErrNotExist so skip and return error from
    42  // initialize
    43  func TestInitializeCannotStatPathFileNameTooLong(t *testing.T) {
    44  	// ENAMETOOLONG
    45  	v := &Volume{Path: stringutils.GenerateRandomAlphaOnlyString(300)}
    46  
    47  	err := v.initialize()
    48  	if err == nil {
    49  		t.Fatal("Expected not to initialize volume with a non existent path")
    50  	}
    51  
    52  	if os.IsNotExist(err) {
    53  		t.Fatal("Expected to not get ErrNotExist")
    54  	}
    55  }