~thumper/juju-core/move-environs-tools

« back to all changes in this revision

Viewing changes to environs/azure/environ_test.go

  • Committer: Tarmac
  • Author(s): Raphael Badin
  • Date: 2013-07-08 08:34:47 UTC
  • mfrom: (1385.1.6 use-hosted-services)
  • Revision ID: tarmac-20130708083447-jmbwbyairfpi5rie
[r=rvb] Azure provider: map instances onto Azure services.

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
package azure
5
5
 
6
6
import (
 
7
        "encoding/base64"
7
8
        "fmt"
8
9
        . "launchpad.net/gocheck"
9
10
        "launchpad.net/gwacl"
13
14
        "launchpad.net/juju-core/errors"
14
15
        "launchpad.net/juju-core/instance"
15
16
        "launchpad.net/juju-core/testing"
 
17
        . "launchpad.net/juju-core/testing/checkers"
16
18
        "net/http"
 
19
        "strings"
17
20
        "sync"
18
21
)
19
22
 
109
112
        // The real test is that this does not panic.
110
113
}
111
114
 
112
 
func patchWithPropertiesResponse(c *C, deployments []gwacl.Deployment) *[]*gwacl.X509Request {
113
 
        propertiesS1 := gwacl.HostedService{
114
 
                ServiceName: "S1", Deployments: deployments}
115
 
        propertiesS1XML, err := propertiesS1.Serialize()
 
115
func patchWithServiceListResponse(c *C, services []gwacl.HostedServiceDescriptor) *[]*gwacl.X509Request {
 
116
        list := gwacl.HostedServiceDescriptorList{HostedServices: services}
 
117
        listXML, err := list.Serialize()
116
118
        c.Assert(err, IsNil)
117
119
        responses := []gwacl.DispatcherResponse{gwacl.NewDispatcherResponse(
118
 
                []byte(propertiesS1XML),
 
120
                []byte(listXML),
119
121
                http.StatusOK,
120
122
                nil,
121
123
        )}
123
125
        return requests
124
126
}
125
127
 
 
128
func (suite EnvironSuite) TestGetEnvPrefixContainsEnvName(c *C) {
 
129
        env := makeEnviron(c)
 
130
        c.Check(strings.Contains(env.getEnvPrefix(), env.Name()), IsTrue)
 
131
}
 
132
 
126
133
func (suite EnvironSuite) TestAllInstances(c *C) {
127
 
        deployments := []gwacl.Deployment{{Name: "deployment-1"}, {Name: "deployment-2"}}
128
 
        requests := patchWithPropertiesResponse(c, deployments)
129
134
        env := makeEnviron(c)
 
135
        prefix := env.getEnvPrefix()
 
136
        services := []gwacl.HostedServiceDescriptor{{ServiceName: "deployment-in-another-env"}, {ServiceName: prefix + "deployment-1"}, {ServiceName: prefix + "deployment-2"}}
 
137
        requests := patchWithServiceListResponse(c, services)
130
138
        instances, err := env.AllInstances()
131
139
        c.Assert(err, IsNil)
132
 
        c.Check(len(instances), Equals, len(deployments))
 
140
        c.Check(len(instances), Equals, 2)
 
141
        c.Check(instances[0].Id(), Equals, instance.Id(prefix+"deployment-1"))
 
142
        c.Check(instances[1].Id(), Equals, instance.Id(prefix+"deployment-2"))
133
143
        c.Check(len(*requests), Equals, 1)
134
144
}
135
145
 
136
146
func (suite EnvironSuite) TestInstancesReturnsFilteredList(c *C) {
137
 
        deployments := []gwacl.Deployment{{Name: "deployment-1"}, {Name: "deployment-2"}}
138
 
        requests := patchWithPropertiesResponse(c, deployments)
 
147
        services := []gwacl.HostedServiceDescriptor{{ServiceName: "deployment-1"}, {ServiceName: "deployment-2"}}
 
148
        requests := patchWithServiceListResponse(c, services)
139
149
        env := makeEnviron(c)
140
150
        instances, err := env.Instances([]instance.Id{"deployment-1"})
141
151
        c.Assert(err, IsNil)
145
155
}
146
156
 
147
157
func (suite EnvironSuite) TestInstancesReturnsErrNoInstancesIfNoInstancesRequested(c *C) {
148
 
        deployments := []gwacl.Deployment{{Name: "deployment-1"}, {Name: "deployment-2"}}
149
 
        patchWithPropertiesResponse(c, deployments)
 
158
        services := []gwacl.HostedServiceDescriptor{{ServiceName: "deployment-1"}, {ServiceName: "deployment-2"}}
 
159
        patchWithServiceListResponse(c, services)
150
160
        env := makeEnviron(c)
151
161
        instances, err := env.Instances([]instance.Id{})
152
162
        c.Check(err, Equals, environs.ErrNoInstances)
154
164
}
155
165
 
156
166
func (suite EnvironSuite) TestInstancesReturnsErrNoInstancesIfNoInstanceFound(c *C) {
157
 
        deployments := []gwacl.Deployment{}
158
 
        patchWithPropertiesResponse(c, deployments)
 
167
        services := []gwacl.HostedServiceDescriptor{}
 
168
        patchWithServiceListResponse(c, services)
159
169
        env := makeEnviron(c)
160
170
        instances, err := env.Instances([]instance.Id{"deploy-id"})
161
171
        c.Check(err, Equals, environs.ErrNoInstances)
163
173
}
164
174
 
165
175
func (suite EnvironSuite) TestInstancesReturnsPartialInstancesIfSomeInstancesAreNotFound(c *C) {
166
 
        deployments := []gwacl.Deployment{{Name: "deployment-1"}, {Name: "deployment-2"}}
167
 
        requests := patchWithPropertiesResponse(c, deployments)
 
176
        services := []gwacl.HostedServiceDescriptor{{ServiceName: "deployment-1"}, {ServiceName: "deployment-2"}}
 
177
        requests := patchWithServiceListResponse(c, services)
168
178
        env := makeEnviron(c)
169
179
        instances, err := env.Instances([]instance.Id{"deployment-1", "unknown-deployment"})
170
180
        c.Assert(err, Equals, environs.ErrPartialInstances)
303
313
 
304
314
func (EnvironSuite) TestStateInfo(c *C) {
305
315
        instanceID := "my-instance"
306
 
        // In the Azure provider, DNS name and instance ID are the same thing.
307
 
        patchWithPropertiesResponse(c, []gwacl.Deployment{{
308
 
                Name: instanceID,
309
 
                URL:  fmt.Sprintf("http://%s/", instanceID),
 
316
        label := "my-label"
 
317
        // In the Azure provider, the hostname of the instance is the
 
318
        // service's label.
 
319
        expectedDNSName := fmt.Sprintf("%s.%s", label, AZURE_DOMAIN_NAME)
 
320
        encodedLabel := base64.StdEncoding.EncodeToString([]byte(label))
 
321
        patchWithServiceListResponse(c, []gwacl.HostedServiceDescriptor{{
 
322
                ServiceName: instanceID,
 
323
                Label:       encodedLabel,
310
324
        }})
311
325
        env := makeEnviron(c)
312
326
        cleanup := setDummyStorage(c, env)
322
336
        config := env.Config()
323
337
        statePortSuffix := fmt.Sprintf(":%d", config.StatePort())
324
338
        apiPortSuffix := fmt.Sprintf(":%d", config.APIPort())
325
 
        c.Check(stateInfo.Addrs, DeepEquals, []string{instanceID + statePortSuffix})
326
 
        c.Check(apiInfo.Addrs, DeepEquals, []string{instanceID + apiPortSuffix})
 
339
        c.Check(stateInfo.Addrs, DeepEquals, []string{expectedDNSName + statePortSuffix})
 
340
        c.Check(apiInfo.Addrs, DeepEquals, []string{expectedDNSName + apiPortSuffix})
327
341
}