add tests
This commit is contained in:
parent
6d6b94d515
commit
9643ad35f1
|
@ -238,7 +238,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
|
||||||
NewStepDeployTemplate(azureClient, ui, &b.config, keyVaultDeploymentName, GetKeyVaultDeployment),
|
NewStepDeployTemplate(azureClient, ui, &b.config, keyVaultDeploymentName, GetKeyVaultDeployment),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
steps = append(steps, NewStepCertificateInKeyVault(azureClient, ui, &b.config))
|
steps = append(steps, NewStepCertificateInKeyVault(&azureClient.VaultClient, ui, &b.config))
|
||||||
}
|
}
|
||||||
steps = append(steps,
|
steps = append(steps,
|
||||||
NewStepGetCertificate(azureClient, ui),
|
NewStepGetCertificate(azureClient, ui),
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/hashicorp/packer/builder/azure/common"
|
||||||
"github.com/hashicorp/packer/builder/azure/common/constants"
|
"github.com/hashicorp/packer/builder/azure/common/constants"
|
||||||
"github.com/hashicorp/packer/helper/multistep"
|
"github.com/hashicorp/packer/helper/multistep"
|
||||||
"github.com/hashicorp/packer/packer"
|
"github.com/hashicorp/packer/packer"
|
||||||
|
@ -11,12 +12,12 @@ import (
|
||||||
|
|
||||||
type StepCertificateInKeyVault struct {
|
type StepCertificateInKeyVault struct {
|
||||||
config *Config
|
config *Config
|
||||||
client *AzureClient
|
client common.AZVaultClientIface
|
||||||
say func(message string)
|
say func(message string)
|
||||||
error func(e error)
|
error func(e error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStepCertificateInKeyVault(cli *AzureClient, ui packer.Ui, config *Config) *StepCertificateInKeyVault {
|
func NewStepCertificateInKeyVault(cli common.AZVaultClientIface, ui packer.Ui, config *Config) *StepCertificateInKeyVault {
|
||||||
var step = &StepCertificateInKeyVault{
|
var step = &StepCertificateInKeyVault{
|
||||||
client: cli,
|
client: cli,
|
||||||
config: config,
|
config: config,
|
||||||
|
@ -29,13 +30,7 @@ func NewStepCertificateInKeyVault(cli *AzureClient, ui packer.Ui, config *Config
|
||||||
|
|
||||||
func (s *StepCertificateInKeyVault) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
func (s *StepCertificateInKeyVault) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
||||||
s.say("Setting the certificate in the KeyVault...")
|
s.say("Setting the certificate in the KeyVault...")
|
||||||
|
|
||||||
var keyVaultName = state.Get(constants.ArmKeyVaultName).(string)
|
var keyVaultName = state.Get(constants.ArmKeyVaultName).(string)
|
||||||
// err := s.client.CreateKey(keyVaultName, DefaultSecretName)
|
|
||||||
// if err != nil {
|
|
||||||
// s.error(fmt.Errorf("Error setting winrm cert in custom keyvault: %s", err))
|
|
||||||
// return multistep.ActionHalt
|
|
||||||
// }
|
|
||||||
|
|
||||||
err := s.client.SetSecret(keyVaultName, DefaultSecretName, s.config.winrmCertificate)
|
err := s.client.SetSecret(keyVaultName, DefaultSecretName, s.config.winrmCertificate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -0,0 +1,66 @@
|
||||||
|
package arm
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
azcommon "github.com/hashicorp/packer/builder/azure/common"
|
||||||
|
"github.com/hashicorp/packer/builder/azure/common/constants"
|
||||||
|
"github.com/hashicorp/packer/helper/multistep"
|
||||||
|
"github.com/hashicorp/packer/packer"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNewStepCertificateInKeyVault(t *testing.T) {
|
||||||
|
cli := azcommon.MockAZVaultClient{}
|
||||||
|
ui := &packer.BasicUi{
|
||||||
|
Reader: new(bytes.Buffer),
|
||||||
|
Writer: new(bytes.Buffer),
|
||||||
|
}
|
||||||
|
state := new(multistep.BasicStateBag)
|
||||||
|
state.Put(constants.ArmKeyVaultName, "testKeyVaultName")
|
||||||
|
|
||||||
|
config := &Config{
|
||||||
|
winrmCertificate: "testCertificateString",
|
||||||
|
}
|
||||||
|
|
||||||
|
certKVStep := NewStepCertificateInKeyVault(&cli, ui, config)
|
||||||
|
stepAction := certKVStep.Run(context.TODO(), state)
|
||||||
|
|
||||||
|
if stepAction == multistep.ActionHalt {
|
||||||
|
t.Fatalf("step should have succeeded.")
|
||||||
|
}
|
||||||
|
if !cli.SetSecretCalled {
|
||||||
|
t.Fatalf("Step should have called SetSecret on Azure client.")
|
||||||
|
}
|
||||||
|
if cli.SetSecretCert != "testCertificateString" {
|
||||||
|
t.Fatalf("Step should have read cert from winRMCertificate field on config.")
|
||||||
|
}
|
||||||
|
if cli.SetSecretVaultName != "testKeyVaultName" {
|
||||||
|
t.Fatalf("step should have read keyvault name from state.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNewStepCertificateInKeyVault_error(t *testing.T) {
|
||||||
|
// Tell mock to return an error
|
||||||
|
cli := azcommon.MockAZVaultClient{}
|
||||||
|
cli.IsError = true
|
||||||
|
|
||||||
|
ui := &packer.BasicUi{
|
||||||
|
Reader: new(bytes.Buffer),
|
||||||
|
Writer: new(bytes.Buffer),
|
||||||
|
}
|
||||||
|
state := new(multistep.BasicStateBag)
|
||||||
|
state.Put(constants.ArmKeyVaultName, "testKeyVaultName")
|
||||||
|
|
||||||
|
config := &Config{
|
||||||
|
winrmCertificate: "testCertificateString",
|
||||||
|
}
|
||||||
|
|
||||||
|
certKVStep := NewStepCertificateInKeyVault(&cli, ui, config)
|
||||||
|
stepAction := certKVStep.Run(context.TODO(), state)
|
||||||
|
|
||||||
|
if stepAction != multistep.ActionHalt {
|
||||||
|
t.Fatalf("step should have failed.")
|
||||||
|
}
|
||||||
|
}
|
|
@ -76,10 +76,6 @@ func (s *StepDeployTemplate) deleteTemplate(ctx context.Context, state multistep
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StepDeployTemplate) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
func (s *StepDeployTemplate) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
||||||
if s.config.BuildKeyVaultName != "" {
|
|
||||||
// Deployment already exists
|
|
||||||
|
|
||||||
}
|
|
||||||
s.say("Deploying deployment template ...")
|
s.say("Deploying deployment template ...")
|
||||||
|
|
||||||
var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string)
|
var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string)
|
||||||
|
|
|
@ -16,6 +16,15 @@ const (
|
||||||
AzureVaultApiVersion = "2016-10-01"
|
AzureVaultApiVersion = "2016-10-01"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Enables us to test steps that access this cli
|
||||||
|
type AZVaultClientIface interface {
|
||||||
|
GetSecret(string, string) (*Secret, error)
|
||||||
|
SetSecret(string, string, string) error
|
||||||
|
DeletePreparer(string, string) (*http.Request, error)
|
||||||
|
DeleteResponder(*http.Response) (autorest.Response, error)
|
||||||
|
DeleteSender(*http.Request) (*http.Response, error)
|
||||||
|
}
|
||||||
|
|
||||||
type VaultClient struct {
|
type VaultClient struct {
|
||||||
autorest.Client
|
autorest.Client
|
||||||
keyVaultEndpoint url.URL
|
keyVaultEndpoint url.URL
|
||||||
|
|
|
@ -0,0 +1,56 @@
|
||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/Azure/go-autorest/autorest"
|
||||||
|
)
|
||||||
|
|
||||||
|
type MockAZVaultClient struct {
|
||||||
|
GetSecretCalled bool
|
||||||
|
SetSecretCalled bool
|
||||||
|
SetSecretVaultName string
|
||||||
|
SetSecretSecretName string
|
||||||
|
SetSecretCert string
|
||||||
|
DeleteResponderCalled bool
|
||||||
|
DeletePreparerCalled bool
|
||||||
|
DeleteSenderCalled bool
|
||||||
|
|
||||||
|
IsError bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockAZVaultClient) GetSecret(vaultName, secretName string) (*Secret, error) {
|
||||||
|
m.GetSecretCalled = true
|
||||||
|
var secret Secret
|
||||||
|
return &secret, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockAZVaultClient) SetSecret(vaultName, secretName string, secretValue string) error {
|
||||||
|
m.SetSecretCalled = true
|
||||||
|
m.SetSecretVaultName = vaultName
|
||||||
|
m.SetSecretSecretName = secretName
|
||||||
|
m.SetSecretCert = secretValue
|
||||||
|
|
||||||
|
if m.IsError {
|
||||||
|
return fmt.Errorf("generic error!!")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockAZVaultClient) DeletePreparer(resourceGroupName string, vaultName string) (*http.Request, error) {
|
||||||
|
m.DeletePreparerCalled = true
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockAZVaultClient) DeleteResponder(resp *http.Response) (autorest.Response, error) {
|
||||||
|
m.DeleteResponderCalled = true
|
||||||
|
var result autorest.Response
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockAZVaultClient) DeleteSender(req *http.Request) (*http.Response, error) {
|
||||||
|
m.DeleteSenderCalled = true
|
||||||
|
return nil, nil
|
||||||
|
}
|
Loading…
Reference in New Issue