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

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/testing/factory/factory.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:
19
19
        "gopkg.in/juju/charm.v6-unstable"
20
20
 
21
21
        "github.com/juju/juju/cloud"
22
 
        "github.com/juju/juju/environs"
23
 
        envtesting "github.com/juju/juju/environs/testing"
 
22
        "github.com/juju/juju/constraints"
24
23
        "github.com/juju/juju/instance"
25
24
        "github.com/juju/juju/network"
26
25
        "github.com/juju/juju/state"
 
26
        "github.com/juju/juju/status"
27
27
        "github.com/juju/juju/testcharms"
28
28
        "github.com/juju/juju/testing"
29
 
        "github.com/juju/juju/version"
 
29
        jujuversion "github.com/juju/juju/version"
 
30
        "github.com/juju/version"
30
31
)
31
32
 
32
33
const (
51
52
        Creator     names.Tag
52
53
        NoModelUser bool
53
54
        Disabled    bool
 
55
        Access      state.ModelAccess
54
56
}
55
57
 
56
58
// ModelUserParams defines the parameters for creating an environment user.
58
60
        User        string
59
61
        DisplayName string
60
62
        CreatedBy   names.Tag
61
 
        ReadOnly    bool
 
63
        Access      state.ModelAccess
62
64
}
63
65
 
64
66
// CharmParams defines the parameters for creating a charm.
75
77
        Jobs            []state.MachineJob
76
78
        Password        string
77
79
        Nonce           string
 
80
        Constraints     constraints.Value
78
81
        InstanceId      instance.Id
79
82
        Characteristics *instance.HardwareCharacteristics
80
83
        Addresses       []network.Address
84
87
 
85
88
// ServiceParams is used when specifying parameters for a new service.
86
89
type ServiceParams struct {
87
 
        Name     string
88
 
        Charm    *state.Charm
89
 
        Creator  names.Tag
90
 
        Status   *state.StatusInfo
91
 
        Settings map[string]interface{}
 
90
        Name        string
 
91
        Charm       *state.Charm
 
92
        Creator     names.Tag
 
93
        Status      *status.StatusInfo
 
94
        Settings    map[string]interface{}
 
95
        Constraints constraints.Value
92
96
}
93
97
 
94
98
// UnitParams are used to create units.
97
101
        Machine     *state.Machine
98
102
        Password    string
99
103
        SetCharmURL bool
100
 
        Status      *state.StatusInfo
 
104
        Status      *status.StatusInfo
 
105
        Constraints constraints.Value
101
106
}
102
107
 
103
108
// RelationParams are used to create relations.
169
174
                c.Assert(err, jc.ErrorIsNil)
170
175
                params.Creator = env.Owner()
171
176
        }
 
177
        if params.Access == state.ModelUndefinedAccess {
 
178
                params.Access = state.ModelAdminAccess
 
179
        }
172
180
        creatorUserTag := params.Creator.(names.UserTag)
173
181
        user, err := factory.st.AddUser(
174
182
                params.Name, params.DisplayName, params.Password, creatorUserTag.Name())
178
186
                        User:        user.UserTag(),
179
187
                        CreatedBy:   names.NewUserTag(user.CreatedBy()),
180
188
                        DisplayName: params.DisplayName,
 
189
                        Access:      params.Access,
181
190
                })
182
191
                c.Assert(err, jc.ErrorIsNil)
183
192
        }
203
212
        if params.DisplayName == "" {
204
213
                params.DisplayName = uniqueString("display name")
205
214
        }
 
215
        if params.Access == state.ModelUndefinedAccess {
 
216
                params.Access = state.ModelAdminAccess
 
217
        }
206
218
        if params.CreatedBy == nil {
207
219
                env, err := factory.st.Model()
208
220
                c.Assert(err, jc.ErrorIsNil)
213
225
                User:        names.NewUserTag(params.User),
214
226
                CreatedBy:   createdByUserTag,
215
227
                DisplayName: params.DisplayName,
216
 
                ReadOnly:    params.ReadOnly,
 
228
                Access:      params.Access,
217
229
        })
218
230
        c.Assert(err, jc.ErrorIsNil)
