github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/facades/client/machinemanager/instance_information_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package machinemanager_test
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/juju/apiserver/common/storagecommon"
     9  	jujutesting "github.com/juju/testing"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  	"gopkg.in/juju/names.v2"
    13  
    14  	"github.com/juju/juju/apiserver/common"
    15  	"github.com/juju/juju/apiserver/facades/client/machinemanager"
    16  	"github.com/juju/juju/apiserver/params"
    17  	"github.com/juju/juju/apiserver/testing"
    18  	"github.com/juju/juju/cloud"
    19  	"github.com/juju/juju/core/constraints"
    20  	"github.com/juju/juju/environs"
    21  	"github.com/juju/juju/environs/config"
    22  	"github.com/juju/juju/environs/context"
    23  	"github.com/juju/juju/environs/instances"
    24  	"github.com/juju/juju/provider/dummy"
    25  	"github.com/juju/juju/state"
    26  )
    27  
    28  type instanceTypesSuite struct{}
    29  
    30  var _ = gc.Suite(&instanceTypesSuite{})
    31  
    32  var over9kCPUCores uint64 = 9001
    33  
    34  func (p *instanceTypesSuite) TestInstanceTypes(c *gc.C) {
    35  	backend := &mockBackend{
    36  		cloudSpec: environs.CloudSpec{},
    37  	}
    38  	pool := &mockPool{}
    39  	authorizer := testing.FakeAuthorizer{Tag: names.NewUserTag("admin"),
    40  		Controller: true}
    41  	itCons := constraints.Value{CpuCores: &over9kCPUCores}
    42  	failureCons := constraints.Value{}
    43  	env := mockEnviron{
    44  		results: map[constraints.Value]instances.InstanceTypesWithCostMetadata{
    45  			itCons: {
    46  				CostUnit:     "USD/h",
    47  				CostCurrency: "USD",
    48  				InstanceTypes: []instances.InstanceType{
    49  					{Name: "instancetype-1"},
    50  					{Name: "instancetype-2"}},
    51  			},
    52  		},
    53  	}
    54  	api, err := machinemanager.NewMachineManagerAPI(backend, backend, pool, authorizer, backend.ModelTag(), context.NewCloudCallContext(), common.NewResources())
    55  	c.Assert(err, jc.ErrorIsNil)
    56  
    57  	cons := params.ModelInstanceTypesConstraints{
    58  		Constraints: []params.ModelInstanceTypesConstraint{{Value: &itCons}, {Value: &failureCons}, {}},
    59  	}
    60  	fakeEnvironGet := func(st environs.EnvironConfigGetter,
    61  		newEnviron environs.NewEnvironFunc,
    62  	) (environs.Environ, error) {
    63  		return &env, nil
    64  	}
    65  	r, err := machinemanager.InstanceTypes(api, fakeEnvironGet, cons)
    66  	c.Assert(err, jc.ErrorIsNil)
    67  	c.Assert(r.Results, gc.HasLen, 3)
    68  	expected := []params.InstanceTypesResult{
    69  		{
    70  			InstanceTypes: []params.InstanceType{
    71  				{
    72  					Name: "instancetype-1",
    73  				},
    74  				{
    75  					Name: "instancetype-2",
    76  				}},
    77  			CostUnit:     "USD/h",
    78  			CostCurrency: "USD",
    79  		},
    80  		{
    81  			Error: &params.Error{Message: "Instances matching constraint  not found", Code: "not found"}},
    82  		{
    83  			Error: &params.Error{Message: "Instances matching constraint  not found", Code: "not found"}}}
    84  	c.Assert(r.Results, gc.DeepEquals, expected)
    85  }
    86  
    87  type mockBackend struct {
    88  	machinemanager.Backend
    89  	storagecommon.StorageAccess
    90  
    91  	cloudSpec environs.CloudSpec
    92  }
    93  
    94  func (st *mockBackend) VolumeAccess() storagecommon.VolumeAccess {
    95  	return nil
    96  }
    97  
    98  func (st *mockBackend) FilesystemAccess() storagecommon.FilesystemAccess {
    99  	return nil
   100  }
   101  
   102  func (b *mockBackend) ModelTag() names.ModelTag {
   103  	return names.NewModelTag("beef1beef1-0000-0000-000011112222")
   104  }
   105  
   106  func (b *mockBackend) Model() (machinemanager.Model, error) {
   107  	return &mockModel{}, nil
   108  }
   109  
   110  func (b *mockBackend) CloudSpec(names.ModelTag) (environs.CloudSpec, error) {
   111  	return b.cloudSpec, nil
   112  }
   113  
   114  func (b *mockBackend) Cloud(name string) (cloud.Cloud, error) {
   115  	return cloud.Cloud{}, nil
   116  }
   117  
   118  func (b *mockBackend) CloudCredential(tag names.CloudCredentialTag) (state.Credential, error) {
   119  	return state.Credential{}, nil
   120  }
   121  
   122  type mockPool struct {
   123  }
   124  
   125  func (*mockPool) GetModel(uuid string) (machinemanager.Model, func(), error) {
   126  	return &mockModel{}, func() {}, nil
   127  }
   128  
   129  type mockEnviron struct {
   130  	environs.Environ
   131  	machinemanager.Backend
   132  	jujutesting.Stub
   133  
   134  	results map[constraints.Value]instances.InstanceTypesWithCostMetadata
   135  }
   136  
   137  func (m *mockEnviron) InstanceTypes(ctx context.ProviderCallContext, c constraints.Value) (instances.InstanceTypesWithCostMetadata, error) {
   138  	it, ok := m.results[c]
   139  	if !ok {
   140  		return instances.InstanceTypesWithCostMetadata{}, errors.NotFoundf("Instances matching constraint %v", c)
   141  	}
   142  	return it, nil
   143  }
   144  
   145  type mockModel struct {
   146  	machinemanager.Model
   147  }
   148  
   149  func (mockModel) CloudCredential() (names.CloudCredentialTag, bool) {
   150  	return names.NewCloudCredentialTag("foo/bob/bar"), true
   151  }
   152  
   153  func (mockModel) ModelTag() names.ModelTag {
   154  	return names.NewModelTag("beef1beef1-0000-0000-000011112222")
   155  }
   156  
   157  func (*mockModel) Config() (*config.Config, error) {
   158  	return config.New(config.UseDefaults, dummy.SampleConfig())
   159  }
   160  
   161  func (*mockModel) Cloud() string {
   162  	return "a-cloud"
   163  }
   164  
   165  func (*mockModel) CloudRegion() string {
   166  	return "a-region"
   167  }