Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more tests #6

Merged
merged 8 commits into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 0 additions & 68 deletions cmd/apply.go

This file was deleted.

81 changes: 43 additions & 38 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,61 @@ import (
"fmt"
"log"
"os"
"strings"
"sync"

"github.com/liweiyi88/onedump/dump"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)

var (
sshHost, sshUser, sshPrivateKeyFile string
dbDsn string
jobName string
dumpOptions []string
gzip bool
)
var file string

var rootCmd = &cobra.Command{
Use: "<dbdriver> </path/to/dump-file.sql>",
Short: "Dump database content from a source to a destination via cli command.",
Args: cobra.ExactArgs(2),
Use: "-f /path/to/jobs.yaml",
Short: "Dump database content from different sources to different destinations with a yaml config file.",
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
driver := strings.TrimSpace(args[0])
content, err := os.ReadFile(file)
if err != nil {
log.Fatalf("failed to read job file from %s, error: %v", file, err)
}

var oneDump dump.Dump
err = yaml.Unmarshal(content, &oneDump)
if err != nil {
log.Fatalf("failed to read job content from %s, error: %v", file, err)
}

dumpFile := strings.TrimSpace(args[1])
if dumpFile == "" {
log.Fatal("you must specify the dump file path. e.g. /download/dump.sql")
err = oneDump.Validate()
if err != nil {
log.Fatalf("invalid job configuration, error: %v", err)
}

name := "dump via cli"
if strings.TrimSpace(jobName) != "" {
name = jobName
numberOfJobs := len(oneDump.Jobs)
if numberOfJobs == 0 {
log.Printf("no job is defined in the file %s", file)
return
}

resultCh := make(chan *dump.JobResult)

for _, job := range oneDump.Jobs {
go func(job *dump.Job, resultCh chan *dump.JobResult) {
resultCh <- job.Run()
}(job, resultCh)
}

job := dump.NewJob(
name,
driver,
dumpFile,
dbDsn,
dump.WithGzip(gzip),
dump.WithSshHost(sshHost),
dump.WithSshUser(sshUser),
dump.WithPrivateKeyFile(sshPrivateKeyFile),
dump.WithDumpOptions(dumpOptions),
)
var wg sync.WaitGroup
wg.Add(numberOfJobs)
go func(resultCh chan *dump.JobResult) {
for result := range resultCh {
result.Print()
wg.Done()
}
}(resultCh)

job.Run().Print()
wg.Wait()
close(resultCh)
},
}

Expand All @@ -59,12 +70,6 @@ func Execute() {
}

func init() {
rootCmd.Flags().StringVarP(&sshHost, "ssh-host", "", "", "SSH host e.g. yourdomain.com (you can omit port as it uses 22 by default) or 56.09.139.09:33. (required) ")
rootCmd.Flags().StringVarP(&sshUser, "ssh-user", "", "root", "SSH username")
rootCmd.Flags().StringVarP(&sshPrivateKeyFile, "privatekey", "f", "", "private key file path for SSH connection")
rootCmd.Flags().StringArrayVarP(&dumpOptions, "dump-options", "", nil, "use options to overwrite or add new dump command options. e.g. for mysql: --dump-options \"--no-create-info\" --dump-options \"--skip-comments\"")
rootCmd.Flags().StringVarP(&dbDsn, "db-dsn", "d", "", "the database dsn for connection. e.g. <dbUser>:<dbPass>@tcp(<dbHost>:<dbPort>)/<dbName>")
rootCmd.MarkFlagRequired("db-dsn")
rootCmd.Flags().BoolVarP(&gzip, "gzip", "g", true, "if need to gzip the file")
rootCmd.Flags().StringVarP(&jobName, "job-name", "", "", "The dump job name")
rootCmd.Flags().StringVarP(&file, "file", "f", "", "jobs yaml file path.")
rootCmd.MarkFlagRequired("file")
}
8 changes: 7 additions & 1 deletion driver/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package driver

import (
"fmt"
"log"
"net"
"os"
"os/exec"
Expand Down Expand Up @@ -115,7 +116,12 @@ host = %s`
return fileName, fmt.Errorf("failed to create temp folder: %w", err)
}

defer file.Close()
defer func() {
err := file.Close()
if err != nil {
log.Printf("failed to close temp file for storing mysql credentials: %v", err)
}
}()

_, err = file.WriteString(contents)
if err != nil {
Expand Down
76 changes: 42 additions & 34 deletions driver/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package driver

import (
"os"
"os/exec"
"strings"
"testing"

"golang.org/x/exp/slices"
Expand Down Expand Up @@ -113,37 +115,43 @@ host = 127.0.0.1`
t.Log("removed temp credential file", fileName)
}

// func TestDump(t *testing.T) {
// dsn := "root@tcp(127.0.0.1:3306)/test_local"
// mysql, err := NewMysqlDumper(dsn, nil, false)
// if err != nil {
// t.Fatal(err)
// }

// dumpfile, err := os.CreateTemp("", "dbdump")
// if err != nil {
// t.Fatal(err)
// }
// defer dumpfile.Close()

// err = mysql.Dump(dumpfile.Name(), false)
// if err != nil {
// t.Fatal(err)
// }

// out, err := os.ReadFile(dumpfile.Name())
// if err != nil {
// t.Fatal("failed to read the test dump file")
// }

// if len(out) == 0 {
// t.Fatal("test dump file is empty")
// }

// t.Log("test dump file content size", len(out))

// err = os.Remove(dumpfile.Name())
// if err != nil {
// t.Fatal("can not cleanup the test dump file", err)
// }
// }
func TestGetSshDumpCommand(t *testing.T) {
mysql, err := NewMysqlDriver(testDBDsn, nil, false)
if err != nil {
t.Fatal(err)
}

command, err := mysql.GetSshDumpCommand()
if err != nil {
t.Errorf("failed to get dump command %v", command)
}

if !strings.Contains(command, "mysqldump --defaults-extra-file") || !strings.Contains(command, "--skip-comments --extended-insert dump_test") {
t.Errorf("unexpected command: %s", command)
}
}

func TestGetDumpCommand(t *testing.T) {
mysql, err := NewMysqlDriver(testDBDsn, nil, false)
if err != nil {
t.Fatal(err)
}

mysqldumpPath, err := exec.LookPath(mysql.MysqlDumpBinaryPath)
if err != nil {
t.Fatal(err)
}

path, args, err := mysql.GetDumpCommand()
if err != nil {
t.Error("failed to get dump command")
}

if mysqldumpPath != path {
t.Errorf("expected mysqldump path: %s, actual got: %s", mysqldumpPath, path)
}

if len(args) != 4 {
t.Errorf("get unexpected args, expected %d args, but got: %d", 4, len(args))
}
}
27 changes: 27 additions & 0 deletions dump/closer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dump

import (
"io"

"github.com/hashicorp/go-multierror"
)

type MultiCloser struct {
closers []io.Closer
}

func NewMultiCloser(closers []io.Closer) *MultiCloser {
return &MultiCloser{
closers: closers,
}
}

func (m *MultiCloser) Close() error {
var err error
for _, c := range m.closers {
if e := c.Close(); e != nil {
err = multierror.Append(err, e)
}
}
return err
}
46 changes: 46 additions & 0 deletions dump/closer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package dump

import (
"bytes"
"fmt"
"io"
"os"
"testing"
)

type mockCloser struct{}

func (m mockCloser) Close() error {
fmt.Print("close")

return nil
}

func TestNewMultiCloser(t *testing.T) {

closer1 := mockCloser{}
closer2 := mockCloser{}

closers := make([]io.Closer, 0)
closers = append(closers, closer1, closer2)

multiCloser := NewMultiCloser(closers)

old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

multiCloser.Close()

w.Close()
os.Stdout = old

var buf bytes.Buffer
io.Copy(&buf, r)

expected := buf.String()
actual := "closeclose"
if expected != actual {
t.Errorf("expected: %s, actual: %s", expected, actual)
}
}
Loading