Skip to content

Commit

Permalink
use new common functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommi2Day committed Jul 16, 2023
1 parent 3a9dc58 commit 79ecede
Show file tree
Hide file tree
Showing 16 changed files with 106 additions and 220 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Go Library

# [v1.9.0 - 2023-07-16]
### New
- common: add URL and Host parsing functions
- common:add more tests
- common: add dockertest helper
- dblib: add RACInfo Lookup per INI and DNS SRV record
### Changed
- use go 1.20
- update dependencies
- use docker_helper for tests
### Fixed
- dblib: fix tns server parsing RegExp

# [v1.8.1 - 2023-06-22]
### Changed
- dblib: enhance ldap functions and test
Expand Down
27 changes: 7 additions & 20 deletions dblib/oracle_docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,31 @@ import (

"github.com/ory/dockertest/v3"
"github.com/ory/dockertest/v3/docker"
"github.com/tommi2day/gomodules/common"
)

const port = "21521"
const dbPort = "21521"
const repo = "docker.io/gvenzl/oracle-xe"
const repoTag = "21.3.0-slim"
const containerTimeout = 600

var containerName string
var pool *dockertest.Pool

// prepareContainer create an Oracle Docker Container
func prepareContainer() (container *dockertest.Resource, err error) {
pool = nil
if os.Getenv("SKIP_ORACLE") != "" {
err = fmt.Errorf("skipping ORACLE Container in CI environment")
return
}
containerName = os.Getenv("CONTAINER_NAME")
if containerName == "" {
containerName = "dblib-oracledb"
containerName = "tnscli-oracledb"
}
pool, err = dockertest.NewPool("")
var pool *dockertest.Pool
pool, err = common.GetDockerPool()
if err != nil {
err = fmt.Errorf("cannot attach to docker: %v", err)
return
}
err = pool.Client.Ping()
if err != nil {
err = fmt.Errorf("could not connect to Docker: %s", err)
return
}

vendorImagePrefix := os.Getenv("VENDOR_IMAGE_PREFIX")
repoString := vendorImagePrefix + repo

Expand All @@ -57,7 +50,7 @@ func prepareContainer() (container *dockertest.Resource, err error) {
// need fixed mapping here
PortBindings: map[docker.Port][]docker.PortBinding{
"1521": {
{HostIP: "0.0.0.0", HostPort: port},
{HostIP: "0.0.0.0", HostPort: dbPort},
},
},
}, func(config *docker.HostConfig) {
Expand All @@ -72,7 +65,7 @@ func prepareContainer() (container *dockertest.Resource, err error) {
}

pool.MaxWait = containerTimeout * time.Second
target = fmt.Sprintf("oracle://%s:%s@%s:%s/xepdb1", "system", DBPASSWORD, dbhost, port)
target = fmt.Sprintf("oracle://%s:%s@%s:%s/xepdb1", "system", DBPASSWORD, dbhost, dbPort)
fmt.Printf("Wait to successfully connect to db with %s (max %ds)...\n", target, containerTimeout)
start := time.Now()
if err = pool.Retry(func() error {
Expand All @@ -92,9 +85,3 @@ func prepareContainer() (container *dockertest.Resource, err error) {
err = nil
return
}

func destroyContainer(container *dockertest.Resource) {
if err := pool.Purge(container); err != nil {
fmt.Printf("Could not purge resource: %s\n", err)
}
}
6 changes: 3 additions & 3 deletions dblib/oracle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/tommi2day/gomodules/common"

"github.com/tommi2day/gomodules/test"

"github.com/ory/dockertest/v3"
Expand All @@ -21,11 +22,10 @@ import (
const DBUSER = "system"
const DBPASSWORD = "XE-manager21"
const TIMEOUT = 5
const TESTDATA = "testdata"

var dbhost = common.GetEnv("DB_HOST", "127.0.0.1")
var oracleContainer *dockertest.Resource
var connectora = fmt.Sprintf("XE.local=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=%s)(PORT=%s)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=XEPDB1)))", dbhost, port)
var connectora = fmt.Sprintf("XE.local=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=%s)(PORT=%s)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=XEPDB1)))", dbhost, dbPort)
var target string

// makeOerr create a pseudo ORA Errorcode
Expand Down Expand Up @@ -72,7 +72,7 @@ func TestWithOracle(t *testing.T) {
oracleContainer, err = prepareContainer()
require.NoErrorf(t, err, "Oracle Server not available:%v", err)
require.NotNil(t, oracleContainer, "Prepare failed")
defer destroyContainer(oracleContainer)
defer common.DestroyDockerContainer(oracleContainer)

t.Run("Direct connect", func(t *testing.T) {
var db *sql.DB
Expand Down
15 changes: 7 additions & 8 deletions dblib/tns.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import (
"regexp"
"strings"

"github.com/tommi2day/gomodules/common"

"gopkg.in/ini.v1"

log "github.com/sirupsen/logrus"
"github.com/tommi2day/gomodules/common"
)

// TNSAddress holds host/port of an address section
// TNSAddress holds host/dbPort of an address section
type TNSAddress struct {
Host string
Port string
Expand Down Expand Up @@ -187,19 +186,19 @@ func tnsSanity(entries TNSEntries) (tnsEntries TNSEntries, deletes int) {
for k, e := range entries {
se := 0
if len(e.Name) == 0 {
log.Errorf("Entry %s has no name set", k)
log.Errorf("Sanity: Entry %s has no name set", k)
se++
}
if len(e.Desc) == 0 {
log.Errorf("Entry %s has no description set", k)
log.Errorf("Sanity: Entry %s has no description set", k)
se++
}
if len(e.Service) == 0 {
log.Errorf("Entry %s has no SERVICE_NAME or SID set", k)
log.Errorf("Sanity: Entry %s has no SERVICE_NAME or SID set", k)
se++
}
if len(e.Servers) == 0 {
log.Errorf("Entry %s has no Oracle Host set", k)
log.Errorf("Sanity: Entry %s has no SERVER set", k)
se++
}
if se > 0 {
Expand Down Expand Up @@ -238,7 +237,7 @@ func getIfile(filename string, recursiv bool) (entries TNSEntries, err error) {

// getServers extract TNSAddress part
func getServers(tnsDesc string) (servers []TNSAddress) {
re := regexp.MustCompile(`(?m)HOST\s*=\s*([\w.]+)\s*\)\s*\(\s*PORT\s*=\s*(\d+)`)
re := regexp.MustCompile(`(?m)HOST\s*=\s*([\w\-_.]+)\s*\)\s*\(\s*PORT\s*=\s*(\d+)`)
match := re.FindAllStringSubmatch(tnsDesc, -1)
for _, a := range match {
if len(a) > 1 {
Expand Down
54 changes: 11 additions & 43 deletions dblib/tns_ldap_docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package dblib

import (
"fmt"
"net/url"
"os"
"strconv"
"strings"
"time"

"github.com/tommi2day/gomodules/common"

"github.com/tommi2day/gomodules/test"

"github.com/go-ldap/ldap/v3"
Expand All @@ -20,36 +19,28 @@ const LdaprepoTag = "1.5.0"
const LdapcontainerTimeout = 120

var ldapcontainerName string
var ldappool *dockertest.Pool
var ldapContainer *dockertest.Resource

// prepareContainer create an Oracle Docker Container
// prepareContainer create an OpenLdap Docker Container
func prepareLdapContainer() (container *dockertest.Resource, err error) {
var mypool *dockertest.Pool
if os.Getenv("SKIP_LDAP") != "" {
err = fmt.Errorf("skipping LDAP Container in CI environment")
return
}
ldapcontainerName = os.Getenv("LDAP_CONTAINER_NAME")
if ldapcontainerName == "" {
ldapcontainerName = "dblib-ldap"
}
mypool, err = dockertest.NewPool("")
if err != nil {
err = fmt.Errorf("cannot attach to docker: %v", err)
return
ldapcontainerName = "tnscli-ldap"
}
err = mypool.Client.Ping()
var pool *dockertest.Pool
pool, err = common.GetDockerPool()
if err != nil {
err = fmt.Errorf("could not connect to Docker: %s", err)
return
}

vendorImagePrefix := os.Getenv("VENDOR_IMAGE_PREFIX")
repoString := vendorImagePrefix + Ldaprepo

fmt.Printf("Try to start docker container for %s:%s\n", repoString, LdaprepoTag)
container, err = mypool.RunWithOptions(&dockertest.RunOptions{
container, err = pool.RunWithOptions(&dockertest.RunOptions{
Repository: repoString,
Tag: LdaprepoTag,
Env: []string{
Expand Down Expand Up @@ -79,47 +70,24 @@ func prepareLdapContainer() (container *dockertest.Resource, err error) {
return
}

mypool.MaxWait = LdapcontainerTimeout * time.Second
myhost, myport := getLdapHostAndPort(container, "389/tcp")
pool.MaxWait = LdapcontainerTimeout * time.Second
myhost, myport := common.GetContainerHostAndPort(container, "389/tcp")
dialURL := fmt.Sprintf("ldap://%s:%d", myhost, myport)
fmt.Printf("Wait to successfully connect to Ldap with %s (max %ds)...\n", dialURL, LdapcontainerTimeout)
start := time.Now()
var l *ldap.Conn
if err = mypool.Retry(func() error {
if err = pool.Retry(func() error {
l, err = ldap.DialURL(dialURL)
return err
}); err != nil {
fmt.Printf("Could not connect to LDAP Container: %s", err)
return
}
l.Close()
_ = l.Close()
// wait 5s to init container
time.Sleep(5 * time.Second)
elapsed := time.Since(start)
fmt.Printf("LDAP Container is available after %s\n", elapsed.Round(time.Millisecond))
err = nil
ldappool = mypool
return
}

func destroyLdapContainer(container *dockertest.Resource) {
if err := ldappool.Purge(container); err != nil {
fmt.Printf("Could not purge resource: %s\n", err)
}
}

func getLdapHostAndPort(container *dockertest.Resource, portID string) (server string, port int) {
dockerURL := os.Getenv("DOCKER_HOST")
if dockerURL == "" {
containerAddress := container.GetHostPort(portID)
a := strings.Split(containerAddress, ":")
server = a[0]
port, _ = strconv.Atoi(a[1])
} else {
u, _ := url.Parse(dockerURL)
server = u.Hostname()
p := container.GetPort(portID)
port, _ = strconv.Atoi(p)
}
return
}
7 changes: 4 additions & 3 deletions dblib/tns_ldap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"testing"

"github.com/tommi2day/gomodules/common"
"github.com/tommi2day/gomodules/ldaplib"
"github.com/tommi2day/gomodules/test"

Expand Down Expand Up @@ -102,15 +103,15 @@ func TestOracleLdap(t *testing.T) {
ldapContainer, err = prepareLdapContainer()
require.NoErrorf(t, err, "Ldap Server not available")
require.NotNil(t, ldapContainer, "Prepare failed")
defer destroyLdapContainer(ldapContainer)
defer common.DestroyDockerContainer(ldapContainer)

base := LdapBaseDn
server, sslport = getLdapHostAndPort(ldapContainer, "636/tcp")
server, sslport = common.GetContainerHostAndPort(ldapContainer, "636/tcp")
lc = ldaplib.NewConfig(server, sslport, true, true, base, ldapTimeout)
context := ""

t.Run("Ldap Connect", func(t *testing.T) {
t.Logf("Connect '%s' using SSL on port %d", LdapAdminUser, sslport)
t.Logf("Connect '%s' using SSL on dbPort %d", LdapAdminUser, sslport)
err = lc.Connect(LdapAdminUser, LdapAdminPassword)
require.NoErrorf(t, err, "admin Connect returned error %v", err)
assert.NotNilf(t, lc.Conn, "Ldap Connect is nil")
Expand Down
3 changes: 1 addition & 2 deletions dblib/tns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import (
"strings"
"testing"

"github.com/tommi2day/gomodules/test"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tommi2day/gomodules/test"
)

const tnsnamesora = `
Expand Down
2 changes: 1 addition & 1 deletion ldaplib/ldap.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func NewConfig(server string, port int, tls bool, insecure bool, basedn string,
func (lc *LdapConfigType) Connect(bindDN string, bindPassword string) (err error) {
l := lc.Conn
if l != nil {
l.Close()
_ = l.Close()
l = nil
}

Expand Down
Loading

0 comments on commit 79ecede

Please sign in to comment.