mchirico / zDaily

Playground to hack and test ideas with Zoe
1 stars 2 forks source link

Day 10: Docker and Go #12

Open mchirico opened 3 years ago

mchirico commented 3 years ago

Docker and Go

A bit more of docker-compose and how the command can be called from Go, and used in testing.

Video

package topic

import (
    "context"
    "fmt"
    "os"
    "os/exec"
    "testing"
    "time"
)

func TestMain(m *testing.M) {
    startKafka()
    code := m.Run()
    stopKafka()
    os.Exit(code)
}

func startKafka() {
    ctx, cancel := context.WithTimeout(context.Background(), 10000*time.Millisecond)
    defer cancel()

    if err := exec.CommandContext(ctx, "bash", "-c", "cd ./../compose;docker-compose up -d").Run(); err != nil {
        // This will fail after 100 milliseconds. The 5 second sleep
        // will be interrupted.
        fmt.Printf("Error starting compose: %v\n", err)
    }
}

func stopKafka() {
    ctx, cancel := context.WithTimeout(context.Background(), 10000*time.Millisecond)
    defer cancel()

    if err := exec.CommandContext(ctx, "bash", "-c", "cd ./../compose;docker-compose down").Run(); err != nil {
        // This will fail after 100 milliseconds. The 5 second sleep
        // will be interrupted.
        fmt.Printf("Error stopping compose: %v\n", err)
    }
}

func TestCreate(t *testing.T) {

    topic := "topic0"
    broker := "localhost:29099"
    err := Create(broker, topic, 4, 1)
    if err != nil {
        t.Fatalf("Can't create topic: %s\n", topic)
    }
    err = Delete(broker, []string{topic})
    if err != nil {
        t.Fatalf("Can't delete topic: %s\n", topic)
    }

}
tacomonkautobot[bot] commented 3 years ago

mchirico, Thanks for opening this issue!

ZoeChiri commented 3 years ago

hello