gopkg.in/goose.v2@v2.0.1/cinder/live_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the LGPLv3, see LICENCE file for details.
     3  
     4  package cinder
     5  
     6  import (
     7  	"log"
     8  	"net/http"
     9  	"net/url"
    10  	"time"
    11  
    12  	gc "gopkg.in/check.v1"
    13  	"gopkg.in/goose.v2/client"
    14  	"gopkg.in/goose.v2/identity"
    15  )
    16  
    17  var _ = gc.Suite(&liveCinderSuite{})
    18  
    19  type liveCinderSuite struct {
    20  	client *Client
    21  }
    22  
    23  func (s *liveCinderSuite) SetUpSuite(c *gc.C) {
    24  	if *live == false {
    25  		return
    26  	}
    27  
    28  	cred, err := identity.CompleteCredentialsFromEnv()
    29  	if err != nil {
    30  		log.Fatalf("error retrieving credentials from the environment: %v", err)
    31  	}
    32  
    33  	authClient := client.NewClient(cred, identity.AuthUserPass, nil)
    34  	if err = authClient.Authenticate(); err != nil {
    35  		log.Fatalf("error authenticating: %v", err)
    36  	}
    37  	endpoint := authClient.EndpointsForRegion(cred.Region)["volume"]
    38  	endpointUrl, err := url.Parse(endpoint)
    39  	if err != nil {
    40  		log.Fatalf("error parsing endpoint: %v", err)
    41  	}
    42  
    43  	handleRequest := SetAuthHeaderFn(authClient.Token,
    44  		func(req *http.Request) (*http.Response, error) {
    45  			log.Printf("Method: %v", req.Method)
    46  			log.Printf("URL: %v", req.URL)
    47  			log.Printf("req.Headers: %v", req.Header)
    48  			log.Printf("req.Body: %d", req.ContentLength)
    49  			return http.DefaultClient.Do(req)
    50  		})
    51  
    52  	s.client = NewClient(authClient.TenantId(), endpointUrl, handleRequest)
    53  }
    54  
    55  func (s *liveCinderSuite) SetUpTest(c *gc.C) {
    56  
    57  	if *live == false {
    58  		c.Skip("Not running live tests.")
    59  	}
    60  }
    61  
    62  func (s *liveCinderSuite) TestVolumesAndSnapshots(c *gc.C) {
    63  
    64  	volInfo, err := s.client.CreateVolume(CreateVolumeVolumeParams{Size: 1})
    65  	c.Assert(err, gc.IsNil)
    66  	defer func() {
    67  		err := s.client.DeleteVolume(volInfo.Volume.ID)
    68  		c.Assert(err, gc.IsNil)
    69  	}()
    70  
    71  	err = <-s.client.VolumeStatusNotifier(volInfo.Volume.ID, "available", 10, 1*time.Second)
    72  	c.Assert(err, gc.IsNil)
    73  
    74  	snapInfo, err := s.client.CreateSnapshot(CreateSnapshotSnapshotParams{VolumeId: volInfo.Volume.ID})
    75  	c.Assert(err, gc.IsNil)
    76  
    77  	c.Check(snapInfo.Snapshot.VolumeID, gc.Equals, volInfo.Volume.ID)
    78  
    79  	knownSnapInfo, err := s.client.GetSnapshot(snapInfo.Snapshot.ID)
    80  	c.Assert(err, gc.IsNil)
    81  
    82  	c.Check(knownSnapInfo.Snapshot.ID, gc.Equals, snapInfo.Snapshot.ID)
    83  
    84  	// Wait for snapshot to be available (or error?) before deleting.
    85  	err = <-s.client.SnapshotStatusNotifier(snapInfo.Snapshot.ID, "available", 10, 1*time.Second)
    86  	c.Check(err, gc.IsNil)
    87  
    88  	err = s.client.DeleteSnapshot(snapInfo.Snapshot.ID)
    89  	c.Assert(err, gc.IsNil)
    90  
    91  	// Wait for the snapshot to be deleted so that the volume can be deleted.
    92  	<-s.client.SnapshotStatusNotifier(snapInfo.Snapshot.ID, "deleted", 10, 1*time.Second)
    93  }
    94  
    95  func (s *liveCinderSuite) TestVolumeTypeOperations(c *gc.C) {
    96  
    97  	typeInfo, err := s.client.CreateVolumeType(CreateVolumeTypeVolumeTypeParams{
    98  		Name: "number-monster",
    99  		ExtraSpecs: CreateVolumeTypeVolumeTypeExtraSpecsParams{
   100  			Capabilities: "gpu",
   101  		},
   102  	})
   103  	c.Assert(err, gc.IsNil)
   104  
   105  	knownTypeInfo, err := s.client.GetVolumeType(typeInfo.VolumeType.ID)
   106  	c.Assert(err, gc.IsNil)
   107  	c.Check(knownTypeInfo.VolumeType.ID, gc.Equals, typeInfo.VolumeType.ID)
   108  
   109  	err = s.client.DeleteVolumeType(typeInfo.VolumeType.ID)
   110  	c.Assert(err, gc.IsNil)
   111  }
   112  
   113  func (s *liveCinderSuite) TestVolumeMetadata(c *gc.C) {
   114  
   115  	metadata := map[string]string{
   116  		"a": "b",
   117  		"c": "d",
   118  	}
   119  	volInfo, err := s.client.CreateVolume(CreateVolumeVolumeParams{
   120  		Size:     1,
   121  		Metadata: metadata,
   122  	})
   123  	c.Assert(err, gc.IsNil)
   124  	defer func() {
   125  		err := s.client.DeleteVolume(volInfo.Volume.ID)
   126  		c.Assert(err, gc.IsNil)
   127  	}()
   128  
   129  	err = <-s.client.VolumeStatusNotifier(volInfo.Volume.ID, "available", 10, 1*time.Second)
   130  	c.Assert(err, gc.IsNil)
   131  
   132  	result, err := s.client.GetVolume(volInfo.Volume.ID)
   133  	c.Assert(err, gc.IsNil)
   134  	c.Assert(result, gc.NotNil)
   135  	c.Assert(result.Volume.Metadata, gc.DeepEquals, metadata)
   136  }
   137  
   138  func (s *liveCinderSuite) TestListVersions(c *gc.C) {
   139  	result, err := s.client.ListVersions()
   140  	c.Assert(err, gc.IsNil)
   141  	c.Assert(result, gc.NotNil)
   142  
   143  	c.Logf("versions: %#v", result.Versions)
   144  	c.Assert(len(result.Versions), gc.Not(gc.Equals), 0)
   145  }
   146  
   147  func (s *liveCinderSuite) TestUpdateVolumeMetadata(c *gc.C) {
   148  	metadata := map[string]string{
   149  		"Fresh": "Born",
   150  		"Twin":  "Killers",
   151  	}
   152  	volInfo, err := s.client.CreateVolume(CreateVolumeVolumeParams{
   153  		Size:     75,
   154  		Metadata: metadata,
   155  	})
   156  	c.Assert(err, gc.IsNil)
   157  	volumeId := volInfo.Volume.ID
   158  
   159  	defer func() {
   160  		err := s.client.DeleteVolume(volumeId)
   161  		c.Assert(err, gc.IsNil)
   162  	}()
   163  
   164  	err = <-s.client.VolumeStatusNotifier(volumeId, "available", 10, 1*time.Second)
   165  	c.Assert(err, gc.IsNil)
   166  
   167  	result, err := s.client.SetVolumeMetadata(volumeId, map[string]string{
   168  		"Twin":     "Huggers",
   169  		"Paradise": "People",
   170  	})
   171  	c.Assert(err, gc.IsNil)
   172  	c.Assert(result["Fresh"], gc.Equals, "Born")
   173  	c.Assert(result["Twin"], gc.Equals, "Huggers")
   174  	c.Assert(result["Paradise"], gc.Equals, "People")
   175  
   176  	volResult, err := s.client.GetVolume(volumeId)
   177  	c.Assert(err, gc.IsNil)
   178  	c.Assert(volResult, gc.NotNil)
   179  	c.Assert(volResult.Volume.Metadata["Fresh"], gc.Equals, "Born")
   180  	c.Assert(volResult.Volume.Metadata["Twin"], gc.Equals, "Huggers")
   181  	c.Assert(volResult.Volume.Metadata["Paradise"], gc.Equals, "People")
   182  }