Merge pull request #276 from vdemeester/add-some-plugin-test

Add some unit tests to Plugin* methods
This commit is contained in:
David Calavera 2016-06-15 09:23:32 -07:00 коммит произвёл GitHub
Родитель cf0f1d7b74 4194e78ccf
Коммит 534e42a119
8 изменённых файлов: 363 добавлений и 1 удалений

Просмотреть файл

@ -7,7 +7,7 @@ deps:
go get -u github.com/golang/lint/golint go get -u github.com/golang/lint/golint
test: test:
go test -race -cover ./... go test -tags experimental -race -cover ./...
validate: lint validate: lint
go vet ./... go vet ./...

Просмотреть файл

@ -0,0 +1,49 @@
// +build experimental
package client
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"
"golang.org/x/net/context"
)
func TestPluginDisableError(t *testing.T) {
client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")),
}
err := client.PluginDisable(context.Background(), "plugin_name")
if err == nil || err.Error() != "Error response from daemon: Server error" {
t.Fatalf("expected a Server Error, got %v", err)
}
}
func TestPluginDisable(t *testing.T) {
expectedURL := "/plugins/plugin_name/disable"
client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
}
if req.Method != "POST" {
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
}, nil
}),
}
err := client.PluginDisable(context.Background(), "plugin_name")
if err != nil {
t.Fatal(err)
}
}

Просмотреть файл

@ -0,0 +1,49 @@
// +build experimental
package client
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"
"golang.org/x/net/context"
)
func TestPluginEnableError(t *testing.T) {
client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")),
}
err := client.PluginEnable(context.Background(), "plugin_name")
if err == nil || err.Error() != "Error response from daemon: Server error" {
t.Fatalf("expected a Server Error, got %v", err)
}
}
func TestPluginEnable(t *testing.T) {
expectedURL := "/plugins/plugin_name/enable"
client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
}
if req.Method != "POST" {
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
}, nil
}),
}
err := client.PluginEnable(context.Background(), "plugin_name")
if err != nil {
t.Fatal(err)
}
}

Просмотреть файл

@ -0,0 +1,54 @@
package client
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"
"github.com/docker/engine-api/types"
"golang.org/x/net/context"
)
func TestPluginInspectError(t *testing.T) {
client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")),
}
_, err := client.PluginInspect(context.Background(), "nothing")
if err == nil || err.Error() != "Error response from daemon: Server error" {
t.Fatalf("expected a Server Error, got %v", err)
}
}
func TestPluginInspect(t *testing.T) {
expectedURL := "/plugins/plugin_name"
client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
}
content, err := json.Marshal(types.Plugin{
ID: "plugin_id",
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader(content)),
}, nil
}),
}
pluginInspect, err := client.PluginInspect(context.Background(), "plugin_name")
if err != nil {
t.Fatal(err)
}
if pluginInspect.ID != "plugin_id" {
t.Fatalf("expected `plugin_id`, got %s", pluginInspect.ID)
}
}

Просмотреть файл

@ -0,0 +1,59 @@
package client
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"
"github.com/docker/engine-api/types"
"golang.org/x/net/context"
)
func TestPluginListError(t *testing.T) {
client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")),
}
_, err := client.PluginList(context.Background())
if err == nil || err.Error() != "Error response from daemon: Server error" {
t.Fatalf("expected a Server Error, got %v", err)
}
}
func TestPluginList(t *testing.T) {
expectedURL := "/plugins"
client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
}
content, err := json.Marshal([]*types.Plugin{
{
ID: "plugin_id1",
},
{
ID: "plugin_id2",
},
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader(content)),
}, nil
}),
}
plugins, err := client.PluginList(context.Background())
if err != nil {
t.Fatal(err)
}
if len(plugins) != 2 {
t.Fatalf("expected 2 plugins, got %v", plugins)
}
}

Просмотреть файл

@ -0,0 +1,53 @@
// +build experimental
package client
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"
"golang.org/x/net/context"
)
func TestPluginPushError(t *testing.T) {
client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")),
}
err := client.PluginPush(context.Background(), "plugin_name", "")
if err == nil || err.Error() != "Error response from daemon: Server error" {
t.Fatalf("expected a Server Error, got %v", err)
}
}
func TestPluginPush(t *testing.T) {
expectedURL := "/plugins/plugin_name"
client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
}
if req.Method != "POST" {
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
}
auth := req.Header.Get("X-Registry-Auth")
if auth != "authtoken" {
return nil, fmt.Errorf("Invalid auth header : expected %s, got %s", "authtoken", auth)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
}, nil
}),
}
err := client.PluginPush(context.Background(), "plugin_name", "authtoken")
if err != nil {
t.Fatal(err)
}
}

Просмотреть файл

@ -0,0 +1,49 @@
// +build experimental
package client
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"
"golang.org/x/net/context"
)
func TestPluginRemoveError(t *testing.T) {
client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")),
}
err := client.PluginRemove(context.Background(), "plugin_name")
if err == nil || err.Error() != "Error response from daemon: Server error" {
t.Fatalf("expected a Server Error, got %v", err)
}
}
func TestPluginRemove(t *testing.T) {
expectedURL := "/plugins/plugin_name"
client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
}
if req.Method != "DELETE" {
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
}, nil
}),
}
err := client.PluginRemove(context.Background(), "plugin_name")
if err != nil {
t.Fatal(err)
}
}

49
client/plugin_set_test.go Normal file
Просмотреть файл

@ -0,0 +1,49 @@
// +build experimental
package client
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"
"golang.org/x/net/context"
)
func TestPluginSetError(t *testing.T) {
client := &Client{
transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")),
}
err := client.PluginSet(context.Background(), "plugin_name", []string{})
if err == nil || err.Error() != "Error response from daemon: Server error" {
t.Fatalf("expected a Server Error, got %v", err)
}
}
func TestPluginSet(t *testing.T) {
expectedURL := "/plugins/plugin_name/set"
client := &Client{
transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
}
if req.Method != "POST" {
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
}, nil
}),
}
err := client.PluginSet(context.Background(), "plugin_name", []string{"arg1"})
if err != nil {
t.Fatal(err)
}
}