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

Unit tests #3344

Merged
merged 14 commits into from
Jul 5, 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
39 changes: 21 additions & 18 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,7 @@ func NewRootCmd() *cobra.Command {
rootCmd.AddCommand(newUpgradeCmd())
rootCmd.AddCommand(newScaleCmd())
rootCmd.AddCommand(newDcosUpgradeCmd())

var completionCmd = &cobra.Command{
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 to a function, fyi @stuartleeks @jackfrancis

Copy link
Contributor

Choose a reason for hiding this comment

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

Cool :-)

Use: "completion",
Short: "Generates bash completion scripts",
Long: `To load completion run

source <(acs-engine completion)

To configure your bash shell to load completions for each session, add this to your bashrc

# ~/.bashrc or ~/.profile
source <(acs-engine completion)
`,
Run: func(cmd *cobra.Command, args []string) {
rootCmd.GenBashCompletion(os.Stdout)
},
}
rootCmd.AddCommand(completionCmd)
rootCmd.AddCommand(getCompletionCmd(rootCmd))

return rootCmd
}
Expand Down Expand Up @@ -144,3 +127,23 @@ func (authArgs *authArgs) getClient() (*armhelpers.AzureClient, error) {
client.AddAcceptLanguages([]string{authArgs.language})
return client, nil
}

func getCompletionCmd(root *cobra.Command) *cobra.Command {
var completionCmd = &cobra.Command{
Use: "completion",
Short: "Generates bash completion scripts",
Long: `To load completion run

source <(acs-engine completion)

To configure your bash shell to load completions for each session, add this to your bashrc

# ~/.bashrc or ~/.profile
source <(acs-engine completion)
`,
Run: func(cmd *cobra.Command, args []string) {
root.GenBashCompletion(os.Stdout)
},
}
return completionCmd
}
21 changes: 21 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cmd

import (
"testing"

"github.com/spf13/cobra"
)

func TestNewRootCmd(t *testing.T) {
output := NewRootCmd()
if output.Use != rootName || output.Short != rootShortDescription || output.Long != rootLongDescription {
t.Fatalf("root command should have use %s equal %s, short %s equal %s and long %s equal to %s", output.Use, rootName, output.Short, rootShortDescription, output.Long, rootLongDescription)
}
expectedCommands := []*cobra.Command{getCompletionCmd(output), newDcosUpgradeCmd(), newDeployCmd(), newGenerateCmd(), newOrchestratorsCmd(), newScaleCmd(), newUpgradeCmd(), newVersionCmd()}
rc := output.Commands()
for i, c := range expectedCommands {
if rc[i].Use != c.Use {
t.Fatalf("root command should have command %s", c.Use)
}
}
}
109 changes: 20 additions & 89 deletions pkg/acsengine/defaults-apiserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import (

"github.com/Azure/acs-engine/pkg/api"
"github.com/Azure/acs-engine/pkg/helpers"
"github.com/satori/go.uuid"
)

const defaultTestClusterVer = "1.7.12"

