Skip to content

Commit

Permalink
test: getTemplateFuncMap unit tests part 1 (#1334)
Browse files Browse the repository at this point in the history
* test: getTemplateFuncMap unit tests part 1

* chore: remove errata

* chore: lint
  • Loading branch information
jackfrancis authored and acs-bot committed May 20, 2019
1 parent 5bb413b commit f51c798
Show file tree
Hide file tree
Showing 9 changed files with 576 additions and 147 deletions.
39 changes: 39 additions & 0 deletions pkg/api/common/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
package common

import (
"bytes"
"fmt"
"regexp"
"sort"
"strings"

"github.com/pkg/errors"
Expand Down Expand Up @@ -226,3 +229,39 @@ func IsSgxEnabledSKU(vmSize string) bool {
}
return false
}

// GetMasterKubernetesLabels returns a k8s API-compliant labels string
func GetMasterKubernetesLabels(rg string) string {
var buf bytes.Buffer
buf.WriteString("kubernetes.io/role=master")
buf.WriteString(",node-role.kubernetes.io/master=")
buf.WriteString(fmt.Sprintf(",kubernetes.azure.com/cluster=%s", rg))
return buf.String()
}

// GetStorageAccountType returns the support managed disk storage tier for a give VM size
func GetStorageAccountType(sizeName string) (string, error) {
spl := strings.Split(sizeName, "_")
if len(spl) < 2 {
return "", errors.Errorf("Invalid sizeName: %s", sizeName)
}
capability := spl[1]
if strings.Contains(strings.ToLower(capability), "s") {
return "Premium_LRS", nil
}
return "Standard_LRS", nil
}

// GetOrderedEscapedKeyValsString returns an ordered string of escaped, quoted key=val
func GetOrderedEscapedKeyValsString(config map[string]string) string {
keys := []string{}
for key := range config {
keys = append(keys, key)
}
sort.Strings(keys)
var buf bytes.Buffer
for _, key := range keys {
buf.WriteString(fmt.Sprintf("\\\"%s=%s\\\", ", key, config[key]))
}
return strings.TrimSuffix(buf.String(), ", ")
}
83 changes: 83 additions & 0 deletions pkg/api/common/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,86 @@ func TestIsSGXEnabledSKU(t *testing.T) {
}
}
}

func TestGetMasterKubernetesLabels(t *testing.T) {
cases := []struct {
rg string
expected string
}{
{
"my-resource-group",
"kubernetes.io/role=master,node-role.kubernetes.io/master=,kubernetes.azure.com/cluster=my-resource-group",
},
{
"",
"kubernetes.io/role=master,node-role.kubernetes.io/master=,kubernetes.azure.com/cluster=",
},
}

for _, c := range cases {
ret := GetMasterKubernetesLabels(c.rg)
if ret != c.expected {
t.Fatalf("expected GetMasterKubernetesLabels(%s) to return %s, but instead got %s", c.rg, c.expected, ret)
}
}
}

func TestGetK8sRuntimeConfigKeyVals(t *testing.T) {
cases := []struct {
input map[string]string
expected string
}{
{
input: map[string]string{},
expected: "",
},
{
input: map[string]string{
"foo": "bar",
"yes": "please",
},
expected: `\"foo=bar\", \"yes=please\"`,
},
}

for _, c := range cases {
ret := GetOrderedEscapedKeyValsString(c.input)
if ret != c.expected {
t.Fatalf("expected GetK8sRuntimeConfigKeyVals(%s) to return %s, but instead got %s", c.input, c.expected, ret)
}
}
}

func TestGetStorageAccountType(t *testing.T) {
validPremiumVMSize := "Standard_DS2_v2"
validStandardVMSize := "Standard_D2_v2"
expectedPremiumTier := "Premium_LRS"
expectedStandardTier := "Standard_LRS"
invalidVMSize := "D2v2"

// test premium VMSize returns premium managed disk tier
premiumTier, err := GetStorageAccountType(validPremiumVMSize)
if err != nil {
t.Fatalf("Invalid sizeName: %s", err)
}

if premiumTier != expectedPremiumTier {
t.Fatalf("premium VM did no match premium managed storage tier")
}

// test standard VMSize returns standard managed disk tier
standardTier, err := GetStorageAccountType(validStandardVMSize)
if err != nil {
t.Fatalf("Invalid sizeName: %s", err)
}

if standardTier != expectedStandardTier {
t.Fatalf("standard VM did no match standard managed storage tier")
}

// test invalid VMSize
result, err := GetStorageAccountType(invalidVMSize)
if err == nil {
t.Errorf("GetStorageAccountType() = (%s, nil), want error", result)
}
}
57 changes: 57 additions & 0 deletions pkg/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
package api