219
231
        return modelUser
261
273
                Jobs:        params.Jobs,
262
274
                Volumes:     params.Volumes,
263
275
                Filesystems: params.Filesystems,
 
276
                Constraints: params.Constraints,
264
277
        }
265
278
 
266
279
        m, err := factory.st.AddMachineInsideMachine(
272
285
        err = m.SetProvisioned(params.InstanceId, params.Nonce, params.Characteristics)
273
286
        c.Assert(err, jc.ErrorIsNil)
274
287
        current := version.Binary{
275
 
                Number: version.Current,
 
288
                Number: jujuversion.Current,
276
289
                Arch:   arch.HostArch(),
277
290
                Series: series.HostSeries(),
278
291
        }
301
314
                Jobs:        params.Jobs,
302
315
                Volumes:     params.Volumes,
303
316
                Filesystems: params.Filesystems,
 
317
                Constraints: params.Constraints,
304
318
        }
305
319
        machine, err := factory.st.AddOneMachine(machineTemplate)
306
320
        c.Assert(err, jc.ErrorIsNil)
313
327
                c.Assert(err, jc.ErrorIsNil)
314
328
        }
315
329
        current := version.Binary{
316
 
                Number: version.Current,
 
330
                Number: jujuversion.Current,
317
331
                Arch:   arch.HostArch(),
318
332
                Series: series.HostSeries(),
319
333
        }
375
389
        }
376
390
        _ = params.Creator.(names.UserTag)
377
391
        service, err := factory.st.AddService(state.AddServiceArgs{
378
 
                Name:     params.Name,
379
 
                Owner:    params.Creator.String(),
380
 
                Charm:    params.Charm,
381
 
                Settings: charm.Settings(params.Settings),
 
392
                Name:        params.Name,
 
393
                Owner:       params.Creator.String(),
 
394
                Charm:       params.Charm,
 
395
                Settings:    charm.Settings(params.Settings),
 
396
                Constraints: params.Constraints,
382
397
        })
383
398
        c.Assert(err, jc.ErrorIsNil)
384
399
 
409
424
                params.Machine = factory.MakeMachine(c, nil)
410
425
        }
411
426
        if params.Service == nil {
412
 
                params.Service = factory.MakeService(c, nil)
 
427
                params.Service = factory.MakeService(c, &ServiceParams{
 
428
                        Constraints: params.Constraints,
 
429
                })
413
430
        }
414
431
        if params.Password == "" {
415
432
                var err error
422
439
        c.Assert(err, jc.ErrorIsNil)
423
440
 
424
441
        agentTools := version.Binary{
425
 
                Number: version.Current,
 
442
                Number: jujuversion.Current,
426
443
                Arch:   arch.HostArch(),
427
444
                Series: params.Service.Series(),
428
445
        }
554
571
        }.Merge(params.ConfigAttrs))
555
572
        _, st, err := factory.st.NewModel(cfg, params.Owner.(names.UserTag))
556
573
        c.Assert(err, jc.ErrorIsNil)
557
 
        if params.Prepare {
558
 
                if params.Credential == nil {
559
 
                        emptyCredential := cloud.NewEmptyCredential()
560
 
                        params.Credential = &emptyCredential
561
 
                }
562
 
                args := environs.PrepareForBootstrapParams{
563
 
                        Config:        cfg,
564
 
                        Credentials:   *params.Credential,
565
 
                        CloudEndpoint: params.CloudEndpoint,
566
 
                        CloudRegion:   params.CloudRegion,
567
 
                }
568
 
                // Prepare the environment.
569
 
                provider, err := environs.Provider(cfg.Type())
570
 
                c.Assert(err, jc.ErrorIsNil)
571
 
                env, err := provider.PrepareForBootstrap(envtesting.BootstrapContext(c), args)
572
 
                c.Assert(err, jc.ErrorIsNil)
573
 
                // Now save the config back.
574
 
                err = st.UpdateModelConfig(env.Config().AllAttrs(), nil, nil)
575
 
                c.Assert(err, jc.ErrorIsNil)
576
 
        }
577
574
        return st
578
575
}