From 3aa7d1a91d2af4a99f5fe720cd82508394b802c0 Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Thu, 30 Aug 2018 20:50:04 -0400 Subject: [PATCH] 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. --- .travis.yml | 17 ++-- Gopkg.lock | 76 +++++++++------ Gopkg.toml | 38 +++++--- cmd/chihaya-e2e/README.md | 5 - cmd/{chihaya-e2e/main.go => chihaya/e2e.go} | 100 +++++++++++--------- cmd/chihaya/main.go | 51 ++++++---- 6 files changed, 163 insertions(+), 124 deletions(-) delete mode 100644 cmd/chihaya-e2e/README.md rename cmd/{chihaya-e2e/main.go => chihaya/e2e.go} (53%) diff --git a/.travis.yml b/.travis.yml index ef13a17..3503d45 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: go go: - - 1.9.x + - 1.11.x sudo: false matrix: include: @@ -18,9 +18,8 @@ matrix: - go install github.com/chihaya/chihaya/cmd/chihaya - chihaya --config=example_config.yaml --debug& - pid=$! - # we move the installation here so chihaya has enough time to start up - - go install github.com/chihaya/chihaya/cmd/chihaya-e2e - - chihaya-e2e + - sleep 2 # wait for chihaya to start up (gross) + - chihaya e2e --debug - kill $pid # Using HEAD of dependencies - install: @@ -35,9 +34,8 @@ matrix: - go install github.com/chihaya/chihaya/cmd/chihaya - chihaya --config=example_config.yaml --debug& - pid=$! - # we move the installation here so chihaya has enough time to start up - - go install github.com/chihaya/chihaya/cmd/chihaya-e2e - - chihaya-e2e + - sleep 2 # wait for chihaya to start up (gross) + - chihaya e2e --debug - kill $pid allow_failures: # Using HEAD of dependencies @@ -53,9 +51,8 @@ matrix: - go install github.com/chihaya/chihaya/cmd/chihaya - chihaya --config=example_config.yaml --debug& - pid=$! - # we move the installation here so chihaya has enough time to start up - - go install github.com/chihaya/chihaya/cmd/chihaya-e2e - - chihaya-e2e + - sleep 2 # wait for chihaya to start up (gross) + - chihaya e2e --debug - kill $pid notifications: irc: diff --git a/Gopkg.lock b/Gopkg.lock index df6d726..5392dd2 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -12,33 +12,39 @@ revision = "f6df55f235c24f236d11dbcf665249a59ac2021f" version = "1.1" +[[projects]] + branch = "master" + name = "github.com/anacrolix/dht" + packages = ["krpc"] + revision = "cae37fd1842087605e61382e82e4f87fab27afdc" + [[projects]] branch = "master" name = "github.com/anacrolix/missinggo" packages = [ ".", - "assert", + "expect", "httptoo", "mime", - "pproffd" + "pproffd", + "slices" ] - revision = "173db517b5f8002d630f815e2097a1858c27f8ef" + revision = "60ef2fbf63df5d871ada2680d4d8a6013dcd1745" [[projects]] branch = "master" name = "github.com/anacrolix/torrent" packages = [ "bencode", - "tracker", - "util" + "tracker" ] - revision = "64d13d86a6ba04269e2abacedcdd890165901010" + revision = "4431464fd62c37843addc79822f7cd30b4467471" [[projects]] branch = "master" name = "github.com/beorn7/perks" packages = ["quantile"] - revision = "4c0e84591b9aa9e6dcfdf3e020114cd81f89d5f9" + revision = "3a771d992973f24aa725d07868b467d1ddfceafb" [[projects]] branch = "master" @@ -49,14 +55,20 @@ [[projects]] name = "github.com/davecgh/go-spew" packages = ["spew"] - revision = "346938d642f2ec3594ed81d874461961cd0faa76" - version = "v1.1.0" + revision = "8991bc29aa16c548c550c7ff78260e27b9ab7c73" + version = "v1.1.1" [[projects]] - branch = "master" name = "github.com/golang/protobuf" packages = ["proto"] - revision = "1e59b77b52bf8e4b449a57e6f79f21226d571845" + revision = "aa810b61a9c79d51363740d207bb46cf8e620ed5" + version = "v1.2.0" + +[[projects]] + name = "github.com/huandu/xstrings" + packages = ["."] + revision = "55ae428c2ac4f74d7430952ef528631e656ac92c" + version = "v1.1.0" [[projects]] name = "github.com/inconshreveable/mousetrap" @@ -73,8 +85,8 @@ [[projects]] name = "github.com/matttproud/golang_protobuf_extensions" packages = ["pbutil"] - revision = "3247c84500bff8d9fb6d579d800f20b3e091582c" - version = "v1.0.0" + revision = "c12348ce28de40eed0136aa2b644d0ee0650e56c" + version = "v1.0.1" [[projects]] branch = "master" @@ -89,10 +101,10 @@ revision = "ad98a36ba0da87206e3378c556abbfeaeaa98668" [[projects]] - branch = "master" name = "github.com/pkg/errors" packages = ["."] - revision = "30136e27e2ac8d167177e8a583aa4c3fea5be833" + revision = "645ef00459ed84a119197bfb8d8205042c6df63d" + version = "v0.8.0" [[projects]] name = "github.com/pmezard/go-difflib" @@ -110,7 +122,7 @@ branch = "master" name = "github.com/prometheus/client_model" packages = ["go"] - revision = "99fa1f4be8e564e8a6b613da7fa6f46c9edafc6c" + revision = "5c3871d89910bfb32f5fcab2aa4b9ec68e65a99f" [[projects]] branch = "master" @@ -120,34 +132,36 @@ "internal/bitbucket.org/ww/goautoneg", "model" ] - revision = "2e54d0b93cba2fd133edc32211dcc32c06ef72ca" + revision = "c7de2306084e37d54b8be01f3541a8464345e9a5" [[projects]] branch = "master" name = "github.com/prometheus/procfs" packages = [ ".", + "internal/util", + "nfs", "xfs" ] - revision = "8f918ac9ab4be3a790338bda8624fec5d71260b3" + revision = "05ee40e3a273f7245e8777337fc7b46e533a9a92" [[projects]] name = "github.com/sirupsen/logrus" packages = ["."] - revision = "d682213848ed68c0a260ca37d6dd5ace8423f5ba" - version = "v1.0.4" + revision = "3e01752db0189b9157070a0e1668a620f9a85da2" + version = "v1.0.6" [[projects]] name = "github.com/spf13/cobra" packages = ["."] - revision = "7b2c5ac9fc04fc5efafb60700713d4fa609b777b" - version = "v0.0.1" + revision = "ef82de70bb3f60c65fb8eebacbb2d122ef517385" + version = "v0.0.3" [[projects]] name = "github.com/spf13/pflag" packages = ["."] - revision = "e57e3eeb33f795204c1ca35f56c44f83227c6e66" - version = "v1.0.0" + revision = "9a97c102cda95a86cec2345a6f09f55a939babf5" + version = "v1.0.2" [[projects]] name = "github.com/stretchr/testify" @@ -155,14 +169,14 @@ "assert", "require" ] - revision = "69483b4bd14f5845b5a1e55bca19e954e827f1d0" - version = "v1.1.4" + revision = "f35b8ab0b5a2cef36673838d662e249dd9c94686" + version = "v1.2.2" [[projects]] branch = "master" name = "golang.org/x/crypto" packages = ["ssh/terminal"] - revision = "d585fd2cc9195196078f516b69daff6744ef5e84" + revision = "182538f80094b6a8efaade63a8fd8e0d9d5843dd" [[projects]] branch = "master" @@ -171,17 +185,17 @@ "unix", "windows" ] - revision = "83801418e1b59fb1880e363299581ee543af32ca" + revision = "49385e6e15226593f68b26af201feec29d5bba22" [[projects]] - branch = "v2" name = "gopkg.in/yaml.v2" packages = ["."] - revision = "287cf08546ab5e7e37d55a84f7ed3fd1db036de5" + revision = "5420a8b6744d3b0345ab293f6fcba19c978f1183" + version = "v2.2.1" [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "70d8adf87b226f38ffea02e5edd2a0adb03b5e1a567eb2ed74ab9d480b9fd68c" + inputs-digest = "073ea42bedc8b51977e04bb4e86deb764315fa1b904e1da4cf48e2247a056caf" solver-name = "gps-cdcl" solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml index 22e04dc..8f59b1f 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -1,7 +1,6 @@ - # Gopkg.toml example # -# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md +# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html # for detailed Gopkg.toml documentation. # # required = ["github.com/user/thing/cmd/thing"] @@ -17,14 +16,23 @@ # source = "github.com/myfork/project2" # # [[override]] -# name = "github.com/x/y" -# version = "2.4.0" +# name = "github.com/x/y" +# version = "2.4.0" +# +# [prune] +# non-go = false +# go-tests = true +# unused-packages = true [[constraint]] name = "github.com/SermoDigital/jose" version = "1.1.0" +[[constraint]] + branch = "master" + name = "github.com/anacrolix/torrent" + [[constraint]] name = "github.com/julienschmidt/httprouter" version = "1.1.0" @@ -37,30 +45,30 @@ branch = "master" name = "github.com/minio/sha256-simd" +[[constraint]] + name = "github.com/pkg/errors" + version = "0.8.0" + [[constraint]] name = "github.com/prometheus/client_golang" version = "0.8.0" [[constraint]] name = "github.com/sirupsen/logrus" - version = "1.0.4" + version = "1.0.6" [[constraint]] name = "github.com/spf13/cobra" - version = "0.0.1" + version = "0.0.3" [[constraint]] name = "github.com/stretchr/testify" - version = "1.1.4" + version = "1.2.2" [[constraint]] - branch = "v2" name = "gopkg.in/yaml.v2" + version = "2.2.1" -[[constraint]] - name = "github.com/pkg/errors" - branch = "master" - -[[constraint]] - name = "github.com/anacrolix/torrent" - branch = "master" \ No newline at end of file +[prune] + go-tests = true + unused-packages = true diff --git a/cmd/chihaya-e2e/README.md b/cmd/chihaya-e2e/README.md deleted file mode 100644 index 6a5a290..0000000 --- a/cmd/chihaya-e2e/README.md +++ /dev/null @@ -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. diff --git a/cmd/chihaya-e2e/main.go b/cmd/chihaya/e2e.go similarity index 53% rename from cmd/chihaya-e2e/main.go rename to cmd/chihaya/e2e.go index 474c446..47e394f 100644 --- a/cmd/chihaya-e2e/main.go +++ b/cmd/chihaya/e2e.go @@ -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") } diff --git a/cmd/chihaya/main.go b/cmd/chihaya/main.go index 0b5f27d..699f3b1 100644 --- a/cmd/chihaya/main.go +++ b/cmd/chihaya/main.go @@ -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()) }