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

« back to all changes in this revision

Viewing changes to src/gopkg.in/juju/charmstore.v5-unstable/internal/storetesting/entities.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:
7
7
        jc "github.com/juju/testing/checkers"
8
8
        gc "gopkg.in/check.v1"
9
9
        "gopkg.in/juju/charm.v6-unstable"
 
10
        "gopkg.in/juju/charmrepo.v2-unstable/csclient/params"
10
11
        "gopkg.in/mgo.v2"
11
12
 
12
13
        "gopkg.in/juju/charmstore.v5-unstable/internal/mongodoc"
29
30
                        Series:              URL.Series,
30
31
                        Revision:            URL.Revision,
31
32
                        User:                URL.User,
32
 
                        BaseURL:             baseURL(URL),
 
33
                        BaseURL:             mongodoc.BaseURL(URL),
33
34
                        PromulgatedRevision: -1,
34
35
                },
35
36
        }
110
111
        return b
111
112
}
112
113
 
113
 
// WithACLs sets the non-development ACLs field on the BaseEntity.
114
 
func (b BaseEntityBuilder) WithACLs(acls mongodoc.ACL) BaseEntityBuilder {
 
114
// WithACLs sets the ACLs field on the BaseEntity.
 
115
func (b BaseEntityBuilder) WithACLs(channel params.Channel, acls mongodoc.ACL) BaseEntityBuilder {
115
116
        b = b.copy()
116
 
        b.baseEntity.ACLs = acls
 
117
        if b.baseEntity.ChannelACLs == nil {
 
118
                b.baseEntity.ChannelACLs = make(map[params.Channel]mongodoc.ACL)
 
119
        }
 
120
        b.baseEntity.ChannelACLs[channel] = acls
117
121
        return b
118
122
}
119
123
 
127
131
        var baseEntity mongodoc.BaseEntity
128
132
        err := db.FindId(expect.URL).One(&baseEntity)
129
133
        c.Assert(err, gc.IsNil)
130
 
        c.Assert(&baseEntity, jc.DeepEquals, expect)
 
134
        c.Assert(NormalizeBaseEntity(&baseEntity), jc.DeepEquals, NormalizeBaseEntity(expect))
131
135
}
132
136
 
133
 
func baseURL(url *charm.URL) *charm.URL {
134
 
        baseURL := *url
135
 
        baseURL.Series = ""
136
 
        baseURL.Revision = -1
137
 
        baseURL.Channel = ""
138
 
        return &baseURL
 
137
// NormalizeBaseEntity modifies a base entity so that it can be compared
 
138
// with another normalized base entity using jc.DeepEquals.
 
139
func NormalizeBaseEntity(be *mongodoc.BaseEntity) *mongodoc.BaseEntity {
 
140
        be1 := *be
 
141
        for c, acls := range be1.ChannelACLs {
 
142
                if len(acls.Read) == 0 && len(acls.Write) == 0 {
 
143
                        delete(be1.ChannelACLs, c)
 
144
                }
 
145
        }
 
146
        if len(be1.ChannelACLs) == 0 {
 
147
                be1.ChannelACLs = nil
 
148
        }
 
149
        for c, entities := range be1.ChannelEntities {
 
150
                if len(entities) == 0 {
 
151
                        delete(be1.ChannelEntities, c)
 
152
                }
 
153
        }
 
154
        if len(be1.ChannelEntities) == 0 {
 
155
                be1.ChannelEntities = nil
 
156
        }
 
157
        return &be1
139
158
}