File: manager_test.go

package info (click to toggle)
golang-github-juju-utils 0.0~git20171220.f38c0b0-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,748 kB
  • sloc: makefile: 20
file content (328 lines) | stat: -rw-r--r-- 9,451 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// Copyright 2015 Canonical Ltd.
// Copyright 2015 Cloudbase Solutions SRL
// Licensed under the LGPLv3, see LICENCE file for details.

package manager_test

import (
	"os"
	"os/exec"
	"strings"

	"github.com/juju/testing"
	jc "github.com/juju/testing/checkers"
	"github.com/juju/utils/proxy"
	gc "gopkg.in/check.v1"

	"github.com/juju/utils/packaging/commands"
	"github.com/juju/utils/packaging/manager"
)

var _ = gc.Suite(&ManagerSuite{})

type ManagerSuite struct {
	apt, yum, zypper manager.PackageManager
	testing.IsolationSuite
	calledCommand string
}

func (s *ManagerSuite) SetUpSuite(c *gc.C) {
	s.IsolationSuite.SetUpSuite(c)
	s.apt = manager.NewAptPackageManager()
	s.yum = manager.NewYumPackageManager()
	s.zypper = manager.NewZypperPackageManager()
}

func (s *ManagerSuite) SetUpTest(c *gc.C) {
	s.IsolationSuite.SetUpTest(c)
}

func (s *ManagerSuite) TearDownTest(c *gc.C) {
	s.IsolationSuite.TearDownTest(c)
}

func (s *ManagerSuite) TearDownSuite(c *gc.C) {
	s.IsolationSuite.TearDownSuite(c)
}

var (
	// aptCmder is the commands.PackageCommander for apt-based
	// systems whose commands will be checked against.
	aptCmder = commands.NewAptPackageCommander()

	// yumCmder is the commands.PackageCommander for yum-based
	// systems whose commands will be checked against.
	yumCmder = commands.NewYumPackageCommander()

	// zypperCmder is the commands.PackageCommander for zypper-based
	// systems whose commands will be checked against.
	zypperCmder = commands.NewZypperPackageCommander()

	// testedPackageName is the package name used in all
	// single-package testing scenarios.
	testedPackageName = "test-package"

	// testedRepoName is the repository name used in all
	// repository-related tests.
	testedRepoName = "some-repo"

	// testedProxySettings is the set of proxy settings used in
	// all proxy-related tests.
	testedProxySettings = proxy.Settings{
		Http:  "http://some-proxy.domain",
		Https: "https://some-proxy.domain",
		Ftp:   "ftp://some-proxy.domain",
	}

	// testedPackageNames is a list of package names used in all
	// multiple-package testing scenarions.
	testedPackageNames = []string{
		"first-test-package",
		"second-test-package",
		"third-test-package",
	}
)

// getMockRunCommandWithRetry returns a function with the same signature as
// RunCommandWithRetry which saves the command it recieves in the provided
// string whilst always returning no output, 0 error code and nil error.
func getMockRunCommandWithRetry(stor *string) func(string, func(string) error) (string, int, error) {
	return func(cmd string, fatalErr func(string) error) (string, int, error) {
		*stor = cmd
		return "", 0, nil
	}
}

// getMockRunCommand returns a function with the same signature as RunCommand
// which saves the command it revieves in the provided string whilst always
// returning empty output and no error.
func getMockRunCommand(stor *string) func(string, ...string) (string, error) {
	return func(cmd string, args ...string) (string, error) {
		*stor = strings.Join(append([]string{cmd}, args...), " ")
		return "", nil
	}
}

// simpleTestCase is a struct containing all the information required for
// running a simple error/no error test case.
type simpleTestCase struct {
	// description of the test:
	desc string

	// the expected apt command which will get executed:
	expectedAptCmd string

	// the expected result of the given apt operation:
	expectedAptResult interface{}

	// the expected yum command which will get executed:
	expectedYumCmd string

	// the expected result of the given yum operation:
	expectedYumResult interface{}

	// the expected yum command which will get executed:
	expectedZypperCmd string

	// the expected result of the given yum operation:
	expectedZypperResult interface{}

	// the function to be applied on the package manager.
	// returns the result of the operation and the error.
	operation func(manager.PackageManager) (interface{}, error)
}

