github.com/portworx/docker@v1.12.1/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/go-connections/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  	mux.HandleFunc("/VolumeDriver.List", func(w http.ResponseWriter, r *http.Request) {
    46  		w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
    47  		fmt.Fprintln(w, `{"Err": "Cannot list volumes"}`)
    48  	})
    49  
    50  	mux.HandleFunc("/VolumeDriver.Get", func(w http.ResponseWriter, r *http.Request) {
    51  		w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
    52  		fmt.Fprintln(w, `{"Err": "Cannot get volume"}`)
    53  	})
    54  
    55  	mux.HandleFunc("/VolumeDriver.Capabilities", func(w http.ResponseWriter, r *http.Request) {
    56  		w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
    57  		http.Error(w, "error", 500)
    58  	})
    59  
    60  	u, _ := url.Parse(server.URL)
    61  	client, err := plugins.NewClient("tcp://"+u.Host, &tlsconfig.Options{InsecureSkipVerify: true})
    62  	if err != nil {
    63  		t.Fatal(err)
    64  	}
    65  
    66  	driver := volumeDriverProxy{client}
    67  
    68  	if err = driver.Create("volume", nil); err == nil {
    69  		t.Fatal("Expected error, was nil")
    70  	}
    71  
    72  	if !strings.Contains(err.Error(), "Cannot create volume") {
    73  		t.Fatalf("Unexpected error: %v\n", err)
    74  	}
    75  
    76  	_, err = driver.Mount("volume", "123")
    77  	if err == nil {
    78  		t.Fatal("Expected error, was nil")
    79  	}
    80  
    81  	if !strings.Contains(err.Error(), "Cannot mount volume") {
    82  		t.Fatalf("Unexpected error: %v\n", err)
    83  	}
    84  
    85  	err = driver.Unmount("volume", "123")
    86  	if err == nil {
    87  		t.Fatal("Expected error, was nil")
    88  	}
    89  
    90  	if !strings.Contains(err.Error(), "Cannot unmount volume") {
    91  		t.Fatalf("Unexpected error: %v\n", err)
    92  	}
    93  
    94  	err = driver.Remove("volume")
    95  	if err == nil {
    96  		t.Fatal("Expected error, was nil")
    97  	}
    98  
    99  	if !strings.Contains(err.Error(), "Cannot remove volume") {
   100  		t.Fatalf("Unexpected error: %v\n", err)
   101  	}
   102  
   103  	_, err = driver.Path("volume")
   104  	if err == nil {
   105  		t.Fatal("Expected error, was nil")
   106  	}
   107  
   108  	if !strings.Contains(err.Error(), "Unknown volume") {
   109  		t.Fatalf("Unexpected error: %v\n", err)
   110  	}
   111  
   112  	_, err = driver.List()
   113  	if err == nil {
   114  		t.Fatal("Expected error, was nil")
   115  	}
   116  	if !strings.Contains(err.Error(), "Cannot list volumes") {
   117  		t.Fatalf("Unexpected error: %v\n", err)
   118  	}
   119  
   120  	_, err = driver.Get("volume")
   121  	if err == nil {
   122  		t.Fatal("Expected error, was nil")
   123  	}
   124  	if !strings.Contains(err.Error(), "Cannot get volume") {
   125  		t.Fatalf("Unexpected error: %v\n", err)
   126  	}
   127  
   128  	_, err = driver.Capabilities()
   129  	if err == nil {
   130  		t.Fatal(err)
   131  	}
   132  }