From b4b625ddb1258aad0c203622fde0d3efcb762809 Mon Sep 17 00:00:00 2001 From: Ian Ngethe Muchiri <100555904+ianmuchyri@users.noreply.github.com> Date: Tue, 8 Aug 2023 15:42:32 +0300 Subject: [PATCH] NOISSUE- update health method in SDK (#1881) * update health function in sdk Signed-off-by: ianmuchyri * update bootstrap name Signed-off-by: ianmuchyri * update health_test Signed-off-by: ianmuchyri * update sdk.go comment Signed-off-by: ianmuchyri * update cli/README Signed-off-by: ianmuchyri * update health_test Signed-off-by: ianmuchyri * remove duplicate import Signed-off-by: ianmuchyri --------- Signed-off-by: ianmuchyri --- cli/README.md | 90 +++++++++++++++++++-------------------- cli/health.go | 14 ++++-- pkg/sdk/go/health.go | 18 +++++++- pkg/sdk/go/health_test.go | 81 ++++++++++++++++++++++++----------- pkg/sdk/go/sdk.go | 6 +-- 5 files changed, 129 insertions(+), 80 deletions(-) diff --git a/cli/README.md b/cli/README.md index d3a7cc4b..6622562c 100644 --- a/cli/README.md +++ b/cli/README.md @@ -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 ``` ### Users management @@ -90,8 +90,8 @@ mainflux-cli things create '{"name":"myThing", "metadata": {"key1":"value1"}}' < mainflux-cli provision things ``` -* `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: @@ -106,22 +106,23 @@ in which the first column is the thing's name. A comparable JSON file would be ```json -[{ - "name": "", - "status": "enabled" - }, - { - "name": "", - "status": "disabled" - }, { - - "name": "", - "status": "enabled", - "credentials": { - "identity": "", - "secret": "" - } +[ + { + "name": "", + "status": "enabled" + }, + { + "name": "", + "status": "disabled" + }, + { + "name": "", + "status": "enabled", + "credentials": { + "identity": "", + "secret": "" } + } ] ``` @@ -181,8 +182,8 @@ mainflux-cli channels create '{"name":"myChannel"}' mainflux-cli provision channels ``` -* `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: @@ -197,21 +198,22 @@ in which the first column is channel names. A comparable JSON file would be ```json -[{ - "name": "", - "description": "", - "status": "enabled" - }, - { - "name": "", - "description": "", - "status": "disabled" - }, { - - "name": "", - "description": "", - "status": "enabled" - } +[ + { + "name": "", + "description": "", + "status": "enabled" + }, + { + "name": "", + "description": "", + "status": "disabled" + }, + { + "name": "", + "description": "", + "status": "enabled" + } ] ``` @@ -267,8 +269,8 @@ mainflux-cli things connect mainflux-cli provision connect ``` -* `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 @@ -277,20 +279,14 @@ An example CSV file might be , ``` -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": [ - "", - "" - ], - "group_ids": [ - "", - "" - ] + "client_ids": ["", ""], + "group_ids": ["", ""] } ``` diff --git a/cli/health.go b/cli/health.go index 11318c2a..29422da8 100644 --- a/cli/health.go +++ b/cli/health.go @@ -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 ", 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 ", + 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 diff --git a/pkg/sdk/go/health.go b/pkg/sdk/go/health.go index 3a27c6b5..6d218ca5 100644 --- a/pkg/sdk/go/health.go +++ b/pkg/sdk/go/health.go @@ -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 { diff --git a/pkg/sdk/go/health_test.go b/pkg/sdk/go/health_test.go index 532dc34c..3d846a62 100644 --- a/pkg/sdk/go/health_test.go +++ b/pkg/sdk/go/health_test.go @@ -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) - pRepo := new(pmocks.Repository) - psvc := policies.NewService(uauth, pRepo, policiesCache, idProvider) + thingspRepo := new(thingspmocks.Repository) + psvc := policies.NewService(uauth, thingspRepo, policiesCache, idProvider) - svc := clients.NewService(uauth, psvc, cRepo, gRepo, thingCache, idProvider) - ts := newThingsServer(svc, psvc) - defer ts.Close() + thsvc := thingsclients.NewService(uauth, psvc, thingcRepo, gRepo, thingCache, idProvider) + ths := newThingsServer(thsvc, psvc) + defer ths.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)) } } diff --git a/pkg/sdk/go/sdk.go b/pkg/sdk/go/sdk.go index a411bc99..69fb13d0 100644 --- a/pkg/sdk/go/sdk.go +++ b/pkg/sdk/go/sdk.go @@ -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 //