import (
"bytes"
"encoding/json"
"fmt"
"hash/fnv"
"math/rand"
"net"
neturl "net/url"
"sort"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -1369,6 +1371,26 @@ func (a *AgentPoolProfile) IsUbuntuNonVHD() bool {
return a.IsUbuntu() && !a.IsVHDDistro()
}

// GetKubernetesLabels returns a k8s API-compliant labels string for nodes in this profile
func (a *AgentPoolProfile) GetKubernetesLabels(rg string) string {
var buf bytes.Buffer
buf.WriteString("node-role.kubernetes.io/agent=")
buf.WriteString(fmt.Sprintf(",kubernetes.io/role=agent,agentpool=%s", a.Name))
if a.StorageProfile == ManagedDisks {
storagetier, _ := common.GetStorageAccountType(a.VMSize)
buf.WriteString(fmt.Sprintf(",storageprofile=managed,storagetier=%s", storagetier))
}
if common.IsNvidiaEnabledSKU(a.VMSize) {
accelerator := "nvidia"
buf.WriteString(fmt.Sprintf(",accelerator=%s", accelerator))
}
buf.WriteString(fmt.Sprintf(",kubernetes.azure.com/cluster=%s", rg))
for k, v := range a.CustomNodeLabels {
buf.WriteString(fmt.Sprintf(",%s=%s", k, v))
}
return buf.String()
}

// HasSecrets returns true if the customer specified secrets to install
func (w *WindowsProfile) HasSecrets() bool {
return len(w.Secrets) > 0
Expand Down Expand Up @@ -1443,6 +1465,13 @@ func (o *OrchestratorProfile) IsDCOS() bool {
return o.OrchestratorType == DCOS
}

// IsDCOS19 returns true if this is a DCOS 1.9 orchestrator using the latest version
func (o *OrchestratorProfile) IsDCOS19() bool {
return o.OrchestratorType == DCOS &&
(o.OrchestratorVersion == common.DCOSVersion1Dot9Dot0 ||
o.OrchestratorVersion == common.DCOSVersion1Dot9Dot8)
}

// IsAzureCNI returns true if Azure CNI network plugin is enabled
func (o *OrchestratorProfile) IsAzureCNI() bool {
if o.KubernetesConfig != nil {
Expand Down Expand Up @@ -1614,6 +1643,34 @@ func (k *KubernetesConfig) GetUserAssignedClientID() string {
return ""
}

// GetOrderedKubeletConfigString returns an ordered string of key/val pairs
func (k *KubernetesConfig) GetOrderedKubeletConfigString() string {
keys := []string{}
for key := range k.KubeletConfig {
keys = append(keys, key)
}
sort.Strings(keys)
var buf bytes.Buffer
for _, key := range keys {
buf.WriteString(fmt.Sprintf("%s=%s ", key, k.KubeletConfig[key]))
}
return buf.String()
}

// GetOrderedKubeletConfigStringForPowershell returns an ordered string of key/val pairs for Powershell script consumption
func (k *KubernetesConfig) GetOrderedKubeletConfigStringForPowershell() string {
keys := []string{}
for key := range k.KubeletConfig {
keys = append(keys, key)
}
sort.Strings(keys)
var buf bytes.Buffer
for _, key := range keys {
buf.WriteString(fmt.Sprintf("\"%s=%s\", ", key, k.KubeletConfig[key]))
}
return strings.TrimSuffix(buf.String(), ", ")
}

// IsNSeriesSKU returns true if the agent pool contains an N-series (NVIDIA GPU) VM
func (a *AgentPoolProfile) IsNSeriesSKU() bool {
return common.IsNvidiaEnabledSKU(a.VMSize)
Expand Down
Loading

0 comments on commit f51c798

Please sign in to comment.