var simpleTestCases = []*simpleTestCase{
	&simpleTestCase{
		"Test install prerequisites.",
		aptCmder.InstallPrerequisiteCmd(),
		nil,
		yumCmder.InstallPrerequisiteCmd(),
		nil,
		zypperCmder.InstallPrerequisiteCmd(),
		nil,
		func(pacman manager.PackageManager) (interface{}, error) {
			return nil, pacman.InstallPrerequisite()
		},
	},
	&simpleTestCase{
		"Test system update.",
		aptCmder.UpdateCmd(),
		nil,
		yumCmder.UpdateCmd(),
		nil,
		zypperCmder.UpdateCmd(),
		nil,
		func(pacman manager.PackageManager) (interface{}, error) {
			return nil, pacman.Update()
		},
	},
	&simpleTestCase{
		"Test system upgrade.",
		aptCmder.UpgradeCmd(),
		nil,
		yumCmder.UpgradeCmd(),
		nil,
		zypperCmder.UpgradeCmd(),
		nil,
		func(pacman manager.PackageManager) (interface{}, error) {
			return nil, pacman.Upgrade()
		},
	},
	&simpleTestCase{
		"Test install packages.",
		aptCmder.InstallCmd(testedPackageNames...),
		nil,
		yumCmder.InstallCmd(testedPackageNames...),
		nil,
		zypperCmder.InstallCmd(testedPackageNames...),
		nil,
		func(pacman manager.PackageManager) (interface{}, error) {
			return nil, pacman.Install(testedPackageNames...)
		},
	},
	&simpleTestCase{
		"Test remove packages.",
		aptCmder.RemoveCmd(testedPackageNames...),
		nil,
		yumCmder.RemoveCmd(testedPackageNames...),
		nil,
		zypperCmder.RemoveCmd(testedPackageNames...),
		nil,
		func(pacman manager.PackageManager) (interface{}, error) {
			return nil, pacman.Remove(testedPackageNames...)
		},
	},
	&simpleTestCase{
		"Test purge packages.",
		aptCmder.PurgeCmd(testedPackageNames...),
		nil,
		yumCmder.PurgeCmd(testedPackageNames...),
		nil,
		zypperCmder.PurgeCmd(testedPackageNames...),
		nil,
		func(pacman manager.PackageManager) (interface{}, error) {
			return nil, pacman.Purge(testedPackageNames...)
		},
	},
	&simpleTestCase{
		"Test repository addition.",
		aptCmder.AddRepositoryCmd(testedRepoName),
		nil,
		yumCmder.AddRepositoryCmd(testedRepoName),
		nil,
		zypperCmder.AddRepositoryCmd(testedRepoName),
		nil,
		func(pacman manager.PackageManager) (interface{}, error) {
			return nil, pacman.AddRepository(testedRepoName)
		},
	},
	&simpleTestCase{
		"Test repository removal.",
		aptCmder.RemoveRepositoryCmd(testedRepoName),
		nil,
		yumCmder.RemoveRepositoryCmd(testedRepoName),
		nil,
		zypperCmder.RemoveRepositoryCmd(testedRepoName),
		nil,
		func(pacman manager.PackageManager) (interface{}, error) {
			return nil, pacman.RemoveRepository(testedRepoName)
		},
	},
	&simpleTestCase{
		"Test running cleanup.",
		aptCmder.CleanupCmd(),
		nil,
		yumCmder.CleanupCmd(),
		nil,
		zypperCmder.CleanupCmd(),
		nil,
		func(pacman manager.PackageManager) (interface{}, error) {
			return nil, pacman.Cleanup()
		},
	},
}

// searchingTestCases are a couple of simple test cases which search for a
// given package; either localy or remotely, and need to be tested seperately
// on the case of their return value being a boolean.
var searchingTestCases = []*simpleTestCase{
	&simpleTestCase{
		"Test package search.",
		aptCmder.SearchCmd(testedPackageName),
		false,
		yumCmder.SearchCmd(testedPackageName),
		true,
		zypperCmder.SearchCmd(testedPackageName),
		false,
		func(pacman manager.PackageManager) (interface{}, error) {
			return pacman.Search(testedPackageName)
		},
	},
	&simpleTestCase{
		"Test local package search.",
		aptCmder.IsInstalledCmd(testedPackageName),
		true,
		yumCmder.IsInstalledCmd(testedPackageName),
		true,
		zypperCmder.IsInstalledCmd(testedPackageName),
		true,
		func(pacman manager.PackageManager) (interface{}, error) {
			return pacman.IsInstalled(testedPackageName), nil
		},
	},
}

func (s *ManagerSuite) TestSimpleCases(c *gc.C) {
	s.PatchValue(&manager.RunCommand, getMockRunCommand(&s.calledCommand))
	s.PatchValue(&manager.RunCommandWithRetry, getMockRunCommandWithRetry(&s.calledCommand))

	testCases := append(simpleTestCases, searchingTestCases...)
	for i, testCase := range testCases {
		c.Logf("Simple test %d: %s", i+1, testCase.desc)

		// run for the apt PackageManager implementation:
		res, err := testCase.operation(s.apt)
		c.Assert(err, jc.ErrorIsNil)
		c.Assert(s.calledCommand, gc.Equals, testCase.expectedAptCmd)
		c.Assert(res, jc.DeepEquals, testCase.expectedAptResult)

		// run for the yum PackageManager implementation.
		res, err = testCase.operation(s.yum)
		c.Assert(err, jc.ErrorIsNil)
		c.Assert(s.calledCommand, gc.Equals, testCase.expectedYumCmd)
		c.Assert(res, jc.DeepEquals, testCase.expectedYumResult)
	}
}

func (s *ManagerSuite) TestSimpleErrorCases(c *gc.C) {
	const (
		expectedErr    = "packaging command failed: exit status 0"
		expectedErrMsg = `E: I done failed :(`
	)
	state := os.ProcessState{}
	cmdError := &exec.ExitError{ProcessState: &state}

	cmdChan := s.HookCommandOutput(&manager.CommandOutput, []byte(expectedErrMsg), error(cmdError))

	for i, testCase := range simpleTestCases {
		c.Logf("Error'd test %d: %s", i+1, testCase.desc)
		s.PatchValue(&manager.ProcessStateSys, func(*os.ProcessState) interface{} {
			return mockExitStatuser(i + 1)
		})

		// run for the apt PackageManager implementation:
		_, err := testCase.operation(s.apt)
		c.Assert(err, gc.ErrorMatches, expectedErr)

		cmd := <-cmdChan
		c.Assert(strings.Join(cmd.Args, " "), gc.DeepEquals, testCase.expectedAptCmd)

		// run for the yum PackageManager implementation:
		_, err = testCase.operation(s.yum)
		c.Assert(err, gc.ErrorMatches, expectedErr)

		cmd = <-cmdChan
		c.Assert(strings.Join(cmd.Args, " "), gc.DeepEquals, testCase.expectedYumCmd)
	}
}