~james-page/ubuntu/raring/juju-core/1.10.0

« back to all changes in this revision

Viewing changes to src/launchpad.net/juju-core/state/export_test.go

  • Committer: James Page
  • Author(s): James Page
  • Date: 2013-04-24 10:08:07 UTC
  • Revision ID: james.page@canonical.com-20130424100807-bb153yi4shsb4ydh
Tags: upstream-1.10.0
ImportĀ upstreamĀ versionĀ 1.10.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package state
 
2
 
 
3
import (
 
4
        "fmt"
 
5
        "io/ioutil"
 
6
        "labix.org/v2/mgo"
 
7
        . "launchpad.net/gocheck"
 
8
        "launchpad.net/juju-core/charm"
 
9
        "launchpad.net/juju-core/environs/config"
 
10
        "launchpad.net/juju-core/testing"
 
11
        "net/url"
 
12
        "path/filepath"
 
13
)
 
14
 
 
15
// TestingEnvironConfig returns a default environment configuration.
 
16
func TestingEnvironConfig(c *C) *config.Config {
 
17
        cfg, err := config.New(map[string]interface{}{
 
18
                "type":            "test",
 
19
                "name":            "test-name",
 
20
                "default-series":  "test-series",
 
21
                "authorized-keys": "test-keys",
 
22
                "agent-version":   "9.9.9.9",
 
23
                "ca-cert":         testing.CACert,
 
24
                "ca-private-key":  "",
 
25
        })
 
26
        c.Assert(err, IsNil)
 
27
        return cfg
 
28
}
 
29
 
 
30
// TestingInitialize initializes the state and returns it. If state was not
 
31
// already initialized, and cfg is nil, the minimal default environment
 
32
// configuration will be used.
 
33
func TestingInitialize(c *C, cfg *config.Config) *State {
 
34
        if cfg == nil {
 
35
                cfg = TestingEnvironConfig(c)
 
36
        }
 
37
        st, err := Initialize(TestingStateInfo(), cfg, TestingDialOpts())
 
38
        c.Assert(err, IsNil)
 
39
        return st
 
40
}
 
41
 
 
42
type (
 
43
        CharmDoc    charmDoc
 
44
        MachineDoc  machineDoc
 
45
        RelationDoc relationDoc
 
46
        ServiceDoc  serviceDoc
 
47
        UnitDoc     unitDoc
 
48
)
 
49
 
 
50
func (doc *MachineDoc) String() string {
 
51
        m := &Machine{doc: machineDoc(*doc)}
 
52
        return m.String()
 
53
}
 
54
 
 
55
func ServiceSettingsRefCount(st *State, serviceName string, curl *charm.URL) (int, error) {
 
56
        key := serviceSettingsKey(serviceName, curl)
 
57
        var doc settingsRefsDoc
 
58
        if err := st.settingsrefs.FindId(key).One(&doc); err == nil {
 
59
                return doc.RefCount, nil
 
60
        }
 
61
        return 0, mgo.ErrNotFound
 
62
}
 
63
 
 
64
func AddTestingCharm(c *C, st *State, name string) *Charm {
 
65
        return addCharm(c, st, "series", testing.Charms.Dir(name))
 
66
}
 
67
 
 
68
func AddCustomCharm(c *C, st *State, name, filename, content, series string, revision int) *Charm {
 
69
        path := testing.Charms.ClonedDirPath(c.MkDir(), name)
 
70
        if filename != "" {
 
71
                config := filepath.Join(path, filename)
 
72
                err := ioutil.WriteFile(config, []byte(content), 0644)
 
73
                c.Assert(err, IsNil)
 
74
        }
 
75
        ch, err := charm.ReadDir(path)
 
76
        c.Assert(err, IsNil)
 
77
        if revision != -1 {
 
78
                ch.SetRevision(revision)
 
79
        }
 
80
        return addCharm(c, st, series, ch)
 
81
}
 
82
 
 
83
func addCharm(c *C, st *State, series string, ch charm.Charm) *Charm {
 
84
        ident := fmt.Sprintf("%s-%s-%d", series, ch.Meta().Name, ch.Revision())
 
85
        curl := charm.MustParseURL("local:" + series + "/" + ident)
 
86
        bundleURL, err := url.Parse("http://bundles.example.com/" + ident)
 
87
        c.Assert(err, IsNil)
 
88
        sch, err := st.AddCharm(ch, curl, bundleURL, ident+"-sha256")
 
89
        c.Assert(err, IsNil)
 
90
        return sch
 
91
}
 
92
 
 
93
func init() {
 
94
        logSize = logSizeTests
 
95
}