Skip to content

Commit

Permalink
Merge pull request juju#18750 from manadart/dqlite-run-test-no-make-c…
Browse files Browse the repository at this point in the history
…harm

juju#18750

See: juju#18441

This removes tests from the action facade that call `MakeCharm` on the testing factory.

The missing coverage is added to a stub test as per convention.
  • Loading branch information
jujubot authored Feb 3, 2025
2 parents 9af5799 + 9f4549f commit b85a572
Showing 1 changed file with 4 additions and 138 deletions.
142 changes: 4 additions & 138 deletions apiserver/facades/client/action/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
apiservertesting "github.com/juju/juju/apiserver/testing"
blockcommanderrors "github.com/juju/juju/domain/blockcommand/errors"
"github.com/juju/juju/internal/testing"
"github.com/juju/juju/internal/testing/factory"
jujutesting "github.com/juju/juju/juju/testing"
"github.com/juju/juju/rpc/params"
"github.com/juju/juju/state"
Expand Down Expand Up @@ -64,135 +63,10 @@ func (s *runSuite) TestBlockRunMachineAndApplication(c *gc.C) {
s.assertBlocked(c, err, "TestBlockRunMachineAndApplication")
}

func (s *runSuite) TestRunMachineAndApplication(c *gc.C) {
defer s.setupMocks(c).Finish()

s.blockCommandService.EXPECT().GetBlockSwitchedOn(gomock.Any(), gomock.Any()).Return("", blockcommanderrors.NotFound)

// We only test that we create the actions correctly
// There is no need to test anything else at this level.
expectedPayload := map[string]interface{}{
"command": "hostname",
"timeout": int64(0),
"workload-context": false,
}
parallel := true
executionGroup := "group"
arg := params.Actions{
Actions: []params.Action{
{Receiver: "unit-magic-0", Name: "juju-exec", Parameters: expectedPayload, Parallel: &parallel, ExecutionGroup: &executionGroup},
{Receiver: "unit-magic-1", Name: "juju-exec", Parameters: expectedPayload, Parallel: &parallel, ExecutionGroup: &executionGroup},
{Receiver: "machine-0", Name: "juju-exec", Parameters: expectedPayload, Parallel: &parallel, ExecutionGroup: &executionGroup},
},
}
s.addMachine(c)

st := s.ControllerModel(c).State()

f, release := s.NewFactory(c, s.ControllerModelUUID())
defer release()
charm := f.MakeCharm(c, &factory.CharmParams{Name: "dummy"})
magic, err := st.AddApplication(

state.AddApplicationArgs{
Name: "magic", Charm: charm,
CharmURL: charm.URL(),
CharmOrigin: &state.CharmOrigin{Platform: &state.Platform{OS: "ubuntu", Channel: "20.04/stable"}},
},
jujutesting.NewObjectStore(c, s.ControllerModelUUID()),
)
c.Assert(err, jc.ErrorIsNil)
s.addUnit(c, magic)
s.addUnit(c, magic)

s.client.Run(
context.Background(),
params.RunParams{
Commands: "hostname",
Machines: []string{"0"},
Applications: []string{"magic"},
Parallel: &parallel,
ExecutionGroup: &executionGroup,
})
op, err := s.client.EnqueueOperation(context.Background(), arg)
c.Assert(err, jc.ErrorIsNil)
c.Assert(op.Actions, gc.HasLen, 3)

emptyActionTag := names.ActionTag{}
for i, r := range op.Actions {
c.Assert(r.Action, gc.NotNil)
c.Assert(r.Action.Tag, gc.Not(gc.Equals), emptyActionTag)
c.Assert(r.Action.Name, gc.Equals, "juju-exec")
c.Assert(r.Action.Receiver, gc.Equals, arg.Actions[i].Receiver)
c.Assert(r.Action.Parameters, jc.DeepEquals, expectedPayload)
c.Assert(r.Action.Parallel, jc.DeepEquals, &parallel)
c.Assert(r.Action.ExecutionGroup, jc.DeepEquals, &executionGroup)
}
}