func TestAPIServerConfigEnableDataEncryptionAtRest(t *testing.T) {
// Test EnableDataEncryptionAtRest = true
cs := createContainerService("testcluster", defaultTestClusterVer, 3, 2)
cs := CreateMockContainerService("testcluster", defaultTestClusterVer, 3, 2, false)
cs.Properties.OrchestratorProfile.KubernetesConfig.EnableDataEncryptionAtRest = helpers.PointerToBool(true)
setAPIServerConfig(cs)
a := cs.Properties.OrchestratorProfile.KubernetesConfig.APIServerConfig
Expand All @@ -22,7 +21,7 @@ func TestAPIServerConfigEnableDataEncryptionAtRest(t *testing.T) {
}

// Test EnableDataEncryptionAtRest = false
cs = createContainerService("testcluster", defaultTestClusterVer, 3, 2)
cs = CreateMockContainerService("testcluster", defaultTestClusterVer, 3, 2, false)
cs.Properties.OrchestratorProfile.KubernetesConfig.EnableDataEncryptionAtRest = helpers.PointerToBool(false)
setAPIServerConfig(cs)
a = cs.Properties.OrchestratorProfile.KubernetesConfig.APIServerConfig
Expand All @@ -34,7 +33,7 @@ func TestAPIServerConfigEnableDataEncryptionAtRest(t *testing.T) {

func TestAPIServerConfigEnableEncryptionWithExternalKms(t *testing.T) {
// Test EnableEncryptionWithExternalKms = true
cs := createContainerService("testcluster", defaultTestClusterVer, 3, 2)
cs := CreateMockContainerService("testcluster", defaultTestClusterVer, 3, 2, false)
cs.Properties.OrchestratorProfile.KubernetesConfig.EnableEncryptionWithExternalKms = helpers.PointerToBool(true)
setAPIServerConfig(cs)
a := cs.Properties.OrchestratorProfile.KubernetesConfig.APIServerConfig
Expand All @@ -44,7 +43,7 @@ func TestAPIServerConfigEnableEncryptionWithExternalKms(t *testing.T) {
}

// Test EnableEncryptionWithExternalKms = false
cs = createContainerService("testcluster", defaultTestClusterVer, 3, 2)
cs = CreateMockContainerService("testcluster", defaultTestClusterVer, 3, 2, false)
cs.Properties.OrchestratorProfile.KubernetesConfig.EnableEncryptionWithExternalKms = helpers.PointerToBool(false)
setAPIServerConfig(cs)
a = cs.Properties.OrchestratorProfile.KubernetesConfig.APIServerConfig
Expand All @@ -56,7 +55,7 @@ func TestAPIServerConfigEnableEncryptionWithExternalKms(t *testing.T) {

func TestAPIServerConfigEnableAggregatedAPIs(t *testing.T) {
// Test EnableAggregatedAPIs = true
cs := createContainerService("testcluster", defaultTestClusterVer, 3, 2)
cs := CreateMockContainerService("testcluster", defaultTestClusterVer, 3, 2, false)
cs.Properties.OrchestratorProfile.KubernetesConfig.EnableAggregatedAPIs = true
setAPIServerConfig(cs)
a := cs.Properties.OrchestratorProfile.KubernetesConfig.APIServerConfig
Expand Down Expand Up @@ -90,7 +89,7 @@ func TestAPIServerConfigEnableAggregatedAPIs(t *testing.T) {
}

// Test EnableAggregatedAPIs = false
cs = createContainerService("testcluster", defaultTestClusterVer, 3, 2)
cs = CreateMockContainerService("testcluster", defaultTestClusterVer, 3, 2, false)
cs.Properties.OrchestratorProfile.KubernetesConfig.EnableAggregatedAPIs = false
setAPIServerConfig(cs)
a = cs.Properties.OrchestratorProfile.KubernetesConfig.APIServerConfig
Expand All @@ -106,7 +105,7 @@ func TestAPIServerConfigEnableAggregatedAPIs(t *testing.T) {

func TestAPIServerConfigUseCloudControllerManager(t *testing.T) {
// Test UseCloudControllerManager = true
cs := createContainerService("testcluster", defaultTestClusterVer, 3, 2)
cs := CreateMockContainerService("testcluster", defaultTestClusterVer, 3, 2, false)
cs.Properties.OrchestratorProfile.KubernetesConfig.UseCloudControllerManager = helpers.PointerToBool(true)
setAPIServerConfig(cs)
a := cs.Properties.OrchestratorProfile.KubernetesConfig.APIServerConfig
Expand All @@ -120,7 +119,7 @@ func TestAPIServerConfigUseCloudControllerManager(t *testing.T) {
}

// Test UseCloudControllerManager = false
cs = createContainerService("testcluster", defaultTestClusterVer, 3, 2)
cs = CreateMockContainerService("testcluster", defaultTestClusterVer, 3, 2, false)
cs.Properties.OrchestratorProfile.KubernetesConfig.UseCloudControllerManager = helpers.PointerToBool(false)
setAPIServerConfig(cs)
a = cs.Properties.OrchestratorProfile.KubernetesConfig.APIServerConfig
Expand All @@ -136,7 +135,7 @@ func TestAPIServerConfigUseCloudControllerManager(t *testing.T) {

func TestAPIServerConfigHasAadProfile(t *testing.T) {
// Test HasAadProfile = true
cs := createContainerService("testcluster", defaultTestClusterVer, 3, 2)
cs := CreateMockContainerService("testcluster", defaultTestClusterVer, 3, 2, false)
cs.Properties.AADProfile = &api.AADProfile{
ServerAppID: "test-id",
TenantID: "test-tenant",
Expand All @@ -161,7 +160,7 @@ func TestAPIServerConfigHasAadProfile(t *testing.T) {
}

// Test OIDC user overrides
cs = createContainerService("testcluster", "1.7.12", 3, 2)
cs = CreateMockContainerService("testcluster", "1.7.12", 3, 2, false)
cs.Properties.AADProfile = &api.AADProfile{
ServerAppID: "test-id",
TenantID: "test-tenant",
Expand Down Expand Up @@ -196,7 +195,7 @@ func TestAPIServerConfigHasAadProfile(t *testing.T) {
}

// Test China Cloud settings
cs = createContainerService("testcluster", defaultTestClusterVer, 3, 2)
cs = CreateMockContainerService("testcluster", defaultTestClusterVer, 3, 2, false)
cs.Properties.AADProfile = &api.AADProfile{
ServerAppID: "test-id",
TenantID: "test-tenant",
Expand Down Expand Up @@ -234,7 +233,7 @@ func TestAPIServerConfigHasAadProfile(t *testing.T) {
}

// Test HasAadProfile = false
cs = createContainerService("testcluster", defaultTestClusterVer, 3, 2)
cs = CreateMockContainerService("testcluster", defaultTestClusterVer, 3, 2, false)
setAPIServerConfig(cs)
a = cs.Properties.OrchestratorProfile.KubernetesConfig.APIServerConfig
for _, key := range []string{"--oidc-username-claim", "--oidc-groups-claim", "--oidc-client-id", "--oidc-issuer-url"} {
Expand All @@ -247,7 +246,7 @@ func TestAPIServerConfigHasAadProfile(t *testing.T) {

func TestAPIServerConfigEnableRbac(t *testing.T) {
// Test EnableRbac = true
cs := createContainerService("testcluster", defaultTestClusterVer, 3, 2)
cs := CreateMockContainerService("testcluster", defaultTestClusterVer, 3, 2, false)
cs.Properties.OrchestratorProfile.KubernetesConfig.EnableRbac = helpers.PointerToBool(true)
setAPIServerConfig(cs)
a := cs.Properties.OrchestratorProfile.KubernetesConfig.APIServerConfig
Expand All @@ -257,7 +256,7 @@ func TestAPIServerConfigEnableRbac(t *testing.T) {
}

// Test EnableRbac = true with 1.6 cluster
cs = createContainerService("testcluster", "1.6.11", 3, 2)
cs = CreateMockContainerService("testcluster", "1.6.11", 3, 2, false)
cs.Properties.OrchestratorProfile.KubernetesConfig.EnableRbac = helpers.PointerToBool(true)
setAPIServerConfig(cs)
a = cs.Properties.OrchestratorProfile.KubernetesConfig.APIServerConfig
Expand All @@ -267,7 +266,7 @@ func TestAPIServerConfigEnableRbac(t *testing.T) {
}

// Test EnableRbac = false
cs = createContainerService("testcluster", defaultTestClusterVer, 3, 2)
cs = CreateMockContainerService("testcluster", defaultTestClusterVer, 3, 2, false)
cs.Properties.OrchestratorProfile.KubernetesConfig.EnableRbac = helpers.PointerToBool(false)
setAPIServerConfig(cs)
a = cs.Properties.OrchestratorProfile.KubernetesConfig.APIServerConfig
Expand All @@ -277,7 +276,7 @@ func TestAPIServerConfigEnableRbac(t *testing.T) {
}

// Test EnableRbac = false with 1.6 cluster
cs = createContainerService("testcluster", "1.6.11", 3, 2)
cs = CreateMockContainerService("testcluster", "1.6.11", 3, 2, false)
cs.Properties.OrchestratorProfile.KubernetesConfig.EnableRbac = helpers.PointerToBool(false)
setAPIServerConfig(cs)
a = cs.Properties.OrchestratorProfile.KubernetesConfig.APIServerConfig
Expand All @@ -289,7 +288,7 @@ func TestAPIServerConfigEnableRbac(t *testing.T) {

func TestAPIServerConfigEnableSecureKubelet(t *testing.T) {
// Test EnableSecureKubelet = true
cs := createContainerService("testcluster", defaultTestClusterVer, 3, 2)
cs := CreateMockContainerService("testcluster", defaultTestClusterVer, 3, 2, false)
cs.Properties.OrchestratorProfile.KubernetesConfig.EnableSecureKubelet = helpers.PointerToBool(true)
setAPIServerConfig(cs)
a := cs.Properties.OrchestratorProfile.KubernetesConfig.APIServerConfig
Expand All @@ -303,7 +302,7 @@ func TestAPIServerConfigEnableSecureKubelet(t *testing.T) {
}

// Test EnableSecureKubelet = false
cs = createContainerService("testcluster", defaultTestClusterVer, 3, 2)
cs = CreateMockContainerService("testcluster", defaultTestClusterVer, 3, 2, false)
cs.Properties.OrchestratorProfile.KubernetesConfig.EnableSecureKubelet = helpers.PointerToBool(false)
setAPIServerConfig(cs)
a = cs.Properties.OrchestratorProfile.KubernetesConfig.APIServerConfig
Expand All @@ -320,7 +319,7 @@ func TestAPIServerConfigDefaultAdmissionControls(t *testing.T) {
version := "1.10.0"
enableAdmissionPluginsKey := "--enable-admission-plugins"
admissonControlKey := "--admission-control"
cs := createContainerService("testcluster", version, 3, 2)
cs := CreateMockContainerService("testcluster", version, 3, 2, false)
cs.Properties.OrchestratorProfile.KubernetesConfig.APIServerConfig = map[string]string{}
cs.Properties.OrchestratorProfile.KubernetesConfig.APIServerConfig[admissonControlKey] = "NamespaceLifecycle,LimitRanger,ServiceAccount,DefaultStorageClass,DefaultTolerationSeconds,MutatingAdmissionWebhook,ValidatingAdmissionWebhook,ResourceQuota,DenyEscalatingExec,AlwaysPullImages,ExtendedResourceToleration"
setAPIServerConfig(cs)
Expand All @@ -338,7 +337,7 @@ func TestAPIServerConfigDefaultAdmissionControls(t *testing.T) {

// Test --admission-control for v1.9 and below
version = "1.9.0"
cs = createContainerService("testcluster", version, 3, 2)
cs = CreateMockContainerService("testcluster", version, 3, 2, false)
setAPIServerConfig(cs)
a = cs.Properties.OrchestratorProfile.KubernetesConfig.APIServerConfig

Expand All @@ -352,71 +351,3 @@ func TestAPIServerConfigDefaultAdmissionControls(t *testing.T) {
t.Fatalf("Admission control key '%s' not set in API server config for version %s", enableAdmissionPluginsKey, version)
}
}

func createContainerService(containerServiceName string, orchestratorVersion string, masterCount int, agentCount int) *api.ContainerService {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

refactored in common place, function was duplicated

cs := api.ContainerService{}
cs.ID = uuid.NewV4().String()
cs.Location = "eastus"
cs.Name = containerServiceName

cs.Properties = &api.Properties{}

cs.Properties.MasterProfile = &api.MasterProfile{}
cs.Properties.MasterProfile.Count = masterCount
cs.Properties.MasterProfile.DNSPrefix = "testmaster"
cs.Properties.MasterProfile.VMSize = "Standard_D2_v2"

cs.Properties.AgentPoolProfiles = []*api.AgentPoolProfile{}
agentPool := &api.AgentPoolProfile{}
agentPool.Count = agentCount
agentPool.Name = "agentpool1"
agentPool.VMSize = "Standard_D2_v2"
agentPool.OSType = "Linux"
agentPool.AvailabilityProfile = "AvailabilitySet"
agentPool.StorageProfile = "StorageAccount"

cs.Properties.AgentPoolProfiles = append(cs.Properties.AgentPoolProfiles, agentPool)

cs.Properties.LinuxProfile = &api.LinuxProfile{
AdminUsername: "azureuser",
SSH: struct {
PublicKeys []api.PublicKey `json:"publicKeys"`
}{},
}

cs.Properties.LinuxProfile.AdminUsername = "azureuser"
cs.Properties.LinuxProfile.SSH.PublicKeys = append(
cs.Properties.LinuxProfile.SSH.PublicKeys, api.PublicKey{KeyData: "test"})

cs.Properties.ServicePrincipalProfile = &api.ServicePrincipalProfile{}
cs.Properties.ServicePrincipalProfile.ClientID = "DEC923E3-1EF1-4745-9516-37906D56DEC4"
cs.Properties.ServicePrincipalProfile.Secret = "DEC923E3-1EF1-4745-9516-37906D56DEC4"

cs.Properties.OrchestratorProfile = &api.OrchestratorProfile{}
cs.Properties.OrchestratorProfile.OrchestratorType = api.Kubernetes
cs.Properties.OrchestratorProfile.OrchestratorVersion = orchestratorVersion
cs.Properties.OrchestratorProfile.KubernetesConfig = &api.KubernetesConfig{
EnableSecureKubelet: helpers.PointerToBool(api.DefaultSecureKubeletEnabled),
EnableRbac: helpers.PointerToBool(api.DefaultRBACEnabled),
EtcdDiskSizeGB: DefaultEtcdDiskSize,
ServiceCIDR: DefaultKubernetesServiceCIDR,
DockerBridgeSubnet: DefaultDockerBridgeSubnet,
DNSServiceIP: DefaultKubernetesDNSServiceIP,
GCLowThreshold: DefaultKubernetesGCLowThreshold,
GCHighThreshold: DefaultKubernetesGCHighThreshold,
MaxPods: DefaultKubernetesMaxPodsVNETIntegrated,
ClusterSubnet: DefaultKubernetesSubnet,
ContainerRuntime: DefaultContainerRuntime,
NetworkPlugin: DefaultNetworkPlugin,
NetworkPolicy: DefaultNetworkPolicy,
EtcdVersion: DefaultEtcdVersion,
KubeletConfig: make(map[string]string),
}

cs.Properties.CertificateProfile = &api.CertificateProfile{}
cs.Properties.CertificateProfile.CaCertificate = "cacert"
cs.Properties.CertificateProfile.KubeConfigCertificate = "kubeconfigcert"
cs.Properties.CertificateProfile.KubeConfigPrivateKey = "kubeconfigkey"

return &cs
}
Loading