Skip to content

Commit

Permalink
NOISSUE- update health method in SDK (#1881)
Browse files Browse the repository at this point in the history
* update health function in sdk

Signed-off-by: ianmuchyri <ianmuchiri8@gmail.com>

* update bootstrap name

Signed-off-by: ianmuchyri <ianmuchiri8@gmail.com>

* update health_test

Signed-off-by: ianmuchyri <ianmuchiri8@gmail.com>

* update sdk.go comment

Signed-off-by: ianmuchyri <ianmuchiri8@gmail.com>

* update cli/README

Signed-off-by: ianmuchyri <ianmuchiri8@gmail.com>

* update health_test

Signed-off-by: ianmuchyri <ianmuchiri8@gmail.com>

* remove duplicate import

Signed-off-by: ianmuchyri <ianmuchiri8@gmail.com>

---------

Signed-off-by: ianmuchyri <ianmuchiri8@gmail.com>
  • Loading branch information
ianmuchyri authored Aug 8, 2023
1 parent 06800c1 commit b4b625d
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 80 deletions.
90 changes: 43 additions & 47 deletions cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ make cli

### Service

#### Get Mainflux Things services Health Check
#### Get Mainflux Services Health Check

```bash
mainflux-cli health
mainflux-cli health <service>
```

### Users management
Expand Down Expand Up @@ -90,8 +90,8 @@ mainflux-cli things create '{"name":"myThing", "metadata": {"key1":"value1"}}' <
mainflux-cli provision things <file> <user_token>
```

* `file` - A CSV or JSON file containing thing names (must have extension `.csv` or `.json`)
* `user_token` - A valid user auth token for the current system
- `file` - A CSV or JSON file containing thing names (must have extension `.csv` or `.json`)
- `user_token` - A valid user auth token for the current system

An example CSV file might be:

Expand All @@ -106,22 +106,23 @@ in which the first column is the thing's name.
A comparable JSON file would be

```json
[{
"name": "<thing1_name>",
"status": "enabled"
},
{
"name": "<thing2_name>",
"status": "disabled"
}, {

"name": "<thing3_name>",
"status": "enabled",
"credentials": {
"identity": "<thing3_identity>",
"secret": "<thing3_secret>"
}
[
{
"name": "<thing1_name>",
"status": "enabled"
},
{
"name": "<thing2_name>",
"status": "disabled"
},
{
"name": "<thing3_name>",
"status": "enabled",
"credentials": {
"identity": "<thing3_identity>",
"secret": "<thing3_secret>"
}
}
]
```

Expand Down Expand Up @@ -181,8 +182,8 @@ mainflux-cli channels create '{"name":"myChannel"}' <user_token>
mainflux-cli provision channels <file> <user_token>
```

* `file` - A CSV or JSON file containing channel names (must have extension `.csv` or `.json`)
* `user_token` - A valid user auth token for the current system
- `file` - A CSV or JSON file containing channel names (must have extension `.csv` or `.json`)
- `user_token` - A valid user auth token for the current system

An example CSV file might be:

Expand All @@ -197,21 +198,22 @@ in which the first column is channel names.
A comparable JSON file would be

```json
[{
"name": "<channel1_name>",
"description": "<channel1_description>",
"status": "enabled"
},
{
"name": "<channel2_name>",
"description": "<channel2_description>",
"status": "disabled"
}, {

"name": "<channel3_name>",
"description": "<channel3_description>",
"status": "enabled"
}
[
{
"name": "<channel1_name>",
"description": "<channel1_description>",
"status": "enabled"
},
{
"name": "<channel2_name>",
"description": "<channel2_description>",
"status": "disabled"
},
{
"name": "<channel3_name>",
"description": "<channel3_description>",
"status": "enabled"
}
]
```

Expand Down Expand Up @@ -267,8 +269,8 @@ mainflux-cli things connect <thing_id> <channel_id> <user_token>
mainflux-cli provision connect <file> <user_token>
```

* `file` - A CSV or JSON file containing thing and channel ids (must have extension `.csv` or `.json`)
* `user_token` - A valid user auth token for the current system
- `file` - A CSV or JSON file containing thing and channel ids (must have extension `.csv` or `.json`)
- `user_token` - A valid user auth token for the current system

An example CSV file might be

Expand All @@ -277,20 +279,14 @@ An example CSV file might be
<thing_id2>,<channel_id2>
```

in which the first column is thing IDs and the second column is channel IDs. A connection will be created for each thing to each channel. This example would result in 4 connections being created.
in which the first column is thing IDs and the second column is channel IDs. A connection will be created for each thing to each channel. This example would result in 4 connections being created.

A comparable JSON file would be

```json
{
"client_ids": [
"<thing_id1>",
"<thing_id2>"
],
"group_ids": [
"<channel_id1>",
"<channel_id2>"
]
"client_ids": ["<thing_id1>", "<thing_id2>"],
"group_ids": ["<channel_id1>", "<channel_id2>"]
}
```

Expand Down
14 changes: 10 additions & 4 deletions cli/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ import "github.com/spf13/cobra"
// NewHealthCmd returns health check command.
func NewHealthCmd() *cobra.Command {
return &cobra.Command{
Use: "health",
Use: "health <service>",
Short: "Health Check",
Long: `Mainflux Things service Health Check`,
Run: func(_ *cobra.Command, _ []string) {
v, err := sdk.Health()
Long: "Mainflux service Health Check\n" +
"usage:\n" +
"\tmainflux-cli health <service>",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
logUsage(cmd.Use)
return
}
v, err := sdk.Health(args[0])
if err != nil {
logError(err)
return
Expand Down
18 changes: 16 additions & 2 deletions pkg/sdk/go/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,22 @@ type HealthInfo struct {
BuildTime string `json:"build_time"`
}

func (sdk mfSDK) Health() (HealthInfo, errors.SDKError) {
url := fmt.Sprintf("%s/health", sdk.thingsURL)
func (sdk mfSDK) Health(service string) (HealthInfo, errors.SDKError) {
var url string
switch service {
case "things":
url = fmt.Sprintf("%s/health", sdk.thingsURL)
case "users":
url = fmt.Sprintf("%s/health", sdk.usersURL)
case "bootstrap":
url = fmt.Sprintf("%s/health", sdk.bootstrapURL)
case "certs":
url = fmt.Sprintf("%s/health", sdk.certsURL)
case "reader":
url = fmt.Sprintf("%s/health", sdk.readerURL)
case "http-adapter":
url = fmt.Sprintf("%s/health", sdk.httpAdapterURL)
}

resp, err := sdk.client.Get(url)
if err != nil {
Expand Down
81 changes: 57 additions & 24 deletions pkg/sdk/go/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,57 +10,90 @@ import (
"github.com/mainflux/mainflux"
"github.com/mainflux/mainflux/pkg/errors"
sdk "github.com/mainflux/mainflux/pkg/sdk/go"
"github.com/mainflux/mainflux/things/clients"
"github.com/mainflux/mainflux/things/clients/mocks"
thingsclients "github.com/mainflux/mainflux/things/clients"
thingsclientsmock "github.com/mainflux/mainflux/things/clients/mocks"
gmocks "github.com/mainflux/mainflux/things/groups/mocks"
"github.com/mainflux/mainflux/things/policies"
pmocks "github.com/mainflux/mainflux/things/policies/mocks"
thingspmocks "github.com/mainflux/mainflux/things/policies/mocks"
usersclients "github.com/mainflux/mainflux/users/clients"
cmocks "github.com/mainflux/mainflux/users/clients/mocks"
"github.com/mainflux/mainflux/users/jwt"
userspmocks "github.com/mainflux/mainflux/users/policies/mocks"
"github.com/stretchr/testify/assert"
)

const (
thingsDescription = "things service"
thingsStatus = "pass"
"github.com/stretchr/testify/require"
)

func TestHealth(t *testing.T) {
cRepo := new(mocks.Repository)
thingcRepo := new(thingsclientsmock.Repository)
usercRepo := new(cmocks.Repository)
gRepo := new(gmocks.Repository)
uauth := cmocks.NewAuthService(users, map[string][]cmocks.SubjectSet{adminID: {uadminPolicy}})
thingCache := mocks.NewCache()
policiesCache := pmocks.NewCache()
thingCache := thingsclientsmock.NewCache()
policiesCache := thingspmocks.NewCache()
tokenizer := jwt.NewRepository([]byte(secret), accessDuration, refreshDuration)

thingspRepo := new(thingspmocks.Repository)
psvc := policies.NewService(uauth, thingspRepo, policiesCache, idProvider)

pRepo := new(pmocks.Repository)
psvc := policies.NewService(uauth, pRepo, policiesCache, idProvider)
thsvc := thingsclients.NewService(uauth, psvc, thingcRepo, gRepo, thingCache, idProvider)
ths := newThingsServer(thsvc, psvc)
defer ths.Close()

svc := clients.NewService(uauth, psvc, cRepo, gRepo, thingCache, idProvider)
ts := newThingsServer(svc, psvc)
defer ts.Close()
userspRepo := new(userspmocks.Repository)
usSvc := usersclients.NewService(usercRepo, userspRepo, tokenizer, emailer, phasher, idProvider, passRegex)
usclsv := newClientServer(usSvc)
defer usclsv.Close()

certSvc, err := newCertService()
require.Nil(t, err, fmt.Sprintf("unexpected error during creating service: %s", err))
CertTs := newCertServer(certSvc)
defer CertTs.Close()

sdkConf := sdk.Config{
ThingsURL: ts.URL,
ThingsURL: ths.URL,
UsersURL: usclsv.URL,
CertsURL: CertTs.URL,
MsgContentType: contentType,
TLSVerification: false,
}

mfsdk := sdk.NewSDK(sdkConf)
cases := map[string]struct {
empty bool
err errors.SDKError
service string
empty bool
description string
status string
err errors.SDKError
}{
"get things service health check": {
empty: false,
err: nil,
service: "things",
empty: false,
err: nil,
description: "things service",
status: "pass",
},
"get users service health check": {
service: "users",
empty: false,
err: nil,
description: "users service",
status: "pass",
},
"get certs service health check": {
service: "certs",
empty: false,
err: nil,
description: "certs service",
status: "pass",
},
}
for desc, tc := range cases {
h, err := mfsdk.Health()
h, err := mfsdk.Health(tc.service)
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected error %s, got %s", desc, tc.err, err))
assert.Equal(t, thingsStatus, h.Status, fmt.Sprintf("%s: expected %s status, got %s", desc, thingsStatus, h.Status))
assert.Equal(t, tc.status, h.Status, fmt.Sprintf("%s: expected %s status, got %s", desc, tc.status, h.Status))
assert.Equal(t, tc.empty, h.Version == "", fmt.Sprintf("%s: expected non-empty version", desc))
assert.Equal(t, mainflux.Commit, h.Commit, fmt.Sprintf("%s: expected non-empty commit", desc))
assert.Equal(t, thingsDescription, h.Description, fmt.Sprintf("%s: expected proper description, got %s", desc, h.Description))
assert.Equal(t, tc.description, h.Description, fmt.Sprintf("%s: expected proper description, got %s", desc, h.Description))
assert.Equal(t, mainflux.BuildTime, h.BuildTime, fmt.Sprintf("%s: expected default epoch date, got %s", desc, h.BuildTime))
}
}
6 changes: 3 additions & 3 deletions pkg/sdk/go/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -804,12 +804,12 @@ type SDK interface {
// fmt.Println(err)
SetContentType(ct ContentType) errors.SDKError

// Health returns things service health check.
// Health returns service health check.
//
// example:
// health, _ := sdk.Health()
// health, _ := sdk.Health("service")
// fmt.Println(health)
Health() (HealthInfo, errors.SDKError)
Health(service string) (HealthInfo, errors.SDKError)

// AddBootstrap add bootstrap configuration
//
Expand Down

0 comments on commit b4b625d

Please sign in to comment.