Skip to content

Commit

Permalink
First e2e test completed
Browse files Browse the repository at this point in the history
  • Loading branch information
wongfei2009 committed Dec 11, 2023
1 parent 3c0e702 commit f2dcba0
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 8 deletions.
34 changes: 28 additions & 6 deletions tests/e2e/marmot_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package main_test

import (
"database/sql"
"log"
"os"
"os/exec"
"path/filepath"
"testing"
"time"

Expand All @@ -19,9 +21,25 @@ func TestMarmot(t *testing.T) {
}

func cleanup() {
os.RemoveAll("/tmp/marmot-1-*")
os.RemoveAll("/tmp/marmot-2-*")
os.RemoveAll("/tmp/marmot-3-*")
patterns := []string{
"/tmp/marmot-1*",
"/tmp/marmot-2*",
"/tmp/marmot-3*",
}

for _, pattern := range patterns {
files, err := filepath.Glob(pattern)
if err != nil {
log.Fatal(err)
}

for _, file := range files {
err := os.RemoveAll(file)
if err != nil {
log.Fatal(err)
}
}
}
os.RemoveAll("/tmp/nats")
}

Expand All @@ -34,13 +52,13 @@ func startCluster() (*exec.Cmd, *exec.Cmd, *exec.Cmd) {
createDB("/tmp/marmot-3.db")

node1 := startNode("examples/node-1-config.toml", "localhost:4221", "nats://localhost:4222/,nats://localhost:4223/")
time.Sleep(time.Second)

node2 := startNode("examples/node-2-config.toml", "localhost:4222", "nats://localhost:4221/,nats://localhost:4223/")
time.Sleep(time.Second)

node3 := startNode("examples/node-3-config.toml", "localhost:4223", "nats://localhost:4221/,nats://localhost:4222/")

time.Sleep(time.Second * 10)

return node1, node2, node3
}

Expand Down Expand Up @@ -98,7 +116,11 @@ func createDB(dbFile string) {
}

func startNode(config, addr, peers string) *exec.Cmd {
cmd := exec.Command("go", "run", "marmot", "-config", config, "-cluster-addr", addr, "-cluster-peers", peers)
cmd := exec.Command("marmot", "-config", config, "-cluster-addr", addr, "-cluster-peers", peers)
if wd, err := os.Getwd(); err == nil {
wd = wd[:len(wd)-len("/tests/e2e")]
cmd.Dir = wd
}
err := cmd.Start()
Expect(err).To(BeNil())
return cmd
Expand Down
44 changes: 42 additions & 2 deletions tests/e2e/marmot_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,48 @@
package main_test

import (
"database/sql"
"fmt"
"log"
"os/exec"
"time"

_ "github.com/mattn/go-sqlite3"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func insertBook() {
db, err := sql.Open("sqlite3", "/tmp/marmot-1.db")
if err != nil {
fmt.Println("Error opening database:", err)
return
}
defer db.Close()

query := `INSERT INTO Books (title, author, publication_year) VALUES (?, ?, ?)`
_, err = db.Exec(query, "Pride and Prejudice", "Jane Austen", 1813)
if err != nil {
fmt.Println("Error inserting book:", err)
}
}

func countBook(title string) int {
db, err := sql.Open("sqlite3", "/tmp/marmot-2.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()

var count int
err = db.QueryRow("SELECT COUNT(*) FROM Books WHERE title = ?", title).Scan(&count)
if err != nil {
log.Fatal(err)
}

return count
}

var _ = Describe("Marmot", Ordered, func() {
var node1, node2, node3 *exec.Cmd
BeforeAll(func() {
Expand All @@ -16,8 +52,12 @@ var _ = Describe("Marmot", Ordered, func() {
stopCluster(node1, node2, node3)
})
Context("when the system is running", func() {
It("should be able to run tests", func() {
Expect(true).To(BeTrue())
It("should be able to replicate a row", func() {
insertBook()

time.Sleep(time.Second * 3)

Expect(countBook("Pride and Prejudice")).To(Equal(1))
})
})
})

0 comments on commit f2dcba0

Please sign in to comment.