github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/crossmodel/offer_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package crossmodel_test 5 6 import ( 7 "github.com/juju/cmd" 8 "github.com/juju/cmd/cmdtesting" 9 "github.com/juju/errors" 10 jc "github.com/juju/testing/checkers" 11 gc "gopkg.in/check.v1" 12 13 "github.com/juju/juju/apiserver/common" 14 "github.com/juju/juju/apiserver/params" 15 "github.com/juju/juju/cmd/juju/crossmodel" 16 ) 17 18 type offerSuite struct { 19 BaseCrossModelSuite 20 mockAPI *mockOfferAPI 21 args []string 22 } 23 24 var _ = gc.Suite(&offerSuite{}) 25 26 func (s *offerSuite) SetUpTest(c *gc.C) { 27 s.BaseCrossModelSuite.SetUpTest(c) 28 29 s.mockAPI = newMockOfferAPI() 30 s.args = nil 31 } 32 33 func (s *offerSuite) TestOfferNoArgs(c *gc.C) { 34 s.assertOfferErrorOutput(c, ".*an offer must at least specify application endpoint.*") 35 } 36 37 func (s *offerSuite) TestOfferTooManyArgs(c *gc.C) { 38 s.args = []string{"tst:db", "alias", "extra"} 39 s.assertOfferErrorOutput(c, `unrecognized args: \["extra"\]`) 40 } 41 42 func (s *offerSuite) TestOfferInvalidApplication(c *gc.C) { 43 s.args = []string{"123:"} 44 s.assertOfferErrorOutput(c, `.*application name "123" not valid.*`) 45 } 46 47 func (s *offerSuite) TestOfferInvalidModel(c *gc.C) { 48 s.args = []string{"$model.123:db"} 49 s.assertOfferErrorOutput(c, `.*model name "\$model" not valid.*`) 50 } 51 52 func (s *offerSuite) TestOfferNoCurrentModel(c *gc.C) { 53 s.store.Models["test-master"].CurrentModel = "" 54 s.args = []string{"app:db"} 55 s.assertOfferErrorOutput(c, `no current model, use juju switch to select a model on which to operate`) 56 } 57 58 func (s *offerSuite) TestOfferInvalidEndpoints(c *gc.C) { 59 s.args = []string{"tst/123"} 60 s.assertOfferErrorOutput(c, `.*endpoints must conform to format.*`) 61 } 62 63 func (s *offerSuite) TestOfferNoEndpoints(c *gc.C) { 64 s.args = []string{"tst:"} 65 s.assertOfferErrorOutput(c, `.*specify endpoints for tst.*`) 66 } 67 68 func (s *offerSuite) assertOfferErrorOutput(c *gc.C, expected string) { 69 _, err := s.runOffer(c, s.args...) 70 c.Assert(errors.Cause(err), gc.ErrorMatches, expected) 71 } 72 73 func (s *offerSuite) runOffer(c *gc.C, args ...string) (*cmd.Context, error) { 74 return cmdtesting.RunCommand(c, crossmodel.NewOfferCommandForTest(s.store, s.mockAPI), args...) 75 } 76 77 func (s *offerSuite) TestOfferCallErred(c *gc.C) { 78 s.args = []string{"tst:db"} 79 s.mockAPI.errCall = true 80 s.assertOfferErrorOutput(c, ".*aborted.*") 81 } 82 83 func (s *offerSuite) TestOfferDataErred(c *gc.C) { 84 s.args = []string{"tst:db"} 85 s.mockAPI.errData = true 86 s.assertOfferErrorOutput(c, ".*failed.*") 87 } 88 89 func (s *offerSuite) TestOfferValid(c *gc.C) { 90 s.args = []string{"tst:db"} 91 s.assertOfferOutput(c, "test", "tst", "tst", []string{"db"}) 92 c.Assert(s.mockAPI.modelUUID, gc.Equals, "fred-uuid") 93 } 94 95 func (s *offerSuite) TestOfferWithAlias(c *gc.C) { 96 s.args = []string{"tst:db", "hosted-tst"} 97 s.assertOfferOutput(c, "test", "hosted-tst", "tst", []string{"db"}) 98 } 99 100 func (s *offerSuite) TestOfferExplicitModel(c *gc.C) { 101 s.args = []string{"bob/prod.tst:db"} 102 s.assertOfferOutput(c, "prod", "tst", "tst", []string{"db"}) 103 } 104 105 func (s *offerSuite) TestOfferMultipleEndpoints(c *gc.C) { 106 s.args = []string{"tst:db,admin"} 107 s.assertOfferOutput(c, "test", "tst", "tst", []string{"db", "admin"}) 108 } 109 110 func (s *offerSuite) assertOfferOutput(c *gc.C, expectedModel, expectedOffer, expectedApplication string, endpoints []string) { 111 _, err := s.runOffer(c, s.args...) 112 c.Assert(err, jc.ErrorIsNil) 113 c.Assert(s.mockAPI.offers[expectedOffer], jc.SameContents, endpoints) 114 } 115 116 type mockOfferAPI struct { 117 errCall, errData bool 118 modelUUID string 119 offers map[string][]string 120 applications map[string]string 121 descs map[string]string 122 } 123 124 func newMockOfferAPI() *mockOfferAPI { 125 mock := &mockOfferAPI{} 126 mock.offers = make(map[string][]string) 127 mock.descs = make(map[string]string) 128 mock.applications = make(map[string]string) 129 return mock 130 } 131 132 func (s *mockOfferAPI) Close() error { 133 return nil 134 } 135 136 func (s *mockOfferAPI) Offer(modelUUID, application string, endpoints []string, offerName, desc string) ([]params.ErrorResult, error) { 137 if s.errCall { 138 return nil, errors.New("aborted") 139 } 140 result := make([]params.ErrorResult, 1) 141 if s.errData { 142 result[0].Error = common.ServerError(errors.New("failed")) 143 return result, nil 144 } 145 s.modelUUID = modelUUID 146 if offerName == "" { 147 offerName = application 148 } 149 s.offers[offerName] = endpoints 150 s.applications[offerName] = application 151 s.descs[offerName] = desc 152 return result, nil 153 }