File: shim_test.go

package info (click to toggle)
golang-github-docker-go-plugins-helpers 0.20211224-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 280 kB
  • sloc: makefile: 29
file content (66 lines) | stat: -rw-r--r-- 2,089 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package shim

import (
	"bytes"
	"encoding/json"
	"net/http"
	"testing"

	"github.com/docker/docker/volume"
	"github.com/docker/go-connections/sockets"
	volumeplugin "github.com/docker/go-plugins-helpers/volume"
)

type testVolumeDriver struct{}
type testVolume struct{}

func (testVolume) Name() string           { return "" }
func (testVolume) Path() string           { return "" }
func (testVolume) Mount() (string, error) { return "", nil }
func (testVolume) Unmount() error         { return nil }
func (testVolume) DriverName() string     { return "" }

func (testVolumeDriver) Name() string                                            { return "" }
func (testVolumeDriver) Create(string, map[string]string) (volume.Volume, error) { return nil, nil }
func (testVolumeDriver) Remove(volume.Volume) error                              { return nil }
func (testVolumeDriver) List() ([]volume.Volume, error)                          { return nil, nil }
func (testVolumeDriver) Get(name string) (volume.Volume, error)                  { return nil, nil }
func (testVolumeDriver) Scope() string                                           { return "local" }

func TestVolumeDriver(t *testing.T) {
	h := NewHandlerFromVolumeDriver(testVolumeDriver{})
	l := sockets.NewInmemSocket("test", 0)
	go h.Serve(l)
	defer l.Close()

	client := &http.Client{Transport: &http.Transport{
		Dial: l.Dial,
	}}

	resp, err := pluginRequest(client, "/VolumeDriver.Create", &volumeplugin.CreateRequest{Name: "foo"})
	if err != nil {
		t.Fatalf(err.Error())
	}

	if resp.Err != "" {
		t.Fatalf("error while creating volume: %v", err)
	}
}

func pluginRequest(client *http.Client, method string, req *volumeplugin.CreateRequest) (*volumeplugin.ErrorResponse, error) {
	b, err := json.Marshal(req)
	if err != nil {
		return nil, err
	}
	resp, err := client.Post("http://localhost"+method, "application/json", bytes.NewReader(b))
	if err != nil {
		return nil, err
	}
	var vResp volumeplugin.ErrorResponse
	err = json.NewDecoder(resp.Body).Decode(&vResp)
	if err != nil {
		return nil, err
	}

	return &vResp, nil
}