diff options
author | Anton Evangelatov <anton.evangelatov@gmail.com> | 2019-02-07 21:38:32 +0800 |
---|---|---|
committer | Rafael Matias <rafael@skyle.net> | 2019-02-19 20:05:55 +0800 |
commit | 637a75d61a13ee8a89a702a2eadb5ace3c79e7da (patch) | |
tree | d862eaa5d9710a35abfd0de777f14e7005ca4852 /cmd/swarm/swarm-smoke/util.go | |
parent | 355d55bd349e7ca6360f626279858c31cfc59898 (diff) | |
download | go-tangerine-637a75d61a13ee8a89a702a2eadb5ace3c79e7da.tar go-tangerine-637a75d61a13ee8a89a702a2eadb5ace3c79e7da.tar.gz go-tangerine-637a75d61a13ee8a89a702a2eadb5ace3c79e7da.tar.bz2 go-tangerine-637a75d61a13ee8a89a702a2eadb5ace3c79e7da.tar.lz go-tangerine-637a75d61a13ee8a89a702a2eadb5ace3c79e7da.tar.xz go-tangerine-637a75d61a13ee8a89a702a2eadb5ace3c79e7da.tar.zst go-tangerine-637a75d61a13ee8a89a702a2eadb5ace3c79e7da.zip |
cmd/swarm/swarm-smoke: refactor generateEndpoints (#19006)
(cherry picked from commit d212535ddd5bf63a0c0b194525246480ae46c537)
Diffstat (limited to 'cmd/swarm/swarm-smoke/util.go')
-rw-r--r-- | cmd/swarm/swarm-smoke/util.go | 98 |
1 files changed, 33 insertions, 65 deletions
diff --git a/cmd/swarm/swarm-smoke/util.go b/cmd/swarm/swarm-smoke/util.go index 003973d41..87abb44b0 100644 --- a/cmd/swarm/swarm-smoke/util.go +++ b/cmd/swarm/swarm-smoke/util.go @@ -25,9 +25,11 @@ import ( "fmt" "io" "io/ioutil" + "math/rand" "net/http" "net/http/httptrace" "os" + "strings" "time" "github.com/ethereum/go-ethereum/log" @@ -36,83 +38,49 @@ import ( "github.com/ethereum/go-ethereum/swarm/api/client" "github.com/ethereum/go-ethereum/swarm/spancontext" opentracing "github.com/opentracing/opentracing-go" + "github.com/pborman/uuid" cli "gopkg.in/urfave/cli.v1" ) var ( commandName = "" + seed = int(time.Now().UTC().UnixNano()) ) -func wrapCliCommand(name string, killOnTimeout bool, command func(*cli.Context) error) func(*cli.Context) error { +func init() { + rand.Seed(int64(seed)) +} + +func httpEndpoint(host string) string { + return fmt.Sprintf("http://%s:%d", host, httpPort) +} + +func wsEndpoint(host string) string { + return fmt.Sprintf("ws://%s:%d", host, wsPort) +} + +func wrapCliCommand(name string, command func(*cli.Context, string) error) func(*cli.Context) error { return func(ctx *cli.Context) error { log.PrintOrigins(true) log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(verbosity), log.StreamHandler(os.Stdout, log.TerminalFormat(false)))) + // test uuid + tuid := uuid.New()[:8] + + commandName = name + + hosts = strings.Split(allhosts, ",") + defer func(now time.Time) { totalTime := time.Since(now) - log.Info("total time", "time", totalTime, "kb", filesize) + log.Info("total time", "tuid", tuid, "time", totalTime, "kb", filesize) metrics.GetOrRegisterResettingTimer(name+".total-time", nil).Update(totalTime) }(time.Now()) - log.Info("smoke test starting", "task", name, "timeout", timeout) - commandName = name + log.Info("smoke test starting", "tuid", tuid, "task", name, "timeout", timeout) metrics.GetOrRegisterCounter(name, nil).Inc(1) - errc := make(chan error) - done := make(chan struct{}) - - if killOnTimeout { - go func() { - <-time.After(time.Duration(timeout) * time.Second) - close(done) - }() - } - - go func() { - errc <- command(ctx) - }() - - select { - case err := <-errc: - if err != nil { - metrics.GetOrRegisterCounter(fmt.Sprintf("%s.fail", name), nil).Inc(1) - } - return err - case <-done: - metrics.GetOrRegisterCounter(fmt.Sprintf("%s.timeout", name), nil).Inc(1) - return fmt.Errorf("timeout after %v sec", timeout) - } - } -} - -func generateEndpoints(scheme string, cluster string, app string, from int, to int) { - if cluster == "prod" { - for port := from; port < to; port++ { - endpoints = append(endpoints, fmt.Sprintf("%s://%v.swarm-gateways.net", scheme, port)) - } - } else if cluster == "private-internal" { - for port := from; port < to; port++ { - endpoints = append(endpoints, fmt.Sprintf("%s://swarm-private-internal-%v:8500", scheme, port)) - } - } else { - for port := from; port < to; port++ { - endpoints = append(endpoints, fmt.Sprintf("%s://%s-%v-%s.stg.swarm-gateways.net", scheme, app, port, cluster)) - } - } - - if includeLocalhost { - endpoints = append(endpoints, "http://localhost:8500") - } -} - -//just use the first endpoint -func generateEndpoint(scheme string, cluster string, app string, from int) string { - if cluster == "prod" { - return fmt.Sprintf("%s://%v.swarm-gateways.net", scheme, from) - } else if cluster == "private-internal" { - return fmt.Sprintf("%s://swarm-private-internal-%v:8500", scheme, from) - } else { - return fmt.Sprintf("%s://%s-%v-%s.stg.swarm-gateways.net", scheme, app, from, cluster) + return command(ctx, tuid) } } @@ -174,11 +142,11 @@ func fetchFeed(topic string, user string, endpoint string, original []byte, ruid } // fetch is getting the requested `hash` from the `endpoint` and compares it with the `original` file -func fetch(hash string, endpoint string, original []byte, ruid string) error { +func fetch(hash string, endpoint string, original []byte, ruid string, tuid string) error { ctx, sp := spancontext.StartSpan(context.Background(), "upload-and-sync.fetch") defer sp.Finish() - log.Trace("http get request", "ruid", ruid, "api", endpoint, "hash", hash) + log.Info("http get request", "tuid", tuid, "ruid", ruid, "endpoint", endpoint, "hash", hash) var tn time.Time reqUri := endpoint + "/bzz:/" + hash + "/" @@ -202,7 +170,7 @@ func fetch(hash string, endpoint string, original []byte, ruid string) error { log.Error(err.Error(), "ruid", ruid) return err } - log.Trace("http get response", "ruid", ruid, "api", endpoint, "hash", hash, "code", res.StatusCode, "len", res.ContentLength) + log.Info("http get response", "tuid", tuid, "ruid", ruid, "endpoint", endpoint, "hash", hash, "code", res.StatusCode, "len", res.ContentLength) if res.StatusCode != 200 { err := fmt.Errorf("expected status code %d, got %v", 200, res.StatusCode) @@ -230,14 +198,14 @@ func fetch(hash string, endpoint string, original []byte, ruid string) error { } // upload an arbitrary byte as a plaintext file to `endpoint` using the api client -func upload(r io.Reader, size int, endpoint string) (string, error) { +func upload(data []byte, endpoint string) (string, error) { swarm := client.NewClient(endpoint) f := &client.File{ - ReadCloser: ioutil.NopCloser(r), + ReadCloser: ioutil.NopCloser(bytes.NewReader(data)), ManifestEntry: api.ManifestEntry{ ContentType: "text/plain", Mode: 0660, - Size: int64(size), + Size: int64(len(data)), }, } |