...
1 package main
2
3 import (
4 "bytes"
5 "flag"
6 "fmt"
7 "golang.conradwood.net/go-easyops/authremote"
8 "golang.conradwood.net/go-easyops/http"
9 "golang.conradwood.net/go-easyops/utils"
10 "os"
11 "strings"
12 "time"
13 )
14
15 var (
16 test_cookie = flag.Bool("test_cookies", false, "if true test cookies")
17 direct = flag.Bool("direct", false, "if true use direct access mode instead of urlcacher")
18 dur = flag.Duration("duration", time.Duration(10)*time.Second, "max duration of http request context")
19 timeout = flag.Duration("timeout", time.Duration(5)*time.Second, "timeout of http request")
20 testfile = flag.String("testfile", "", "if set, use this file as a list of urls to download from cache and directly and compare")
21 )
22
23 func main() {
24 flag.Parse()
25 if *test_cookie {
26 utils.Bail("failed cookie", TestCookie())
27 os.Exit(0)
28 }
29 if *testfile != "" {
30 utils.Bail("failed test", TestFile())
31 os.Exit(0)
32 }
33 url := flag.Args()[0]
34
35 started := time.Now()
36 h := getClient()
37 hr := h.Head(url)
38 hs := hr.Header("content-length")
39 fmt.Printf("Content-Length: %s\n", hs)
40
41 h = getClient()
42 hr = h.Get(url)
43 err := hr.Error()
44 utils.Bail("failed to get url", err)
45 dur := time.Since(started)
46 fmt.Printf("Duration: %0.2fs\n", dur.Seconds())
47 }
48 func getClient() http.HTTPIF {
49 var h http.HTTPIF
50 if *direct {
51 h = http.NewDirectClient()
52 } else {
53 ctx := authremote.ContextWithTimeout(*dur)
54 h = http.NewCachingClient(ctx)
55 }
56 return h
57 }
58 func TestFile() error {
59 b, err := utils.ReadFile(*testfile)
60 if err != nil {
61 return err
62 }
63 sx := strings.Split(string(b), "\n")
64 for _, line := range sx {
65 if len(line) < 3 {
66 continue
67 }
68 if strings.Contains(line, "latest") {
69 continue
70 }
71 if strings.Contains(line, "list") {
72 continue
73 }
74 err = compare(line)
75 if err != nil {
76 return fmt.Errorf("url %s failed: %s", line, err)
77 }
78 }
79 return nil
80 }
81 func compare(url string) error {
82 fmt.Printf("Comparing %s..", url)
83 fmt.Printf("fetching direct...")
84 var h http.HTTPIF
85 h = http.NewDirectClient()
86 h.SetHeader("accept-encoding", "*")
87 h.SetTimeout(*timeout)
88 hr := h.Get(url)
89 err := hr.Error()
90 if err != nil {
91 fmt.Printf("Body: %s\n", hr.Body())
92 return fmt.Errorf("Unable to retrieve %s direct: %s", url, err)
93 }
94 b1 := hr.Body()
95
96 fmt.Printf("fetching cached #1...")
97 ctx := authremote.ContextWithTimeout(*dur)
98 h = http.NewCachingClient(ctx)
99 h.SetTimeout(*timeout)
100 h.SetTimeout(*timeout)
101 hr = h.Get(url)
102 err = hr.Error()
103 if err != nil {
104 return fmt.Errorf("Unable to retrieve %s via 1st cached attempt: %s", url, err)
105 }
106 b2 := hr.Body()
107
108 fmt.Printf("Comparing 1/2...")
109 if !bytes.Equal(b1, b2) {
110 return fmt.Errorf("URL %s - b1 (%d bytes)/b2 (%d bytes) mismatch", url, len(b1), len(b2))
111 }
112
113 fmt.Printf("fetching cached #2...")
114 ctx = authremote.ContextWithTimeout(*dur)
115 h = http.NewCachingClient(ctx)
116 h.SetTimeout(*timeout)
117 hr = h.Get(url)
118 err = hr.Error()
119 if err != nil {
120 return fmt.Errorf("Unable to retrieve %s via 2nd cached attempt: %s", url, err)
121 }
122 b3 := hr.Body()
123
124 fmt.Printf("Comparing 1/3...")
125 if !bytes.Equal(b1, b3) {
126 return fmt.Errorf("URL %s - b1 (%d bytes)/b3 (%d bytes) mismatch", url, len(b1), len(b3))
127 }
128 fmt.Printf("OK\n")
129 return nil
130 }
131
View as plain text