azure_cloud_id_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. Copyright © The ESO Authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package akeyless
  14. import (
  15. "testing"
  16. "github.com/stretchr/testify/require"
  17. corev1 "k8s.io/api/core/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. )
  20. func TestAzureClientID(t *testing.T) {
  21. t.Parallel()
  22. sa := &corev1.ServiceAccount{
  23. ObjectMeta: metav1.ObjectMeta{
  24. Annotations: map[string]string{
  25. annotationClientID: "from-annotation",
  26. },
  27. },
  28. }
  29. id, err := azureClientID(sa, "from-param")
  30. require.NoError(t, err)
  31. require.Equal(t, "from-annotation", id)
  32. id, err = azureClientID(&corev1.ServiceAccount{}, "from-param")
  33. require.NoError(t, err)
  34. require.Equal(t, "from-param", id)
  35. _, err = azureClientID(&corev1.ServiceAccount{}, "")
  36. require.Error(t, err)
  37. }
  38. func TestAzureCloudSettingsFromName(t *testing.T) {
  39. t.Parallel()
  40. tests := []struct {
  41. name string
  42. env string
  43. expected azureCloudSettings
  44. ok bool
  45. }{
  46. {
  47. name: "public cloud",
  48. env: "AzurePublicCloud",
  49. expected: publicAzureCloudSettings,
  50. ok: true,
  51. },
  52. {
  53. name: "us gov",
  54. env: "AzureUSGovernment",
  55. expected: usGovAzureCloudSettings,
  56. ok: true,
  57. },
  58. {
  59. name: "china",
  60. env: "AzureChinaCloud",
  61. expected: chinaAzureCloudSettings,
  62. ok: true,
  63. },
  64. {
  65. name: "unknown",
  66. env: "UnknownCloud",
  67. ok: false,
  68. },
  69. }
  70. for _, tt := range tests {
  71. t.Run(tt.name, func(t *testing.T) {
  72. t.Parallel()
  73. got, ok := azureCloudSettingsFromName(tt.env)
  74. require.Equal(t, tt.ok, ok)
  75. if ok {
  76. require.Equal(t, tt.expected, got)
  77. }
  78. })
  79. }
  80. }
  81. func TestAzureCloudSettingsFromEnv(t *testing.T) {
  82. t.Run("AZURE_ENVIRONMENT", func(t *testing.T) {
  83. t.Setenv("AZURE_ENVIRONMENT", "AzureUSGovernment")
  84. t.Setenv("AZURE_CLOUD", "")
  85. require.Equal(t, usGovAzureCloudSettings, azureCloudSettingsFromEnv())
  86. })
  87. t.Run("AZURE_CLOUD fallback", func(t *testing.T) {
  88. t.Setenv("AZURE_ENVIRONMENT", "")
  89. t.Setenv("AZURE_CLOUD", "AzureChinaCloud")
  90. require.Equal(t, chinaAzureCloudSettings, azureCloudSettingsFromEnv())
  91. })
  92. }
  93. func TestAzureTenantID(t *testing.T) {
  94. t.Setenv("AZURE_TENANT_ID", "from-env")
  95. sa := &corev1.ServiceAccount{
  96. ObjectMeta: metav1.ObjectMeta{
  97. Annotations: map[string]string{
  98. annotationTenantID: "from-annotation",
  99. },
  100. },
  101. }
  102. id, err := azureTenantID(sa)
  103. require.NoError(t, err)
  104. require.Equal(t, "from-annotation", id)
  105. id, err = azureTenantID(&corev1.ServiceAccount{})
  106. require.NoError(t, err)
  107. require.Equal(t, "from-env", id)
  108. t.Setenv("AZURE_TENANT_ID", "")
  109. _, err = azureTenantID(&corev1.ServiceAccount{})
  110. require.Error(t, err)
  111. }