|
1 | 1 | //Package provisiontest provides utilities for testing provisioners |
2 | 2 | package provisiontest |
3 | 3 |
|
4 | | -import ( |
5 | | - "errors" |
6 | | - "strings" |
7 | | -) |
| 4 | +import "errors" |
| 5 | + |
| 6 | +//FakeSSHCommanderOptions is intended to create a FakeSSHCommander without actually knowing the underlying sshcommands by passing it to NewSSHCommander |
| 7 | +type FakeSSHCommanderOptions struct { |
| 8 | + //Result of the ssh command to look up the FilesystemType |
| 9 | + FilesystemType string |
| 10 | +} |
8 | 11 |
|
9 | 12 | //FakeSSHCommander is an implementation of provision.SSHCommander to provide predictable responses set by testing code |
10 | 13 | //Extend it when needed |
11 | 14 | type FakeSSHCommander struct { |
12 | | - //Result of the ssh command to look up the FilesystemType |
13 | | - FilesystemType string |
| 15 | + Responses map[string]string |
| 16 | +} |
| 17 | + |
| 18 | +//NewFakeSSHCommander creates a FakeSSHCommander without actually knowing the underlying sshcommands |
| 19 | +func NewFakeSSHCommander(options FakeSSHCommanderOptions) *FakeSSHCommander { |
| 20 | + if options.FilesystemType == "" { |
| 21 | + options.FilesystemType = "ext4" |
| 22 | + } |
| 23 | + sshCmder := &FakeSSHCommander{ |
| 24 | + Responses: map[string]string{ |
| 25 | + "stat -f -c %T /var/lib": options.FilesystemType + "\n", |
| 26 | + }, |
| 27 | + } |
| 28 | + |
| 29 | + return sshCmder |
14 | 30 | } |
15 | 31 |
|
16 | 32 | //SSHCommand is an implementation of provision.SSHCommander.SSHCommand to provide predictable responses set by testing code |
17 | | -func (sshCmder FakeSSHCommander) SSHCommand(args string) (string, error) { |
18 | | - if !strings.HasPrefix(args, "stat -f") { |
19 | | - return "", errors.New("Not implemented by FakeSSHCommander") |
| 33 | +func (sshCmder *FakeSSHCommander) SSHCommand(args string) (string, error) { |
| 34 | + response, commandRegistered := sshCmder.Responses[args] |
| 35 | + if !commandRegistered { |
| 36 | + return "", errors.New("Command not registered in FakeSSHCommander") |
20 | 37 | } |
21 | | - return sshCmder.FilesystemType + "\n", nil |
| 38 | + return response, nil |
22 | 39 | } |
0 commit comments