Skip to content

Commit ab06da3

Browse files
authored
refactor: move subprocess to common package (#591)
# Description Refactor: move subprocess up to the common package which other packages rely on, make process runner var public ## Type of change - Clean code (code refactor, test updates; does not introduce functional changes) ## Contributor checklist Please check that you have: - [x] self-reviewed the code in this PR Signed-off-by: Peter Sabaini <[email protected]>
1 parent 9dfa0c7 commit ab06da3

23 files changed

+182
-165
lines changed

microceph/ceph/bootstrap_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (s *bootstrapSuite) TestBootstrap() {
104104
addEnableMsgr2Expectations(r)
105105
addNetworkExpectationsBootstrap(nw, s.TestStateInterface)
106106

107-
processExec = r
107+
common.ProcessExec = r
108108
common.Network = nw
109109

110110
err := Bootstrap(context.Background(), s.TestStateInterface, common.BootstrapConfig{MonIp: "1.1.1.1", PublicNet: "1.1.1.1/24"})
@@ -121,7 +121,7 @@ func (s *bootstrapSuite) TestBootstrapDataPrep() {
121121

122122
addNetworkExpectations(nw, s.TestStateInterface)
123123

124-
processExec = r
124+
common.ProcessExec = r
125125
common.Network = nw
126126

127127
// Case 1. Only mon-ip is provided.

microceph/ceph/config.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func GetConfigItem(c types.Config) (types.Configs, error) {
145145
}
146146

147147
ret[0].Key = c.Key
148-
ret[0].Value, err = processExec.RunCommand("ceph", args...)
148+
ret[0].Value, err = common.ProcessExec.RunCommand("ceph", args...)
149149
if err != nil {
150150
return nil, err
151151
}
@@ -167,7 +167,7 @@ func RemoveConfigItem(c types.Config) error {
167167
c.Key,
168168
}
169169

170-
_, err = processExec.RunCommand("ceph", args...)
170+
_, err = common.ProcessExec.RunCommand("ceph", args...)
171171
if err != nil {
172172
return err
173173
}
@@ -186,7 +186,7 @@ func ListConfigs() (types.Configs, error) {
186186
"json-pretty",
187187
}
188188

189-
output, err := processExec.RunCommand("ceph", args...)
189+
output, err := common.ProcessExec.RunCommand("ceph", args...)
190190
if err != nil {
191191
return configs, err
192192
}
@@ -217,7 +217,7 @@ func setConfigItem(c types.Config) error {
217217
"json-pretty",
218218
}
219219

220-
_, err := processExec.RunCommand("ceph", args...)
220+
_, err := common.ProcessExec.RunCommand("ceph", args...)
221221
if err != nil {
222222
return err
223223
}

microceph/ceph/config_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package ceph
22

33
import (
44
"encoding/json"
5+
"github.com/canonical/microceph/microceph/common"
56
"testing"
67

78
"github.com/canonical/microceph/microceph/tests"
@@ -51,7 +52,7 @@ func (s *configSuite) TestSetConfig() {
5152

5253
r := mocks.NewRunner(s.T())
5354
addConfigSetExpectations(r, t.Key, t.Value)
54-
processExec = r
55+
common.ProcessExec = r
5556

5657
err := SetConfigItem(t)
5758
assert.NoError(s.T(), err)
@@ -69,7 +70,7 @@ func (s *configSuite) TestSetROConfigBypassChecks() {
6970

7071
r := mocks.NewRunner(s.T())
7172
addConfigSetExpectations(r, t.Key, t.Value)
72-
processExec = r
73+
common.ProcessExec = r
7374

7475
err := SetConfigItemUnsafe(t)
7576
assert.NoError(s.T(), err)
@@ -87,7 +88,7 @@ func (s *configSuite) TestGetConfig() {
8788

8889
r := mocks.NewRunner(s.T())
8990
addConfigOpExpectations(r, "get", "mon", t.Key, t.Value)
90-
processExec = r
91+
common.ProcessExec = r
9192

9293
_, err := GetConfigItem(t)
9394
assert.NoError(s.T(), err)
@@ -105,7 +106,7 @@ func (s *configSuite) TestResetConfig() {
105106

106107
r := mocks.NewRunner(s.T())
107108
addConfigOpExpectations(r, "rm", "global", t.Key, t.Value)
108-
processExec = r
109+
common.ProcessExec = r
109110

110111
err := RemoveConfigItem(t)
111112
assert.NoError(s.T(), err)
@@ -116,7 +117,7 @@ func (s *configSuite) TestListConfig() {
116117

117118
r := mocks.NewRunner(s.T())
118119
addListConfigExpectations(r, t.Key, t.Value)
119-
processExec = r
120+
common.ProcessExec = r
120121

121122
configs, err := ListConfigs()
122123
assert.NoError(s.T(), err)

microceph/ceph/crush.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package ceph
33
import (
44
"encoding/json"
55
"fmt"
6+
"github.com/canonical/microceph/microceph/common"
67
"strings"
78

89
"github.com/canonical/microceph/microceph/api/types"
@@ -12,7 +13,7 @@ import (
1213

1314
// addCrushRule creates a new default crush rule with a given name and failure domain
1415
func addCrushRule(name string, failureDomain string) error {
15-
_, err := processExec.RunCommand("ceph", "osd", "crush", "rule", "create-replicated", name, "default", failureDomain)
16+
_, err := common.ProcessExec.RunCommand("ceph", "osd", "crush", "rule", "create-replicated", name, "default", failureDomain)
1617
if err != nil {
1718
return err
1819
}
@@ -22,7 +23,7 @@ func addCrushRule(name string, failureDomain string) error {
2223

2324
// listCrushRules returns a list of crush rule names
2425
func listCrushRules() ([]string, error) {
25-
output, err := processExec.RunCommand("ceph", "osd", "crush", "rule", "ls")
26+
output, err := common.ProcessExec.RunCommand("ceph", "osd", "crush", "rule", "ls")
2627
if err != nil {
2728
return nil, err
2829
}
@@ -46,7 +47,7 @@ func haveCrushRule(name string) bool {
4647

4748
// getCrushRuleID returns the id of a crush rule with the given name
4849
func getCrushRuleID(name string) (string, error) {
49-
output, err := processExec.RunCommand("ceph", "osd", "crush", "rule", "dump", name)
50+
output, err := common.ProcessExec.RunCommand("ceph", "osd", "crush", "rule", "dump", name)
5051
if err != nil {
5152
return "", err
5253
}
@@ -77,7 +78,7 @@ func getPoolsForDomain(domain string) ([]string, error) {
7778
return nil, err
7879
}
7980

80-
output, err := processExec.RunCommand("ceph", "osd", "pool", "ls", "detail", "--format=json")
81+
output, err := common.ProcessExec.RunCommand("ceph", "osd", "pool", "ls", "detail", "--format=json")
8182
if err != nil {
8283
return nil, err
8384
}
@@ -90,7 +91,7 @@ func getPoolsForDomain(domain string) ([]string, error) {
9091

9192
// setPoolCrushRule sets the crush rule for a given pool
9293
func setPoolCrushRule(pool string, rule string) error {
93-
_, err := processExec.RunCommand("ceph", "osd", "pool", "set", pool, "crush_rule", rule)
94+
_, err := common.ProcessExec.RunCommand("ceph", "osd", "pool", "set", pool, "crush_rule", rule)
9495
if err != nil {
9596
return err
9697
}

microceph/ceph/keyring.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package ceph
33
import (
44
"bufio"
55
"fmt"
6+
"github.com/canonical/microceph/microceph/common"
67
"os"
78
"strings"
89
)
@@ -23,7 +24,7 @@ func genKeyring(path, name string, caps ...[]string) error {
2324
args = append(args, "--cap", capability[0], capability[1])
2425
}
2526

26-
_, err := processExec.RunCommand("ceph-authtool", args...)
27+
_, err := common.ProcessExec.RunCommand("ceph-authtool", args...)
2728
if err != nil {
2829
return err
2930
}
@@ -38,7 +39,7 @@ func importKeyring(path string, source string) error {
3839
source,
3940
}
4041

41-
_, err := processExec.RunCommand("ceph-authtool", args...)
42+
_, err := common.ProcessExec.RunCommand("ceph-authtool", args...)
4243
if err != nil {
4344
return err
4445
}

microceph/ceph/keyring_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ceph
22

33
import (
4+
"github.com/canonical/microceph/microceph/common"
45
"testing"
56

67
"github.com/canonical/microceph/microceph/mocks"
@@ -31,7 +32,7 @@ func (ks *KeyringSuite) TestClientKeyringCreation() {
3132
"ceph", "auth", "get-or-create", "client.RemoteName"}...).Return("ok", nil).Once()
3233
r.On("RunCommand", []interface{}{
3334
"ceph", "auth", "print-key", "client.RemoteName"}...).Return("ABCD", nil).Once()
34-
processExec = r
35+
common.ProcessExec = r
3536

3637
// Method call
3738
clientKey, err := CreateClientKey("RemoteName")
@@ -46,7 +47,7 @@ func (ks *KeyringSuite) TestClientKeyringDelete() {
4647
// mocks and expectations
4748
r.On("RunCommand", []interface{}{
4849
"ceph", "auth", "del", "client.RemoteName"}...).Return("ok", nil).Once()
49-
processExec = r
50+
common.ProcessExec = r
5051

5152
// Method call
5253
err := DeleteClientKey("RemoteName")

microceph/ceph/monitor.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package ceph
33
import (
44
"fmt"
55
"github.com/canonical/lxd/shared/logger"
6+
"github.com/canonical/microceph/microceph/common"
67
"os"
78
"path/filepath"
89
)
@@ -14,7 +15,7 @@ func genMonmap(path string, fsid string) error {
1415
path,
1516
}
1617

17-
_, err := processExec.RunCommand("monmaptool", args...)
18+
_, err := common.ProcessExec.RunCommand("monmaptool", args...)
1819
if err != nil {
1920
return err
2021
}
@@ -30,7 +31,7 @@ func addMonmap(path string, name string, address string) error {
3031
path,
3132
}
3233

33-
_, err := processExec.RunCommand("monmaptool", args...)
34+
_, err := common.ProcessExec.RunCommand("monmaptool", args...)
3435
if err != nil {
3536
return err
3637
}
@@ -47,7 +48,7 @@ func bootstrapMon(hostname string, path string, monmap string, keyring string) e
4748
"--keyring", keyring,
4849
}
4950

50-
_, err := processExec.RunCommand("ceph-mon", args...)
51+
_, err := common.ProcessExec.RunCommand("ceph-mon", args...)
5152
if err != nil {
5253
return err
5354
}

microceph/ceph/nfs.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package ceph
33
import (
44
"context"
55
"fmt"
6+
"github.com/canonical/microceph/microceph/common"
67
"os"
78
"path/filepath"
89
"strconv"
@@ -300,5 +301,5 @@ func removeNodeFromSharedGraceMgmtDb(cephConfPath, clusterID, userID, node strin
300301

301302
// ganeshaRadosGraceRun runs the ganesha-rados-grace command with the given arguments.
302303
func ganeshaRadosGraceRun(args ...string) (string, error) {
303-
return processExec.RunCommand("ganesha-rados-grace", args...)
304+
return common.ProcessExec.RunCommand("ganesha-rados-grace", args...)
304305
}

microceph/ceph/nfs_test.go

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package ceph
33
import (
44
"context"
55
"fmt"
6+
"github.com/canonical/microceph/microceph/common"
67
"os"
78
"path/filepath"
89
"testing"
@@ -72,8 +73,8 @@ func (s *NFSSuite) TestEnableNFS() {
7273
// startNFS call
7374
r.On("RunCommand", "snapctl", "start", "microceph.nfs", "--enable").Return("ok", nil).Once()
7475

75-
// patch processExec
76-
processExec = r
76+
// patch ProcessExec
77+
common.ProcessExec = r
7778

7879
nfs := &NFSServicePlacement{
7980
ClusterID: clusterID,
@@ -199,8 +200,8 @@ func (s *NFSSuite) TestDisableNFS() {
199200
clientUser := fmt.Sprintf("client.%s", userID)
200201
r.On("RunCommand", "ceph", "auth", "del", clientUser).Return("ok", nil).Once()
201202

202-
// patch processExec
203-
processExec = r
203+
// patch ProcessExec
204+
common.ProcessExec = r
204205

205206
// function call
206207
err = DisableNFS(ctx, s.TestStateInterface, clusterID)
@@ -217,8 +218,8 @@ func (s *NFSSuite) TestStartNFS() {
217218
r := mocks.NewRunner(s.T())
218219
r.On("RunCommand", "snapctl", "start", "microceph.nfs", "--enable").Return("ok", nil).Once()
219220

220-
// patch processExec
221-
processExec = r
221+
// patch ProcessExec
222+
common.ProcessExec = r
222223

223224
// function call
224225
err := startNFS()
@@ -229,8 +230,8 @@ func (s *NFSSuite) TestStopNFS() {
229230
r := mocks.NewRunner(s.T())
230231
r.On("RunCommand", "snapctl", "stop", "microceph.nfs", "--disable").Return("ok", nil).Once()
231232

232-
// patch processExec
233-
processExec = r
233+
// patch ProcessExec
234+
common.ProcessExec = r
234235

235236
// function call
236237
err := stopNFS()
@@ -244,8 +245,8 @@ func (s *NFSSuite) TestCreateNFSKeyring() {
244245
// mocks and expectations
245246
r.On("RunCommand", []interface{}{"ceph", "auth", "get-or-create", "client.lish", "mon", "allow r", "osd", "allow rw pool=.nfs namespace=foo", "-o", keyringPath}...).Return("ok", nil).Once()
246247

247-
// patch processExec
248-
processExec = r
248+
// patch ProcessExec
249+
common.ProcessExec = r
249250

250251
// function call
251252
err := createNFSKeyring(s.Tmp, "foo", "lish")
@@ -262,8 +263,8 @@ func (s *NFSSuite) TestEnsureNFSPoolFailPool() {
262263
r.On("RunCommand", []interface{}{
263264
"rados", "ls", "--pool", ".nfs", "--all", "--create"}...).Return("", expectedErr).Once()
264265

265-
// patch processExec
266-
processExec = r
266+
// patch ProcessExec
267+
common.ProcessExec = r
267268

268269
// function call
269270
err := ensureNFSPool(clusterID)
@@ -284,8 +285,8 @@ func (s *NFSSuite) TestEnsureNFSPoolFailEnable() {
284285
r.On("RunCommand", []interface{}{
285286
"ceph", "osd", "pool", "application", "enable", ".nfs", "nfs"}...).Return("", expectedErr).Once()
286287

287-
// patch processExec
288-
processExec = r
288+
// patch ProcessExec
289+
common.ProcessExec = r
289290

290291
// function call
291292
err := ensureNFSPool(clusterID)
@@ -309,8 +310,8 @@ func (s *NFSSuite) TestEnsureNFSPoolFailCreateObj() {
309310
r.On("RunCommand", []interface{}{
310311
"rados", "create", "--pool", ".nfs", "-N", clusterID, obj}...).Return("", expectedErr).Once()
311312

312-
// patch processExec
313-
processExec = r
313+
// patch ProcessExec
314+
common.ProcessExec = r
314315

315316
// function call
316317
err := ensureNFSPool(clusterID)
@@ -334,8 +335,8 @@ func (s *NFSSuite) TestEnsureNFSPool() {
334335
r.On("RunCommand", []interface{}{
335336
"rados", "create", "--pool", ".nfs", "-N", clusterID, obj}...).Return("", existsErr).Once()
336337

337-
// patch processExec
338-
processExec = r
338+
// patch ProcessExec
339+
common.ProcessExec = r
339340

340341
// function call
341342
err := ensureNFSPool(clusterID)
@@ -359,8 +360,8 @@ func (s *NFSSuite) TestAddNodeToSharedGraceMgmtDb() {
359360
r.On("RunCommand", []interface{}{
360361
"ganesha-rados-grace", "--cephconf", cephconf, "--pool", ".nfs", "--ns", clusterID, "--userid", userID, "add", node2}...).Return("", expectedErr).Once()
361362

362-
// patch processExec
363-
processExec = r
363+
// patch ProcessExec
364+
common.ProcessExec = r
364365

365366
// function call
366367
err := addNodeToSharedGraceMgmtDb(cephconf, clusterID, userID, node)
@@ -389,8 +390,8 @@ func (s *NFSSuite) TestRemoveNodeFromSharedGraceMgmtDb() {
389390
r.On("RunCommand", []interface{}{
390391
"ganesha-rados-grace", "--cephconf", cephconf, "--pool", ".nfs", "--ns", clusterID, "--userid", userID, "remove", node2}...).Return("", expectedErr).Once()
391392

392-
// patch processExec
393-
processExec = r
393+
// patch ProcessExec
394+
common.ProcessExec = r
394395

395396
// function call
396397
err := removeNodeFromSharedGraceMgmtDb(cephconf, clusterID, userID, node)

0 commit comments

Comments
 (0)