diff --git a/client/lib.go b/client/lib.go index 5ebbb5f475d05461b7d47ad7484e94600592c34d..449497b8daea3ca09c8efdbf734b153e4e1f5f47 100644 --- a/client/lib.go +++ b/client/lib.go @@ -28,7 +28,7 @@ var ( const ( routeBroadcastData = "broadcastData" - routeGetMessageById = "getMessageById" + routeGetMessageById = "findMessageById" routeGetNeighbors = "getNeighbors" routeSpammer = "spammer" routeLogin = "login" @@ -39,15 +39,15 @@ const ( func NewGoShimmerAPI(node string, httpClient ...http.Client) *GoShimmerAPI { if len(httpClient) > 0 { - return &GoShimmerAPI{node: node, httpClient: httpClient[0]} + return &GoShimmerAPI{baseUrl: node, httpClient: httpClient[0]} } - return &GoShimmerAPI{node: node} + return &GoShimmerAPI{baseUrl: node} } // GoShimmerAPI is an API wrapper over the web API of GoShimmer. type GoShimmerAPI struct { httpClient http.Client - node string + baseUrl string jwt string } @@ -75,7 +75,7 @@ func interpretBody(res *http.Response, decodeTo interface{}) error { case http.StatusInternalServerError: return fmt.Errorf("%w: %s", ErrInternalServerError, errRes.Error) case http.StatusNotFound: - return fmt.Errorf("%w: %s", ErrNotFound, errRes.Error) + return fmt.Errorf("%w: %s", ErrNotFound, res.Request.URL) case http.StatusBadRequest: return fmt.Errorf("%w: %s", ErrBadRequest, errRes.Error) case http.StatusUnauthorized: @@ -99,7 +99,7 @@ func (api *GoShimmerAPI) do(method string, route string, reqObj interface{}, res } // construct request - req, err := http.NewRequest(method, fmt.Sprintf("%s/%s", api.node, route), func() io.Reader { + req, err := http.NewRequest(method, fmt.Sprintf("%s/%s", api.baseUrl, route), func() io.Reader { if data == nil { return nil } @@ -212,3 +212,7 @@ func (api *GoShimmerAPI) ToggleSpammer(enable bool) (*webapi_spammer.Response, e } return res, nil } + +func (api *GoShimmerAPI) BaseUrl() string { + return api.baseUrl +} diff --git a/tools/integration-tests/tester/api/api.go b/tools/integration-tests/tester/api/api.go deleted file mode 100644 index 0890de3cf62b7ff74a5e44cd3553bada3ccad38a..0000000000000000000000000000000000000000 --- a/tools/integration-tests/tester/api/api.go +++ /dev/null @@ -1,178 +0,0 @@ -package api - -import ( - "bytes" - "encoding/json" - "errors" - "fmt" - "io" - "io/ioutil" - "net/http" -) - -var ( - ErrBadRequest = errors.New("bad request") - ErrInternalServerError = errors.New("internal server error") - ErrNotFound = errors.New("not found") - ErrUnauthorized = errors.New("unauthorized") - ErrUnknownError = errors.New("unknown error") - ErrNotImplemented = errors.New("operation not implemented/supported/available") -) - -const ( - routeBroadcastData = "broadcastData" - routeGetNeighbors = "getNeighbors" - routeGetMessageByHash = "getMessageByHash" - - contentTypeJSON = "application/json" -) - -func New(baseUrl string, httpClient http.Client) *Api { - return &Api{BaseUrl: baseUrl, httpClient: httpClient} -} - -// Api is an API wrapper over the web API of GoShimmer. -type Api struct { - httpClient http.Client - BaseUrl string - jwt string -} - -type errorResponse struct { - Error string `json:"error"` -} - -func interpretBody(res *http.Response, decodeTo interface{}) error { - resBody, err := ioutil.ReadAll(res.Body) - if err != nil { - return fmt.Errorf("unable to read response body: %w", err) - } - defer res.Body.Close() - - if res.StatusCode == http.StatusOK || res.StatusCode == http.StatusCreated { - return json.Unmarshal(resBody, decodeTo) - } - - errRes := &errorResponse{} - if err := json.Unmarshal(resBody, errRes); err != nil { - return fmt.Errorf("unable to read error from response body: %w", err) - } - - switch res.StatusCode { - case http.StatusInternalServerError: - return fmt.Errorf("%w: %s", ErrInternalServerError, errRes.Error) - case http.StatusNotFound: - return fmt.Errorf("%w: %s", ErrNotFound, res.Request.URL.String()) - case http.StatusBadRequest: - return fmt.Errorf("%w: %s", ErrBadRequest, errRes.Error) - case http.StatusUnauthorized: - return fmt.Errorf("%w: %s", ErrUnauthorized, errRes.Error) - case http.StatusNotImplemented: - return fmt.Errorf("%w: %s", ErrNotImplemented, errRes.Error) - } - - return fmt.Errorf("%w: %s", ErrUnknownError, errRes.Error) -} - -func (api *Api) do(method string, route string, reqObj interface{}, resObj interface{}) error { - // marshal request object - var data []byte - if reqObj != nil { - var err error - data, err = json.Marshal(reqObj) - if err != nil { - return err - } - } - - // construct request - req, err := http.NewRequest(method, fmt.Sprintf("%s/%s", api.BaseUrl, route), func() io.Reader { - if data == nil { - return nil - } - return bytes.NewReader(data) - }()) - if err != nil { - return err - } - - if data != nil { - req.Header.Set("Content-Type", contentTypeJSON) - } - - // add authorization header with JWT - if len(api.jwt) > 0 { - req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", api.jwt)) - } - - // make the request - res, err := api.httpClient.Do(req) - if err != nil { - return err - } - - if resObj == nil { - return nil - } - - // write response into response object - if err := interpretBody(res, resObj); err != nil { - return err - } - return nil -} - -func (api *Api) BroadcastData(data string) (string, error) { - res := &BroadcastDataResponse{} - - err := api.do( - http.MethodPost, - routeBroadcastData, - &BroadcastDataRequest{Data: data}, - res, - ) - if err != nil { - return "", err - } - - return res.Hash, nil -} - -// GetNeighbors gets the chosen/accepted neighbors. -// If knownPeers is set, also all known peers to the node are returned additionally. -func (api *Api) GetNeighbors(knownPeers bool) (*GetNeighborResponse, error) { - res := &GetNeighborResponse{} - - err := api.do( - http.MethodGet, - func() string { - if !knownPeers { - return routeGetNeighbors - } - return fmt.Sprintf("%s?known=1", routeGetNeighbors) - }(), - nil, - res, - ) - if err != nil { - return nil, err - } - - return res, nil -} - -func (api *Api) GetMessageByHash(hashes []string) (*GetMessageByHashResponse, error) { - res := &GetMessageByHashResponse{} - - err := api.do( - http.MethodPost, - routeGetMessageByHash, - &GetMessageByHashRequest{Hashes: hashes}, - res, - ) - if err != nil { - return nil, err - } - - return res, nil -} diff --git a/tools/integration-tests/tester/api/model.go b/tools/integration-tests/tester/api/model.go deleted file mode 100644 index 85c6d4428cf494a80b15612122fde8d48867f3a5..0000000000000000000000000000000000000000 --- a/tools/integration-tests/tester/api/model.go +++ /dev/null @@ -1,48 +0,0 @@ -package api - -type BroadcastDataResponse struct { - Hash string `json:"hash,omitempty"` - Error string `json:"error,omitempty"` -} - -type BroadcastDataRequest struct { - Data string `json:"data"` -} - -type GetMessageByHashResponse struct { - Messages []Message `json:"messages,omitempty"` - Error string `json:"error,omitempty"` -} - -type GetMessageByHashRequest struct { - Hashes []string `json:"hashes"` -} - -type Message struct { - MessageId string `json:"messageId,omitempty"` - TrunkTransactionId string `json:"trunkTransactionId,omitempty"` - BranchTransactionId string `json:"branchTransactionId,omitempty"` - IssuerPublicKey string `json:"issuerPublicKey,omitempty"` - IssuingTime string `json:"issuingTime,omitempty"` - SequenceNumber uint64 `json:"sequenceNumber,omitempty"` - Payload string `json:"payload,omitempty"` - Signature string `json:"signature,omitempty"` -} - -type GetNeighborResponse struct { - KnownPeers []Neighbor `json:"known,omitempty"` - Chosen []Neighbor `json:"chosen"` - Accepted []Neighbor `json:"accepted"` - Error string `json:"error,omitempty"` -} - -type Neighbor struct { - ID string `json:"id"` // comparable node identifier - PublicKey string `json:"publicKey"` // public key used to verify signatures - Services []PeerService `json:"services,omitempty"` -} - -type PeerService struct { - ID string `json:"id"` // ID of the service - Address string `json:"address"` // network address of the service -} diff --git a/tools/integration-tests/tester/framework/peer.go b/tools/integration-tests/tester/framework/peer.go index a2256619cb0e3892878e8e4705425f17c8549e41..3562d79d5425c9e2ab79a067238d21d0d20eb515 100644 --- a/tools/integration-tests/tester/framework/peer.go +++ b/tools/integration-tests/tester/framework/peer.go @@ -9,42 +9,43 @@ import ( "time" "github.com/docker/docker/api/types" - "github.com/docker/docker/client" + dockerclient "github.com/docker/docker/client" + "github.com/iotaledger/goshimmer/plugins/webapi/getNeighbors" - "github.com/iotaledger/goshimmer/tools/integration-tests/tester/api" + "github.com/iotaledger/goshimmer/client" ) type Peer struct { name string ip net.IP - *api.Api - dockerCli *client.Client - chosen []api.Neighbor - accepted []api.Neighbor + *client.GoShimmerAPI + dockerCli *dockerclient.Client + chosen []getNeighbors.Neighbor + accepted []getNeighbors.Neighbor } -func NewPeer(name string, ip net.IP, dockerCli *client.Client) *Peer { +func NewPeer(name string, ip net.IP, dockerCli *dockerclient.Client) *Peer { return &Peer{ - name: name, - ip: ip, - Api: api.New(getWebApiBaseUrl(ip), http.Client{Timeout: 30 * time.Second}), - dockerCli: dockerCli, + name: name, + ip: ip, + GoShimmerAPI: client.NewGoShimmerAPI(getWebApiBaseUrl(ip), http.Client{Timeout: 30 * time.Second}), + dockerCli: dockerCli, } } func (p *Peer) String() string { - return fmt.Sprintf("Peer:{%s, %s, %s, %d}", p.name, p.ip.String(), p.BaseUrl, p.TotalNeighbors()) + return fmt.Sprintf("Peer:{%s, %s, %s, %d}", p.name, p.ip.String(), p.BaseUrl(), p.TotalNeighbors()) } func (p *Peer) TotalNeighbors() int { return len(p.chosen) + len(p.accepted) } -func (p *Peer) SetNeighbors(chosen, accepted []api.Neighbor) { - p.chosen = make([]api.Neighbor, len(chosen)) +func (p *Peer) SetNeighbors(chosen, accepted []getNeighbors.Neighbor) { + p.chosen = make([]getNeighbors.Neighbor, len(chosen)) copy(p.chosen, chosen) - p.accepted = make([]api.Neighbor, len(accepted)) + p.accepted = make([]getNeighbors.Neighbor, len(accepted)) copy(p.accepted, accepted) } @@ -63,7 +64,7 @@ func (p *Peer) Logs() (io.ReadCloser, error) { }) } -func getAvailablePeers(dockerCli *client.Client) (peers []*Peer) { +func getAvailablePeers(dockerCli *dockerclient.Client) (peers []*Peer) { // get peer master if addr, err := net.LookupIP(hostnamePeerMaster); err != nil { fmt.Printf("Could not resolve %s\n", hostnamePeerMaster) diff --git a/tools/integration-tests/tester/tests/relaymessage_test.go b/tools/integration-tests/tester/tests/relaymessage_test.go index f4dfc7cc2994e826771cdb1be32b7ce426ad0d9d..41f88ec1e6215bb59602f43fc30c7091abe675a2 100644 --- a/tools/integration-tests/tester/tests/relaymessage_test.go +++ b/tools/integration-tests/tester/tests/relaymessage_test.go @@ -6,18 +6,22 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + + "github.com/iotaledger/goshimmer/packages/binary/messagelayer/payload" ) func TestRelayMessages(t *testing.T) { numMessages := 100 - hashes := make([]string, numMessages) + ids := make([]string, numMessages) + + data := payload.NewData([]byte("Test")).Bytes() // create messages on random peers for i := 0; i < numMessages; i++ { - hash, err := f.RandomPeer().BroadcastData("Test") + id, err := f.RandomPeer().BroadcastData(data) require.NoError(t, err) - hashes[i] = hash + ids[i] = id } // wait for messages to be gossiped @@ -25,7 +29,7 @@ func TestRelayMessages(t *testing.T) { // check for messages on every peer for _, peer := range f.Peers() { - resp, err := peer.GetMessageByHash(hashes) + resp, err := peer.FindMessageById(ids) require.NoError(t, err) // check for the count of messages @@ -33,15 +37,15 @@ func TestRelayMessages(t *testing.T) { // check that all messages are present in response outer: - for _, hash := range hashes { + for _, id := range ids { for _, msg := range resp.Messages { // if message found skip to next - if msg.MessageId == hash { + if msg.Id == id { continue outer } } - t.Errorf("MessageId=%s not found in peer %s.", hash, peer.String()) + t.Errorf("MessageId=%s not found in peer %s.", id, peer.String()) } } }