github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/provider/openstack/export_test.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package openstack
     5  
     6  import (
     7  	"bytes"
     8  	"fmt"
     9  	"strings"
    10  	"text/template"
    11  
    12  	"launchpad.net/goose/errors"
    13  	"launchpad.net/goose/identity"
    14  	"launchpad.net/goose/nova"
    15  	"launchpad.net/goose/swift"
    16  
    17  	"github.com/juju/juju/constraints"
    18  	"github.com/juju/juju/environs"
    19  	"github.com/juju/juju/environs/instances"
    20  	"github.com/juju/juju/environs/jujutest"
    21  	"github.com/juju/juju/environs/simplestreams"
    22  	"github.com/juju/juju/environs/storage"
    23  	"github.com/juju/juju/instance"
    24  	"github.com/juju/juju/network"
    25  )
    26  
    27  // This provides the content for code accessing test:///... URLs. This allows
    28  // us to set the responses for things like the Metadata server, by pointing
    29  // metadata requests at test:///... rather than http://169.254.169.254
    30  var testRoundTripper = &jujutest.ProxyRoundTripper{}
    31  
    32  func init() {
    33  	testRoundTripper.RegisterForScheme("test")
    34  }
    35  
    36  var (
    37  	ShortAttempt   = &shortAttempt
    38  	StorageAttempt = &storageAttempt
    39  )
    40  
    41  // MetadataStorage returns a Storage instance which is used to store simplestreams metadata for tests.
    42  func MetadataStorage(e environs.Environ) storage.Storage {
    43  	ecfg := e.(*environ).ecfg()
    44  	authModeCfg := AuthMode(ecfg.authMode())
    45  	container := "juju-dist-test"
    46  	metadataStorage := &openstackstorage{
    47  		containerName: container,
    48  		swift:         swift.New(e.(*environ).authClient(ecfg, authModeCfg)),
    49  	}
    50  
    51  	// Ensure the container exists.
    52  	err := metadataStorage.makeContainer(container, swift.PublicRead)
    53  	if err != nil {
    54  		panic(fmt.Errorf("cannot create %s container: %v", container, err))
    55  	}
    56  	return metadataStorage
    57  }
    58  
    59  func InstanceAddress(publicIP string, addresses map[string][]nova.IPAddress) string {
    60  	return network.SelectPublicAddress(convertNovaAddresses(publicIP, addresses))
    61  }
    62  
    63  func InstanceServerDetail(inst instance.Instance) *nova.ServerDetail {
    64  	return inst.(*openstackInstance).serverDetail
    65  }
    66  
    67  func InstanceFloatingIP(inst instance.Instance) *nova.FloatingIP {
    68  	return inst.(*openstackInstance).floatingIP
    69  }
    70  
    71  var (
    72  	NovaListAvailabilityZones   = &novaListAvailabilityZones
    73  	AvailabilityZoneAllocations = &availabilityZoneAllocations
    74  )
    75  
    76  var indexData = `
    77  		{
    78  		 "index": {
    79  		  "com.ubuntu.cloud:released:openstack": {
    80  		   "updated": "Wed, 01 May 2013 13:31:26 +0000",
    81  		   "clouds": [
    82  			{
    83  			 "region": "{{.Region}}",
    84  			 "endpoint": "{{.URL}}"
    85  			}
    86  		   ],
    87  		   "cloudname": "test",
    88  		   "datatype": "image-ids",
    89  		   "format": "products:1.0",
    90  		   "products": [
    91  			"com.ubuntu.cloud:server:14.04:amd64",
    92  			"com.ubuntu.cloud:server:14.04:i386",
    93  			"com.ubuntu.cloud:server:14.04:ppc64el",
    94  			"com.ubuntu.cloud:server:12.10:amd64",
    95  			"com.ubuntu.cloud:server:13.04:amd64"
    96  		   ],
    97  		   "path": "image-metadata/products.json"
    98  		  }
    99  		 },
   100  		 "updated": "Wed, 01 May 2013 13:31:26 +0000",
   101  		 "format": "index:1.0"
   102  		}
   103  `
   104  
   105  var imagesData = `
   106  {
   107   "content_id": "com.ubuntu.cloud:released:openstack",
   108   "products": {
   109     "com.ubuntu.cloud:server:14.04:amd64": {
   110       "release": "trusty",
   111       "version": "14.04",
   112       "arch": "amd64",
   113       "versions": {
   114         "20121218": {
   115           "items": {
   116             "inst1": {
   117               "root_store": "ebs",
   118               "virt": "pv",
   119               "region": "some-region",
   120               "id": "1"
   121             },
   122             "inst2": {
   123               "root_store": "ebs",
   124               "virt": "pv",
   125               "region": "another-region",
   126               "id": "2"
   127             }
   128           },
   129           "pubname": "ubuntu-trusty-14.04-amd64-server-20121218",
   130           "label": "release"
   131         },
   132         "20121111": {
   133           "items": {
   134             "inst3": {
   135               "root_store": "ebs",
   136               "virt": "pv",
   137               "region": "some-region",
   138               "id": "3"
   139             }
   140           },
   141           "pubname": "ubuntu-trusty-14.04-amd64-server-20121111",
   142           "label": "release"
   143         }
   144       }
   145     },
   146     "com.ubuntu.cloud:server:14.04:i386": {
   147       "release": "trusty",
   148       "version": "14.04",
   149       "arch": "i386",
   150       "versions": {
   151         "20121111": {
   152           "items": {
   153             "inst33": {
   154               "root_store": "ebs",
   155               "virt": "pv",
   156               "region": "some-region",
   157               "id": "33"
   158             }
   159           },
   160           "pubname": "ubuntu-trusty-14.04-i386-server-20121111",
   161           "label": "release"
   162         }
   163       }
   164     },
   165     "com.ubuntu.cloud:server:14.04:ppc64el": {
   166       "release": "trusty",
   167       "version": "14.04",
   168       "arch": "ppc64el",
   169       "versions": {
   170         "20121111": {
   171           "items": {
   172             "inst33": {
   173               "root_store": "ebs",
   174               "virt": "pv",
   175               "region": "some-region",
   176               "id": "33"
   177             }
   178           },
   179           "pubname": "ubuntu-trusty-14.04-ppc64el-server-20121111",
   180           "label": "release"
   181         }
   182       }
   183     },
   184     "com.ubuntu.cloud:server:12.10:amd64": {
   185       "release": "quantal",
   186       "version": "12.10",
   187       "arch": "amd64",
   188       "versions": {
   189         "20121218": {
   190           "items": {
   191             "inst3": {
   192               "root_store": "ebs",
   193               "virt": "pv",
   194               "region": "region-1",
   195               "id": "id-1"
   196             },
   197             "inst4": {
   198               "root_store": "ebs",
   199               "virt": "pv",
   200               "region": "region-2",
   201               "id": "id-2"
   202             }
   203           },
   204           "pubname": "ubuntu-quantal-12.14-amd64-server-20121218",
   205           "label": "release"
   206         }
   207       }
   208     },
   209     "com.ubuntu.cloud:server:13.04:amd64": {
   210       "release": "raring",
   211       "version": "13.04",
   212       "arch": "amd64",
   213       "versions": {
   214         "20121218": {
   215           "items": {
   216             "inst5": {
   217               "root_store": "ebs",
   218               "virt": "pv",
   219               "region": "some-region",
   220               "id": "id-y"
   221             },
   222             "inst6": {
   223               "root_store": "ebs",
   224               "virt": "pv",
   225               "region": "another-region",
   226               "id": "id-z"
   227             }
   228           },
   229           "pubname": "ubuntu-raring-13.04-amd64-server-20121218",
   230           "label": "release"
   231         }
   232       }
   233     }
   234   },
   235   "format": "products:1.0"
   236  }
   237  `
   238  
   239  const productMetadatafile = "image-metadata/products.json"
   240  
   241  func UseTestImageData(stor storage.Storage, cred *identity.Credentials) {
   242  	// Put some image metadata files into the public storage.
   243  	t := template.Must(template.New("").Parse(indexData))
   244  	var metadata bytes.Buffer
   245  	if err := t.Execute(&metadata, cred); err != nil {
   246  		panic(fmt.Errorf("cannot generate index metdata: %v", err))
   247  	}
   248  	data := metadata.Bytes()
   249  	stor.Put(simplestreams.UnsignedIndex("v1", 1), bytes.NewReader(data), int64(len(data)))
   250  	stor.Put(
   251  		productMetadatafile, strings.NewReader(imagesData), int64(len(imagesData)))
   252  }
   253  
   254  func RemoveTestImageData(stor storage.Storage) {
   255  	stor.Remove(simplestreams.UnsignedIndex("v1", 1))
   256  	stor.Remove(productMetadatafile)
   257  }
   258  
   259  // DiscardSecurityGroup cleans up a security group, it is not an error to
   260  // delete something that doesn't exist.
   261  func DiscardSecurityGroup(e environs.Environ, name string) error {
   262  	env := e.(*environ)
   263  	novaClient := env.nova()
   264  	group, err := novaClient.SecurityGroupByName(name)
   265  	if err != nil {
   266  		if errors.IsNotFound(err) {
   267  			// Group already deleted, done
   268  			return nil
   269  		}
   270  	}
   271  	err = novaClient.DeleteSecurityGroup(group.Id)
   272  	if err != nil {
   273  		return err
   274  	}
   275  	return nil
   276  }
   277  
   278  func FindInstanceSpec(e environs.Environ, series, arch, cons string) (spec *instances.InstanceSpec, err error) {
   279  	env := e.(*environ)
   280  	spec, err = findInstanceSpec(env, &instances.InstanceConstraint{
   281  		Series:      series,
   282  		Arches:      []string{arch},
   283  		Region:      env.ecfg().region(),
   284  		Constraints: constraints.MustParse(cons),
   285  	})
   286  	return
   287  }
   288  
   289  func ControlBucketName(e environs.Environ) string {
   290  	env := e.(*environ)
   291  	return env.ecfg().controlBucket()
   292  }
   293  
   294  func GetSwiftURL(e environs.Environ) (string, error) {
   295  	return e.(*environ).client.MakeServiceURL("object-store", nil)
   296  }
   297  
   298  func SetUseFloatingIP(e environs.Environ, val bool) {
   299  	env := e.(*environ)
   300  	env.ecfg().attrs["use-floating-ip"] = val
   301  }
   302  
   303  func SetUpGlobalGroup(e environs.Environ, name string, apiPort int) (nova.SecurityGroup, error) {
   304  	return e.(*environ).setUpGlobalGroup(name, apiPort)
   305  }
   306  
   307  func EnsureGroup(e environs.Environ, name string, rules []nova.RuleInfo) (nova.SecurityGroup, error) {
   308  	return e.(*environ).ensureGroup(name, rules)
   309  }
   310  
   311  func CollectInstances(e environs.Environ, ids []instance.Id, out map[string]instance.Instance) []instance.Id {
   312  	return e.(*environ).collectInstances(ids, out)
   313  }
   314  
   315  // ImageMetadataStorage returns a Storage object pointing where the goose
   316  // infrastructure sets up its keystone entry for image metadata
   317  func ImageMetadataStorage(e environs.Environ) storage.Storage {
   318  	env := e.(*environ)
   319  	return &openstackstorage{
   320  		containerName: "imagemetadata",
   321  		swift:         swift.New(env.client),
   322  	}
   323  }
   324  
   325  // CreateCustomStorage creates a swift container and returns the Storage object
   326  // so you can put data into it.
   327  func CreateCustomStorage(e environs.Environ, containerName string) storage.Storage {
   328  	env := e.(*environ)
   329  	swiftClient := swift.New(env.client)
   330  	if err := swiftClient.CreateContainer(containerName, swift.PublicRead); err != nil {
   331  		panic(err)
   332  	}
   333  	return &openstackstorage{
   334  		containerName: containerName,
   335  		swift:         swiftClient,
   336  	}
   337  }
   338  
   339  // BlankContainerStorage creates a Storage object with blank container name.
   340  func BlankContainerStorage() storage.Storage {
   341  	return &openstackstorage{}
   342  }
   343  
   344  func GetNovaClient(e environs.Environ) *nova.Client {
   345  	return e.(*environ).nova()
   346  }
   347  
   348  // ResolveNetwork exposes environ helper function resolveNetwork for testing
   349  func ResolveNetwork(e environs.Environ, networkName string) (string, error) {
   350  	return e.(*environ).resolveNetwork(networkName)
   351  }
   352  
   353  var PortsToRuleInfo = portsToRuleInfo
   354  var RuleMatchesPortRange = ruleMatchesPortRange
   355  
   356  var MakeServiceURL = &makeServiceURL