NOISSUE - Fix provision test command (#2182)

Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
This commit is contained in:
b1ackd0t
2024-04-17 10:40:08 +03:00
committed by GitHub
parent 28b4087b69
commit 3cfcf14a50
11 changed files with 278 additions and 204 deletions
+11 -1
View File
@@ -26,7 +26,7 @@ const (
defDomainsURL string = defURL + ":8189"
defCertsURL string = defURL + ":9019"
defInvitationsURL string = defURL + ":9020"
defHTTPURL string = defURL + ":9016/http"
defHTTPURL string = defURL + ":8008"
defTLSVerification bool = false
defOffset string = "0"
defLimit string = "10"
@@ -42,6 +42,7 @@ type remotes struct {
HTTPAdapterURL string `toml:"http_adapter_url"`
BootstrapURL string `toml:"bootstrap_url"`
CertsURL string `toml:"certs_url"`
InvitationsURL string `toml:"invitations_url"`
TLSVerification bool `toml:"tls_verification"`
}
@@ -110,6 +111,7 @@ func ParseConfig(sdkConf mgxsdk.Config) (mgxsdk.Config, error) {
HTTPAdapterURL: defHTTPURL,
BootstrapURL: defBootstrapURL,
CertsURL: defCertsURL,
InvitationsURL: defInvitationsURL,
TLSVerification: defTLSVerification,
},
Filter: filter{
@@ -176,6 +178,10 @@ func ParseConfig(sdkConf mgxsdk.Config) (mgxsdk.Config, error) {
sdkConf.ReaderURL = config.Remotes.ReaderURL
}
if sdkConf.DomainsURL == "" && config.Remotes.DomainsURL != "" {
sdkConf.DomainsURL = config.Remotes.DomainsURL
}
if sdkConf.HTTPAdapterURL == "" && config.Remotes.HTTPAdapterURL != "" {
sdkConf.HTTPAdapterURL = config.Remotes.HTTPAdapterURL
}
@@ -188,6 +194,10 @@ func ParseConfig(sdkConf mgxsdk.Config) (mgxsdk.Config, error) {
sdkConf.CertsURL = config.Remotes.CertsURL
}
if sdkConf.InvitationsURL == "" && config.Remotes.InvitationsURL != "" {
sdkConf.InvitationsURL = config.Remotes.InvitationsURL
}
sdkConf.TLSVerification = config.Remotes.TLSVerification || sdkConf.TLSVerification
return sdkConf, nil
+55 -17
View File
@@ -14,8 +14,8 @@ import (
"path/filepath"
"time"
"github.com/0x6flab/namegenerator"
mgxsdk "github.com/absmach/magistrala/pkg/sdk/go"
"github.com/docker/docker/pkg/namesgenerator"
"github.com/spf13/cobra"
)
@@ -24,6 +24,11 @@ const (
csvExt = ".csv"
)
var (
msgFormat = `[{"bn":"provision:", "bu":"V", "t": %d, "bver":5, "n":"voltage", "u":"V", "v":%d}]`
namesgenerator = namegenerator.NewGenerator()
)
var cmdProvision = []cobra.Command{
{
Use: "things <things_file> <user_token>",
@@ -71,11 +76,16 @@ var cmdProvision = []cobra.Command{
return
}
channels, err = sdk.CreateChannels(channels, args[1])
if err != nil {
logError(err)
return
var chs []mgxsdk.Channel
for _, c := range channels {
c, err = sdk.CreateChannel(c, args[1])
if err != nil {
logError(err)
return
}
chs = append(chs, c)
}
channels = chs
logJSON(channels)
},
@@ -122,9 +132,8 @@ var cmdProvision = []cobra.Command{
return
}
rand.Seed(time.Now().UnixNano())
name := namesgenerator.GetRandomName(0)
// Create test user
name := namesgenerator.Generate()
user := mgxsdk.User{
Name: name,
Credentials: mgxsdk.Credentials{
@@ -146,11 +155,28 @@ var cmdProvision = []cobra.Command{
return
}
// create domain
domain := mgxsdk.Domain{
Name: fmt.Sprintf("%s-domain", name),
Status: mgxsdk.EnabledStatus,
}
domain, err = sdk.CreateDomain(domain, ut.AccessToken)
if err != nil {
logError(err)
return
}
// domain login
ut, err = sdk.CreateToken(mgxsdk.Login{Identity: user.Credentials.Identity, Secret: user.Credentials.Secret, DomainID: domain.ID})
if err != nil {
logError(err)
return
}
// Create things
for i := 0; i < numThings; i++ {
n := fmt.Sprintf("d%d", i)
t := mgxsdk.Thing{
Name: n,
Name: fmt.Sprintf("%s-thing-%d", name, i),
Status: mgxsdk.EnabledStatus,
}
@@ -164,20 +190,18 @@ var cmdProvision = []cobra.Command{
// Create channels
for i := 0; i < numChan; i++ {
n := fmt.Sprintf("c%d", i)
c := mgxsdk.Channel{
Name: n,
Name: fmt.Sprintf("%s-channel-%d", name, i),
Status: mgxsdk.EnabledStatus,
}
c, err = sdk.CreateChannel(c, ut.AccessToken)
if err != nil {
logError(err)
return
}
channels = append(channels, c)
}
channels, err = sdk.CreateChannels(channels, ut.AccessToken)
if err != nil {
logError(err)
return
}
// Connect things to channels - first thing to both channels, second only to first
conIDs := mgxsdk.Connection{
@@ -207,6 +231,20 @@ var cmdProvision = []cobra.Command{
return
}
// send message to test connectivity
if err := sdk.SendMessage(channels[0].ID, fmt.Sprintf(msgFormat, time.Now().Unix(), rand.Int()), things[0].Credentials.Secret); err != nil {
logError(err)
return
}
if err := sdk.SendMessage(channels[0].ID, fmt.Sprintf(msgFormat, time.Now().Unix(), rand.Int()), things[1].Credentials.Secret); err != nil {
logError(err)
return
}
if err := sdk.SendMessage(channels[1].ID, fmt.Sprintf(msgFormat, time.Now().Unix(), rand.Int()), things[0].Credentials.Secret); err != nil {
logError(err)
return
}
logJSON(user, ut, things, channels)
},
},
+2 -1
View File
@@ -13,7 +13,8 @@ user_token = ""
bootstrap_url = "http://localhost:9013"
certs_url = "http://localhost:9019"
domains_url = "http://localhost:8189"
http_adapter_url = "http://localhost:9016/http"
http_adapter_url = "http://localhost:8008"
invitations_url = "http://localhost:9020"
reader_url = "http://localhost:9011"
things_url = "http://localhost:9000"
tls_verification = false
+1 -1
View File
@@ -11,7 +11,6 @@ require (
github.com/authzed/grpcutil v0.0.0-20240123194739-2ea1e3d2d98b
github.com/caarlos0/env/v10 v10.0.0
github.com/cenkalti/backoff/v4 v4.3.0
github.com/docker/docker v26.0.1+incompatible
github.com/eclipse/paho.mqtt.golang v1.4.3
github.com/fatih/color v1.16.0
github.com/fiorix/go-smpp v0.0.0-20210403173735-2894b96e70ba
@@ -82,6 +81,7 @@ require (
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/docker/cli v26.0.0+incompatible // indirect
github.com/docker/docker v26.0.1+incompatible // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/dsnet/golib/memfile v1.0.0 // indirect
-21
View File
@@ -51,27 +51,6 @@ func (sdk mgSDK) CreateChannel(c Channel, token string) (Channel, errors.SDKErro
return c, nil
}
func (sdk mgSDK) CreateChannels(chs []Channel, token string) ([]Channel, errors.SDKError) {
data, err := json.Marshal(chs)
if err != nil {
return []Channel{}, errors.NewSDKError(err)
}
url := fmt.Sprintf("%s/%s/%s", sdk.thingsURL, channelsEndpoint, "bulk")
_, body, sdkerr := sdk.processRequest(http.MethodPost, url, token, data, nil, http.StatusOK)
if sdkerr != nil {
return []Channel{}, sdkerr
}
var ccr createChannelsRes
if err := json.Unmarshal(body, &ccr); err != nil {
return []Channel{}, errors.NewSDKError(err)
}
return ccr.Channels, nil
}
func (sdk mgSDK) Channels(pm PageMetadata, token string) (ChannelsPage, errors.SDKError) {
url, err := sdk.withQueryParams(sdk.thingsURL, channelsEndpoint, pm)
if err != nil {
+1 -1
View File
@@ -38,7 +38,7 @@ func (sdk mgSDK) CreateDomain(domain Domain, token string) (Domain, errors.SDKEr
url := fmt.Sprintf("%s/%s", sdk.domainsURL, domainsEndpoint)
_, body, sdkerr := sdk.processRequest(http.MethodPost, url, token, data, nil, http.StatusOK)
_, body, sdkerr := sdk.processRequest(http.MethodPost, url, token, data, nil, http.StatusCreated)
if sdkerr != nil {
return Domain{}, sdkerr
}
-4
View File
@@ -13,10 +13,6 @@ type createThingsRes struct {
Things []Thing `json:"things"`
}
type createChannelsRes struct {
Channels []Channel `json:"channels"`
}
type pageRes struct {
Total uint64 `json:"total"`
Offset uint64 `json:"offset"`
-21
View File
@@ -633,27 +633,6 @@ type SDK interface {
// fmt.Println(channel)
CreateChannel(channel Channel, token string) (Channel, errors.SDKError)
// CreateChannels registers new channels and returns their ids.
//
// example:
// channels := []sdk.Channel{
// {
// Name: "My Channel 1",
// Metadata: sdk.Metadata{
// "key": "value",
// },
// },
// {
// Name: "My Channel 2",
// Metadata: sdk.Metadata{
// "key": "value",
// },
// },
// }
// channels, _ := sdk.CreateChannels(channels, "token")
// fmt.Println(channels)
CreateChannels(channels []Channel, token string) ([]Channel, errors.SDKError)
// Channels returns page of channels.
//
// example:
-32
View File
@@ -456,38 +456,6 @@ func (_m *SDK) CreateChannel(channel sdk.Channel, token string) (sdk.Channel, er
return r0, r1
}
// CreateChannels provides a mock function with given fields: channels, token
func (_m *SDK) CreateChannels(channels []sdk.Channel, token string) ([]sdk.Channel, errors.SDKError) {
ret := _m.Called(channels, token)
if len(ret) == 0 {
panic("no return value specified for CreateChannels")
}
var r0 []sdk.Channel
var r1 errors.SDKError
if rf, ok := ret.Get(0).(func([]sdk.Channel, string) ([]sdk.Channel, errors.SDKError)); ok {
return rf(channels, token)
}
if rf, ok := ret.Get(0).(func([]sdk.Channel, string) []sdk.Channel); ok {
r0 = rf(channels, token)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]sdk.Channel)
}
}
if rf, ok := ret.Get(1).(func([]sdk.Channel, string) errors.SDKError); ok {
r1 = rf(channels, token)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(errors.SDKError)
}
}
return r0, r1
}
// CreateDomain provides a mock function with given fields: d, token
func (_m *SDK) CreateDomain(d sdk.Domain, token string) (sdk.Domain, errors.SDKError) {
ret := _m.Called(d, token)
+196 -100
View File
@@ -42,176 +42,272 @@ func TestClientsSave(t *testing.T) {
secret := testsutil.GenerateUUID(t)
cases := []struct {
desc string
client clients.Client
err error
desc string
clients []clients.Client
err error
}{
{
desc: "add new client successfully",
client: clients.Client{
ID: uid,
Domain: domainID,
Name: clientName,
Credentials: clients.Credentials{
Identity: clientIdentity,
Secret: secret,
clients: []clients.Client{
{
ID: uid,
Domain: domainID,
Name: clientName,
Credentials: clients.Credentials{
Identity: clientIdentity,
Secret: secret,
},
Metadata: clients.Metadata{},
Status: clients.EnabledStatus,
},
},
err: nil,
},
{
desc: "add multiple clients successfully",
clients: []clients.Client{
{
ID: testsutil.GenerateUUID(t),
Domain: testsutil.GenerateUUID(t),
Name: namesgen.Generate(),
Credentials: clients.Credentials{
Secret: testsutil.GenerateUUID(t),
},
Metadata: clients.Metadata{},
Status: clients.EnabledStatus,
},
{
ID: testsutil.GenerateUUID(t),
Domain: testsutil.GenerateUUID(t),
Name: namesgen.Generate(),
Credentials: clients.Credentials{
Secret: testsutil.GenerateUUID(t),
},
Metadata: clients.Metadata{},
Status: clients.EnabledStatus,
},
{
ID: testsutil.GenerateUUID(t),
Domain: testsutil.GenerateUUID(t),
Name: namesgen.Generate(),
Credentials: clients.Credentials{
Secret: testsutil.GenerateUUID(t),
},
Metadata: clients.Metadata{},
Status: clients.EnabledStatus,
},
Metadata: clients.Metadata{},
Status: clients.EnabledStatus,
},
err: nil,
},
{
desc: "add new client with duplicate secret",
client: clients.Client{
ID: uid,
Domain: domainID,
Name: clientName,
Credentials: clients.Credentials{
Identity: clientIdentity,
Secret: secret,
clients: []clients.Client{
{
ID: testsutil.GenerateUUID(t),
Domain: domainID,
Name: namesgen.Generate(),
Credentials: clients.Credentials{
Identity: clientIdentity,
Secret: secret,
},
Metadata: clients.Metadata{},
Status: clients.EnabledStatus,
},
Metadata: clients.Metadata{},
Status: clients.EnabledStatus,
},
err: repoerr.ErrCreateEntity,
},
{
desc: "add new client with duplicate secret",
client: clients.Client{
ID: uid,
Domain: domainID,
Name: clientName,
Credentials: clients.Credentials{
Identity: clientIdentity,
Secret: testsutil.GenerateUUID(t),
desc: "add multiple clients with one client having duplicate secret",
clients: []clients.Client{
{
ID: testsutil.GenerateUUID(t),
Domain: testsutil.GenerateUUID(t),
Name: namesgen.Generate(),
Credentials: clients.Credentials{
Secret: testsutil.GenerateUUID(t),
},
Metadata: clients.Metadata{},
Status: clients.EnabledStatus,
},
{
ID: testsutil.GenerateUUID(t),
Domain: domainID,
Name: namesgen.Generate(),
Credentials: clients.Credentials{
Identity: clientIdentity,
Secret: secret,
},
Metadata: clients.Metadata{},
Status: clients.EnabledStatus,
},
Metadata: clients.Metadata{},
Status: clients.EnabledStatus,
},
err: repoerr.ErrCreateEntity,
},
{
desc: "add new client without domain id",
client: clients.Client{
ID: testsutil.GenerateUUID(t),
Name: clientName,
Credentials: clients.Credentials{
Identity: "withoutdomain-client@example.com",
Secret: testsutil.GenerateUUID(t),
clients: []clients.Client{
{
ID: testsutil.GenerateUUID(t),
Name: clientName,
Credentials: clients.Credentials{
Identity: "withoutdomain-client@example.com",
Secret: testsutil.GenerateUUID(t),
},
Metadata: clients.Metadata{},
Status: clients.EnabledStatus,
},
Metadata: clients.Metadata{},
Status: clients.EnabledStatus,
},
err: nil,
},
{
desc: "add client with invalid client id",
client: clients.Client{
ID: invalidName,
Domain: domainID,
Name: clientName,
Credentials: clients.Credentials{
Identity: "invalidid-client@example.com",
Secret: testsutil.GenerateUUID(t),
clients: []clients.Client{
{
ID: invalidName,
Domain: domainID,
Name: clientName,
Credentials: clients.Credentials{
Identity: "invalidid-client@example.com",
Secret: testsutil.GenerateUUID(t),
},
Metadata: clients.Metadata{},
Status: clients.EnabledStatus,
},
},
err: repoerr.ErrCreateEntity,
},
{
desc: "add multiple clients with one client having invalid client id",
clients: []clients.Client{
{
ID: testsutil.GenerateUUID(t),
Domain: testsutil.GenerateUUID(t),
Name: namesgen.Generate(),
Credentials: clients.Credentials{
Secret: testsutil.GenerateUUID(t),
},
Metadata: clients.Metadata{},
Status: clients.EnabledStatus,
},
{
ID: invalidName,
Domain: testsutil.GenerateUUID(t),
Name: namesgen.Generate(),
Credentials: clients.Credentials{
Secret: testsutil.GenerateUUID(t),
},
Metadata: clients.Metadata{},
Status: clients.EnabledStatus,
},
Metadata: clients.Metadata{},
Status: clients.EnabledStatus,
},
err: repoerr.ErrCreateEntity,
},
{
desc: "add client with invalid client name",
client: clients.Client{
ID: testsutil.GenerateUUID(t),
Name: invalidName,
Domain: domainID,
Credentials: clients.Credentials{
Identity: "invalidname-client@example.com",
Secret: testsutil.GenerateUUID(t),
clients: []clients.Client{
{
ID: testsutil.GenerateUUID(t),
Name: invalidName,
Domain: domainID,
Credentials: clients.Credentials{
Identity: "invalidname-client@example.com",
Secret: testsutil.GenerateUUID(t),
},
Metadata: clients.Metadata{},
Status: clients.EnabledStatus,
},
Metadata: clients.Metadata{},
Status: clients.EnabledStatus,
},
err: repoerr.ErrCreateEntity,
},
{
desc: "add client with invalid client domain id",
client: clients.Client{
ID: testsutil.GenerateUUID(t),
Domain: invalidDomainID,
Credentials: clients.Credentials{
Identity: "invaliddomainid-client@example.com",
Secret: testsutil.GenerateUUID(t),
clients: []clients.Client{
{
ID: testsutil.GenerateUUID(t),
Domain: invalidDomainID,
Credentials: clients.Credentials{
Identity: "invaliddomainid-client@example.com",
Secret: testsutil.GenerateUUID(t),
},
Metadata: clients.Metadata{},
Status: clients.EnabledStatus,
},
Metadata: clients.Metadata{},
Status: clients.EnabledStatus,
},
err: repoerr.ErrCreateEntity,
},
{
desc: "add client with invalid client identity",
client: clients.Client{
ID: testsutil.GenerateUUID(t),
Name: clientName,
Credentials: clients.Credentials{
Identity: invalidName,
Secret: testsutil.GenerateUUID(t),
clients: []clients.Client{
{
ID: testsutil.GenerateUUID(t),
Name: clientName,
Credentials: clients.Credentials{
Identity: invalidName,
Secret: testsutil.GenerateUUID(t),
},
Metadata: clients.Metadata{},
Status: clients.EnabledStatus,
},
Metadata: clients.Metadata{},
Status: clients.EnabledStatus,
},
err: repoerr.ErrCreateEntity,
},
{
desc: "add client with a missing client identity",
client: clients.Client{
ID: testsutil.GenerateUUID(t),
Domain: testsutil.GenerateUUID(t),
Name: "missing-client-identity",
Credentials: clients.Credentials{
Identity: "",
Secret: testsutil.GenerateUUID(t),
clients: []clients.Client{
{
ID: testsutil.GenerateUUID(t),
Domain: testsutil.GenerateUUID(t),
Name: "missing-client-identity",
Credentials: clients.Credentials{
Identity: "",
Secret: testsutil.GenerateUUID(t),
},
Metadata: clients.Metadata{},
},
Metadata: clients.Metadata{},
},
err: nil,
},
{
desc: "add client with a missing client secret",
client: clients.Client{
ID: testsutil.GenerateUUID(t),
Domain: testsutil.GenerateUUID(t),
Credentials: clients.Credentials{
Identity: "missing-client-secret@example.com",
Secret: "",
clients: []clients.Client{
{
ID: testsutil.GenerateUUID(t),
Domain: testsutil.GenerateUUID(t),
Credentials: clients.Credentials{
Identity: "missing-client-secret@example.com",
Secret: "",
},
Metadata: clients.Metadata{},
},
Metadata: clients.Metadata{},
},
err: nil,
},
{
desc: "add a client with invalid metadata",
client: clients.Client{
ID: testsutil.GenerateUUID(t),
Name: namesgen.Generate(),
Credentials: clients.Credentials{
Identity: fmt.Sprintf("%s@example.com", namesgen.Generate()),
Secret: testsutil.GenerateUUID(t),
},
Metadata: map[string]interface{}{
"key": make(chan int),
clients: []clients.Client{
{
ID: testsutil.GenerateUUID(t),
Name: namesgen.Generate(),
Credentials: clients.Credentials{
Identity: fmt.Sprintf("%s@example.com", namesgen.Generate()),
Secret: testsutil.GenerateUUID(t),
},
Metadata: map[string]interface{}{
"key": make(chan int),
},
},
},
err: errors.ErrMalformedEntity,
},
}
for _, tc := range cases {
rClient, err := repo.Save(context.Background(), tc.client)
rClients, err := repo.Save(context.Background(), tc.clients...)
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
if err == nil {
rClient[0].Credentials.Secret = tc.client.Credentials.Secret
assert.Equal(t, tc.client, rClient[0], fmt.Sprintf("%s: expected %v got %v\n", tc.desc, tc.client, rClient[0]))
for i := range rClients {
tc.clients[i].Credentials.Secret = rClients[i].Credentials.Secret
}
assert.Equal(t, tc.clients, rClients, fmt.Sprintf("%s: expected %v got %v\n", tc.desc, tc.clients, rClients))
}
}
}
+12 -5
View File
@@ -19,8 +19,8 @@ import (
"os"
"time"
"github.com/0x6flab/namegenerator"
sdk "github.com/absmach/magistrala/pkg/sdk/go"
"github.com/docker/docker/pkg/namesgenerator"
)
const (
@@ -28,6 +28,8 @@ const (
defReaderURL = "http://localhost:9005"
)
var namesgenerator = namegenerator.NewGenerator()
// MgConn - structure describing Magistrala connection set.
type MgConn struct {
ChannelID string
@@ -78,7 +80,7 @@ func Provision(conf Config) error {
}
if user.Credentials.Identity == "" {
user.Credentials.Identity = fmt.Sprintf("%s@email.com", namesgenerator.GetRandomName(0))
user.Credentials.Identity = fmt.Sprintf("%s@email.com", namesgenerator.Generate())
user.Credentials.Secret = defPass
}
@@ -138,10 +140,15 @@ func Provision(conf Config) error {
return fmt.Errorf("failed to create the things: %s", err.Error())
}
channels, err = s.CreateChannels(channels, token.AccessToken)
if err != nil {
return fmt.Errorf("failed to create the chennels: %s", err.Error())
var chs []sdk.Channel
for _, c := range channels {
c, err = s.CreateChannel(c, token.AccessToken)
if err != nil {
return fmt.Errorf("failed to create the chennels: %s", err.Error())
}
chs = append(chs, c)
}
channels = chs
for _, t := range things {
tIDs = append(tIDs, t.ID)