Skip to content

Commit

Permalink
Provide integration tests via the key Ditto features - ContainerFactory
Browse files Browse the repository at this point in the history
[eclipse-kanto#70] Provide integration tests via the key Ditto features - ContainerFactory
- remove go:build tag from the common file
- fix the issue with the deffer function

Signed-off-by: Guzgunova Antonia <Antonia.Guzgunova@bosch.io>
  • Loading branch information
antoniyatrifonova authored and konstantina-gramatova committed Dec 16, 2022
1 parent 6cad160 commit f7000fc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 deletions.
4 changes: 2 additions & 2 deletions integration/container_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (suite *ctrFactorySuite) testCreate(operation string, params interface{}) {

func (suite *ctrFactorySuite) assertHTTPServer() {
req, err := http.NewRequest(http.MethodGet, httpdRequestURL, nil)
require.NoError(suite.T(), err, "failed to create an HTTP request from the container")
require.NoError(suite.T(), err, "failed to create an HTTP request to the container")

resp, err := http.DefaultClient.Do(req)
require.NoError(suite.T(), err, "failed to get an HTTP response from the container")
Expand All @@ -102,6 +102,6 @@ func (suite *ctrFactorySuite) assertHTTPServer() {
require.Equal(suite.T(), 200, resp.StatusCode, "HTTP response status code from the container is not expected")

body, err := io.ReadAll(resp.Body)
require.NoError(suite.T(), err, "failed to reach the requested URL on the host from the container")
require.NoError(suite.T(), err, "failed to reach the requested URL on the host to the container")
require.Equal(suite.T(), httpdResponse, string(body), "HTTP response from the container is not expected")
}
28 changes: 10 additions & 18 deletions integration/container_management_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0

//go:build integration

package integration

import (
Expand Down Expand Up @@ -94,19 +92,15 @@ func (suite *ctrManagementSuite) createWSConnection() *websocket.Conn {
require.NoError(suite.T(), err, "failed to create a websocket connection")

err = util.SubscribeForWSMessages(suite.Cfg, wsConnection, typeEvents, "like(resource:path,'/features/Container:*')")
defer closeWSConnection(wsConnection, err)
require.NoError(suite.T(), err, "failed to subscribe for the %s messages", typeEvents)

suite.assertNoError(wsConnection, err, "failed to subscribe for the %s messages", typeEvents)
return wsConnection
}

func (suite *ctrManagementSuite) create(operation string, params interface{}) (*websocket.Conn, string) {
wsConnection := suite.createWSConnection()

_, err := util.ExecuteOperation(suite.Cfg, suite.ctrFactoryFeatureURL, operation, params)
defer closeWSConnection(wsConnection, err)

require.NoError(suite.T(), err, "failed to execute the %s operation", operation)
suite.assertNoError(wsConnection, err, "failed to execute the %s operation", operation)

var ctrFeatureID string
var isCtrFeatureCreated bool
Expand All @@ -133,35 +127,33 @@ func (suite *ctrManagementSuite) create(operation string, params interface{}) (*
}
return true, fmt.Errorf("event for modify the container feature status is not received")
}
return false, fmt.Errorf("unknown message received")
return true, fmt.Errorf("unknown message is received")
})

require.NoError(suite.T(), err, "event for creating the container feature is not received")

suite.assertNoError(wsConnection, err, "failed to process creating the container feature")
return wsConnection, ctrFeatureID
}

func (suite *ctrManagementSuite) remove(wsConnection *websocket.Conn, ctrFeatureID string) {
filter := fmt.Sprintf("like(resource:path,'/features/%s')", ctrFeatureID)
err := util.SubscribeForWSMessages(suite.Cfg, wsConnection, typeEvents, filter)
defer closeWSConnection(wsConnection, err)
require.NoError(suite.T(), err, "failed to subscribe for the %s messages", typeEvents)
suite.assertNoError(wsConnection, err, "failed to subscribe for the %s messages", typeEvents)

_, err = util.ExecuteOperation(suite.Cfg, util.GetFeatureURL(suite.ctrThingURL, ctrFeatureID), operationRemove, true)
require.NoError(suite.T(), err, "failed to remove the container feature with ID %s", ctrFeatureID)
suite.assertNoError(wsConnection, err, "failed to remove the container feature with ID %s", ctrFeatureID)

err = util.ProcessWSMessages(suite.Cfg, wsConnection, func(event *protocol.Envelope) (bool, error) {
if event.Topic.String() == suite.topicDeleted {
return true, nil
}
return false, fmt.Errorf("unknown message received")
return true, fmt.Errorf("unknown message is received")
})
suite.assertNoError(wsConnection, err, "failed to process removing the container feature")

require.NoError(suite.T(), err, "event for removing the container feature is not received")
}

func closeWSConnection(wsConnection *websocket.Conn, err error) {
func (suite *ctrManagementSuite) assertNoError(wsConnection *websocket.Conn, err error, message string, messageArs ...interface{}) {
if err != nil {
wsConnection.Close()
}
require.NoError(suite.T(), err, message, messageArs)
}

0 comments on commit f7000fc

Please sign in to comment.