diff --git a/apiserver/facades/client/application/application_test.go b/apiserver/facades/client/application/application_test.go index 5e383eacaae..f111f088398 100644 --- a/apiserver/facades/client/application/application_test.go +++ b/apiserver/facades/client/application/application_test.go @@ -12,6 +12,7 @@ import ( gomock "go.uber.org/mock/gomock" gc "gopkg.in/check.v1" + "github.com/juju/juju/core/application" coreassumes "github.com/juju/juju/core/assumes" charmtesting "github.com/juju/juju/core/charm/testing" "github.com/juju/juju/core/network" @@ -44,7 +45,7 @@ func (s *applicationSuite) TestSetCharm(c *gc.C) { s.setupAPI(c) s.expectApplication(c, "foo") s.expectCharm(c, "foo") - s.expectCharmConfig(c) + s.expectCharmConfig(c, 1) s.expectCharmAssumes(c) s.expectCharmFormatCheck(c, "foo") @@ -138,7 +139,7 @@ func (s *applicationSuite) TestSetCharmEndpointBindings(c *gc.C) { s.expectApplication(c, "foo") s.expectSpaceName(c, "bar") s.expectCharm(c, "foo") - s.expectCharmConfig(c) + s.expectCharmConfig(c, 1) s.expectCharmAssumes(c) s.expectCharmFormatCheck(c, "foo") @@ -260,7 +261,7 @@ func (s *applicationSuite) TestSetCharmInvalidConfig(c *gc.C) { s.setupAPI(c) s.expectApplication(c, "foo") s.expectCharm(c, "foo") - s.expectCharmConfig(c) + s.expectCharmConfig(c, 1) err := s.api.SetCharm(context.Background(), params.ApplicationSetCharmV2{ ApplicationName: "foo", @@ -292,7 +293,7 @@ func (s *applicationSuite) TestSetCharmWithoutTrust(c *gc.C) { s.setupAPI(c) s.expectApplication(c, "foo") s.expectCharm(c, "foo") - s.expectCharmConfig(c) + s.expectCharmConfig(c, 1) s.expectCharmAssumes(c) s.expectCharmFormatCheck(c, "foo") @@ -347,7 +348,7 @@ func (s *applicationSuite) TestSetCharmFormatDowngrade(c *gc.C) { s.setupAPI(c) s.expectApplication(c, "foo") s.expectCharm(c, "foo") - s.expectCharmConfig(c) + s.expectCharmConfig(c, 1) s.expectCharmAssumes(c) s.expectCharmFormatCheckDowngrade(c, "foo") @@ -375,6 +376,72 @@ func (s *applicationSuite) TestSetCharmFormatDowngrade(c *gc.C) { c.Assert(err, gc.ErrorMatches, "cannot downgrade from v2 charm format to v1") } +func (s *applicationSuite) TestDeploy(c *gc.C) { + defer s.setupMocks(c).Finish() + + s.setupAPI(c) + s.expectCharm(c, "foo") + s.expectCharmConfig(c, 2) + s.expectCharmMeta("foo", 6) + s.expectReadSequence("foo", 1) + s.expectAddApplication() + s.expectCreateApplication("foo") + + errorResults, err := s.api.Deploy(context.Background(), params.ApplicationsDeploy{ + Applications: []params.ApplicationDeploy{ + { + ApplicationName: "foo", + CharmURL: "local:foo-42", + CharmOrigin: ¶ms.CharmOrigin{ + Type: "charm", + Source: "local", + Base: params.Base{ + Name: "ubuntu", + Channel: "24.04", + }, + Architecture: "amd64", + Revision: ptr(42), + Track: ptr("1.0"), + Risk: "stable", + }, + }, + }, + }) + c.Assert(err, jc.ErrorIsNil) + c.Assert(errorResults.Results, gc.HasLen, 1) + c.Assert(errorResults.Results[0].Error, gc.IsNil) +} + +func (s *applicationSuite) TestDeployInvalidSource(c *gc.C) { + defer s.setupMocks(c).Finish() + + s.setupAPI(c) + + errorResults, err := s.api.Deploy(context.Background(), params.ApplicationsDeploy{ + Applications: []params.ApplicationDeploy{ + { + ApplicationName: "foo", + CharmURL: "bad:foo-42", + CharmOrigin: ¶ms.CharmOrigin{ + Type: "charm", + Source: "bad", + Base: params.Base{ + Name: "ubuntu", + Channel: "24.04", + }, + Architecture: "amd64", + Revision: ptr(42), + Track: ptr("1.0"), + Risk: "stable", + }, + }, + }, + }) + c.Assert(err, jc.ErrorIsNil) + c.Assert(errorResults.Results, gc.HasLen, 1) + c.Assert(errorResults.Results[0].Error, gc.ErrorMatches, "\"bad\" not a valid charm origin source") +} + func (s *applicationSuite) setupMocks(c *gc.C) *gomock.Controller { ctrl := s.baseSuite.setupMocks(c) @@ -400,6 +467,23 @@ func (s *applicationSuite) expectApplicationNotFound(c *gc.C, name string) { s.backend.EXPECT().Application(name).Return(nil, errors.NotFoundf("application %q", name)) } +func (s *applicationSuite) expectReadSequence(name string, seqResult int) { + s.backend.EXPECT().ReadSequence(name).Return(seqResult, nil) +} + +func (s *applicationSuite) expectAddApplication() { + s.backend.EXPECT().AddApplication(gomock.Any(), s.objectStore).Return(s.application, nil) +} + +func (s *applicationSuite) expectCreateApplication(name string) { + s.applicationService.EXPECT().CreateApplication(gomock.Any(), + name, + gomock.Any(), + gomock.Any(), + gomock.Any(), + ).Return(application.ID("app-"+name), nil) +} + func (s *applicationSuite) expectSpaceName(c *gc.C, name string) { s.networkService.EXPECT().SpaceByName(gomock.Any(), name).Return(&network.SpaceInfo{ ID: "space-1", @@ -416,6 +500,7 @@ func (s *applicationSuite) expectCharm(c *gc.C, name string) { s.applicationService.EXPECT().GetCharmID(gomock.Any(), applicationcharm.GetCharmArgs{ Name: name, Revision: ptr(42), + Source: applicationcharm.LocalSource, }).Return(id, nil) s.applicationService.EXPECT().GetCharm(gomock.Any(), id).Return(s.charm, applicationcharm.CharmOrigin{ @@ -427,10 +512,11 @@ func (s *applicationSuite) expectCharmNotFound(c *gc.C, name string) { s.applicationService.EXPECT().GetCharmID(gomock.Any(), applicationcharm.GetCharmArgs{ Name: name, Revision: ptr(42), + Source: applicationcharm.LocalSource, }).Return("", applicationerrors.CharmNotFound) } -func (s *applicationSuite) expectCharmConfig(c *gc.C) { +func (s *applicationSuite) expectCharmConfig(c *gc.C, times int) { cfg, err := internalcharm.ReadConfig(strings.NewReader(` options: stringOption: @@ -440,7 +526,13 @@ options: `)) c.Assert(err, jc.ErrorIsNil) - s.charm.EXPECT().Config().Return(cfg) + s.charm.EXPECT().Config().Return(cfg).Times(times) +} + +func (s *applicationSuite) expectCharmMeta(name string, times int) { + s.charm.EXPECT().Meta().Return(&internalcharm.Meta{ + Name: name, + }).Times(times) } func (s *applicationSuite) expectCharmAssumes(c *gc.C) { diff --git a/apiserver/facades/client/application/package_test.go b/apiserver/facades/client/application/package_test.go index db7b6d02c89..f2e4be3a323 100644 --- a/apiserver/facades/client/application/package_test.go +++ b/apiserver/facades/client/application/package_test.go @@ -137,6 +137,7 @@ func (s *baseSuite) newAPI(c *gc.C, modelType model.ModelType) { UUID: s.modelUUID, Type: modelType, } + s.deployApplication = DeployApplication var err error s.api, err = NewAPIBase(