azure_credentials_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. Copyright 2016 The Kubernetes 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. http://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 azure
  14. import (
  15. "bytes"
  16. "context"
  17. "testing"
  18. "github.com/Azure/azure-sdk-for-go/services/containerregistry/mgmt/2019-05-01/containerregistry"
  19. "github.com/Azure/go-autorest/autorest/to"
  20. )
  21. type fakeClient struct {
  22. results []containerregistry.Registry
  23. }
  24. func (f *fakeClient) List(ctx context.Context) ([]containerregistry.Registry, error) {
  25. return f.results, nil
  26. }
  27. func Test(t *testing.T) {
  28. configStr := `
  29. {
  30. "aadClientId": "foo",
  31. "aadClientSecret": "bar"
  32. }`
  33. result := []containerregistry.Registry{
  34. {
  35. Name: to.StringPtr("foo"),
  36. RegistryProperties: &containerregistry.RegistryProperties{
  37. LoginServer: to.StringPtr("*.azurecr.io"),
  38. },
  39. },
  40. {
  41. Name: to.StringPtr("bar"),
  42. RegistryProperties: &containerregistry.RegistryProperties{
  43. LoginServer: to.StringPtr("*.azurecr.cn"),
  44. },
  45. },
  46. {
  47. Name: to.StringPtr("baz"),
  48. RegistryProperties: &containerregistry.RegistryProperties{
  49. LoginServer: to.StringPtr("*.azurecr.de"),
  50. },
  51. },
  52. {
  53. Name: to.StringPtr("bus"),
  54. RegistryProperties: &containerregistry.RegistryProperties{
  55. LoginServer: to.StringPtr("*.azurecr.us"),
  56. },
  57. },
  58. }
  59. fakeClient := &fakeClient{
  60. results: result,
  61. }
  62. provider := &acrProvider{
  63. registryClient: fakeClient,
  64. }
  65. provider.loadConfig(bytes.NewBufferString(configStr))
  66. creds := provider.Provide("")
  67. if len(creds) != len(result)+1 {
  68. t.Errorf("Unexpected list: %v, expected length %d", creds, len(result)+1)
  69. }
  70. for _, cred := range creds {
  71. if cred.Username != "" && cred.Username != "foo" {
  72. t.Errorf("expected 'foo' for username, saw: %v", cred.Username)
  73. }
  74. if cred.Password != "" && cred.Password != "bar" {
  75. t.Errorf("expected 'bar' for password, saw: %v", cred.Username)
  76. }
  77. }
  78. for _, val := range result {
  79. registryName := getLoginServer(val)
  80. if _, found := creds[registryName]; !found {
  81. t.Errorf("Missing expected registry: %s", registryName)
  82. }
  83. }
  84. }
  85. func TestParseACRLoginServerFromImage(t *testing.T) {
  86. tests := []struct {
  87. image string
  88. expected string
  89. }{
  90. {
  91. image: "invalidImage",
  92. expected: "",
  93. },
  94. {
  95. image: "docker.io/library/busybox:latest",
  96. expected: "",
  97. },
  98. {
  99. image: "foo.azurecr.io/bar/image:version",
  100. expected: "foo.azurecr.io",
  101. },
  102. {
  103. image: "foo.azurecr.cn/bar/image:version",
  104. expected: "foo.azurecr.cn",
  105. },
  106. {
  107. image: "foo.azurecr.de/bar/image:version",
  108. expected: "foo.azurecr.de",
  109. },
  110. {
  111. image: "foo.azurecr.us/bar/image:version",
  112. expected: "foo.azurecr.us",
  113. },
  114. }
  115. for _, test := range tests {
  116. if loginServer := parseACRLoginServerFromImage(test.image); loginServer != test.expected {
  117. t.Errorf("function parseACRLoginServerFromImage returns \"%s\" for image %s, expected \"%s\"", loginServer, test.image, test.expected)
  118. }
  119. }
  120. }