github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/worker/uniter/storage/mock_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package storage_test
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/juju/names"
    10  
    11  	"github.com/juju/juju/apiserver/common"
    12  	"github.com/juju/juju/apiserver/params"
    13  	"github.com/juju/juju/worker/uniter/hook"
    14  	"github.com/juju/juju/worker/uniter/operation"
    15  )
    16  
    17  type mockStorageAccessor struct {
    18  	storageAttachment             func(names.StorageTag, names.UnitTag) (params.StorageAttachment, error)
    19  	storageAttachmentLife         func([]params.StorageAttachmentId) ([]params.LifeResult, error)
    20  	unitStorageAttachments        func(names.UnitTag) ([]params.StorageAttachmentId, error)
    21  	destroyUnitStorageAttachments func(names.UnitTag) error
    22  	remove                        func(names.StorageTag, names.UnitTag) error
    23  }
    24  
    25  func (m *mockStorageAccessor) StorageAttachment(s names.StorageTag, u names.UnitTag) (params.StorageAttachment, error) {
    26  	return m.storageAttachment(s, u)
    27  }
    28  
    29  func (m *mockStorageAccessor) StorageAttachmentLife(ids []params.StorageAttachmentId) ([]params.LifeResult, error) {
    30  	if m.storageAttachmentLife != nil {
    31  		return m.storageAttachmentLife(ids)
    32  	}
    33  	results := make([]params.LifeResult, len(ids))
    34  	for i, id := range ids {
    35  		storageTag, err := names.ParseStorageTag(id.StorageTag)
    36  		if err != nil {
    37  			results[i].Error = common.ServerError(err)
    38  			continue
    39  		}
    40  		unitTag, err := names.ParseUnitTag(id.UnitTag)
    41  		if err != nil {
    42  			results[i].Error = common.ServerError(err)
    43  			continue
    44  		}
    45  		att, err := m.storageAttachment(storageTag, unitTag)
    46  		if err != nil {
    47  			results[i].Error = common.ServerError(err)
    48  			continue
    49  		}
    50  		results[i].Life = att.Life
    51  	}
    52  	return results, nil
    53  }
    54  
    55  func (m *mockStorageAccessor) UnitStorageAttachments(u names.UnitTag) ([]params.StorageAttachmentId, error) {
    56  	return m.unitStorageAttachments(u)
    57  }
    58  
    59  func (m *mockStorageAccessor) DestroyUnitStorageAttachments(u names.UnitTag) error {
    60  	return m.destroyUnitStorageAttachments(u)
    61  }
    62  
    63  func (m *mockStorageAccessor) RemoveStorageAttachment(s names.StorageTag, u names.UnitTag) error {
    64  	return m.remove(s, u)
    65  }
    66  
    67  type mockOperations struct {
    68  	operation.Factory
    69  }
    70  
    71  func (m *mockOperations) NewUpdateStorage(tags []names.StorageTag) (operation.Operation, error) {
    72  	return &mockOperation{"update storage"}, nil
    73  }
    74  
    75  func (m *mockOperations) NewRunHook(hookInfo hook.Info) (operation.Operation, error) {
    76  	return &mockOperation{fmt.Sprintf("run hook %v", hookInfo.Kind)}, nil
    77  }
    78  
    79  type mockOperation struct {
    80  	name string
    81  }
    82  
    83  func (m *mockOperation) String() string {
    84  	return m.name
    85  }
    86  
    87  func (m *mockOperation) NeedsGlobalMachineLock() bool {
    88  	return false
    89  }
    90  
    91  func (m *mockOperation) Prepare(state operation.State) (*operation.State, error) {
    92  	return &state, nil
    93  }
    94  
    95  func (m *mockOperation) Execute(state operation.State) (*operation.State, error) {
    96  	return &state, nil
    97  }
    98  
    99  func (m *mockOperation) Commit(state operation.State) (*operation.State, error) {
   100  	return &state, nil
   101  }