test: migrate openldap tests to test-containers (#11169)
This commit is contained in:
parent
5857966df1
commit
912e3362d2
|
|
@ -45,11 +45,3 @@ services:
|
||||||
ports:
|
ports:
|
||||||
- "15672:15672"
|
- "15672:15672"
|
||||||
- "5672:5672"
|
- "5672:5672"
|
||||||
openldap:
|
|
||||||
image: cobaugh/openldap-alpine
|
|
||||||
environment:
|
|
||||||
- SLAPD_CONFIG_ROOTDN="cn=manager,cn=config"
|
|
||||||
- SLAPD_CONFIG_ROOTPW="secret"
|
|
||||||
ports:
|
|
||||||
- "389:389"
|
|
||||||
- "636:636"
|
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,13 @@
|
||||||
package openldap
|
package openldap
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/go-ldap/ldap/v3"
|
"github.com/go-ldap/ldap/v3"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
"github.com/testcontainers/testcontainers-go/wait"
|
||||||
|
|
||||||
"github.com/influxdata/telegraf/testutil"
|
"github.com/influxdata/telegraf/testutil"
|
||||||
)
|
)
|
||||||
|
|
@ -51,87 +53,253 @@ func TestOpenldapNoConnectionIntegration(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestOpenldapGeneratesMetricsIntegration(t *testing.T) {
|
func TestOpenldapGeneratesMetricsIntegration(t *testing.T) {
|
||||||
t.Skip("skipping test as unable to read LDAP response packet: unexpected EOF")
|
if testing.Short() {
|
||||||
|
t.Skip("Skipping integration test in short mode")
|
||||||
|
}
|
||||||
|
|
||||||
|
container := testutil.Container{
|
||||||
|
Image: "bitnami/openldap",
|
||||||
|
ExposedPorts: []string{"1389"},
|
||||||
|
Env: map[string]string{
|
||||||
|
"LDAP_ADMIN_USERNAME": "manager",
|
||||||
|
"LDAP_ADMIN_PASSWORD": "secret",
|
||||||
|
},
|
||||||
|
WaitingFor: wait.ForLog("Starting slapd"),
|
||||||
|
}
|
||||||
|
err := container.Start()
|
||||||
|
require.NoError(t, err, "failed to start container")
|
||||||
|
defer func() {
|
||||||
|
require.NoError(t, container.Terminate(), "terminating container failed")
|
||||||
|
}()
|
||||||
|
|
||||||
|
port, err := strconv.Atoi(container.Port)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
o := &Openldap{
|
o := &Openldap{
|
||||||
Host: testutil.GetLocalHost(),
|
Host: container.Address,
|
||||||
Port: 389,
|
Port: port,
|
||||||
|
BindDn: "CN=manager,DC=example,DC=org",
|
||||||
|
BindPassword: "secret",
|
||||||
}
|
}
|
||||||
|
|
||||||
var acc testutil.Accumulator
|
var acc testutil.Accumulator
|
||||||
err := o.Gather(&acc)
|
err = o.Gather(&acc)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
commonTests(t, o, &acc)
|
commonTests(t, o, &acc)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestOpenldapStartTLSIntegration(t *testing.T) {
|
func TestOpenldapStartTLSIntegration(t *testing.T) {
|
||||||
t.Skip("skipping test as unable to read LDAP response packet: unexpected EOF")
|
if testing.Short() {
|
||||||
|
t.Skip("Skipping integration test in short mode")
|
||||||
|
}
|
||||||
|
|
||||||
|
pki := testutil.NewPKI("../../../testutil/pki")
|
||||||
|
|
||||||
|
tlsPem, err := filepath.Abs(pki.ServerCertAndKeyPath())
|
||||||
|
require.NoError(t, err)
|
||||||
|
tlsCert, err := filepath.Abs(pki.ServerCertPath())
|
||||||
|
require.NoError(t, err)
|
||||||
|
tlsKey, err := filepath.Abs(pki.ServerKeyPath())
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
container := testutil.Container{
|
||||||
|
Image: "bitnami/openldap",
|
||||||
|
ExposedPorts: []string{"1389", "1636"},
|
||||||
|
Env: map[string]string{
|
||||||
|
"LDAP_ADMIN_USERNAME": "manager",
|
||||||
|
"LDAP_ADMIN_PASSWORD": "secret",
|
||||||
|
"LDAP_ENABLE_TLS": "yes",
|
||||||
|
"LDAP_TLS_CA_FILE": "server.pem",
|
||||||
|
"LDAP_TLS_CERT_FILE": "server.crt",
|
||||||
|
"LDAP_TLS_KEY_FILE": "server.key",
|
||||||
|
},
|
||||||
|
BindMounts: map[string]string{
|
||||||
|
"/server.pem": tlsPem,
|
||||||
|
"/server.crt": tlsCert,
|
||||||
|
"/server.key": tlsKey,
|
||||||
|
},
|
||||||
|
WaitingFor: wait.ForListeningPort("1389/tcp"),
|
||||||
|
}
|
||||||
|
err = container.Start()
|
||||||
|
require.NoError(t, err, "failed to start container")
|
||||||
|
defer func() {
|
||||||
|
require.NoError(t, container.Terminate(), "terminating container failed")
|
||||||
|
}()
|
||||||
|
|
||||||
|
port, err := strconv.Atoi(container.Port)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
cert, err := filepath.Abs(pki.ClientCertPath())
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
o := &Openldap{
|
o := &Openldap{
|
||||||
Host: testutil.GetLocalHost(),
|
Host: container.Address,
|
||||||
Port: 389,
|
Port: port,
|
||||||
SSL: "starttls",
|
SSL: "starttls",
|
||||||
InsecureSkipVerify: true,
|
InsecureSkipVerify: true,
|
||||||
|
BindDn: "CN=manager,DC=example,DC=org",
|
||||||
|
BindPassword: "secret",
|
||||||
|
TLSCA: cert,
|
||||||
}
|
}
|
||||||
|
|
||||||
var acc testutil.Accumulator
|
var acc testutil.Accumulator
|
||||||
err := o.Gather(&acc)
|
err = o.Gather(&acc)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
commonTests(t, o, &acc)
|
commonTests(t, o, &acc)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestOpenldapLDAPSIntegration(t *testing.T) {
|
func TestOpenldapLDAPSIntegration(t *testing.T) {
|
||||||
t.Skip("skipping test as unable to read LDAP response packet: unexpected EOF")
|
if testing.Short() {
|
||||||
|
t.Skip("Skipping integration test in short mode")
|
||||||
|
}
|
||||||
|
|
||||||
|
pki := testutil.NewPKI("../../../testutil/pki")
|
||||||
|
|
||||||
|
tlsPem, err := filepath.Abs(pki.ServerCertAndKeyPath())
|
||||||
|
require.NoError(t, err)
|
||||||
|
tlsCert, err := filepath.Abs(pki.ServerCertPath())
|
||||||
|
require.NoError(t, err)
|
||||||
|
tlsKey, err := filepath.Abs(pki.ServerKeyPath())
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
container := testutil.Container{
|
||||||
|
Image: "bitnami/openldap",
|
||||||
|
ExposedPorts: []string{"1636"},
|
||||||
|
Env: map[string]string{
|
||||||
|
"LDAP_ADMIN_USERNAME": "manager",
|
||||||
|
"LDAP_ADMIN_PASSWORD": "secret",
|
||||||
|
"LDAP_ENABLE_TLS": "yes",
|
||||||
|
"LDAP_TLS_CA_FILE": "server.pem",
|
||||||
|
"LDAP_TLS_CERT_FILE": "server.crt",
|
||||||
|
"LDAP_TLS_KEY_FILE": "server.key",
|
||||||
|
},
|
||||||
|
BindMounts: map[string]string{
|
||||||
|
"/server.pem": tlsPem,
|
||||||
|
"/server.crt": tlsCert,
|
||||||
|
"/server.key": tlsKey,
|
||||||
|
},
|
||||||
|
WaitingFor: wait.ForListeningPort("1636/tcp"),
|
||||||
|
}
|
||||||
|
err = container.Start()
|
||||||
|
require.NoError(t, err, "failed to start container")
|
||||||
|
defer func() {
|
||||||
|
require.NoError(t, container.Terminate(), "terminating container failed")
|
||||||
|
}()
|
||||||
|
|
||||||
|
port, err := strconv.Atoi(container.Port)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
o := &Openldap{
|
o := &Openldap{
|
||||||
Host: testutil.GetLocalHost(),
|
Host: container.Address,
|
||||||
Port: 636,
|
Port: port,
|
||||||
SSL: "ldaps",
|
SSL: "ldaps",
|
||||||
InsecureSkipVerify: true,
|
InsecureSkipVerify: true,
|
||||||
|
BindDn: "CN=manager,DC=example,DC=org",
|
||||||
|
BindPassword: "secret",
|
||||||
}
|
}
|
||||||
|
|
||||||
var acc testutil.Accumulator
|
var acc testutil.Accumulator
|
||||||
err := o.Gather(&acc)
|
err = o.Gather(&acc)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
commonTests(t, o, &acc)
|
commonTests(t, o, &acc)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestOpenldapInvalidSSLIntegration(t *testing.T) {
|
func TestOpenldapInvalidSSLIntegration(t *testing.T) {
|
||||||
t.Skip("skipping test as unable to read LDAP response packet: unexpected EOF")
|
if testing.Short() {
|
||||||
|
t.Skip("Skipping integration test in short mode")
|
||||||
|
}
|
||||||
|
|
||||||
|
pki := testutil.NewPKI("../../../testutil/pki")
|
||||||
|
tlsPem, err := filepath.Abs(pki.ServerCertAndKeyPath())
|
||||||
|
require.NoError(t, err)
|
||||||
|
tlsCert, err := filepath.Abs(pki.ServerCertPath())
|
||||||
|
require.NoError(t, err)
|
||||||
|
tlsKey, err := filepath.Abs(pki.ServerKeyPath())
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
container := testutil.Container{
|
||||||
|
Image: "bitnami/openldap",
|
||||||
|
ExposedPorts: []string{"1636"},
|
||||||
|
Env: map[string]string{
|
||||||
|
"LDAP_ADMIN_USERNAME": "manager",
|
||||||
|
"LDAP_ADMIN_PASSWORD": "secret",
|
||||||
|
"LDAP_ENABLE_TLS": "yes",
|
||||||
|
"LDAP_TLS_CA_FILE": "server.pem",
|
||||||
|
"LDAP_TLS_CERT_FILE": "server.crt",
|
||||||
|
"LDAP_TLS_KEY_FILE": "server.key",
|
||||||
|
},
|
||||||
|
BindMounts: map[string]string{
|
||||||
|
"/server.pem": tlsPem,
|
||||||
|
"/server.crt": tlsCert,
|
||||||
|
"/server.key": tlsKey,
|
||||||
|
},
|
||||||
|
WaitingFor: wait.ForListeningPort("1636/tcp"),
|
||||||
|
}
|
||||||
|
err = container.Start()
|
||||||
|
require.NoError(t, err, "failed to start container")
|
||||||
|
defer func() {
|
||||||
|
require.NoError(t, container.Terminate(), "terminating container failed")
|
||||||
|
}()
|
||||||
|
|
||||||
|
port, err := strconv.Atoi(container.Port)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
o := &Openldap{
|
o := &Openldap{
|
||||||
Host: testutil.GetLocalHost(),
|
Host: container.Address,
|
||||||
Port: 636,
|
Port: port,
|
||||||
SSL: "invalid",
|
SSL: "invalid",
|
||||||
InsecureSkipVerify: true,
|
InsecureSkipVerify: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
var acc testutil.Accumulator
|
var acc testutil.Accumulator
|
||||||
err := o.Gather(&acc)
|
err = o.Gather(&acc)
|
||||||
require.NoError(t, err) // test that we didn't return an error
|
require.NoError(t, err) // test that we didn't return an error
|
||||||
require.Zero(t, acc.NFields()) // test that we didn't return any fields
|
require.Zero(t, acc.NFields()) // test that we didn't return any fields
|
||||||
require.NotEmpty(t, acc.Errors) // test that we set an error
|
require.NotEmpty(t, acc.Errors) // test that we set an error
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestOpenldapBindIntegration(t *testing.T) {
|
func TestOpenldapBindIntegration(t *testing.T) {
|
||||||
t.Skip("skipping test as unable to read LDAP response packet: unexpected EOF")
|
if testing.Short() {
|
||||||
|
t.Skip("Skipping integration test in short mode")
|
||||||
|
}
|
||||||
|
|
||||||
|
container := testutil.Container{
|
||||||
|
Image: "bitnami/openldap",
|
||||||
|
ExposedPorts: []string{"1389"},
|
||||||
|
Env: map[string]string{
|
||||||
|
"LDAP_ADMIN_USERNAME": "manager",
|
||||||
|
"LDAP_ADMIN_PASSWORD": "secret",
|
||||||
|
},
|
||||||
|
WaitingFor: wait.ForListeningPort("1389/tcp"),
|
||||||
|
}
|
||||||
|
err := container.Start()
|
||||||
|
require.NoError(t, err, "failed to start container")
|
||||||
|
defer func() {
|
||||||
|
require.NoError(t, container.Terminate(), "terminating container failed")
|
||||||
|
}()
|
||||||
|
|
||||||
|
port, err := strconv.Atoi(container.Port)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
o := &Openldap{
|
o := &Openldap{
|
||||||
Host: testutil.GetLocalHost(),
|
Host: container.Address,
|
||||||
Port: 389,
|
Port: port,
|
||||||
SSL: "",
|
SSL: "",
|
||||||
InsecureSkipVerify: true,
|
InsecureSkipVerify: true,
|
||||||
BindDn: "cn=manager,cn=config",
|
BindDn: "CN=manager,DC=example,DC=org",
|
||||||
BindPassword: "secret",
|
BindPassword: "secret",
|
||||||
}
|
}
|
||||||
|
|
||||||
var acc testutil.Accumulator
|
var acc testutil.Accumulator
|
||||||
err := o.Gather(&acc)
|
err = o.Gather(&acc)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
commonTests(t, o, &acc)
|
commonTests(t, o, &acc)
|
||||||
}
|
}
|
||||||
|
|
||||||
func commonTests(t *testing.T, o *Openldap, acc *testutil.Accumulator) {
|
func commonTests(t *testing.T, o *Openldap, acc *testutil.Accumulator) {
|
||||||
|
// helpful local commands to run:
|
||||||
|
// ldapwhoami -D "CN=manager,DC=example,DC=org" -H ldap://localhost:1389 -w secret
|
||||||
|
// ldapsearch -D "CN=manager,DC=example,DC=org" -H "ldap://localhost:1389" -b cn=Monitor -w secret
|
||||||
require.Empty(t, acc.Errors, "accumulator had no errors")
|
require.Empty(t, acc.Errors, "accumulator had no errors")
|
||||||
require.True(t, acc.HasMeasurement("openldap"), "Has a measurement called 'openldap'")
|
require.True(t, acc.HasMeasurement("openldap"), "Has a measurement called 'openldap'")
|
||||||
require.Equal(t, o.Host, acc.TagValue("openldap", "server"), "Has a tag value of server=o.Host")
|
require.Equal(t, o.Host, acc.TagValue("openldap", "server"), "Has a tag value of server=o.Host")
|
||||||
|
|
@ -140,20 +308,40 @@ func commonTests(t *testing.T, o *Openldap, acc *testutil.Accumulator) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestOpenldapReverseMetricsIntegration(t *testing.T) {
|
func TestOpenldapReverseMetricsIntegration(t *testing.T) {
|
||||||
t.Skip("skipping test as unable to read LDAP response packet: unexpected EOF")
|
if testing.Short() {
|
||||||
|
t.Skip("Skipping integration test in short mode")
|
||||||
|
}
|
||||||
|
|
||||||
|
container := testutil.Container{
|
||||||
|
Image: "bitnami/openldap",
|
||||||
|
ExposedPorts: []string{"1389"},
|
||||||
|
Env: map[string]string{
|
||||||
|
"LDAP_ADMIN_USERNAME": "manager",
|
||||||
|
"LDAP_ADMIN_PASSWORD": "secret",
|
||||||
|
},
|
||||||
|
WaitingFor: wait.ForListeningPort("1389/tcp"),
|
||||||
|
}
|
||||||
|
err := container.Start()
|
||||||
|
require.NoError(t, err, "failed to start container")
|
||||||
|
defer func() {
|
||||||
|
require.NoError(t, container.Terminate(), "terminating container failed")
|
||||||
|
}()
|
||||||
|
|
||||||
|
port, err := strconv.Atoi(container.Port)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
o := &Openldap{
|
o := &Openldap{
|
||||||
Host: testutil.GetLocalHost(),
|
Host: container.Address,
|
||||||
Port: 389,
|
Port: port,
|
||||||
SSL: "",
|
SSL: "",
|
||||||
InsecureSkipVerify: true,
|
InsecureSkipVerify: true,
|
||||||
BindDn: "cn=manager,cn=config",
|
BindDn: "CN=manager,DC=example,DC=org",
|
||||||
BindPassword: "secret",
|
BindPassword: "secret",
|
||||||
ReverseMetricNames: true,
|
ReverseMetricNames: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
var acc testutil.Accumulator
|
var acc testutil.Accumulator
|
||||||
err := o.Gather(&acc)
|
err = o.Gather(&acc)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.True(t, acc.HasInt64Field("openldap", "connections_total"), "Has an integer field called connections_total")
|
require.True(t, acc.HasInt64Field("openldap", "connections_total"), "Has an integer field called connections_total")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@ type Container struct {
|
||||||
Entrypoint []string
|
Entrypoint []string
|
||||||
Env map[string]string
|
Env map[string]string
|
||||||
ExposedPorts []string
|
ExposedPorts []string
|
||||||
|
BindMounts map[string]string
|
||||||
WaitingFor wait.Strategy
|
WaitingFor wait.Strategy
|
||||||
|
|
||||||
Address string
|
Address string
|
||||||
|
|
@ -34,6 +35,7 @@ func (c *Container) Start() error {
|
||||||
Image: c.Image,
|
Image: c.Image,
|
||||||
Env: c.Env,
|
Env: c.Env,
|
||||||
ExposedPorts: c.ExposedPorts,
|
ExposedPorts: c.ExposedPorts,
|
||||||
|
BindMounts: c.BindMounts,
|
||||||
Entrypoint: c.Entrypoint,
|
Entrypoint: c.Entrypoint,
|
||||||
WaitingFor: c.WaitingFor,
|
WaitingFor: c.WaitingFor,
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue