Skip to content

Commit

Permalink
Merge pull request #198 from mackerelio/add-services-package
Browse files Browse the repository at this point in the history
Create services package and add tests for services subcommand
  • Loading branch information
itchyny committed Mar 14, 2019
2 parents d046f81 + ae712f4 commit 06bdf4c
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 22 deletions.
22 changes: 2 additions & 20 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/mackerelio/mkr/mackerelclient"
"github.com/mackerelio/mkr/org"
"github.com/mackerelio/mkr/plugin"
"github.com/mackerelio/mkr/services"
"github.com/mackerelio/mkr/wrap"
cli "gopkg.in/urfave/cli.v1"
)
Expand All @@ -29,7 +30,7 @@ var Commands = []cli.Command{
commandMetrics,
commandFetch,
commandRetire,
commandServices,
services.Command,
commandMonitors,
commandAlerts,
commandDashboards,
Expand Down Expand Up @@ -147,18 +148,6 @@ var commandRetire = cli.Command{
},
}

var commandServices = cli.Command{
Name: "services",
Usage: "List services",
ArgsUsage: "",
Description: `
List the information of the services.
Requests "GET /api/v0/services". See https://mackerel.io/api-docs/entry/services#list.
`,
Action: doServices,
Flags: []cli.Flag{},
}

func doStatus(c *cli.Context) error {
confFile := c.GlobalString("conf")
argHostID := c.Args().Get(0)
Expand Down Expand Up @@ -386,10 +375,3 @@ func doRetire(c *cli.Context) error {
}
return nil
}

func doServices(c *cli.Context) error {
services, err := mackerelclient.NewFromContext(c).FindServices()
logger.DieIf(err)
format.PrettyPrintJSON(os.Stdout, services)
return nil
}
1 change: 1 addition & 0 deletions mackerelclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ import mackerel "github.com/mackerelio/mackerel-client-go"
// Client represents a client of Mackerel API
type Client interface {
FindHosts(param *mackerel.FindHostsParam) ([]*mackerel.Host, error)
FindServices() ([]*mackerel.Service, error)
GetOrg() (*mackerel.Org, error)
}
20 changes: 18 additions & 2 deletions mackerelclient/mock_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import mackerel "github.com/mackerelio/mackerel-client-go"

// MockClient represents a mock client of Mackerel API
type MockClient struct {
findHostsCallback func(param *mackerel.FindHostsParam) ([]*mackerel.Host, error)
getOrgCallback func() (*mackerel.Org, error)
findHostsCallback func(param *mackerel.FindHostsParam) ([]*mackerel.Host, error)
findServicesCallback func() ([]*mackerel.Service, error)
getOrgCallback func() (*mackerel.Org, error)
}

// MockClientOption represents an option of mock client of Mackerel API
Expand Down Expand Up @@ -46,6 +47,21 @@ func MockFindHosts(callback func(param *mackerel.FindHostsParam) ([]*mackerel.Ho
}
}

// FindServices ...
func (c *MockClient) FindServices() ([]*mackerel.Service, error) {
if c.findServicesCallback != nil {
return c.findServicesCallback()
}
return nil, errCallbackNotFound("FindServices")
}

// MockFindServices returns an option to set the callback of FindServices
func MockFindServices(callback func() ([]*mackerel.Service, error)) MockClientOption {
return func(c *MockClient) {
c.findServicesCallback = callback
}
}

// GetOrg ...
func (c *MockClient) GetOrg() (*mackerel.Org, error) {
if c.getOrgCallback != nil {
Expand Down
23 changes: 23 additions & 0 deletions services/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package services

import (
"io"

"github.com/mackerelio/mkr/format"
"github.com/mackerelio/mkr/mackerelclient"
)

type servicesApp struct {
client mackerelclient.Client
outStream io.Writer
}

func (app *servicesApp) run() error {
services, err := app.client.FindServices()
if err != nil {
return err
}

format.PrettyPrintJSON(app.outStream, services)
return nil
}
87 changes: 87 additions & 0 deletions services/app_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package services

import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"

mackerel "github.com/mackerelio/mackerel-client-go"

"github.com/mackerelio/mkr/mackerelclient"
)

func TestServicesApp_Run(t *testing.T) {
testCases := []struct {
id string
services []*mackerel.Service
expected string
}{
{
id: "default",
services: []*mackerel.Service{
&mackerel.Service{
Name: "sample-service-1",
Memo: "sample memo 1",
Roles: []string{"role1", "role2", "role3"},
},
&mackerel.Service{
Name: "sample-service-2",
Memo: "sample memo 2",
Roles: []string{"role"},
},
&mackerel.Service{
Name: "sample-service-3",
Memo: "",
Roles: []string{},
},
},
expected: `[
{
"name": "sample-service-1",
"memo": "sample memo 1",
"roles": [
"role1",
"role2",
"role3"
]
},
{
"name": "sample-service-2",
"memo": "sample memo 2",
"roles": [
"role"
]
},
{
"name": "sample-service-3",
"memo": "",
"roles": []
}
]
`,
},
{
id: "no services",
services: []*mackerel.Service{},
expected: `[]
`,
},
}
for _, tc := range testCases {
client := mackerelclient.NewMockClient(
mackerelclient.MockFindServices(func() ([]*mackerel.Service, error) {
return tc.services, nil
}),
)
t.Run(tc.id, func(t *testing.T) {
out := new(bytes.Buffer)
app := &servicesApp{
client: client,
outStream: out,
}
assert.NoError(t, app.run())
assert.Equal(t, tc.expected, out.String())
})
}
}
33 changes: 33 additions & 0 deletions services/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package services

import (
"os"

cli "gopkg.in/urfave/cli.v1"

"github.com/mackerelio/mkr/mackerelclient"
)

// Command is the definition of services subcommand
var Command = cli.Command{
Name: "services",
Usage: "List services",
ArgsUsage: "",
Description: `
List the information of the services.
Requests "GET /api/v0/services". See https://mackerel.io/api-docs/entry/services#list.
`,
Action: doServices,
}

func doServices(c *cli.Context) error {
client, err := mackerelclient.New(c.GlobalString("conf"), c.GlobalString("apibase"))
if err != nil {
return err
}

return (&servicesApp{
client: client,
outStream: os.Stdout,
}).run()
}

0 comments on commit 06bdf4c

Please sign in to comment.