~juju-qa/ubuntu/yakkety/juju/2.0-beta17

« back to all changes in this revision

Viewing changes to src/gopkg.in/juju/jujusvg.v1/iconfetcher_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-08-26 19:28:00 UTC
  • mfrom: (1.5.1)
  • Revision ID: nicholas.skaggs@canonical.com-20160826192800-b2rgj3da7tgfdca4
* New upstream release 2.0-beta16
* Remove all quilt patches
* Ensure autopkgtests run on LXD upload (LP: #1614724)
* Update debian/copyrights

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package jujusvg
2
 
 
3
 
import (
4
 
        "fmt"
5
 
        "net/http"
6
 
        "net/http/httptest"
7
 
        "strings"
8
 
 
9
 
        gc "gopkg.in/check.v1"
10
 
        "gopkg.in/juju/charm.v6-unstable"
11
 
)
12
 
 
13
 
type IconFetcherSuite struct{}
14
 
 
15
 
var _ = gc.Suite(&IconFetcherSuite{})
16
 
 
17
 
func (s *IconFetcherSuite) TestLinkFetchIcons(c *gc.C) {
18
 
        tests := map[string][]byte{
19
 
                "~charming-devs/precise/elasticsearch-2": []byte(`
20
 
                        <svg xmlns:xlink="http://www.w3.org/1999/xlink">
21
 
                                <image width="96" height="96" xlink:href="/~charming-devs/precise/elasticsearch-2.svg" />
22
 
                        </svg>`),
23
 
                "~juju-jitsu/precise/charmworld-58": []byte(`
24
 
                        <svg xmlns:xlink="http://www.w3.org/1999/xlink">
25
 
                                <image width="96" height="96" xlink:href="/~juju-jitsu/precise/charmworld-58.svg" />
26
 
                        </svg>`),
27
 
                "precise/mongodb-21": []byte(`
28
 
                        <svg xmlns:xlink="http://www.w3.org/1999/xlink">
29
 
                                <image width="96" height="96" xlink:href="/precise/mongodb-21.svg" />
30
 
                        </svg>`),
31
 
        }
32
 
        iconURL := func(ref *charm.URL) string {
33
 
                return "/" + ref.Path() + ".svg"
34
 
        }
35
 
        b, err := charm.ReadBundleData(strings.NewReader(bundle))
36
 
        c.Assert(err, gc.IsNil)
37
 
        err = b.Verify(nil, nil)
38
 
        c.Assert(err, gc.IsNil)
39
 
        fetcher := LinkFetcher{
40
 
                IconURL: iconURL,
41
 
        }
42
 
        iconMap, err := fetcher.FetchIcons(b)
43
 
        c.Assert(err, gc.IsNil)
44
 
        for charm, link := range tests {
45
 
                assertXMLEqual(c, []byte(iconMap[charm]), []byte(link))
46
 
        }
47
 
}
48
 
 
49
 
func (s *IconFetcherSuite) TestHTTPFetchIcons(c *gc.C) {
50
 
        fetchCount := 0
51
 
        ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
52
 
                fetchCount++
53
 
                fmt.Fprintln(w, fmt.Sprintf("<svg>%s</svg>", r.URL.Path))
54
 
        }))
55
 
        defer ts.Close()
56
 
 
57
 
        tsIconURL := func(ref *charm.URL) string {
58
 
                return ts.URL + "/" + ref.Path() + ".svg"
59
 
        }
60
 
        b, err := charm.ReadBundleData(strings.NewReader(bundle))
61
 
        c.Assert(err, gc.IsNil)
62
 
        err = b.Verify(nil, nil)
63
 
        c.Assert(err, gc.IsNil)
64
 
        // Only one copy of precise/mongodb-21
65
 
        b.Applications["duplicateApplication"] = &charm.ApplicationSpec{
66
 
                Charm:    "cs:precise/mongodb-21",
67
 
                NumUnits: 1,
68
 
        }
69
 
        fetcher := HTTPFetcher{
70
 
                Concurrency: 1,
71
 
                IconURL:     tsIconURL,
72
 
        }
73
 
        iconMap, err := fetcher.FetchIcons(b)
74
 
        c.Assert(err, gc.IsNil)
75
 
        c.Assert(iconMap, gc.DeepEquals, map[string][]byte{
76
 
                "~charming-devs/precise/elasticsearch-2": []byte("<svg>/~charming-devs/precise/elasticsearch-2.svg</svg>\n"),
77
 
                "~juju-jitsu/precise/charmworld-58":      []byte("<svg>/~juju-jitsu/precise/charmworld-58.svg</svg>\n"),
78
 
                "precise/mongodb-21":                     []byte("<svg>/precise/mongodb-21.svg</svg>\n"),
79
 
        })
80
 
 
81
 
        fetcher.Concurrency = 10
82
 
        iconMap, err = fetcher.FetchIcons(b)
83
 
        c.Assert(err, gc.IsNil)
84
 
        c.Assert(iconMap, gc.DeepEquals, map[string][]byte{
85
 
                "~charming-devs/precise/elasticsearch-2": []byte("<svg>/~charming-devs/precise/elasticsearch-2.svg</svg>\n"),
86
 
                "~juju-jitsu/precise/charmworld-58":      []byte("<svg>/~juju-jitsu/precise/charmworld-58.svg</svg>\n"),
87
 
                "precise/mongodb-21":                     []byte("<svg>/precise/mongodb-21.svg</svg>\n"),
88
 
        })
89
 
        c.Assert(fetchCount, gc.Equals, 6)
90
 
}
91
 
 
92
 
func (s *IconFetcherSuite) TestHTTPBadIconURL(c *gc.C) {
93
 
        ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
94
 
                http.Error(w, "bad-wolf", http.StatusForbidden)
95
 
                return
96
 
        }))
97
 
        defer ts.Close()
98
 
 
99
 
        tsIconURL := func(ref *charm.URL) string {
100
 
                return ts.URL + "/" + ref.Path() + ".svg"
101
 
        }
102
 
 
103
 
        b, err := charm.ReadBundleData(strings.NewReader(bundle))
104
 
        c.Assert(err, gc.IsNil)
105
 
        err = b.Verify(nil, nil)
106
 
        c.Assert(err, gc.IsNil)
107
 
        fetcher := HTTPFetcher{
108
 
                Concurrency: 1,
109
 
                IconURL:     tsIconURL,
110
 
        }
111
 
        iconMap, err := fetcher.FetchIcons(b)
112
 
        c.Assert(err, gc.ErrorMatches, fmt.Sprintf("cannot retrieve icon from %s.+\\.svg: 403 Forbidden.*", ts.URL))
113
 
        c.Assert(iconMap, gc.IsNil)
114
 
 
115
 
        fetcher.Concurrency = 10
116
 
        iconMap, err = fetcher.FetchIcons(b)
117
 
        c.Assert(err, gc.ErrorMatches, fmt.Sprintf("cannot retrieve icon from %s.+\\.svg: 403 Forbidden.*", ts.URL))
118
 
        c.Assert(iconMap, gc.IsNil)
119
 
}