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

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package applicationoffers_test
     5  
     6  import (
     7  	jtesting "github.com/juju/testing"
     8  	jc "github.com/juju/testing/checkers"
     9  	gc "gopkg.in/check.v1"
    10  	"gopkg.in/juju/charm.v6"
    11  	"gopkg.in/juju/names.v2"
    12  
    13  	"github.com/juju/juju/apiserver/common"
    14  	"github.com/juju/juju/apiserver/common/crossmodel"
    15  	"github.com/juju/juju/apiserver/facades/client/applicationoffers"
    16  	"github.com/juju/juju/apiserver/testing"
    17  	jujucrossmodel "github.com/juju/juju/core/crossmodel"
    18  	"github.com/juju/juju/environs"
    19  	"github.com/juju/juju/network"
    20  	"github.com/juju/juju/permission"
    21  	"github.com/juju/juju/state"
    22  	coretesting "github.com/juju/juju/testing"
    23  )
    24  
    25  const (
    26  	addOffersBackendCall  = "addOffersCall"
    27  	listOffersBackendCall = "listOffersCall"
    28  )
    29  
    30  type baseSuite struct {
    31  	jtesting.IsolationSuite
    32  
    33  	resources  *common.Resources
    34  	authorizer *testing.FakeAuthorizer
    35  
    36  	mockState         *mockState
    37  	mockStatePool     *mockStatePool
    38  	env               *mockEnviron
    39  	bakery            *mockBakeryService
    40  	authContext       *crossmodel.AuthContext
    41  	applicationOffers *stubApplicationOffers
    42  }
    43  
    44  func (s *baseSuite) SetUpTest(c *gc.C) {
    45  	s.IsolationSuite.SetUpTest(c)
    46  	s.resources = common.NewResources()
    47  	s.authorizer = &testing.FakeAuthorizer{
    48  		Tag:      names.NewUserTag("read"),
    49  		AdminTag: names.NewUserTag("admin"),
    50  	}
    51  
    52  	s.env = &mockEnviron{}
    53  	s.mockState = &mockState{
    54  		modelUUID:         coretesting.ModelTag.Id(),
    55  		users:             make(map[string]applicationoffers.User),
    56  		applicationOffers: make(map[string]jujucrossmodel.ApplicationOffer),
    57  		accessPerms:       make(map[offerAccess]permission.Access),
    58  		spaces:            make(map[string]applicationoffers.Space),
    59  		relations:         make(map[string]crossmodel.Relation),
    60  		relationNetworks:  &mockRelationNetworks{},
    61  	}
    62  	s.mockStatePool = &mockStatePool{map[string]applicationoffers.Backend{s.mockState.modelUUID: s.mockState}}
    63  }
    64  
    65  func (s *baseSuite) addApplication(c *gc.C, name string) jujucrossmodel.ApplicationOffer {
    66  	return jujucrossmodel.ApplicationOffer{
    67  		OfferName:              "offer-" + name,
    68  		OfferUUID:              "offer-" + name + "-uuid",
    69  		ApplicationName:        name,
    70  		Endpoints:              map[string]charm.Relation{"db": {Name: "db"}},
    71  		ApplicationDescription: "applicaion description",
    72  	}
    73  }
    74  
    75  func (s *baseSuite) setupOffers(c *gc.C, filterAppName string, filterWithEndpoints bool) {
    76  	applicationName := "test"
    77  	offerName := "hosted-db2"
    78  
    79  	anOffer := jujucrossmodel.ApplicationOffer{
    80  		OfferName:              offerName,
    81  		OfferUUID:              offerName + "-uuid",
    82  		ApplicationName:        applicationName,
    83  		ApplicationDescription: "description",
    84  		Endpoints:              map[string]charm.Relation{"db": {Name: "db2"}},
    85  	}
    86  
    87  	s.applicationOffers.listOffers = func(filters ...jujucrossmodel.ApplicationOfferFilter) ([]jujucrossmodel.ApplicationOffer, error) {
    88  		c.Assert(filters, gc.HasLen, 1)
    89  		expectedFilter := jujucrossmodel.ApplicationOfferFilter{
    90  			OfferName:       offerName,
    91  			ApplicationName: filterAppName,
    92  		}
    93  		if filterWithEndpoints {
    94  			expectedFilter.Endpoints = []jujucrossmodel.EndpointFilterTerm{{
    95  				Interface: "db2",
    96  			}}
    97  		}
    98  		c.Assert(filters[0], jc.DeepEquals, expectedFilter)
    99  		return []jujucrossmodel.ApplicationOffer{anOffer}, nil
   100  	}
   101  	ch := &mockCharm{meta: &charm.Meta{Description: "A pretty popular database"}}
   102  	s.mockState.applications = map[string]crossmodel.Application{
   103  		"test": &mockApplication{
   104  			name:  "test",
   105  			charm: ch, curl: charm.MustParseURL("db2-2"),
   106  			bindings: map[string]string{"db2": "myspace"},
   107  		},
   108  	}
   109  	s.mockState.model = &mockModel{
   110  		uuid: coretesting.ModelTag.Id(), name: "prod", owner: "fred", modelType: state.ModelTypeIAAS}
   111  	s.mockState.relations["hosted-db2:db wordpress:db"] = &mockRelation{
   112  		id: 1,
   113  		endpoint: state.Endpoint{
   114  			ApplicationName: "test",
   115  			Relation: charm.Relation{
   116  				Name:      "db",
   117  				Interface: "db2",
   118  				Role:      "provider",
   119  			},
   120  		},
   121  	}
   122  	s.mockState.connections = []applicationoffers.OfferConnection{
   123  		&mockOfferConnection{
   124  			username:    "fred",
   125  			modelUUID:   coretesting.ModelTag.Id(),
   126  			relationKey: "hosted-db2:db wordpress:db",
   127  			relationId:  1,
   128  		},
   129  	}
   130  	s.mockState.spaces["myspace"] = &mockSpace{
   131  		name:       "myspace",
   132  		providerId: "juju-space-myspace",
   133  		subnets: []applicationoffers.Subnet{
   134  			&mockSubnet{cidr: "4.3.2.0/24", providerId: "juju-subnet-1", zones: []string{"az1"}},
   135  		},
   136  	}
   137  	s.env.spaceInfo = &environs.ProviderSpaceInfo{
   138  		SpaceInfo: network.SpaceInfo{
   139  			Name:       "myspace",
   140  			ProviderId: "juju-space-myspace",
   141  			Subnets: []network.SubnetInfo{{
   142  				CIDR:              "4.3.2.0/24",
   143  				ProviderId:        "juju-subnet-1",
   144  				AvailabilityZones: []string{"az1"},
   145  			}},
   146  		},
   147  	}
   148  }