Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

More unit tests #3300

Merged
merged 4 commits into from
Jun 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 12 additions & 70 deletions pkg/acsengine/ssh_test.go
Original file line number Diff line number Diff line change
@@ -1,87 +1,29 @@
package acsengine

import (
"math/rand"
"os"
"testing"

"github.com/Azure/acs-engine/pkg/helpers"

"github.com/Azure/acs-engine/pkg/i18n"
)

func TestCreateSSH(t *testing.T) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved this down to the right test file

rg := rand.New(rand.NewSource(42))

expectedPublicKeyString := "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCyx5MHXjJvJAx5DJ9FZNIDa/QTWorSF+Ra21Tz49DQWfdSESnCGFFVBh/MQUFGv5kCenbmqEjsWF177kFOdv1vOTz4sKRlHg7u3I9uCyyZQrWx4X4RdNk7eX+isQVjFXYw2W1rRDUrnK/82qVTv1f0gu1DV4Z7GoIa2jfJ0zBUY3IW0VN9jYaPVuwv4t5y2GwSZF+HBRuOfLfiUgt4+qVFOz4KwRaEBsVfWxlidlT3K3/+ztWpFOmaKIOjQreEWV10ZSo3f9g6j/HdMPtwYvRCtYStbFCRmcbPr9nuR84SAX/4f95KvBAKLnXwb5Bt71D2vAlZSW1Ylv2VbcaZ73+43EpyphYCSg3kOCdwsqE/EU+Swued82SguLALD3mNKbxHGJppFjz3GMyPpJuSH5EE1OANyPxABCwCYycKiNWbOPi3l6o4tMrASYRXi8l3l9JCvioUJ3bXXH6cDpcP4P6QgsuxhwVkUiECU+dbjJXK4gAUVuWKkMOdY7ITh82oU3wOWXbk8K3bdIUp2ylcHeAd2pekGMuaEKGbrXGRiBitCEjl67Bj5opQflgSmI63g8Sa3mKOP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hYdbJ675GFoGoq+X21QaqsdvoXmmuJF9qq9Tq+JaWloUNq
2FWXLhSX02saIdbIheS1fv/LqekXZd8eFXUj7VZ15tPG3SJqORS0pMtxSAJvLw==
-----END RSA PRIVATE KEY-----
`

func TestCreateSaveSSH(t *testing.T) {
translator := &i18n.Translator{
Locale: nil,
}
username := "test_user"
outputDirectory := "unit_tests"
expectedFile := outputDirectory + "/" + username + "_rsa"

privateKey, publicKey, err := helpers.CreateSSH(rg, translator)
if err != nil {
t.Fatalf("failed to generate SSH: %s", err)
}
privateKeyString := string(privateKeyToPem(privateKey))
defer os.Remove(expectedFile)

if privateKeyString != expectedPrivateKeyString {
t.Fatalf("Private Key did not match expected format/value")
_, _, err := CreateSaveSSH(username, outputDirectory, translator)

if err != nil {
t.Fatalf("Unexpected error creating and saving ssh key: %s", err)
}

if publicKey != expectedPublicKeyString {
t.Fatalf("Public Key did not match expected format/value")
if _, err := os.Stat(expectedFile); os.IsNotExist(err) {
t.Fatalf("ssh file was not created")
}
}
138 changes: 131 additions & 7 deletions pkg/helpers/helpers_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,36 @@
package helpers

import "testing"
import (
"bytes"
"crypto/x509"
"encoding/pem"
"math/rand"
"testing"

func TestPointerToBool(t *testing.T) {
boolVar := true
ret := PointerToBool(boolVar)
if *ret != boolVar {
t.Fatalf("expected PointerToBool(true) to return *true, instead returned %#v", ret)
"github.com/Azure/acs-engine/pkg/i18n"
)

type ContainerService struct {
ID string `json:"id"`
Location string `json:"location"`
Name string `json:"name"`
}

func TestJSONMarshal(t *testing.T) {
input := &ContainerService{}
result, _ := JSONMarshal(input, false)
expected := "{\"id\":\"\",\"location\":\"\",\"name\":\"\"}\n"
if string(result) != expected {
t.Fatalf("JSONMarshal returned unexpected result: expected %s but got %s", expected, string(result))
}
result, _ = JSONMarshalIndent(input, "", "", false)
expected = "{\n\"id\": \"\",\n\"location\": \"\",\n\"name\": \"\"\n}\n"
if string(result) != expected {
t.Fatalf("JSONMarshal returned unexpected result: expected \n%sbut got \n%s", expected, result)
}
}

func TestIsRegionNormalized(t *testing.T) {
func TestNormalizaAzureRegion(t *testing.T) {
cases := []struct {
input string
expectedResult string
Expand Down Expand Up @@ -40,3 +60,107 @@ func TestIsRegionNormalized(t *testing.T) {
}
}
}

func TestPointerToBool(t *testing.T) {
boolVar := true
ret := PointerToBool(boolVar)
if *ret != boolVar {
t.Fatalf("expected PointerToBool(true) to return *true, instead returned %#v", ret)
}

if IsTrueBoolPointer(ret) != boolVar {
t.Fatalf("expected IsTrueBoolPointer(*true) to return true, instead returned %#v", IsTrueBoolPointer(ret))
}

boolVar = false
ret = PointerToBool(boolVar)
if *ret != boolVar {
t.Fatalf("expected PointerToBool(false) to return *false, instead returned %#v", ret)
}

if IsTrueBoolPointer(ret) != boolVar {
t.Fatalf("expected IsTrueBoolPointer(*false) to return false, instead returned %#v", IsTrueBoolPointer(ret))
}
}

func TestCreateSSH(t *testing.T) {
rg := rand.New(rand.NewSource(42))

expectedPublicKeyString := "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCyx5MHXjJvJAx5DJ9FZNIDa/QTWorSF+Ra21Tz49DQWfdSESnCGFFVBh/MQUFGv5kCenbmqEjsWF177kFOdv1vOTz4sKRlHg7u3I9uCyyZQrWx4X4RdNk7eX+isQVjFXYw2W1rRDUrnK/82qVTv1f0gu1DV4Z7GoIa2jfJ0zBUY3IW0VN9jYaPVuwv4t5y2GwSZF+HBRuOfLfiUgt4+qVFOz4KwRaEBsVfWxlidlT3K3/+ztWpFOmaKIOjQreEWV10ZSo3f9g6j/HdMPtwYvRCtYStbFCRmcbPr9nuR84SAX/4f95KvBAKLnXwb5Bt71D2vAlZSW1Ylv2VbcaZ73+43EpyphYCSg3kOCdwsqE/EU+Swued82SguLALD3mNKbxHGJppFjz3GMyPpJuSH5EE1OANyPxABCwCYycKiNWbOPi3l6o4tMrASYRXi8l3l9JCvioUJ3bXXH6cDpcP4P6Qgsuxhw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S/4ouzzfl8qbF7FjQ4
Qq/HPWSIC9Z8rKtUDDHeZYaLqvdhqbas/drqCXmeLeYM6Om4lQJdP+zip3Ctulp1
EPDesL0rH+3s1CKpgkhYdbJ675GFoGoq+X21QaqsdvoXmmuJF9qq9Tq+JaWloUNq
2FWXLhSX02saIdbIheS1fv/LqekXZd8eFXUj7VZ15tPG3SJqORS0pMtxSAJvLw==
-----END RSA PRIVATE KEY-----
`

translator := &i18n.Translator{
Locale: nil,
}

privateKey, publicKey, err := CreateSSH(rg, translator)
if err != nil {
t.Fatalf("failed to generate SSH: %s", err)
}
pemBlock := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
}
pemBuffer := bytes.Buffer{}
pem.Encode(&pemBuffer, pemBlock)

if string(pemBuffer.Bytes()) != expectedPrivateKeyString {
t.Fatalf("Private Key did not match expected format/value")
}

if publicKey != expectedPublicKeyString {
t.Fatalf("Public Key did not match expected format/value")
}
}