Skip to content

Commit

Permalink
test: move opcua to test-containers
Browse files Browse the repository at this point in the history
  • Loading branch information
powersj committed May 24, 2022
1 parent 59a7f76 commit d0fca66
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 29 deletions.
4 changes: 0 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ services:
ports:
- "4150:4150"
command: "/nsqd"
opcua:
image: open62541/open62541
ports:
- "4840:4840"
openldap:
image: cobaugh/openldap-alpine
environment:
Expand Down
45 changes: 20 additions & 25 deletions plugins/inputs/opcua/opcua_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package opcua

import (
"context"
"fmt"
"reflect"
"testing"
"time"

"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"

"github.com/gopcua/opcua/ua"
Expand All @@ -29,29 +27,17 @@ func TestGetDataBadNodeContainerIntegration(t *testing.T) {
t.Skip("Skipping integration test in short mode")
}

// Spin-up the container
ctx := context.Background()
req := testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "open62541/open62541:1.0",
ExposedPorts: []string{"4840/tcp"},
WaitingFor: wait.ForListeningPort("4840/tcp"),
},
Started: true,
container := testutil.Container{
Image: "open62541/open62541",
ExposedPorts: []string{"4840"},
WaitingFor: wait.ForListeningPort("4840/tcp"),
}
container, err := testcontainers.GenericContainer(ctx, req)
require.NoError(t, err, "starting container failed")
err := container.Start()
require.NoError(t, err, "failed to start container")
defer func() {
require.NoError(t, container.Terminate(ctx), "terminating container failed")
require.NoError(t, container.Terminate(), "terminating container failed")
}()

// Get the connection details from the container
addr, err := container.Host(ctx)
require.NoError(t, err, "getting container host address failed")
p, err := container.MappedPort(ctx, "4840/tcp")
require.NoError(t, err, "getting container host port failed")
port := p.Port()

var testopctags = []OPCTags{
{"ProductName", "1", "i", "2261", "open62541 OPC UA Server"},
{"ProductUri", "0", "i", "2262", "http://open62541.org"},
Expand All @@ -60,7 +46,7 @@ func TestGetDataBadNodeContainerIntegration(t *testing.T) {

var o OpcUA
o.MetricName = "testing"
o.Endpoint = fmt.Sprintf("opc.tcp://%s:%s", addr, port)
o.Endpoint = fmt.Sprintf("opc.tcp://%s:%s", container.Address, container.Port)
fmt.Println(o.Endpoint)
o.AuthMethod = "Anonymous"
o.ConnectTimeout = config.Duration(10 * time.Second)
Expand Down Expand Up @@ -94,6 +80,17 @@ func TestClient1Integration(t *testing.T) {
t.Skip("Skipping integration test in short mode")
}

container := testutil.Container{
Image: "open62541/open62541",
ExposedPorts: []string{"4840"},
WaitingFor: wait.ForListeningPort("4840/tcp"),
}
err := container.Start()
require.NoError(t, err, "failed to start container")
defer func() {
require.NoError(t, container.Terminate(), "terminating container failed")
}()

var testopctags = []OPCTags{
{"ProductName", "0", "i", "2261", "open62541 OPC UA Server"},
{"ProductUri", "0", "i", "2262", "http://open62541.org"},
Expand All @@ -103,10 +100,8 @@ func TestClient1Integration(t *testing.T) {
}

var o OpcUA
var err error

o.MetricName = "testing"
o.Endpoint = "opc.tcp://localhost:4840"
o.Endpoint = fmt.Sprintf("opc.tcp://%s:%s", container.Address, container.Port)
o.AuthMethod = "Anonymous"
o.ConnectTimeout = config.Duration(10 * time.Second)
o.RequestTimeout = config.Duration(1 * time.Second)
Expand Down

0 comments on commit d0fca66

Please sign in to comment.