Skip to content

Commit

Permalink
tests: Get linking working again
Browse files Browse the repository at this point in the history
It broke when I added stricter SID checks during linking.
  • Loading branch information
horgh committed Aug 18, 2018
1 parent 30e9443 commit 48fafe8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
42 changes: 31 additions & 11 deletions internal/catbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
// Catbox holds information about a harnessed catbox.
type Catbox struct {
Name string
SID string
Port uint16
Stderr io.ReadCloser
Stdout io.ReadCloser
Expand All @@ -33,12 +34,15 @@ type Catbox struct {
var catboxDir = filepath.Join(os.Getenv("GOPATH"), "src", "github.com", "horgh",
"catbox")

func harnessCatbox(name string) (*Catbox, error) {
func harnessCatbox(
name,
sid string,
) (*Catbox, error) {
if err := buildCatbox(); err != nil {
return nil, fmt.Errorf("error building catbox: %s", err)
}

catbox, err := startCatbox(name)
catbox, err := startCatbox(name, sid)
if err != nil {
return nil, fmt.Errorf("error starting catbox: %s", err)
}
Expand Down Expand Up @@ -98,7 +102,10 @@ func buildCatbox() error {
return nil
}

func startCatbox(name string) (*Catbox, error) {
func startCatbox(
name,
sid string,
) (*Catbox, error) {
tmpDir, err := ioutil.TempDir("", "boxcat-")
if err != nil {
return nil, fmt.Errorf("error retrieving a temporary directory: %s", err)
Expand All @@ -112,7 +119,7 @@ func startCatbox(name string) (*Catbox, error) {
return nil, fmt.Errorf("error opening random port: %s", err)
}

catbox, err := runCatbox(catboxConf, listener, port, name)
catbox, err := runCatbox(catboxConf, listener, port, name, sid)
if err != nil {
_ = os.RemoveAll(tmpDir)
_ = listener.Close()
Expand Down Expand Up @@ -145,9 +152,11 @@ func runCatbox(
conf string,
ln net.Listener,
port uint16,
name string,
name,
sid string,
) (*Catbox, error) {
if err := writeConf(conf, name, ""); err != nil {
var extra string
if err := writeConf(conf, name, sid, extra); err != nil {
return nil, err
}

Expand Down Expand Up @@ -189,17 +198,28 @@ func runCatbox(
}, nil
}

func writeConf(conf, name, extra string) error {
func writeConf(
filename,
serverName,
sid,
extra string,
) error {
// -1 because we pass in fd.
buf := fmt.Sprintf(`
listen-port = %d
server-name = %s
ts6-sid = %s
connect-attempt-time = 100ms
%s
`, -1, name, extra)
`,
-1,
serverName,
sid,
extra,
)

if err := ioutil.WriteFile(conf, []byte(buf), 0644); err != nil {
return fmt.Errorf("error writing conf: %s: %s", name, err)
if err := ioutil.WriteFile(filename, []byte(buf), 0644); err != nil {
return fmt.Errorf("error writing conf: %s: %s", serverName, err)
}

return nil
Expand Down Expand Up @@ -251,7 +271,7 @@ func (c *Catbox) linkServer(other *Catbox) error {
serversConf := filepath.Join(c.ConfigDir, "servers.conf")
extra := fmt.Sprintf("servers-config = %s", serversConf)

if err := writeConf(conf, c.Name, extra); err != nil {
if err := writeConf(conf, c.Name, c.SID, extra); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion internal/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

// Test one client sending a message to another client.
func TestPRIVMSG(t *testing.T) {
catbox, err := harnessCatbox("irc.example.org")
catbox, err := harnessCatbox("irc.example.org", "000")
if err != nil {
t.Fatalf("error harnessing catbox: %s", err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/mode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import (
// Also test that the TS gets propagated between servers and a client on
// another server gets the same TS
func TestMODETS(t *testing.T) {
catbox1, err := harnessCatbox("irc1.example.org")
catbox1, err := harnessCatbox("irc1.example.org", "001")
if err != nil {
t.Fatalf("error harnessing catbox: %s", err)
}
defer catbox1.stop()

catbox2, err := harnessCatbox("irc2.example.org")
catbox2, err := harnessCatbox("irc2.example.org", "002")
if err != nil {
t.Fatalf("error harnessing catbox: %s", err)
}
Expand Down

0 comments on commit 48fafe8

Please sign in to comment.