From 5dd3fd927d9ac8baaaa303c9ef01e09f33cb1baa Mon Sep 17 00:00:00 2001 From: bubbajoe Date: Sun, 12 May 2024 00:38:13 +0900 Subject: [PATCH] refactor dgate cli run function, add more test to increase coverage for dgate-cli --- cmd/dgate-cli/commands/run_cmd.go | 7 +- cmd/dgate-cli/commands/run_cmd_test.go | 203 ++++++++++++++----------- cmd/dgate-cli/main.go | 8 +- pkg/dgclient/collection_client_test.go | 97 ++++++++++++ pkg/dgclient/document_client_test.go | 115 ++++++++++++++ pkg/dgclient/domain_client_test.go | 97 ++++++++++++ pkg/dgclient/module_client_test.go | 97 ++++++++++++ pkg/dgclient/namespace_client_test.go | 97 ++++++++++++ pkg/dgclient/secret_client_test.go | 97 ++++++++++++ pkg/dgclient/service_client_test.go | 97 ++++++++++++ 10 files changed, 822 insertions(+), 93 deletions(-) create mode 100644 pkg/dgclient/collection_client_test.go create mode 100644 pkg/dgclient/document_client_test.go create mode 100644 pkg/dgclient/domain_client_test.go create mode 100644 pkg/dgclient/module_client_test.go create mode 100644 pkg/dgclient/namespace_client_test.go create mode 100644 pkg/dgclient/secret_client_test.go create mode 100644 pkg/dgclient/service_client_test.go diff --git a/cmd/dgate-cli/commands/run_cmd.go b/cmd/dgate-cli/commands/run_cmd.go index 3a3f535..c48a2c5 100644 --- a/cmd/dgate-cli/commands/run_cmd.go +++ b/cmd/dgate-cli/commands/run_cmd.go @@ -12,7 +12,7 @@ import ( "golang.org/x/term" ) -func Run(client dgclient.DGateClient, version string) { +func Run(client dgclient.DGateClient, version string) error { if buildInfo, ok := debug.ReadBuildInfo(); ok { bv := buildInfo.Main.Version if bv != "" && bv != "(devel)" { @@ -106,8 +106,5 @@ func Run(client dgclient.DGateClient, version string) { }, } - if err := app.Run(os.Args); err != nil { - fmt.Println(err) - os.Exit(1) - } + return app.Run(os.Args) } diff --git a/cmd/dgate-cli/commands/run_cmd_test.go b/cmd/dgate-cli/commands/run_cmd_test.go index 85cf0b0..56284bc 100644 --- a/cmd/dgate-cli/commands/run_cmd_test.go +++ b/cmd/dgate-cli/commands/run_cmd_test.go @@ -1,12 +1,14 @@ package commands import ( + "errors" "os" "strings" "testing" "github.com/dgate-io/dgate/pkg/dgclient" "github.com/dgate-io/dgate/pkg/spec" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" ) @@ -76,9 +78,90 @@ func TestGenericCommands(t *testing.T) { mock.Anything, mock.Anything, mock.Anything, + ).Return(nil, nil) + + mockClient.On("Init", "localhost.com").Return(nil) + assert.Nil(t, Run(mockClient, version)) + } + } +} + +func TestCommands_ClientError(t *testing.T) { + stdout := os.Stdout + os.Stdout = os.NewFile(0, os.DevNull) + defer func() { os.Stdout = stdout }() + + resources := []string{ + "namespace", "route", "service", + "module", "domain", "secret", + "collection", "document", + } + actions := []string{ + "get", "list", + "create", "delete", + } + + for _, resource := range resources { + for _, action := range actions { + os.Args = []string{ + "dgate-cli", + "--admin=localhost.com", + resource, action, + } + switch action { + case "delete", "get", "create": + if resource == "document" { + os.Args = append( + os.Args, + "id=test", + ) + } else { + os.Args = append(os.Args, "name=test") + } + } + if resource == "document" { + os.Args = append( + os.Args, + "collection=test", + ) + } + if action == "create" { + switch resource { + case "route": + os.Args = append(os.Args, "paths=/", "methods=GET") + case "service": + os.Args = append(os.Args, "urls=http://localhost.net") + case "module": + os.Args = append(os.Args, "payload=QUJD") + case "domain": + os.Args = append(os.Args, "patterns=*") + case "secret": + os.Args = append(os.Args, "data=123") + case "collection": + os.Args = append(os.Args, "schema:={}") + case "document": + os.Args = append(os.Args, "data:={}") + } + } + mockClient := new(mockDGClient) + funcName := firstUpper(action) + firstUpper(resource) + mockCall := mockClient.On( + funcName, + mock.Anything, + mock.Anything, + mock.Anything, ) + switch action { + case "get", "list": + mockCall.Return(nil, errors.New("error")) + case "create", "delete": + mockCall.Return(errors.New("error")) + } mockClient.On("Init", "localhost.com").Return(nil) - Run(mockClient, version) + err := Run(mockClient, version) + if assert.NotNil(t, err, "no error on %s:%s", action, resource) { + assert.Equal(t, "error", err.Error()) + } } } } @@ -100,9 +183,6 @@ var _ dgclient.DGateClient = &mockDGClient{} func (m *mockDGClient) Init(baseUrl string, opts ...dgclient.Options) error { args := m.Called(baseUrl) - if len(args) == 0 { - return nil - } return args.Error(0) } @@ -113,262 +193,211 @@ func (m *mockDGClient) BaseUrl() string { func (m *mockDGClient) GetRoute(name, namespace string) (*spec.Route, error) { args := m.Called(name, namespace) - if len(args) == 0 { - return nil, nil + if args.Get(0) == nil { + return nil, args.Error(1) } return args[0].(*spec.Route), args.Error(1) } func (m *mockDGClient) CreateRoute(rt *spec.Route) error { args := m.Called(rt) - if len(args) == 0 { - return nil - } return args.Error(0) } func (m *mockDGClient) DeleteRoute(name, namespace string) error { args := m.Called(name, namespace) - if len(args) == 0 { - return nil - } return args.Error(0) } func (m *mockDGClient) ListRoute(namespace string) ([]*spec.Route, error) { args := m.Called(namespace) - if len(args) == 0 { - return nil, nil + if args.Get(0) == nil { + return nil, args.Error(1) } return args[0].([]*spec.Route), args.Error(1) } func (m *mockDGClient) GetNamespace(name string) (*spec.Namespace, error) { args := m.Called(name) - if len(args) == 0 { - return nil, nil + if args.Get(0) == nil { + return nil, args.Error(1) } return args[0].(*spec.Namespace), args.Error(1) } func (m *mockDGClient) CreateNamespace(ns *spec.Namespace) error { args := m.Called(ns) - if len(args) == 0 { - return nil - } return args.Error(0) } func (m *mockDGClient) DeleteNamespace(name string) error { args := m.Called(name) - if len(args) == 0 { - return nil - } return args.Error(0) } func (m *mockDGClient) ListNamespace() ([]*spec.Namespace, error) { args := m.Called() - if len(args) == 0 { - return nil, nil + if args.Get(0) == nil { + return nil, args.Error(1) } return args[0].([]*spec.Namespace), args.Error(1) } func (m *mockDGClient) CreateSecret(sec *spec.Secret) error { args := m.Called(sec) - if len(args) == 0 { - return nil - } return args.Error(0) } func (m *mockDGClient) DeleteSecret(name, namespace string) error { args := m.Called(name, namespace) - if len(args) == 0 { - return nil - } return args.Error(0) } func (m *mockDGClient) GetSecret(name, namespace string) (*spec.Secret, error) { args := m.Called(name, namespace) - if len(args) == 0 { - return nil, nil + if args.Get(0) == nil { + return nil, args.Error(1) } return args[0].(*spec.Secret), args.Error(1) } func (m *mockDGClient) ListSecret(namespace string) ([]*spec.Secret, error) { args := m.Called(namespace) - if len(args) == 0 { - return nil, nil + if args.Get(0) == nil { + return nil, args.Error(1) } return args[0].([]*spec.Secret), args.Error(1) } func (m *mockDGClient) GetService(name string, namespace string) (*spec.Service, error) { args := m.Called(name, namespace) - if len(args) == 0 { - return nil, nil + if args.Get(0) == nil { + return nil, args.Error(1) } return args[0].(*spec.Service), args.Error(1) } func (m *mockDGClient) CreateService(svc *spec.Service) error { args := m.Called(svc) - if len(args) == 0 { - return nil - } return args.Error(0) } func (m *mockDGClient) DeleteService(name, namespace string) error { args := m.Called(name, namespace) - if len(args) == 0 { - return nil - } return args.Error(0) } func (m *mockDGClient) ListService(namespace string) ([]*spec.Service, error) { args := m.Called(namespace) - if len(args) == 0 { - return nil, nil + if args.Get(0) == nil { + return nil, args.Error(1) } return args[0].([]*spec.Service), args.Error(1) } func (m *mockDGClient) GetModule(name, namespace string) (*spec.Module, error) { args := m.Called(name, namespace) - if len(args) == 0 { - return nil, nil + if args.Get(0) == nil { + return nil, args.Error(1) } return args[0].(*spec.Module), args.Error(1) } func (m *mockDGClient) CreateModule(mod *spec.Module) error { args := m.Called(mod) - if len(args) == 0 { - return nil - } return args.Error(0) } func (m *mockDGClient) DeleteModule(name, namespace string) error { args := m.Called(name, namespace) - if len(args) == 0 { - return nil - } return args.Error(0) } func (m *mockDGClient) ListModule(namespace string) ([]*spec.Module, error) { args := m.Called(namespace) - if len(args) == 0 { - return nil, nil + if args.Get(0) == nil { + return nil, args.Error(1) } return args[0].([]*spec.Module), args.Error(1) } func (m *mockDGClient) CreateDomain(domain *spec.Domain) error { args := m.Called(domain) - if len(args) == 0 { - return nil - } return args.Error(0) } func (m *mockDGClient) DeleteDomain(name, namespace string) error { args := m.Called(name, namespace) - if len(args) == 0 { - return nil - } return args.Error(0) } func (m *mockDGClient) GetDomain(name, namespace string) (*spec.Domain, error) { args := m.Called(name, namespace) - if len(args) == 0 { - return nil, nil + if args.Get(0) == nil { + return nil, args.Error(1) } return args[0].(*spec.Domain), args.Error(1) } func (m *mockDGClient) ListDomain(namespace string) ([]*spec.Domain, error) { args := m.Called(namespace) - if len(args) == 0 { - return nil, nil + if args.Get(0) == nil { + return nil, args.Error(1) } return args[0].([]*spec.Domain), args.Error(1) } func (m *mockDGClient) CreateCollection(svc *spec.Collection) error { args := m.Called(svc) - if len(args) == 0 { - return nil - } return args.Error(0) } func (m *mockDGClient) DeleteCollection(name, namespace string) error { args := m.Called(name, namespace) - if len(args) == 0 { - return nil - } return args.Error(0) } func (m *mockDGClient) ListCollection(namespace string) ([]*spec.Collection, error) { args := m.Called(namespace) - if len(args) == 0 { - return nil, nil + if args.Get(0) == nil { + return nil, args.Error(1) } return args[0].([]*spec.Collection), args.Error(1) } func (m *mockDGClient) GetCollection(name, namespace string) (*spec.Collection, error) { args := m.Called(name, namespace) - if len(args) == 0 { - return nil, nil + if args.Get(0) == nil { + return nil, args.Error(1) } return args[0].(*spec.Collection), args.Error(1) } func (m *mockDGClient) CreateDocument(doc *spec.Document) error { args := m.Called(doc) - if len(args) == 0 { - return nil - } return args.Error(0) } func (m *mockDGClient) DeleteDocument(id, collection, namespace string) error { args := m.Called(id, collection, namespace) - if len(args) == 0 { - return nil - } return args.Error(0) } func (m *mockDGClient) ListDocument(namespace, collection string) ([]*spec.Document, error) { args := m.Called(namespace) - if len(args) == 0 { - return nil, nil + if args.Get(0) == nil { + return nil, args.Error(1) } return args[0].([]*spec.Document), args.Error(1) } func (m *mockDGClient) GetDocument(id, collection, namespace string) (*spec.Document, error) { args := m.Called(id, collection, namespace) - if len(args) == 0 { - return nil, nil + if args.Get(0) == nil { + return nil, args.Error(1) } return args[0].(*spec.Document), args.Error(1) } func (m *mockDGClient) DeleteAllDocument(namespace string, collection string) error { args := m.Called(namespace) - if len(args) == 0 { - return nil - } return args.Error(0) } diff --git a/cmd/dgate-cli/main.go b/cmd/dgate-cli/main.go index c17bb0b..e778297 100644 --- a/cmd/dgate-cli/main.go +++ b/cmd/dgate-cli/main.go @@ -1,6 +1,8 @@ package main import ( + "os" + "github.com/dgate-io/dgate/cmd/dgate-cli/commands" "github.com/dgate-io/dgate/pkg/dgclient" ) @@ -9,5 +11,9 @@ var version string = "dev" func main() { client := dgclient.NewDGateClient() - commands.Run(client, version) + err := commands.Run(client, version) + if err != nil { + os.Stderr.WriteString(err.Error()) + os.Exit(1) + } } diff --git a/pkg/dgclient/collection_client_test.go b/pkg/dgclient/collection_client_test.go new file mode 100644 index 0000000..32fddc6 --- /dev/null +++ b/pkg/dgclient/collection_client_test.go @@ -0,0 +1,97 @@ +package dgclient_test + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + + "github.com/dgate-io/dgate/pkg/dgclient" + "github.com/dgate-io/dgate/pkg/spec" + "github.com/stretchr/testify/assert" +) + +func TestDGClient_GetCollection(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "/api/v1/collection/test", r.URL.Path) + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(&dgclient.ResponseWrapper[*spec.Collection]{ + Data: &spec.Collection{ + Name: "test", + }, + }) + })) + client := dgclient.NewDGateClient() + err := client.Init(server.URL) + if err != nil { + t.Fatal(err) + } + + Collection, err := client.GetCollection("test", "test") + if err != nil { + t.Fatal(err) + } + assert.Equal(t, "test", Collection.Name) +} + +func TestDGClient_CreateCollection(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "/api/v1/collection", r.URL.Path) + w.WriteHeader(http.StatusCreated) + })) + client := dgclient.NewDGateClient() + err := client.Init(server.URL) + if err != nil { + t.Fatal(err) + } + + err = client.CreateCollection(&spec.Collection{ + Name: "test", + }) + if err != nil { + t.Fatal(err) + } +} + +func TestDGClient_DeleteCollection(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "/api/v1/collection", r.URL.Path) + w.WriteHeader(http.StatusNoContent) + })) + client := dgclient.NewDGateClient() + err := client.Init(server.URL) + if err != nil { + t.Fatal(err) + } + + err = client.DeleteCollection("test", "test") + if err != nil { + t.Fatal(err) + } +} + +func TestDGClient_ListCollection(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "/api/v1/collection", r.URL.Path) + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(&dgclient.ResponseWrapper[[]*spec.Collection]{ + Data: []*spec.Collection{ + { + Name: "test", + }, + }, + }) + })) + client := dgclient.NewDGateClient() + err := client.Init(server.URL) + if err != nil { + t.Fatal(err) + } + + Collections, err := client.ListCollection("test") + if err != nil { + t.Fatal(err) + } + assert.Equal(t, 1, len(Collections)) + assert.Equal(t, "test", Collections[0].Name) +} diff --git a/pkg/dgclient/document_client_test.go b/pkg/dgclient/document_client_test.go new file mode 100644 index 0000000..4892196 --- /dev/null +++ b/pkg/dgclient/document_client_test.go @@ -0,0 +1,115 @@ +package dgclient_test + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + + "github.com/dgate-io/dgate/pkg/dgclient" + "github.com/dgate-io/dgate/pkg/spec" + "github.com/stretchr/testify/assert" +) + +func TestDGClient_GetDocument(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "/api/v1/document/test", r.URL.Path) + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(&dgclient.ResponseWrapper[*spec.Document]{ + Data: &spec.Document{ + ID: "test", + CollectionName: "test", + }, + }) + })) + client := dgclient.NewDGateClient() + err := client.Init(server.URL) + if err != nil { + t.Fatal(err) + } + + Document, err := client.GetDocument("test", "test", "test") + if err != nil { + t.Fatal(err) + } + assert.Equal(t, "test", Document.ID) +} + +func TestDGClient_CreateDocument(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "/api/v1/document", r.URL.Path) + w.WriteHeader(http.StatusCreated) + })) + client := dgclient.NewDGateClient() + err := client.Init(server.URL) + if err != nil { + t.Fatal(err) + } + + err = client.CreateDocument(&spec.Document{ + ID: "test", + }) + if err != nil { + t.Fatal(err) + } +} + +func TestDGClient_DeleteAllDocument(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "/api/v1/document", r.URL.Path) + w.WriteHeader(http.StatusNoContent) + })) + client := dgclient.NewDGateClient() + err := client.Init(server.URL) + if err != nil { + t.Fatal(err) + } + + err = client.DeleteAllDocument("test", "test") + if err != nil { + t.Fatal(err) + } +} + +func TestDGClient_DeleteDocument(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "/api/v1/document/test", r.URL.Path) + w.WriteHeader(http.StatusNoContent) + })) + client := dgclient.NewDGateClient() + err := client.Init(server.URL) + if err != nil { + t.Fatal(err) + } + + err = client.DeleteDocument("test", "test", "test") + if err != nil { + t.Fatal(err) + } +} + +func TestDGClient_ListDocument(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "/api/v1/document", r.URL.Path) + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(&dgclient.ResponseWrapper[[]*spec.Document]{ + Data: []*spec.Document{ + { + ID: "test", + }, + }, + }) + })) + client := dgclient.NewDGateClient() + err := client.Init(server.URL) + if err != nil { + t.Fatal(err) + } + + Documents, err := client.ListDocument("test", "test") + if err != nil { + t.Fatal(err) + } + assert.Equal(t, 1, len(Documents)) + assert.Equal(t, "test", Documents[0].ID) +} diff --git a/pkg/dgclient/domain_client_test.go b/pkg/dgclient/domain_client_test.go new file mode 100644 index 0000000..7cadcae --- /dev/null +++ b/pkg/dgclient/domain_client_test.go @@ -0,0 +1,97 @@ +package dgclient_test + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + + "github.com/dgate-io/dgate/pkg/dgclient" + "github.com/dgate-io/dgate/pkg/spec" + "github.com/stretchr/testify/assert" +) + +func TestDGClient_GetDomain(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "/api/v1/domain/test", r.URL.Path) + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(&dgclient.ResponseWrapper[*spec.Domain]{ + Data: &spec.Domain{ + Name: "test", + }, + }) + })) + client := dgclient.NewDGateClient() + err := client.Init(server.URL) + if err != nil { + t.Fatal(err) + } + + Domain, err := client.GetDomain("test", "test") + if err != nil { + t.Fatal(err) + } + assert.Equal(t, "test", Domain.Name) +} + +func TestDGClient_CreateDomain(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "/api/v1/domain", r.URL.Path) + w.WriteHeader(http.StatusCreated) + })) + client := dgclient.NewDGateClient() + err := client.Init(server.URL) + if err != nil { + t.Fatal(err) + } + + err = client.CreateDomain(&spec.Domain{ + Name: "test", + }) + if err != nil { + t.Fatal(err) + } +} + +func TestDGClient_DeleteDomain(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "/api/v1/domain", r.URL.Path) + w.WriteHeader(http.StatusNoContent) + })) + client := dgclient.NewDGateClient() + err := client.Init(server.URL) + if err != nil { + t.Fatal(err) + } + + err = client.DeleteDomain("test", "test") + if err != nil { + t.Fatal(err) + } +} + +func TestDGClient_ListDomain(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "/api/v1/domain", r.URL.Path) + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(&dgclient.ResponseWrapper[[]*spec.Domain]{ + Data: []*spec.Domain{ + { + Name: "test", + }, + }, + }) + })) + client := dgclient.NewDGateClient() + err := client.Init(server.URL) + if err != nil { + t.Fatal(err) + } + + Domains, err := client.ListDomain("test") + if err != nil { + t.Fatal(err) + } + assert.Equal(t, 1, len(Domains)) + assert.Equal(t, "test", Domains[0].Name) +} diff --git a/pkg/dgclient/module_client_test.go b/pkg/dgclient/module_client_test.go new file mode 100644 index 0000000..9bcc59b --- /dev/null +++ b/pkg/dgclient/module_client_test.go @@ -0,0 +1,97 @@ +package dgclient_test + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + + "github.com/dgate-io/dgate/pkg/dgclient" + "github.com/dgate-io/dgate/pkg/spec" + "github.com/stretchr/testify/assert" +) + +func TestDGClient_GetModule(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "/api/v1/module/test", r.URL.Path) + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(&dgclient.ResponseWrapper[*spec.Module]{ + Data: &spec.Module{ + Name: "test", + }, + }) + })) + client := dgclient.NewDGateClient() + err := client.Init(server.URL) + if err != nil { + t.Fatal(err) + } + + Module, err := client.GetModule("test", "test") + if err != nil { + t.Fatal(err) + } + assert.Equal(t, "test", Module.Name) +} + +func TestDGClient_CreateModule(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "/api/v1/module", r.URL.Path) + w.WriteHeader(http.StatusCreated) + })) + client := dgclient.NewDGateClient() + err := client.Init(server.URL) + if err != nil { + t.Fatal(err) + } + + err = client.CreateModule(&spec.Module{ + Name: "test", + }) + if err != nil { + t.Fatal(err) + } +} + +func TestDGClient_DeleteModule(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "/api/v1/module", r.URL.Path) + w.WriteHeader(http.StatusNoContent) + })) + client := dgclient.NewDGateClient() + err := client.Init(server.URL) + if err != nil { + t.Fatal(err) + } + + err = client.DeleteModule("test", "test") + if err != nil { + t.Fatal(err) + } +} + +func TestDGClient_ListModule(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "/api/v1/module", r.URL.Path) + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(&dgclient.ResponseWrapper[[]*spec.Module]{ + Data: []*spec.Module{ + { + Name: "test", + }, + }, + }) + })) + client := dgclient.NewDGateClient() + err := client.Init(server.URL) + if err != nil { + t.Fatal(err) + } + + Modules, err := client.ListModule("test") + if err != nil { + t.Fatal(err) + } + assert.Equal(t, 1, len(Modules)) + assert.Equal(t, "test", Modules[0].Name) +} diff --git a/pkg/dgclient/namespace_client_test.go b/pkg/dgclient/namespace_client_test.go new file mode 100644 index 0000000..d761889 --- /dev/null +++ b/pkg/dgclient/namespace_client_test.go @@ -0,0 +1,97 @@ +package dgclient_test + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + + "github.com/dgate-io/dgate/pkg/dgclient" + "github.com/dgate-io/dgate/pkg/spec" + "github.com/stretchr/testify/assert" +) + +func TestDGClient_GetNamespace(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "/api/v1/namespace/test", r.URL.Path) + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(&dgclient.ResponseWrapper[*spec.Namespace]{ + Data: &spec.Namespace{ + Name: "test", + }, + }) + })) + client := dgclient.NewDGateClient() + err := client.Init(server.URL) + if err != nil { + t.Fatal(err) + } + + Namespace, err := client.GetNamespace("test") + if err != nil { + t.Fatal(err) + } + assert.Equal(t, "test", Namespace.Name) +} + +func TestDGClient_CreateNamespace(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "/api/v1/namespace", r.URL.Path) + w.WriteHeader(http.StatusCreated) + })) + client := dgclient.NewDGateClient() + err := client.Init(server.URL) + if err != nil { + t.Fatal(err) + } + + err = client.CreateNamespace(&spec.Namespace{ + Name: "test", + }) + if err != nil { + t.Fatal(err) + } +} + +func TestDGClient_DeleteNamespace(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "/api/v1/namespace", r.URL.Path) + w.WriteHeader(http.StatusNoContent) + })) + client := dgclient.NewDGateClient() + err := client.Init(server.URL) + if err != nil { + t.Fatal(err) + } + + err = client.DeleteNamespace("test") + if err != nil { + t.Fatal(err) + } +} + +func TestDGClient_ListNamespace(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "/api/v1/namespace", r.URL.Path) + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(&dgclient.ResponseWrapper[[]*spec.Namespace]{ + Data: []*spec.Namespace{ + { + Name: "test", + }, + }, + }) + })) + client := dgclient.NewDGateClient() + err := client.Init(server.URL) + if err != nil { + t.Fatal(err) + } + + Namespaces, err := client.ListNamespace() + if err != nil { + t.Fatal(err) + } + assert.Equal(t, 1, len(Namespaces)) + assert.Equal(t, "test", Namespaces[0].Name) +} diff --git a/pkg/dgclient/secret_client_test.go b/pkg/dgclient/secret_client_test.go new file mode 100644 index 0000000..2ab769a --- /dev/null +++ b/pkg/dgclient/secret_client_test.go @@ -0,0 +1,97 @@ +package dgclient_test + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + + "github.com/dgate-io/dgate/pkg/dgclient" + "github.com/dgate-io/dgate/pkg/spec" + "github.com/stretchr/testify/assert" +) + +func TestDGClient_GetSecret(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "/api/v1/secret/test", r.URL.Path) + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(&dgclient.ResponseWrapper[*spec.Secret]{ + Data: &spec.Secret{ + Name: "test", + }, + }) + })) + client := dgclient.NewDGateClient() + err := client.Init(server.URL) + if err != nil { + t.Fatal(err) + } + + Secret, err := client.GetSecret("test", "test") + if err != nil { + t.Fatal(err) + } + assert.Equal(t, "test", Secret.Name) +} + +func TestDGClient_CreateSecret(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "/api/v1/secret", r.URL.Path) + w.WriteHeader(http.StatusCreated) + })) + client := dgclient.NewDGateClient() + err := client.Init(server.URL) + if err != nil { + t.Fatal(err) + } + + err = client.CreateSecret(&spec.Secret{ + Name: "test", + }) + if err != nil { + t.Fatal(err) + } +} + +func TestDGClient_DeleteSecret(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "/api/v1/secret", r.URL.Path) + w.WriteHeader(http.StatusNoContent) + })) + client := dgclient.NewDGateClient() + err := client.Init(server.URL) + if err != nil { + t.Fatal(err) + } + + err = client.DeleteSecret("test", "test") + if err != nil { + t.Fatal(err) + } +} + +func TestDGClient_ListSecret(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "/api/v1/secret", r.URL.Path) + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(&dgclient.ResponseWrapper[[]*spec.Secret]{ + Data: []*spec.Secret{ + { + Name: "test", + }, + }, + }) + })) + client := dgclient.NewDGateClient() + err := client.Init(server.URL) + if err != nil { + t.Fatal(err) + } + + Secrets, err := client.ListSecret("test") + if err != nil { + t.Fatal(err) + } + assert.Equal(t, 1, len(Secrets)) + assert.Equal(t, "test", Secrets[0].Name) +} diff --git a/pkg/dgclient/service_client_test.go b/pkg/dgclient/service_client_test.go new file mode 100644 index 0000000..2ba4483 --- /dev/null +++ b/pkg/dgclient/service_client_test.go @@ -0,0 +1,97 @@ +package dgclient_test + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + + "github.com/dgate-io/dgate/pkg/dgclient" + "github.com/dgate-io/dgate/pkg/spec" + "github.com/stretchr/testify/assert" +) + +func TestDGClient_GetService(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "/api/v1/service/test", r.URL.Path) + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(&dgclient.ResponseWrapper[*spec.Service]{ + Data: &spec.Service{ + Name: "test", + }, + }) + })) + client := dgclient.NewDGateClient() + err := client.Init(server.URL) + if err != nil { + t.Fatal(err) + } + + Service, err := client.GetService("test", "test") + if err != nil { + t.Fatal(err) + } + assert.Equal(t, "test", Service.Name) +} + +func TestDGClient_CreateService(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "/api/v1/service", r.URL.Path) + w.WriteHeader(http.StatusCreated) + })) + client := dgclient.NewDGateClient() + err := client.Init(server.URL) + if err != nil { + t.Fatal(err) + } + + err = client.CreateService(&spec.Service{ + Name: "test", + }) + if err != nil { + t.Fatal(err) + } +} + +func TestDGClient_DeleteService(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "/api/v1/service", r.URL.Path) + w.WriteHeader(http.StatusNoContent) + })) + client := dgclient.NewDGateClient() + err := client.Init(server.URL) + if err != nil { + t.Fatal(err) + } + + err = client.DeleteService("test", "test") + if err != nil { + t.Fatal(err) + } +} + +func TestDGClient_ListService(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "/api/v1/service", r.URL.Path) + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(&dgclient.ResponseWrapper[[]*spec.Service]{ + Data: []*spec.Service{ + { + Name: "test", + }, + }, + }) + })) + client := dgclient.NewDGateClient() + err := client.Init(server.URL) + if err != nil { + t.Fatal(err) + } + + Services, err := client.ListService("test") + if err != nil { + t.Fatal(err) + } + assert.Equal(t, 1, len(Services)) + assert.Equal(t, "test", Services[0].Name) +}