mysqltest

package module
v0.0.0-...-eb89471 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 23, 2018 License: MIT Imports: 16 Imported by: 7

README

go-test-mysqld

Build Status

GoDoc

WARNING

This repository has been moved to github.com/lestrrat-go/test-mysqld. This repository exists so that libraries pointing to this URL will keep functioning, but this repository will NOT be updated in the future. Please use the new import path.

Create real MySQL server instance for testing

To install, simply issue a go get:

go get github.com/lestrrat/go-test-mysqld

By default importing github.com/lestrrat/go-test-mysqld will import package mysqltest

import (
    "database/sql"
    "log"
    "github.com/lestrrat/go-test-mysqld"
)

mysqld, err := mysqltest.NewMysqld(nil)
if err != nil {
   log.Fatalf("Failed to start mysqld: %s", err)
}
defer mysqld.Stop()

db, err := sql.Open("mysql", mysqld.Datasource("test", "", "", 0))
// Now use db, which is connected to a mysql db

go-test-mysqld is a port of Test::mysqld

When you create a new struct via NewMysqld() a new mysqld instance is automatically setup and launched. Don't forget to call Stop() on this struct to stop the launched mysqld

If you want to customize the configuration, create a new config and set each field on the struct:


config := mysqltest.NewConfig()
config.SkipNetworking = false
config.Port = 13306

// Starts mysqld listening on port 13306
mysqld, _ := mysqltest.NewMysqld(config)

Generating DSN

DSN strings can be generated using the DSN method:

// Use default
dsn := mysqld.DSN()

// Pass explicit parameters
dsn := mysqld.DSN(mysqltest.WithUser("foo"), mysqltest.WithPassword("passw0rd!"))

// Tell the mysql driver to parse time values
dsn := mysqld.DSN(mysqltest.WithParseTime(true))

// ...And pass the dsn to sql.Open
db, err := sql.Open("mysql", dsn)

Following is a list of possible parameters to DSN. I

Option Description Default
mysqltest.WithProto(string) Specifies the protocol ("unix" or "tcp") Depends on value of config.SkipNetworking
mysqltest.WithSocket(string) Specifies the path to the unix socket value of config.Socket
mysqltest.WithHost(string) Specifies the hostname value of config.BindAddress
mysqltest.WithPort(int) Specifies the port number value of config.Port
mysqltest.WithUser(string) Specifies the username "root"
mysqltest.WithPassword(string) Specifies the password ""
mysqltest.WithDbname(string) Specifies the database name to connect "test"
mysqltest.WithParseTime(bool) Specifies if mysql driver should parse time values to time.Time false

Documentation

Index

Constants

This section is empty.

Variables

View Source
var MysqlSearchPaths = []string{
	".",
	filepath.FromSlash("/usr/local/mysql/bin"),
}
View Source
var MysqldSearchDirs = []string{
	"bin", "libexec", "sbin",
}

Functions

func Datasource

func Datasource(options ...DatasourceOption) string

Datasource is a utility function to format the DSN that can be passed to mysqld driver.

func Dircopy

func Dircopy(from string, to string) error

Dircopy recursively copies directories and files

Types

type DatasourceOption

type DatasourceOption interface {
	Name() string
	Value() interface{}
}

DatasourceOption is an object that can be passed to the various methods that generate datasource names

func WithDbname

func WithDbname(s string) DatasourceOption

WithDbname specifies the database name to connect.

func WithHost

func WithHost(s string) DatasourceOption

WithHost specifies the host name to connect. This is only respected if connection protocol is "tcp"

func WithParseTime

func WithParseTime(t bool) DatasourceOption

WithParseTime specifies whethere the `parseTime` parameter should be appended to the DSN

func WithPassword

func WithPassword(s string) DatasourceOption

WithPassword specifies the password to use when authenticating.

func WithPort

func WithPort(p int) DatasourceOption

WithPort specifies the port number to connect. This is only respected if connection protocol is "tcp"

func WithProto

func WithProto(s string) DatasourceOption

WithProto specifies the connection protocol ("unix" or "tcp")

func WithSocket

func WithSocket(s string) DatasourceOption

WithProto specifies the path to the unix socket This is only respected if connection protocol is "unix"

func WithUser

func WithUser(s string) DatasourceOption

WithUser specifies the user name to use when authenticating.

type MysqldConfig

type MysqldConfig struct {
	BaseDir        string
	BindAddress    string
	CopyDataFrom   string
	DataDir        string
	PidFile        string
	Port           int
	SkipNetworking bool
	Socket         string
	TmpDir         string

	AutoStart      int
	MysqlInstallDb string
	Mysqld         string
}

MysqldConfig is used to configure the new mysql instance

func NewConfig

func NewConfig() *MysqldConfig

NewConfig creates a new MysqldConfig struct with default values

type TestMysqld

type TestMysqld struct {
	Config       *MysqldConfig
	Command      *exec.Cmd
	DefaultsFile string
	Guards       []func()
	LogFile      string
}

TestMysqld is the main struct that handles the execution of mysqld

func NewMysqld

func NewMysqld(config *MysqldConfig) (*TestMysqld, error)

NewMysqld creates a new TestMysqld instance

func (*TestMysqld) AssertNotRunning

func (m *TestMysqld) AssertNotRunning() error

AssertNotRunning returns nil if mysqld is not running

func (*TestMysqld) BaseDir

func (m *TestMysqld) BaseDir() string

BaseDir returns the base dir for mysqld

func (*TestMysqld) ConnectString

func (m *TestMysqld) ConnectString(port int) string

ConnectString returns the connect string `tcp(...)` or `unix(...)` This method is deprecated, and will be removed in a future version.

func (*TestMysqld) DSN

func (m *TestMysqld) DSN(options ...DatasourceOption) string

DSN creates a datasource name string that is appropriate for connecting to the database instance started by TestMysqld.

This method respects networking settings and host:port/socket settings, and provide sane defaults for those parameters. If you want to forcefully override them, you still can do so by providing explicit DatasourceOption values

func (*TestMysqld) Datasource

func (m *TestMysqld) Datasource(dbname string, user string, pass string, port int, options ...DatasourceOption) string

Datasource is a DEPRECATED method to create a datasource string that can be passed to sql.Open(). Please consider using `DSN` instead

func (*TestMysqld) ReadLog

func (m *TestMysqld) ReadLog() ([]byte, error)

ReadLog reads the output log file specified by LogFile and returns its content

func (*TestMysqld) Setup

func (m *TestMysqld) Setup() error

Setup sets up all the files and directories needed to start mysqld

func (*TestMysqld) Socket

func (m *TestMysqld) Socket() string

Socket returns the unix socket location

func (*TestMysqld) Start

func (m *TestMysqld) Start() error

Start starts the mysqld process

func (*TestMysqld) Stop

func (m *TestMysqld) Stop()

Stop explicitly stops the execution of mysqld

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL