~juju-qa/ubuntu/xenial/juju/xenial-2.0-beta3

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/environs/jujutest/livetests.go

  • Committer: Martin Packman
  • Date: 2016-03-30 19:31:08 UTC
  • mfrom: (1.1.41)
  • Revision ID: martin.packman@canonical.com-20160330193108-h9iz3ak334uk0z5r
Merge new upstream source 2.0~beta3

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
        "github.com/juju/utils"
14
14
        "github.com/juju/utils/arch"
15
15
        "github.com/juju/utils/series"
 
16
        "github.com/juju/version"
16
17
        gc "gopkg.in/check.v1"
17
18
        "gopkg.in/juju/charmrepo.v2-unstable"
18
19
 
23
24
        "github.com/juju/juju/environs"
24
25
        "github.com/juju/juju/environs/bootstrap"
25
26
        "github.com/juju/juju/environs/config"
26
 
        "github.com/juju/juju/environs/configstore"
27
27
        "github.com/juju/juju/environs/filestorage"
28
28
        sstesting "github.com/juju/juju/environs/simplestreams/testing"
29
29
        "github.com/juju/juju/environs/storage"
43
43
        "github.com/juju/juju/testcharms"
44
44
        coretesting "github.com/juju/juju/testing"
45
45
        coretools "github.com/juju/juju/tools"
46
 
        "github.com/juju/juju/version"
 
46
        jujuversion "github.com/juju/juju/version"
47
47
)
48
48
 
49
49
// LiveTests contains tests that are designed to run against a live server
84
84
        // This is set by PrepareOnce and BootstrapOnce.
85
85
        Env environs.Environ
86
86
 
87
 
        // ConfigStore holds the configuration storage
88
 
        // used when preparing the environment.
89
 
        // This is initialized by SetUpSuite.
90
 
        ConfigStore configstore.Storage
91
 
 
92
87
        // ControllerStore holds the controller related informtion
93
88
        // such as controllers, accounts, etc., used when preparing
94
89
        // the environment. This is initialized by SetUpSuite.
102
97
func (t *LiveTests) SetUpSuite(c *gc.C) {
103
98
        t.CleanupSuite.SetUpSuite(c)
104
99
        t.TestDataSuite.SetUpSuite(c)
105
 
        t.ConfigStore = configstore.NewMem()
106
100
        t.ControllerStore = jujuclienttesting.NewMemStore()
107
101
        t.PatchValue(&juju.JujuPublicKey, sstesting.SignedMetadataPublicKey)
108
102
}
109
103
 
110
104
func (t *LiveTests) SetUpTest(c *gc.C) {
111
105
        t.CleanupSuite.SetUpTest(c)
112
 
        t.PatchValue(&version.Current, coretesting.FakeVersionNumber)
 
106
        t.PatchValue(&jujuversion.Current, coretesting.FakeVersionNumber)
113
107
        storageDir := c.MkDir()
114
108
        t.DefaultBaseURL = "file://" + storageDir + "/tools"
115
109
        t.ToolsFixture.SetUpTest(c)
120
114
        t.CleanupSuite.PatchValue(&envtools.BundleTools, envtoolstesting.GetMockBundleTools(c))
121
115
}
122
116
 
123
 
func publicAttrs(e environs.Environ) map[string]interface{} {
124
 
        cfg := e.Config()
125
 
        secrets, err := e.Provider().SecretAttrs(cfg)
126
 
        if err != nil {
127
 
                panic(err)
128
 
        }
129
 
        attrs := cfg.AllAttrs()
130
 
        for attr := range secrets {
131
 
                delete(attrs, attr)
132
 
        }
133
 
        return attrs
134
 
}
135
 
 
136
117
func (t *LiveTests) TearDownSuite(c *gc.C) {
137
118
        t.Destroy(c)
138
119
        t.TestDataSuite.TearDownSuite(c)
151
132
                return
152
133
        }
153
134
        args := t.prepareForBootstrapParams(c)
154
 
        e, err := environs.Prepare(envtesting.BootstrapContext(c), t.ConfigStore, t.ControllerStore, args.Config.Name(), args)
 
135
        e, err := environs.Prepare(envtesting.BootstrapContext(c), t.ControllerStore, args)
155
136
        c.Assert(err, gc.IsNil, gc.Commentf("preparing environ %#v", t.TestConfig))
156
137
        c.Assert(e, gc.NotNil)
157
138
        t.Env = e
158
139
        t.prepared = true
159
140
}
160
141
 
161
 
func (t *LiveTests) prepareForBootstrapParams(c *gc.C) environs.PrepareForBootstrapParams {
162
 
        cfg, err := config.New(config.NoDefaults, t.TestConfig)
163
 
        c.Assert(err, jc.ErrorIsNil)
 
142
func (t *LiveTests) prepareForBootstrapParams(c *gc.C) environs.PrepareParams {
164
143
        credential := t.Credential
165
144
        if credential.AuthType() == "" {
166
145
                credential = cloud.NewEmptyCredential()
167
146
        }
168
 
        return environs.PrepareForBootstrapParams{
169
 
                Config:        cfg,
170
 
                Credentials:   credential,
171
 
                CloudEndpoint: t.CloudEndpoint,
172
 
                CloudRegion:   t.CloudRegion,
 
147
        return environs.PrepareParams{
 
148
                BaseConfig:     t.TestConfig,
 
149
                Credential:     credential,
 
150
                CloudEndpoint:  t.CloudEndpoint,
 
151
                CloudRegion:    t.CloudRegion,
 
152
                ControllerName: t.TestConfig["name"].(string),
 
153
                CloudName:      t.TestConfig["type"].(string),
173
154
        }
174
155
}
175
156
 
187
168
        }
188
169
        err := bootstrap.Bootstrap(envtesting.BootstrapContext(c), t.Env, bootstrap.BootstrapParams{
189
170
                BootstrapConstraints: cons,
190
 
                EnvironConstraints:   cons,
 
171
                ModelConstraints:     cons,
191
172
        })
192
173
        c.Assert(err, jc.ErrorIsNil)
193
174
        t.bootstrapped = true
197
178
        if t.Env == nil {
198
179
                return
199
180
        }
200
 
        err := environs.Destroy(t.Env.Config().Name(), t.Env, t.ConfigStore, t.ControllerStore)
 
181
        err := environs.Destroy(t.Env.Config().Name(), t.Env, t.ControllerStore)
201
182
        c.Assert(err, jc.ErrorIsNil)
202
183
        t.bootstrapped = false
203
184
        t.prepared = false
470
451
        c.Assert(err, jc.ErrorIsNil)
471
452
        agentVersion, ok := cfg.AgentVersion()
472
453
        c.Check(ok, jc.IsTrue)
473
 
        c.Check(agentVersion, gc.Equals, version.Current)
 
454
        c.Check(agentVersion, gc.Equals, jujuversion.Current)
474
455
 
475
456
        // Check that the constraints have been set in the environment.
476
457
        cons, err := st.ModelConstraints()
495
476
 
496
477
        // If the series has not been specified, we expect the most recent Ubuntu LTS release to be used.
497
478
        expectedVersion := version.Binary{
498
 
                Number: version.Current,
 
479
                Number: jujuversion.Current,
499
480
                Arch:   arch.HostArch(),
500
481
                Series: config.LatestLtsSeries(),
501
482
        }
709
690
        Delay: 1 * time.Second,
710
691
}
711
692
 
712
 
func (t *LiveTests) assertStartInstance(c *gc.C, m *state.Machine) {
713
 
        // Wait for machine to get an instance id.
714
 
        for a := waitAgent.Start(); a.Next(); {
715
 
                err := m.Refresh()
716
 
                c.Assert(err, jc.ErrorIsNil)
717
 
                instId, err := m.InstanceId()
718
 
                if err != nil {
719
 
                        c.Assert(err, jc.Satisfies, errors.IsNotProvisioned)
720
 
                        continue
721
 
                }
722
 
                _, err = t.Env.Instances([]instance.Id{instId})
723
 
                c.Assert(err, jc.ErrorIsNil)
724
 
                return
725
 
        }
726
 
        c.Fatalf("provisioner failed to start machine after %v", waitAgent.Total)
727
 
}
728
 
 
729
693
func (t *LiveTests) assertStopInstance(c *gc.C, env environs.Environ, instId instance.Id) {
730
694
        var err error
731
695
        for a := waitAgent.Start(); a.Next(); {
741
705
        c.Fatalf("provisioner failed to stop machine after %v", waitAgent.Total)
742
706
}
743
707
 
744
 
// assertInstanceId asserts that the machine has an instance id
745
 
// that matches that of the given instance. If the instance is nil,
746
 
// It asserts that the instance id is unset.
747
 
func assertInstanceId(c *gc.C, m *state.Machine, inst instance.Instance) {
748
 
        var wantId, gotId instance.Id
749
 
        var err error
750
 
        if inst != nil {
751
 
                wantId = inst.Id()
752
 
        }
753
 
        for a := waitAgent.Start(); a.Next(); {
754
 
                err := m.Refresh()
755
 
                c.Assert(err, jc.ErrorIsNil)
756
 
                gotId, err = m.InstanceId()
757
 
                if err != nil {
758
 
                        c.Assert(err, jc.Satisfies, errors.IsNotProvisioned)
759
 
                        if inst == nil {
760
 
                                return
761
 
                        }
762
 
                        continue
763
 
                }
764
 
                break
765
 
        }
766
 
        c.Assert(err, jc.ErrorIsNil)
767
 
        c.Assert(gotId, gc.Equals, wantId)
768
 
}
769
 
 
770
708
// Check that we get a consistent error when asking for an instance without
771
709
// a valid machine config.
772
710
func (t *LiveTests) TestStartInstanceWithEmptyNonceFails(c *gc.C) {
806
744
        }
807
745
 
808
746
        current := version.Binary{
809
 
                Number: version.Current,
 
747
                Number: jujuversion.Current,
810
748
                Arch:   arch.HostArch(),
811
749
                Series: series.HostSeries(),
812
750
        }
816
754
                other.Series = "precise"
817
755
        }
818
756
 
819
 
        dummyCfg, err := config.New(config.NoDefaults, dummy.SampleConfig().Merge(coretesting.Attrs{
 
757
        dummyCfg := dummy.SampleConfig().Merge(coretesting.Attrs{
820
758
                "controller": false,
821
759
                "name":       "dummy storage",
822
 
        }))
 
760
        })
823
761
        args := t.prepareForBootstrapParams(c)
824
 
        args.Config = dummyCfg
 
762
        args.BaseConfig = dummyCfg
825
763
        dummyenv, err := environs.Prepare(envtesting.BootstrapContext(c),
826
 
                configstore.NewMem(),
827
764
                jujuclienttesting.NewMemStore(),
828
 
                dummyCfg.Name(),
829
 
                args)
 
765
                args,
 
766
        )
830
767
        c.Assert(err, jc.ErrorIsNil)
831
768
        defer dummyenv.Destroy()
832
769
 
833
770
        t.Destroy(c)
834
771
 
835
 
        attrs := t.TestConfig.Merge(coretesting.Attrs{"default-series": other.Series})
836
 
        cfg, err := config.New(config.NoDefaults, attrs)
837
 
        c.Assert(err, jc.ErrorIsNil)
838
 
        args.Config = cfg
 
772
        attrs := t.TestConfig.Merge(coretesting.Attrs{
 
773
                "name":           "livetests",
 
774
                "default-series": other.Series,
 
775
        })
 
776
        args.BaseConfig = attrs
839
777
        env, err := environs.Prepare(envtesting.BootstrapContext(c),
840
 
                t.ConfigStore,
841
778
                t.ControllerStore,
842
 
                "livetests", args)
 
779
                args)
843
780
        c.Assert(err, jc.ErrorIsNil)
844
 
        defer environs.Destroy("livetests", env, t.ConfigStore, t.ControllerStore)
 
781
        defer environs.Destroy("livetests", env, t.ControllerStore)
845
782
 
846
783
        err = bootstrap.Bootstrap(envtesting.BootstrapContext(c), env, bootstrap.BootstrapParams{})
847
784
        c.Assert(err, jc.ErrorIsNil)