~rogpeppe/juju-core/axwalk-lp1300889-disable-mongo-keyfile

« back to all changes in this revision

Viewing changes to environs/tools/validation_test.go

  • Committer: Ian Booth
  • Date: 2013-08-13 04:55:12 UTC
  • mto: (1603.8.2 simplify-tools-search)
  • mto: This revision was merged to the branch mainline in revision 1680.
  • Revision ID: ian.booth@canonical.com-20130813045512-e68m4emfaoyn01h9
Add support for parsing and validating tools metadata

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2012, 2013 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package tools
 
5
 
 
6
import (
 
7
        "net/http"
 
8
 
 
9
        gc "launchpad.net/gocheck"
 
10
 
 
11
        "launchpad.net/juju-core/environs/config"
 
12
        "launchpad.net/juju-core/environs/simplestreams"
 
13
        coretesting "launchpad.net/juju-core/testing"
 
14
)
 
15
 
 
16
type ValidateSuite struct {
 
17
        home      *coretesting.FakeHome
 
18
        oldClient *http.Client
 
19
}
 
20
 
 
21
var _ = gc.Suite(&ValidateSuite{})
 
22
 
 
23
func (s *ValidateSuite) makeLocalMetadata(c *gc.C, version, region, series, endpoint string) error {
 
24
        tm := ToolsMetadata{
 
25
                Version:  version,
 
26
                Release:  series,
 
27
                Arch:     "amd64",
 
28
                Path:     "/tools/tools.tar.gz",
 
29
                Size:     1234,
 
30
                FileType: "tar.gz",
 
31
                Hash:     "f65a92b3b41311bdf398663ee1c5cd0c",
 
32
        }
 
33
        cloudSpec := simplestreams.CloudSpec{
 
34
                Region:   region,
 
35
                Endpoint: endpoint,
 
36
        }
 
37
        _, err := MakeBoilerplate("", series, &tm, &cloudSpec, false)
 
38
        if err != nil {
 
39
                return err
 
40
        }
 
41
 
 
42
        t := &http.Transport{}
 
43
        t.RegisterProtocol("file", http.NewFileTransport(http.Dir("/")))
 
44
        s.oldClient = simplestreams.SetHttpClient(&http.Client{Transport: t})
 
45
        return nil
 
46
}
 
47
 
 
48
func (s *ValidateSuite) SetUpTest(c *gc.C) {
 
49
        s.home = coretesting.MakeEmptyFakeHome(c)
 
50
}
 
51
 
 
52
func (s *ValidateSuite) TearDownTest(c *gc.C) {
 
53
        s.home.Restore()
 
54
        if s.oldClient != nil {
 
55
                simplestreams.SetHttpClient(s.oldClient)
 
56
        }
 
57
}
 
58
 
 
59
func (s *ValidateSuite) TestMatch(c *gc.C) {
 
60
        s.makeLocalMetadata(c, "1.11.2", "region-2", "raring", "some-auth-url")
 
61
        metadataDir := config.JujuHomePath("")
 
62
        params := &MetadataLookupParams{
 
63
                Version:       "1.11.2",
 
64
                Region:        "region-2",
 
65
                Series:        "raring",
 
66
                Architectures: []string{"amd64"},
 
67
                Endpoint:      "some-auth-url",
 
68
                BaseURLs:      []string{"file://" + metadataDir},
 
69
        }
 
70
        versions, err := ValidateToolsMetadata(params)
 
71
        c.Assert(err, gc.IsNil)
 
72
        c.Assert(versions, gc.DeepEquals, []string{"1.11.2-raring-amd64"})
 
73
}
 
74
 
 
75
func (s *ValidateSuite) TestNoMatch(c *gc.C) {
 
76
        s.makeLocalMetadata(c, "1.11.2", "region-2", "raring", "some-auth-url")
 
77
        metadataDir := config.JujuHomePath("")
 
78
        params := &MetadataLookupParams{
 
79
                Version:       "1.11.2",
 
80
                Region:        "region-2",
 
81
                Series:        "precise",
 
82
                Architectures: []string{"amd64"},
 
83
                Endpoint:      "some-auth-url",
 
84
                BaseURLs:      []string{"file://" + metadataDir},
 
85
        }
 
86
        _, err := ValidateToolsMetadata(params)
 
87
        c.Assert(err, gc.Not(gc.IsNil))
 
88
}