cmd: add e2e command

This change unifies chihaya and chihaya-e2e binaries.
It also vendors the code missing from the project that was used in
chihaya-e2e.

Fixes #402.
This commit is contained in:
Jimmy Zelinskie
2018-08-30 20:50:04 -04:00
parent 085234044a
commit 3aa7d1a91d
6 changed files with 163 additions and 124 deletions
-5
View File
@@ -1,5 +0,0 @@
# chihaya-e2e
A very simple tool to black-box test a bittorrent tracker.
This tool uses [github.com/anacrolix/torrent/tracker](github.com/anacrolix/torrent/tracker) to make a UDP and an HTTP announce to given trackers.
It is used by chihaya for end-to-end testing the tracker during CI.
+55 -45
View File
@@ -2,52 +2,57 @@ package main
import (
"crypto/rand"
"flag"
"fmt"
"net/http"
"os"
"time"
"github.com/anacrolix/torrent/tracker"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/chihaya/chihaya/bittorrent"
"github.com/chihaya/chihaya/pkg/log"
)
func init() {
flag.StringVar(&httpTrackerURL, "http", "http://127.0.0.1:6969/announce", "the address of the HTTP tracker")
flag.StringVar(&udpTrackerURL, "udp", "udp://127.0.0.1:6969", "the address of the UDP tracker")
flag.DurationVar(&delay, "delay", 1*time.Second, "the delay between announces")
}
var (
httpTrackerURL string
udpTrackerURL string
delay time.Duration
)
func main() {
flag.Parse()
if len(httpTrackerURL) != 0 {
fmt.Println("testing HTTP...")
err := testHTTP()
if err != nil {
fmt.Println("failed:", err)
os.Exit(1)
}
fmt.Println("success")
// EndToEndRunCmdFunc implements a Cobra command that runs the end-to-end test
// suite for a Chihaya build.
func EndToEndRunCmdFunc(cmd *cobra.Command, args []string) error {
delay, err := cmd.Flags().GetDuration("delay")
if err != nil {
return err
}
if len(udpTrackerURL) != 0 {
fmt.Println("testing UDP...")
err := testUDP()
if err != nil {
fmt.Println("failed:", err)
os.Exit(1)
}
fmt.Println("success")
// Test the HTTP tracker
httpAddr, err := cmd.Flags().GetString("httpaddr")
if err != nil {
return err
}
if len(httpAddr) != 0 {
log.Info("testing HTTP...")
err := test(httpAddr, delay)
if err != nil {
return err
}
log.Info("success")
}
// Test the UDP tracker.
udpAddr, err := cmd.Flags().GetString("udpaddr")
if err != nil {
return err
}
if len(udpAddr) != 0 {
log.Info("testing UDP...")
err := test(udpAddr, delay)
if err != nil {
return err
}
log.Info("success")
}
return nil
}
func generateInfohash() [20]byte {
@@ -64,17 +69,12 @@ func generateInfohash() [20]byte {
return [20]byte(bittorrent.InfoHashFromBytes(b))
}
func testUDP() error {
func test(addr string, delay time.Duration) error {
ih := generateInfohash()
return testWithInfohash(ih, udpTrackerURL)
return testWithInfohash(ih, addr, delay)
}
func testHTTP() error {
ih := generateInfohash()
return testWithInfohash(ih, httpTrackerURL)
}
func testWithInfohash(infoHash [20]byte, url string) error {
func testWithInfohash(infoHash [20]byte, url string, delay time.Duration) error {
req := tracker.AnnounceRequest{
InfoHash: infoHash,
PeerId: [20]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20},
@@ -82,12 +82,17 @@ func testWithInfohash(infoHash [20]byte, url string) error {
Left: 100,
Uploaded: 50,
Event: tracker.Started,
IPAddress: int32(50<<24 | 10<<16 | 12<<8 | 1),
IPAddress: uint32(50<<24 | 10<<16 | 12<<8 | 1),
NumWant: 50,
Port: 10001,
}
resp, err := tracker.Announce(&http.Client{}, "ekop", url, &req)
resp, err := tracker.Announce{
TrackerUrl: url,
Request: req,
UserAgent: "chihaya-e2e",
HttpClient: &http.Client{},
}.Do()
if err != nil {
return errors.Wrap(err, "announce failed")
}
@@ -105,12 +110,17 @@ func testWithInfohash(infoHash [20]byte, url string) error {
Left: 100,
Uploaded: 50,
Event: tracker.Started,
IPAddress: int32(50<<24 | 10<<16 | 12<<8 | 2),
IPAddress: uint32(50<<24 | 10<<16 | 12<<8 | 2),
NumWant: 50,
Port: 10002,
}
resp, err = tracker.Announce(&http.Client{}, "ekop", url, &req)
resp, err = tracker.Announce{
TrackerUrl: url,
Request: req,
UserAgent: "chihaya-e2e",
HttpClient: &http.Client{},
}.Do()
if err != nil {
return errors.Wrap(err, "announce failed")
}
+33 -18
View File
@@ -9,6 +9,7 @@ import (
"runtime/trace"
"strings"
"syscall"
"time"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
@@ -132,9 +133,9 @@ func (r *Run) Stop(keepPeerStore bool) (storage.PeerStore, error) {
return r.peerStore, nil
}
// RunCmdFunc implements a Cobra command that runs an instance of Chihaya and
// handles reloading and shutdown via process signals.
func RunCmdFunc(cmd *cobra.Command, args []string) error {
// RootRunCmdFunc implements a Cobra command that runs an instance of Chihaya
// and handles reloading and shutdown via process signals.
func RootRunCmdFunc(cmd *cobra.Command, args []string) error {
configFilePath, err := cmd.Flags().GetString("config")
if err != nil {
return err
@@ -173,8 +174,8 @@ func RunCmdFunc(cmd *cobra.Command, args []string) error {
}
}
// PreRunCmdFunc handles command line flags for the Run command.
func PreRunCmdFunc(cmd *cobra.Command, args []string) error {
// RootPreRunCmdFunc handles command line flags for the Run command.
func RootPreRunCmdFunc(cmd *cobra.Command, args []string) error {
noColors, err := cmd.Flags().GetBool("nocolors")
if err != nil {
return err
@@ -230,9 +231,9 @@ func PreRunCmdFunc(cmd *cobra.Command, args []string) error {
return nil
}
// PostRunCmdFunc handles clean up of any state initialized by command line
// RootPostRunCmdFunc handles clean up of any state initialized by command line
// flags.
func PostRunCmdFunc(cmd *cobra.Command, args []string) error {
func RootPostRunCmdFunc(cmd *cobra.Command, args []string) error {
// These can be called regardless because it noops when not profiling.
pprof.StopCPUProfile()
trace.Stop()
@@ -245,22 +246,36 @@ func main() {
Use: "chihaya",
Short: "BitTorrent Tracker",
Long: "A customizable, multi-protocol BitTorrent Tracker",
PersistentPreRunE: PreRunCmdFunc,
RunE: RunCmdFunc,
PersistentPostRunE: PostRunCmdFunc,
PersistentPreRunE: RootPreRunCmdFunc,
RunE: RootRunCmdFunc,
PersistentPostRunE: RootPostRunCmdFunc,
}
rootCmd.PersistentFlags().String("cpuprofile", "", "location to save a CPU profile")
rootCmd.PersistentFlags().String("trace", "", "location to save a trace")
rootCmd.PersistentFlags().Bool("debug", false, "enable debug logging")
rootCmd.PersistentFlags().Bool("json", false, "enable json logging")
if runtime.GOOS == "windows" {
rootCmd.PersistentFlags().Bool("nocolors", true, "disable log coloring")
} else {
rootCmd.PersistentFlags().Bool("nocolors", false, "disable log coloring")
}
rootCmd.Flags().String("config", "/etc/chihaya.yaml", "location of configuration file")
rootCmd.Flags().String("cpuprofile", "", "location to save a CPU profile")
rootCmd.Flags().String("trace", "", "location to save a trace")
rootCmd.Flags().Bool("debug", false, "enable debug logging")
rootCmd.Flags().Bool("json", false, "enable json logging")
if runtime.GOOS == "windows" {
rootCmd.Flags().Bool("nocolors", true, "disable log coloring")
} else {
rootCmd.Flags().Bool("nocolors", false, "disable log coloring")
var e2eCmd = &cobra.Command{
Use: "e2e",
Short: "exec e2e tests",
Long: "Execute the Chihaya end-to-end test suite",
RunE: EndToEndRunCmdFunc,
}
e2eCmd.Flags().String("httpaddr", "http://127.0.0.1:6969/announce", "address of the HTTP tracker")
e2eCmd.Flags().String("udpaddr", "udp://127.0.0.1:6969", "address of the UDP tracker")
e2eCmd.Flags().Duration("delay", time.Second, "delay between announces")
rootCmd.AddCommand(e2eCmd)
if err := rootCmd.Execute(); err != nil {
log.Fatal("failed when executing root cobra command: " + err.Error())
}