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
- refactor the usage of contants

Signed-off-by: Guzgunova Antonia <Antonia.Guzgunova@bosch.io>
  • Loading branch information
antoniyatrifonova authored and konstantina-gramatova committed Dec 16, 2022
1 parent 4dfc7d3 commit 293c115
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 51 deletions.
26 changes: 7 additions & 19 deletions integration/ctr_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,17 @@ func (suite *ctrFactorySuite) TestCreateWithConfig() {
}

func (suite *ctrFactorySuite) TestCreateWithConfigPortMapping() {
const (
paramExtraHosts = "extraHosts"
paramPortMapping = "portMappings"
paramHostPort = "hostPort"
paramContainerPort = "containerPort"
httpdImageRef = "docker.io/library/httpd:latest"
)
config := make(map[string]interface{})
config[paramExtraHosts] = []string{"ctrhost:host_ip"}
config[paramPortMapping] = []map[string]interface{}{
config["extraHosts"] = []string{"ctrhost:host_ip"}
config["portMappings"] = []map[string]interface{}{
{
paramHostPort: 5000,
paramContainerPort: 80,
"hostPort": 5000,
"containerPort": 80,
},
}

params := make(map[string]interface{})
params[paramImageRef] = httpdImageRef
params[paramImageRef] = "docker.io/library/httpd:latest"
params[paramStart] = true
params[paramConfig] = config

Expand All @@ -92,12 +85,7 @@ func (suite *ctrFactorySuite) TestCreateWithConfigPortMapping() {
}

func (suite *ctrFactorySuite) assertHTTPServer() {
const (
httpdRequestURL = "http://127.0.0.1:5000"
httpdResponse = "<html><body><h1>It works!</h1></body></html>\n"
)

req, err := http.NewRequest(http.MethodGet, httpdRequestURL, nil)
req, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:5000", nil)
require.NoError(suite.T(), err, "failed to create an HTTP request to the container")

resp, err := http.DefaultClient.Do(req)
Expand All @@ -109,5 +97,5 @@ func (suite *ctrFactorySuite) assertHTTPServer() {

body, err := io.ReadAll(resp.Body)
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")
require.Equal(suite.T(), "<html><body><h1>It works!</h1></body></html>\n", string(body), "HTTP response from the container is not expected")
}
50 changes: 18 additions & 32 deletions integration/ctr_management_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@ type ctrManagementSuite struct {
}

func (suite *ctrManagementSuite) SetupCtrManagementSuite() {
const ctrFactoryFeatureID = "ContainerFactory"
suite.Setup(suite.T())

suite.ctrThingID = suite.ThingCfg.DeviceID + ":edge:containers"
suite.ctrThingURL = util.GetThingURL(suite.Cfg.DigitalTwinAPIAddress, suite.ctrThingID)
suite.ctrFactoryFeatureURL = util.GetFeatureURL(suite.ctrThingURL, ctrFactoryFeatureID)
suite.ctrFactoryFeatureURL = util.GetFeatureURL(suite.ctrThingURL, "ContainerFactory")

suite.topicCreated = util.GetTwinEventTopic(suite.ctrThingID, protocol.ActionCreated)
suite.topicModified = util.GetTwinEventTopic(suite.ctrThingID, protocol.ActionModified)
Expand All @@ -50,8 +49,11 @@ func (suite *ctrManagementSuite) SetupCtrManagementSuite() {
suite.assertCtrFactoryFeature()
}

func getCtrFeatureID(topic string) string {
result := strings.Split(topic, "/")
func getCtrFeatureID(path string) string {
result := strings.Split(path, "/")
if len(result) < 3 {
return ""
}
return result[2]
}

Expand All @@ -64,36 +66,29 @@ func (suite *ctrManagementSuite) getActualCtrStatus(ctrFeatureID string) string
}

func (suite *ctrManagementSuite) assertCtrFactoryFeature() {
const ctrFactoryFeatureDefinition = "[\"com.bosch.iot.suite.edge.containers:ContainerFactory:1.3.0\"]"
ctrFactoryDefinition := suite.ctrFactoryFeatureURL + "/definition"
body, err := util.SendDigitalTwinRequest(suite.Cfg, http.MethodGet, ctrFactoryDefinition, nil)

require.NoError(suite.T(), err, "failed to get the container factory feature definition")
require.Equal(suite.T(), ctrFactoryFeatureDefinition, string(body), "the container factory definition is not expected")
require.Equal(suite.T(), "[\"com.bosch.iot.suite.edge.containers:ContainerFactory:1.3.0\"]", string(body), "the container factory definition is not expected")
}

func (suite *ctrManagementSuite) createWSConnection() *websocket.Conn {
const filterCtrFeatures = "like(resource:path,'/features/Container:*')"

wsConnection, err := util.NewDigitalTwinWSConnection(suite.Cfg)
require.NoError(suite.T(), err, "failed to create a websocket connection")

err = util.SubscribeForWSMessages(suite.Cfg, wsConnection, util.StartSendEvents, filterCtrFeatures)
suite.assertNoError(wsConnection, err, "failed to subscribe for the %s messages", util.StartSendEvents)
err = util.SubscribeForWSMessages(suite.Cfg, wsConnection, util.StartSendEvents, "like(resource:path,'/features/Container:*')")
suite.closeOnError(wsConnection, err, "failed to subscribe for the %s messages", util.StartSendEvents)
return wsConnection
}

func (suite *ctrManagementSuite) createOperation(operation string, params map[string]interface{}) (*websocket.Conn, string) {
const (
propertyStatus = "status"
statusCreated = "CREATED"
statusRunning = "RUNNING"
)
const propertyStatus = "status"

wsConnection := suite.createWSConnection()

_, err := util.ExecuteOperation(suite.Cfg, suite.ctrFactoryFeatureURL, operation, params)
suite.assertNoError(wsConnection, err, "failed to execute the %s operation", operation)
suite.closeOnError(wsConnection, err, "failed to execute the %s operation", operation)

var ctrFeatureID string
var isCtrFeatureCreated bool
Expand Down Expand Up @@ -123,18 +118,14 @@ func (suite *ctrManagementSuite) createOperation(operation string, params map[st
}
return true, fmt.Errorf("unknown message is received")
})
suite.assertNoError(wsConnection, err, "failed to process creating the container feature")
suite.closeOnError(wsConnection, err, "failed to process creating the container feature")
defer util.UnsubscribeFromWSMessages(suite.Cfg, wsConnection, util.StopSendEvents)
return wsConnection, ctrFeatureID
}

func (suite *ctrManagementSuite) expectedStatus(status string, isStarted bool) {
const (
statusCreated = "CREATED"
statusRunning = "RUNNING"
)
if isStarted {
require.Equal(suite.T(), statusRunning, status, "container status is not expected")
require.Equal(suite.T(), "RUNNING", status, "container status is not expected")
return
}
require.Equal(suite.T(), statusCreated, status, "container status is not expected")
Expand All @@ -149,30 +140,25 @@ func (suite *ctrManagementSuite) createWithConfig(params map[string]interface{})
}

func (suite *ctrManagementSuite) remove(wsConnection *websocket.Conn, ctrFeatureID string) {
const (
filterCtrFeature = "like(resource:path,'/features/%s')"
operationRemove = "remove"
)

filter := fmt.Sprintf(filterCtrFeature, ctrFeatureID)
filter := fmt.Sprintf("like(resource:path,'/features/%s')", ctrFeatureID)
err := util.SubscribeForWSMessages(suite.Cfg, wsConnection, util.StartSendEvents, filter)
suite.assertNoError(wsConnection, err, "failed to subscribe for the %s messages", util.StartSendEvents)
suite.closeOnError(wsConnection, err, "failed to subscribe for the %s messages", util.StartSendEvents)
defer util.UnsubscribeFromWSMessages(suite.Cfg, wsConnection, util.StopSendEvents)

_, err = util.ExecuteOperation(suite.Cfg, util.GetFeatureURL(suite.ctrThingURL, ctrFeatureID), "remove", true)
suite.assertNoError(wsConnection, err, "failed to remove the container feature with ID %s", ctrFeatureID)
suite.closeOnError(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 true, fmt.Errorf("unknown message is received")
})
suite.assertNoError(wsConnection, err, "failed to process removing the container feature")
suite.closeOnError(wsConnection, err, "failed to process removing the container feature")

}

func (suite *ctrManagementSuite) assertNoError(wsConnection *websocket.Conn, err error, message string, messageArs ...interface{}) {
func (suite *ctrManagementSuite) closeOnError(wsConnection *websocket.Conn, err error, message string, messageArs ...interface{}) {
if err != nil {
wsConnection.Close()
}
Expand Down
1 change: 1 addition & 0 deletions integration/ctr_management_test_constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ const (
paramImageRef = "imageRef"
paramStart = "start"
paramConfig = "config"
statusCreated = "CREATED"
)

0 comments on commit 293c115

Please sign in to comment.