generated from hashicorp/packer-plugin-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathstep_create_ssh_key_test.go
98 lines (87 loc) · 2.69 KB
/
step_create_ssh_key_test.go
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
package exoscale
import (
"context"
"os"
"github.com/hashicorp/packer-plugin-sdk/common"
"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/stretchr/testify/mock"
egoscale "github.com/exoscale/egoscale/v2"
)
func (ts *testSuite) TestStepCreateSSHKey_Run() {
var (
testConfig = Config{
InstanceZone: testInstanceZone,
PackerConfig: common.PackerConfig{PackerDebug: true},
}
sshKeyRegistered bool
)
ts.exo.(*exoscaleClientMock).
On(
"RegisterSSHKey",
mock.Anything, // ctx
testInstanceZone, // zone
mock.Anything, // name
mock.Anything, // publicKey
).
Run(func(args mock.Arguments) {
ts.Require().Equal(testConfig.InstanceSSHKey, args.Get(2))
ts.Require().NotEmpty(args.Get(3))
sshKeyRegistered = true
}).
Return(&egoscale.SSHKey{}, nil)
testBuilder := Builder{
buildID: ts.randomID(),
config: &testConfig,
exo: ts.exo,
}
stepAction := (&stepCreateSSHKey{builder: &testBuilder}).
Run(context.Background(), ts.state)
ts.Require().True(sshKeyRegistered)
ts.Require().Empty(ts.ui.(*packer.MockUi).ErrorMessage)
ts.Require().Equal(stepAction, multistep.ActionContinue)
ts.Require().Equal("packer-"+testBuilder.buildID, testConfig.InstanceSSHKey)
ts.Require().NotEmpty(testBuilder.config.Comm.SSHPrivateKey)
ts.Require().True(ts.state.Get("delete_ssh_key").(bool))
ts.Require().NotEmpty(ts.state.Get("delete_ssh_private_key"))
ts.Require().NoError(os.Remove(ts.state.Get("delete_ssh_private_key").(string)))
}
func (ts *testSuite) TestStepCreateSSHKey_Cleanup() {
var (
testConfig = Config{
InstanceSSHKey: "packer-" + ts.randomID(),
InstanceZone: testInstanceZone,
PackerConfig: common.PackerConfig{PackerDebug: true},
}
sshKeyDeleted bool
)
ts.exo.(*exoscaleClientMock).
On(
"DeleteSSHKey",
mock.Anything, // ctx
testInstanceZone, // zone
mock.Anything, // sshKey
).
Run(func(args mock.Arguments) {
ts.Require().Equal(&egoscale.SSHKey{Name: &testConfig.InstanceSSHKey}, args.Get(2))
sshKeyDeleted = true
}).
Return(nil)
tmpFile, err := os.CreateTemp(os.TempDir(), "packer-plugin-exoscale-*")
ts.Require().NoError(err, "unable to create temporary file")
ts.Require().NoError(tmpFile.Close())
ts.Require().FileExists(tmpFile.Name())
ts.state.Put("delete_ssh_private_key", tmpFile.Name())
ts.state.Put("delete_ssh_key", true)
(&stepCreateSSHKey{
builder: &Builder{
config: &testConfig,
exo: ts.exo,
},
}).
Cleanup(ts.state)
ts.Require().True(sshKeyDeleted)
ts.Require().Empty(ts.ui.(*packer.MockUi).ErrorMessage)
ts.Require().NotEmpty(ts.state.Get("delete_ssh_private_key"))
ts.Require().NoFileExists(tmpFile.Name())
}