Skip to content

Commit

Permalink
#22: Allow specifying custom docker-db version
Browse files Browse the repository at this point in the history
  • Loading branch information
kaklakariada committed Oct 27, 2022
1 parent ecfa58c commit 0707749
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 18 deletions.
2 changes: 1 addition & 1 deletion doc/developers_guide/developers_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ You must [install the server](#install-server) before running the client tests.

```shell
cd go-client
go test ./...
go test -p 1 ./...
```

### Linter
Expand Down
14 changes: 7 additions & 7 deletions go-client/Builder.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
package exasol_test_setup_abstraction_go

type Configuration struct {
type Builder struct {
configFilePath string
dockerDbVersion string
}

// New creates a new configuration object that allows creating a new TestSetupAbstraction.
func New() Configuration {
return Configuration{}
// New creates a new builder that allows creating a new TestSetupAbstraction.
func New() Builder {
return Builder{}
}

// CloudSetupConfigFilePath sets the path to the cloud setup config file.
// Call this to use a cloud test setup e.g. in AWS.
// This will fall back to a local Docker container in case the file does not exist.
func (c Configuration) CloudSetupConfigFilePath(path string) Configuration {
func (c Builder) CloudSetupConfigFilePath(path string) Builder {
c.configFilePath = path
return c
}

// DockerDbVersion sets the Exasol Docker DB version to start.
// This defaults to the version defined in exasol-test-setup-abstraction-java.
func (c Configuration) DockerDbVersion(dockerDbVersion string) Configuration {
func (c Builder) DockerDbVersion(dockerDbVersion string) Builder {
c.dockerDbVersion = dockerDbVersion
return c
}

// Start launches the test setup using the given configuration.
// Don't forget to stop the setup after usage by calling TestSetupAbstraction.Stop().
func (c Configuration) Start() (*TestSetupAbstraction, error) {
func (c Builder) Start() (*TestSetupAbstraction, error) {
return startTestSetupAbstraction(c)
}
76 changes: 76 additions & 0 deletions go-client/Builder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package exasol_test_setup_abstraction_go

import (
"os"
"path"
"testing"

"github.com/stretchr/testify/suite"
)

type BuilderSuite struct {
suite.Suite
setup *TestSetupAbstraction
}

func TestBuilderSuite(t *testing.T) {
suite.Run(t, new(BuilderSuite))
}

func (suite *BuilderSuite) BeforeTest(suiteName, testName string) {
suite.setup = nil
}

func (suite *BuilderSuite) AfterTest(suiteName, testName string) {
if suite.setup != nil {
suite.NoError(suite.setup.Stop())
}
}

func (suite *BuilderSuite) TestDefaultConfiguration() {
var err error
suite.setup, err = New().Start()
suite.NoError(err)
suite.NotNil(suite.getExasolDbVersion())
}

func (suite *BuilderSuite) TestCustomMissingConfigFile() {
var err error
suite.setup, err = New().CloudSetupConfigFilePath("missing-config-file.json").Start()
suite.NoError(err)
suite.NotNil(suite.getExasolDbVersion())
}

func (suite *BuilderSuite) TestConfigFileWithInvalidContent() {
var err error
suite.setup, err = New().CloudSetupConfigFilePath(suite.writeTempFile("invalid json content")).Start()
suite.ErrorContains(err, "failed to start server. The server did not print a port number")
suite.ErrorContains(err, "E-ETSAS-7: Failed to start server: 'Unexpected char 105 at")
suite.Nil(suite.setup)
}

func (suite *BuilderSuite) TestCustomExasolVersion() {
var err error
suite.setup, err = New().DockerDbVersion("7.1.14").Start()
suite.NoError(err)
suite.Equal("7.1.14", suite.getExasolDbVersion())
}

func (suite *BuilderSuite) writeTempFile(content string) string {
tempDir := suite.T().TempDir()
file := path.Join(tempDir, "temp-file")
err := os.WriteFile(file, []byte(content), 0600)
suite.NoError(err)
return file
}

func (suite *BuilderSuite) getExasolDbVersion() string {
db, err := suite.setup.CreateConnection()
suite.NoError(err)
defer db.Close()
row := db.QueryRow("select param_value from exa_metadata where param_name = 'databaseProductVersion'")
suite.NoError(err)
var result string
suite.NoError(row.Scan(&result))
return result
}
4 changes: 2 additions & 2 deletions go-client/TestSetupAbstraction.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ const serverVersion = "0.3.0"
// Create creates a new Exasol test setup with the given path to the config file
// and starts a local server.
// If the file does not exists, a local Docker container will be started.
func startTestSetupAbstraction(config Configuration) (*TestSetupAbstraction, error) {
server, err := startServer(serverVersion, config.configFilePath)
func startTestSetupAbstraction(config Builder) (*TestSetupAbstraction, error) {
server, err := startServer(serverVersion, config)
if err != nil {
return nil, err
}
Expand Down
22 changes: 17 additions & 5 deletions go-client/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package exasol_test_setup_abstraction_go
import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"path"
Expand All @@ -21,15 +22,14 @@ type serverProcess struct {
}

// startServer starts the server in the given version and with the given config file
func startServer(serverVersion, configFilePath string) (*serverProcess, error) {
func startServer(serverVersion string, config Builder) (*serverProcess, error) {
serverPath, err := downloadServerIfNotPresent()
if err != nil {
return nil, err
}
if configFilePath == "" {
configFilePath = fmt.Sprintf("non-existing-config-file-%d.json", time.Now().Unix())
}
process := exec.Command("java", "-jar", serverPath, configFilePath)
args := getServerProcessArguments(serverPath, config)
log.Printf("Starting server with arguments %v", args)
process := exec.Command("java", args...)
var output, errorStream bytes.Buffer
process.Stdout = &output
process.Stderr = &errorStream
Expand All @@ -44,6 +44,18 @@ func startServer(serverVersion, configFilePath string) (*serverProcess, error) {
return &serverProcess{process: process, serverEndpoint: serverEndpoint, stopped: &stopped, stoppedMutex: stoppedMutex, errorStream: &errorStream}, nil
}

func getServerProcessArguments(serverPath string, config Builder) []string {
var args = []string{}
if config.dockerDbVersion != "" {
args = append(args, "-Dcom.exasol.dockerdb.image="+config.dockerDbVersion)
}
args = append(args, "-jar", serverPath)
if config.configFilePath != "" {
args = append(args, config.configFilePath)
}
return args
}

func downloadServerIfNotPresent() (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ private static void configureLogging() {
public static void main(final String[] args) {
applyTargetDirWorkaround();
final int port = findFreePort();
final Path configPath = Path.of(args[0]).toAbsolutePath();
LOGGER.info(() -> "Starting exasol test setup using config file '" + configPath + "'...");
try (final ExasolTestSetup exasol = new ExasolTestSetupFactory(configPath).getTestSetup();
try (final ExasolTestSetup exasol = new ExasolTestSetupFactory(getConfigPath(args)).getTestSetup();
final TestSetupServer server = new TestSetupServer(exasol, port)) {
System.out.println("Server running on port: " + port);
server.join();
Expand All @@ -49,6 +47,17 @@ public static void main(final String[] args) {
}
}

private static Path getConfigPath(final String[] args) {
if (args.length>0){
final Path configPath = Path.of(args[0]).toAbsolutePath();
LOGGER.info(() -> "Starting exasol test setup using config file '" + configPath + "'");
return configPath;
}else {
LOGGER.info(() -> "Starting exasol test setup using dummy config file");
return Paths.get("non-existing-config-file-"+System.currentTimeMillis()+".json");
}
}

/**
* Apply workaround for https://github.com/exasol/exasol-test-setup-abstraction-java/issues/46
*/
Expand Down

0 comments on commit 0707749

Please sign in to comment.