github.com/slava-ustovytski/docker@v1.8.2-rc1/volume/drivers/proxy_test.go (about)

     1  package volumedrivers
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"net/url"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/docker/docker/pkg/plugins"
    12  	"github.com/docker/docker/pkg/tlsconfig"
    13  )
    14  
    15  func TestVolumeRequestError(t *testing.T) {
    16  	mux := http.NewServeMux()
    17  	server := httptest.NewServer(mux)
    18  	defer server.Close()
    19  
    20  	mux.HandleFunc("/VolumeDriver.Create", func(w http.ResponseWriter, r *http.Request) {
    21  		w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
    22  		fmt.Fprintln(w, `{"Err": "Cannot create volume"}`)
    23  	})
    24  
    25  	mux.HandleFunc("/VolumeDriver.Remove", func(w http.ResponseWriter, r *http.Request) {
    26  		w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
    27  		fmt.Fprintln(w, `{"Err": "Cannot remove volume"}`)
    28  	})
    29  
    30  	mux.HandleFunc("/VolumeDriver.Mount", func(w http.ResponseWriter, r *http.Request) {
    31  		w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
    32  		fmt.Fprintln(w, `{"Err": "Cannot mount volume"}`)
    33  	})
    34  
    35  	mux.HandleFunc("/VolumeDriver.Unmount", func(w http.ResponseWriter, r *http.Request) {
    36  		w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
    37  		fmt.Fprintln(w, `{"Err": "Cannot unmount volume"}`)
    38  	})
    39  
    40  	mux.HandleFunc("/VolumeDriver.Path", func(w http.ResponseWriter, r *http.Request) {
    41  		w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
    42  		fmt.Fprintln(w, `{"Err": "Unknown volume"}`)
    43  	})
    44  
    45  	u, _ := url.Parse(server.URL)
    46  	client, err := plugins.NewClient("tcp://"+u.Host, tlsconfig.Options{InsecureSkipVerify: true})
    47  	if err != nil {
    48  		t.Fatal(err)
    49  	}
    50  
    51  	driver := volumeDriverProxy{client}
    52  
    53  	if err = driver.Create("volume"); err == nil {
    54  		t.Fatal("Expected error, was nil")
    55  	}
    56  
    57  	if !strings.Contains(err.Error(), "Cannot create volume") {
    58  		t.Fatalf("Unexpected error: %v\n", err)
    59  	}
    60  
    61  	_, err = driver.Mount("volume")
    62  	if err == nil {
    63  		t.Fatal("Expected error, was nil")
    64  	}
    65  
    66  	if !strings.Contains(err.Error(), "Cannot mount volume") {
    67  		t.Fatalf("Unexpected error: %v\n", err)
    68  	}
    69  
    70  	err = driver.Unmount("volume")
    71  	if err == nil {
    72  		t.Fatal("Expected error, was nil")
    73  	}
    74  
    75  	if !strings.Contains(err.Error(), "Cannot unmount volume") {
    76  		t.Fatalf("Unexpected error: %v\n", err)
    77  	}
    78  
    79  	err = driver.Remove("volume")
    80  	if err == nil {
    81  		t.Fatal("Expected error, was nil")
    82  	}
    83  
    84  	if !strings.Contains(err.Error(), "Cannot remove volume") {
    85  		t.Fatalf("Unexpected error: %v\n", err)
    86  	}
    87  
    88  	_, err = driver.Path("volume")
    89  	if err == nil {
    90  		t.Fatal("Expected error, was nil")
    91  	}
    92  
    93  	if !strings.Contains(err.Error(), "Unknown volume") {
    94  		t.Fatalf("Unexpected error: %v\n", err)
    95  	}
    96  }