From 79ecede5770d9c3f068f828b80037d2bd6da92eb Mon Sep 17 00:00:00 2001 From: Tommi2Day Date: Sun, 16 Jul 2023 18:10:29 +0200 Subject: [PATCH] use new common functions --- CHANGELOG.md | 13 ++++++++ dblib/oracle_docker_test.go | 27 +++++----------- dblib/oracle_test.go | 6 ++-- dblib/tns.go | 15 +++++---- dblib/tns_ldap_docker_test.go | 54 +++++++------------------------- dblib/tns_ldap_test.go | 7 +++-- dblib/tns_test.go | 3 +- ldaplib/ldap.go | 2 +- ldaplib/ldap_docker_test.go | 42 ++++--------------------- ldaplib/ldap_test.go | 24 +++++++------- maillib/mail_docker_test.go | 49 +++++++++-------------------- maillib/mail_test.go | 9 ++++-- pwlib/get_password_test.go | 4 +-- pwlib/openssl_test.go | 4 +-- pwlib/vault_docker_test.go | 59 +++++++---------------------------- pwlib/vault_test.go | 8 +++-- 16 files changed, 106 insertions(+), 220 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 02e5bfc..78b9706 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/dblib/oracle_docker_test.go b/dblib/oracle_docker_test.go index 71a1e8c..a9b3999 100644 --- a/dblib/oracle_docker_test.go +++ b/dblib/oracle_docker_test.go @@ -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 @@ -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) { @@ -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 { @@ -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) - } -} diff --git a/dblib/oracle_test.go b/dblib/oracle_test.go index 24b21c4..7ca7929 100644 --- a/dblib/oracle_test.go +++ b/dblib/oracle_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/tommi2day/gomodules/common" + "github.com/tommi2day/gomodules/test" "github.com/ory/dockertest/v3" @@ -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 @@ -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 diff --git a/dblib/tns.go b/dblib/tns.go index 10338fe..0fdd6e8 100644 --- a/dblib/tns.go +++ b/dblib/tns.go @@ -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 @@ -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 { @@ -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 { diff --git a/dblib/tns_ldap_docker_test.go b/dblib/tns_ldap_docker_test.go index a8de4bd..751cae9 100644 --- a/dblib/tns_ldap_docker_test.go +++ b/dblib/tns_ldap_docker_test.go @@ -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" @@ -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{ @@ -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 } diff --git a/dblib/tns_ldap_test.go b/dblib/tns_ldap_test.go index 6871289..1fd2ed6 100644 --- a/dblib/tns_ldap_test.go +++ b/dblib/tns_ldap_test.go @@ -5,6 +5,7 @@ import ( "os" "testing" + "github.com/tommi2day/gomodules/common" "github.com/tommi2day/gomodules/ldaplib" "github.com/tommi2day/gomodules/test" @@ -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") diff --git a/dblib/tns_test.go b/dblib/tns_test.go index 61d9fd3..758d3b7 100644 --- a/dblib/tns_test.go +++ b/dblib/tns_test.go @@ -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 = ` diff --git a/ldaplib/ldap.go b/ldaplib/ldap.go index 2356bc9..ffa9fb2 100644 --- a/ldaplib/ldap.go +++ b/ldaplib/ldap.go @@ -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 } diff --git a/ldaplib/ldap_docker_test.go b/ldaplib/ldap_docker_test.go index 69a653e..29238b2 100644 --- a/ldaplib/ldap_docker_test.go +++ b/ldaplib/ldap_docker_test.go @@ -2,15 +2,13 @@ package ldaplib import ( "fmt" - "net/url" "os" - "strconv" - "strings" "time" "github.com/go-ldap/ldap/v3" "github.com/ory/dockertest/v3" "github.com/ory/dockertest/v3/docker" + "github.com/tommi2day/gomodules/common" ) const repo = "docker.io/osixia/openldap" @@ -18,23 +16,17 @@ const repoTag = "1.5.0" const containerTimeout = 120 var containerName string -var pool *dockertest.Pool var ldapContainer *dockertest.Resource // prepareContainer create an Oracle Docker Container func prepareContainer() (container *dockertest.Resource, err error) { - pool = nil if os.Getenv("SKIP_LDAP") != "" { err = fmt.Errorf("skipping ORACLE Container in CI environment") return } + var pool *dockertest.Pool containerName = os.Getenv("CONTAINER_NAME") - pool, err = dockertest.NewPool("") - if err != nil { - err = fmt.Errorf("cannot attach to docker: %v", err) - return - } - err = pool.Client.Ping() + pool, err = common.GetDockerPool() if err != nil { err = fmt.Errorf("could not connect to Docker: %s", err) return @@ -61,7 +53,7 @@ func prepareContainer() (container *dockertest.Resource, err error) { /* PortBindings: map[docker.Port][]docker.PortBinding{ "389": { - {HostIP: "0.0.0.0", HostPort: fmt.Sprintf("%d", port)}, + {HostIP: "0.0.0.0", HostPort: fmt.Sprintf("%d", ldapPort)}, }, "636": { {HostIP: "0.0.0.0", HostPort: fmt.Sprintf("%d", sslport)}, @@ -80,7 +72,7 @@ func prepareContainer() (container *dockertest.Resource, err error) { } pool.MaxWait = containerTimeout * time.Second - host, port := getHostAndPort(container, "389/tcp") + host, port := common.GetContainerHostAndPort(container, "389/tcp") dialURL := fmt.Sprintf("ldap://%s:%d", host, port) fmt.Printf("Wait to successfully connect to Ldap with %s (max %ds)...\n", dialURL, containerTimeout) start := time.Now() @@ -92,7 +84,7 @@ func prepareContainer() (container *dockertest.Resource, err error) { 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) @@ -100,25 +92,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) - } -} - -func getHostAndPort(container *dockertest.Resource, portID string) (server string, port int) { - dockerURL := os.Getenv("DOCKER_HOST") - if dockerURL == "" { - address := container.GetHostPort(portID) - a := strings.Split(address, ":") - 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 -} diff --git a/ldaplib/ldap_test.go b/ldaplib/ldap_test.go index 74c77ba..09e56e7 100644 --- a/ldaplib/ldap_test.go +++ b/ldaplib/ldap_test.go @@ -4,6 +4,8 @@ import ( "os" "testing" + "github.com/tommi2day/gomodules/common" + ldap "github.com/go-ldap/ldap/v3" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -16,7 +18,7 @@ const LdapAdminUser = "cn=admin," + LdapBaseDn const LdapAdminPassword = "admin" const LdapConfigPassword = "config" -var port = 10389 +var ldapPort = 10389 var sslport = 10636 var lc *LdapConfigType var timeout = 20 @@ -26,7 +28,7 @@ func TestLdapConfig(t *testing.T) { lc = NewConfig("ldap.test", 0, true, true, LdapBaseDn, timeout) actual := lc assert.Equal(t, "ldap.test", actual.Server, "Server not equal") - assert.Equal(t, 636, actual.Port, "with tls=true port should be 636") + assert.Equal(t, 636, actual.Port, "with tls=true ldapPort should be 636") assert.Equal(t, "ldaps://ldap.test:636", actual.URL, "with tls=true should be ldaps") }) } @@ -43,25 +45,25 @@ func TestBaseLdap(t *testing.T) { ldapContainer, err = prepareContainer() require.NoErrorf(t, err, "Ldap Server not available") require.NotNil(t, ldapContainer, "Prepare failed") - defer destroyContainer(ldapContainer) + defer common.DestroyDockerContainer(ldapContainer) - server, port = getHostAndPort(ldapContainer, "389/tcp") + server, ldapPort = common.GetContainerHostAndPort(ldapContainer, "389/tcp") base := LdapBaseDn - lc = NewConfig(server, port, false, false, LdapBaseDn, timeout) + lc = NewConfig(server, ldapPort, false, false, LdapBaseDn, timeout) t.Run("Anonymous Connect", func(t *testing.T) { - t.Logf("Connect anonymous plain on port %d", port) + t.Logf("Connect anonymous plain on ldapPort %d", ldapPort) err = lc.Connect("", "") l = lc.Conn require.NoErrorf(t, err, "anonymous Connect returned error: %v", err) assert.NotNilf(t, l, "Ldap Connect is nil") assert.IsType(t, &ldap.Conn{}, l, "returned object ist not ldap connection") - l.Close() + _ = l.Close() }) // test container should not be validaed - server, sslport = getHostAndPort(ldapContainer, "636/tcp") + server, sslport = common.GetContainerHostAndPort(ldapContainer, "636/tcp") lc = NewConfig(server, sslport, true, true, LdapBaseDn, timeout) t.Run("Admin SSL Connect", func(t *testing.T) { - t.Logf("Connect Admin '%s' using SSL on port %d", LdapAdminUser, sslport) + t.Logf("Connect Admin '%s' using SSL on ldapPort %d", LdapAdminUser, sslport) err = lc.Connect(LdapAdminUser, LdapAdminPassword) l = lc.Conn require.NoErrorf(t, err, "admin Connect returned error %v", err) @@ -124,14 +126,14 @@ func TestBaseLdap(t *testing.T) { require.NoErrorf(t, err, "Generate Password returned Error: %v", err) assert.NotEmptyf(t, genPass, "no password was generated") t.Logf("generated Password: %s", genPass) - l.Close() + _ = l.Close() // reconnect with new password err = lc.Connect(userDN, genPass) l = lc.Conn assert.NoErrorf(t, err, "Test Bind with generated password returned error %v", err) if l != nil { - l.Close() + _ = l.Close() } }) diff --git a/maillib/mail_docker_test.go b/maillib/mail_docker_test.go index eae25c5..ff288a7 100644 --- a/maillib/mail_docker_test.go +++ b/maillib/mail_docker_test.go @@ -1,15 +1,15 @@ package maillib import ( - "bytes" "fmt" "net" - "github.com/tommi2day/gomodules/test" - "os" "time" + "github.com/tommi2day/gomodules/common" + "github.com/tommi2day/gomodules/test" + "github.com/ory/dockertest/v3" "github.com/ory/dockertest/v3/docker" ) @@ -24,13 +24,11 @@ const imapsPort = 31993 const containerTimeout = 120 var mailContainerName string -var mailPool *dockertest.Pool var mailContainer *dockertest.Resource var mailServer = "127.0.0.1" // prepareContainer create an Oracle Docker Container func prepareMailContainer() (container *dockertest.Resource, err error) { - mailPool = nil if os.Getenv("SKIP_MAIL") != "" { err = fmt.Errorf("skipping Mail Container in CI environment") return @@ -44,22 +42,16 @@ func prepareMailContainer() (container *dockertest.Resource, err error) { if mailContainerName == "" { mailContainerName = "mailserver" } - mailPool, err = dockertest.NewPool("") + pool, err := common.GetDockerPool() if err != nil { err = fmt.Errorf("cannot attach to docker: %v", err) return } - err = mailPool.Client.Ping() - if err != nil { - err = fmt.Errorf("could not connect to Docker: %s", err) - return - } vendorImagePrefix := os.Getenv("VENDOR_IMAGE_PREFIX") repoString := vendorImagePrefix + mailRepo - fmt.Printf("Try to start docker container for %s:%s\n", repoString, mailRepoTag) - container, err = mailPool.RunWithOptions(&dockertest.RunOptions{ + container, err = pool.RunWithOptions(&dockertest.RunOptions{ Repository: repoString, Tag: mailRepoTag, @@ -115,11 +107,11 @@ func prepareMailContainer() (container *dockertest.Resource, err error) { return } - mailPool.MaxWait = containerTimeout * time.Second + pool.MaxWait = containerTimeout * time.Second fmt.Printf("Wait to successfully connect to Mailserver with %s:%d (max %ds)...\n", mailServer, tlsPort, containerTimeout) start := time.Now() var c net.Conn - if err = mailPool.Retry(func() error { + if err = pool.Retry(func() error { c, err = net.Dial("tcp", fmt.Sprintf("%s:%d", mailServer, tlsPort)) if err != nil { fmt.Printf("Err:%s\n", err) @@ -132,7 +124,7 @@ func prepareMailContainer() (container *dockertest.Resource, err error) { _ = c.Close() // show env - // execCmd(container, []string{"bash", "-c", "env|sort"}) + // cmdout, _, err=execCmd(container, []string{"bash", "-c", "env|sort"}) // wait 20s to init container time.Sleep(20 * time.Second) @@ -140,27 +132,14 @@ func prepareMailContainer() (container *dockertest.Resource, err error) { fmt.Printf("Mail Container is available after %s\n", elapsed.Round(time.Millisecond)) // test main.cf - execCmd(container, []string{"ls", "-l", "/etc/postfix/main.cf"}) - - err = nil - return -} - -// destroy container resource -func destroyMailContainer(container *dockertest.Resource) { - if err := mailPool.Purge(container); err != nil { - fmt.Printf("Could not purge resource: %s\n", err) - } -} - -// executes an OS cmd within container and print output -func execCmd(container *dockertest.Resource, cmd []string) { - var cmdout bytes.Buffer - cmdout.Reset() - _, err := container.Exec(cmd, dockertest.ExecOptions{StdOut: &cmdout}) + cmdout := "" + cmd := []string{"/bin/ls", "-l", "/etc/postfix/*"} + cmdout, _, err = common.ExecDockerCmd(container, cmd) if err != nil { fmt.Printf("Exec Error %s", err) } else { - fmt.Printf("Cmd:%v\n %s", cmd, cmdout.String()) + fmt.Printf("Cmd:%v\n %s", cmd, cmdout) } + err = nil + return } diff --git a/maillib/mail_test.go b/maillib/mail_test.go index 6cf78c6..8862d5d 100644 --- a/maillib/mail_test.go +++ b/maillib/mail_test.go @@ -7,9 +7,11 @@ import ( "testing" "time" - "github.com/emersion/go-imap" + "github.com/tommi2day/gomodules/common" + "github.com/tommi2day/gomodules/test" + "github.com/emersion/go-imap" "github.com/stretchr/testify/require" "github.com/wneessen/go-mail" @@ -71,12 +73,12 @@ func TestMail(t *testing.T) { if os.Getenv("SKIP_MAIL") != "" { t.Skip("Skipping Mail testing in CI environment") } - + test.Testinit(t) var err error mailContainer, err = prepareMailContainer() require.NoErrorf(t, err, "Mailserver not available: %s", err) require.NotNil(t, mailContainer, "Prepare failed") - defer destroyMailContainer(mailContainer) + defer common.DestroyDockerContainer(mailContainer) t.Run("Send Mail anonym", func(t *testing.T) { s := NewSendMailConfig(mailServer, smtpPort, "", "") @@ -88,6 +90,7 @@ func TestMail(t *testing.T) { assert.NoErrorf(t, err, "Sendmail anonym returned error %v", err) }) t.Run("Send Mail TLS 25", func(t *testing.T) { + // will not work with mailserver 12.1.0,: no SMTP_AUTH supported s := NewSendMailConfig(mailServer, smtpPort, FROM, rootPass) s.ServerConfig.EnableTLS(true) l := NewMail(FROM, TO) diff --git a/pwlib/get_password_test.go b/pwlib/get_password_test.go index 3214590..5826cf6 100644 --- a/pwlib/get_password_test.go +++ b/pwlib/get_password_test.go @@ -4,10 +4,10 @@ import ( "os" "testing" - "github.com/tommi2day/gomodules/test" - "github.com/tommi2day/gomodules/common" + "github.com/tommi2day/gomodules/test" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) diff --git a/pwlib/openssl_test.go b/pwlib/openssl_test.go index fac42ce..16781c3 100644 --- a/pwlib/openssl_test.go +++ b/pwlib/openssl_test.go @@ -10,10 +10,10 @@ import ( "strings" "testing" - "github.com/tommi2day/gomodules/test" - "github.com/tommi2day/gomodules/common" + "github.com/tommi2day/gomodules/test" + openssl "github.com/Luzifer/go-openssl/v4" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" diff --git a/pwlib/vault_docker_test.go b/pwlib/vault_docker_test.go index b1eebd6..a8d25f7 100644 --- a/pwlib/vault_docker_test.go +++ b/pwlib/vault_docker_test.go @@ -1,47 +1,37 @@ package pwlib import ( - "bytes" "fmt" "net/http" - "net/url" "os" - "strconv" - "strings" "time" - "github.com/tommi2day/gomodules/test" + "github.com/tommi2day/gomodules/common" "github.com/ory/dockertest/v3" "github.com/ory/dockertest/v3/docker" + "github.com/tommi2day/gomodules/test" ) const repo = "docker.io/hashicorp/vault" -const repoTag = "1.13.1" +const repoTag = "1.14.0" const containerTimeout = 120 const rootToken = "pwlib-test" var containerName string -var pool *dockertest.Pool // prepareVaultContainer create an Oracle Docker Container func prepareVaultContainer() (container *dockertest.Resource, err error) { - pool = nil if os.Getenv("SKIP_VAULT") != "" { err = fmt.Errorf("skipping ORACLE Container in CI environment") return } containerName = os.Getenv("CONTAINER_NAME") - pool, err = dockertest.NewPool("") + 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 @@ -81,7 +71,7 @@ func prepareVaultContainer() (container *dockertest.Resource, err error) { } pool.MaxWait = containerTimeout * time.Second - host, port := getHostAndPort(container, "8200/tcp") + host, port := common.GetContainerHostAndPort(container, "8200/tcp") address := fmt.Sprintf("http://%s:%d", host, port) fmt.Printf("Wait to successfully connect to Vault with %s (max %ds)...\n", address, containerTimeout) start := time.Now() @@ -107,41 +97,14 @@ func prepareVaultContainer() (container *dockertest.Resource, err error) { fmt.Printf("vault Container is available after %s\n", elapsed.Round(time.Millisecond)) // provision - execCmd(container, []string{"/vault_provision/vault_init.sh"}) - err = nil - return -} - -func destroyContainer(container *dockertest.Resource) { - if err := pool.Purge(container); err != nil { - fmt.Printf("Could not purge resource: %s\n", err) - } -} - -func getHostAndPort(container *dockertest.Resource, portID string) (server string, port int) { - dockerURL := os.Getenv("DOCKER_HOST") - if dockerURL == "" { - address := container.GetHostPort(portID) - a := strings.Split(address, ":") - 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 -} - -// executes an OS cmd within container and print output -func execCmd(container *dockertest.Resource, cmd []string) { - var cmdout bytes.Buffer - cmdout.Reset() - _, err := container.Exec(cmd, dockertest.ExecOptions{StdOut: &cmdout}) + cmdout := "" + cmd := []string{"/vault_provision/vault_init.sh"} + cmdout, _, err = common.ExecDockerCmd(container, cmd) if err != nil { fmt.Printf("Exec Error %s", err) } else { - fmt.Printf("Cmd:%v\n %s", cmd, cmdout.String()) + fmt.Printf("Cmd:%v\n %s", cmd, cmdout) } + err = nil + return } diff --git a/pwlib/vault_test.go b/pwlib/vault_test.go index d4f0c47..7212604 100644 --- a/pwlib/vault_test.go +++ b/pwlib/vault_test.go @@ -6,6 +6,8 @@ import ( "path" "testing" + "github.com/tommi2day/gomodules/common" + "github.com/tommi2day/gomodules/test" vault "github.com/hashicorp/vault/api" @@ -23,10 +25,10 @@ func TestVault(t *testing.T) { vaultContainer, err := prepareVaultContainer() require.NoErrorf(t, err, "Vault Server not available") require.NotNil(t, vaultContainer, "Prepare failed") - defer destroyContainer(vaultContainer) + defer common.DestroyDockerContainer(vaultContainer) - host, port := getHostAndPort(vaultContainer, "8200/tcp") - address := fmt.Sprintf("http://%s:%d", host, port) + host, vaultPort := common.GetContainerHostAndPort(vaultContainer, "8200/tcp") + address := fmt.Sprintf("http://%s:%d", host, vaultPort) _ = os.Unsetenv("VAULT_ADDR") _ = os.Unsetenv("VAULT_TOKEN") t.Run("Vault Connect direct", func(t *testing.T) {