func (s *runSuite) TestRunApplicationWorkload(c *gc.C) {
defer s.setupMocks(c).Finish()

s.blockCommandService.EXPECT().GetBlockSwitchedOn(gomock.Any(), gomock.Any()).Return("", blockcommanderrors.NotFound)

// We only test that we create the actions correctly
// There is no need to test anything else at this level.
expectedPayload := map[string]interface{}{
"command": "hostname",
"timeout": int64(0),
"workload-context": true,
}
parallel := true
executionGroup := "group"
arg := params.Actions{
Actions: []params.Action{
{Receiver: "unit-magic-0", Name: "juju-exec", Parameters: expectedPayload, Parallel: &parallel, ExecutionGroup: &executionGroup},
{Receiver: "unit-magic-1", Name: "juju-exec", Parameters: expectedPayload, Parallel: &parallel, ExecutionGroup: &executionGroup},
},
}
s.addMachine(c)

st := s.ControllerModel(c).State()

f, release := s.NewFactory(c, s.ControllerModelUUID())
defer release()
charm := f.MakeCharm(c, &factory.CharmParams{Name: "dummy"})
magic, err := st.AddApplication(

state.AddApplicationArgs{
Name: "magic", Charm: charm,
CharmURL: charm.URL(),
CharmOrigin: &state.CharmOrigin{Platform: &state.Platform{OS: "ubuntu", Channel: "20.04/stable"}},
},
jujutesting.NewObjectStore(c, s.ControllerModelUUID()),
)
c.Assert(err, jc.ErrorIsNil)
s.addUnit(c, magic)
s.addUnit(c, magic)

s.client.Run(
context.Background(),
params.RunParams{
Commands: "hostname",
Applications: []string{"magic"},
WorkloadContext: true,
Parallel: &parallel,
ExecutionGroup: &executionGroup,
})
op, err := s.client.EnqueueOperation(context.Background(), arg)
c.Assert(err, jc.ErrorIsNil)
c.Assert(op.Actions, gc.HasLen, 2)

emptyActionTag := names.ActionTag{}
for i, r := range op.Actions {
c.Assert(r.Action, gc.NotNil)
c.Assert(r.Action.Tag, gc.Not(gc.Equals), emptyActionTag)
c.Assert(r.Action.Name, gc.Equals, "juju-exec")
c.Assert(r.Action.Receiver, gc.Equals, arg.Actions[i].Receiver)
c.Assert(r.Action.Parameters, jc.DeepEquals, expectedPayload)
c.Assert(r.Action.Parallel, jc.DeepEquals, &parallel)
c.Assert(r.Action.ExecutionGroup, jc.DeepEquals, &executionGroup)
}
func (s *runSuite) TestStub(c *gc.C) {
c.Skip(`This suite is missing tests for the following scenarios:
Running juju-exec with machine, application and unit targets.
`)
}

func (s *runSuite) TestRunOnAllMachines(c *gc.C) {
Expand Down Expand Up @@ -314,14 +188,6 @@ func (s *runSuite) addMachine(c *gc.C) *state.Machine {
return machine
}

func (s *runSuite) addUnit(c *gc.C, application *state.Application) *state.Unit {
unit, err := application.AddUnit(state.AddUnitParams{})
c.Assert(err, jc.ErrorIsNil)
err = unit.AssignToNewMachine()
c.Assert(err, jc.ErrorIsNil)
return unit
}

func (s *runSuite) assertBlocked(c *gc.C, err error, msg string) {
c.Assert(params.IsCodeOperationBlocked(err), jc.IsTrue, gc.Commentf("error: %#v", err))
c.Assert(errors.Cause(err), gc.DeepEquals, &params.Error{
Expand Down

0 comments on commit b85a572

Please sign in to comment.