github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/provider/azure/internal/azuretesting/storage.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package azuretesting
     5  
     6  import (
     7  	"github.com/Azure/azure-sdk-for-go/storage"
     8  	"github.com/juju/errors"
     9  	"github.com/juju/testing"
    10  
    11  	"github.com/juju/juju/provider/azure/internal/azurestorage"
    12  )
    13  
    14  type MockStorageClient struct {
    15  	testing.Stub
    16  	Containers map[string]azurestorage.Container
    17  }
    18  
    19  // NewClient exists to satisfy users who want a NewClientFunc.
    20  func (c *MockStorageClient) NewClient(
    21  	accountName, accountKey, blobServiceBaseURL, apiVersion string,
    22  	useHTTPS bool,
    23  ) (azurestorage.Client, error) {
    24  	c.AddCall("NewClient", accountName, accountKey, blobServiceBaseURL, apiVersion, useHTTPS)
    25  	return c, c.NextErr()
    26  }
    27  
    28  func (c *MockStorageClient) GetBlobService() azurestorage.BlobStorageClient {
    29  	return c
    30  }
    31  
    32  func (c *MockStorageClient) GetContainerReference(name string) azurestorage.Container {
    33  	c.MethodCall(c, "GetContainerReference", name)
    34  	container := c.Containers[name]
    35  	if container == nil {
    36  		container = notFoundContainer{name}
    37  	}
    38  	return container
    39  }
    40  
    41  type MockStorageContainer struct {
    42  	testing.Stub
    43  	Blobs_ []azurestorage.Blob
    44  }
    45  
    46  func (c *MockStorageContainer) Blobs() ([]azurestorage.Blob, error) {
    47  	c.MethodCall(c, "Blobs")
    48  	return c.Blobs_, c.NextErr()
    49  }
    50  
    51  func (c *MockStorageContainer) Blob(name string) azurestorage.Blob {
    52  	c.MethodCall(c, "Blob", name)
    53  	for _, blob := range c.Blobs_ {
    54  		if blob.Name() == name {
    55  			return blob
    56  		}
    57  	}
    58  	return notFoundBlob{name: name}
    59  }
    60  
    61  type MockStorageBlob struct {
    62  	testing.Stub
    63  	Name_       string
    64  	Properties_ storage.BlobProperties
    65  }
    66  
    67  func (c *MockStorageBlob) Name() string {
    68  	return c.Name_
    69  }
    70  
    71  func (c *MockStorageBlob) Properties() storage.BlobProperties {
    72  	return c.Properties_
    73  }
    74  
    75  func (c *MockStorageBlob) DeleteIfExists(opts *storage.DeleteBlobOptions) (bool, error) {
    76  	c.MethodCall(c, "DeleteIfExists", opts)
    77  	return true, c.NextErr()
    78  }
    79  
    80  type notFoundContainer struct {
    81  	name string
    82  }
    83  
    84  func (c notFoundContainer) Blobs() ([]azurestorage.Blob, error) {
    85  	return nil, errors.NotFoundf("container %q", c.name)
    86  }
    87  
    88  func (c notFoundContainer) Blob(name string) azurestorage.Blob {
    89  	return notFoundBlob{
    90  		name:      name,
    91  		deleteErr: errors.NotFoundf("container %q", c.name),
    92  	}
    93  }
    94  
    95  type notFoundBlob struct {
    96  	name      string
    97  	deleteErr error
    98  }
    99  
   100  func (b notFoundBlob) Name() string {
   101  	return b.name
   102  }
   103  
   104  func (notFoundBlob) Properties() storage.BlobProperties {
   105  	return storage.BlobProperties{}
   106  }
   107  
   108  func (b notFoundBlob) DeleteIfExists(opts *storage.DeleteBlobOptions) (bool, error) {
   109  	// TODO(axw) should this return an error if the container doesn't exist?
   110  	return false, b.deleteErr
   111  }