cloud_environment_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. Copyright 2020 Docker Compose CLI 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 login
  14. import (
  15. "testing"
  16. "gotest.tools/v3/assert"
  17. )
  18. func TestNormalizeCloudEnvironmentURLs(t *testing.T) {
  19. ce := CloudEnvironment{
  20. Name: "SecretCloud",
  21. Authentication: CloudEnvironmentAuthentication{
  22. LoginEndpoint: "https://login.here.com/",
  23. Audiences: []string{
  24. "https://audience1",
  25. "https://audience2/",
  26. },
  27. Tenant: "common",
  28. },
  29. ResourceManagerURL: "invalid URL",
  30. }
  31. ce.normalizeURLs()
  32. assert.Equal(t, ce.Authentication.LoginEndpoint, "https://login.here.com")
  33. assert.Equal(t, ce.Authentication.Audiences[0], "https://audience1")
  34. assert.Equal(t, ce.Authentication.Audiences[1], "https://audience2")
  35. }
  36. func TestApplyInvalidCloudMetadataJSON(t *testing.T) {
  37. ce := newCloudEnvironmentService()
  38. bb := []byte(`This isn't really valid JSON`)
  39. err := ce.applyCloudMetadata(bb)
  40. assert.Assert(t, err != nil, "Cloud metadata was invalid, so the application should have failed")
  41. ensureWellKnownCloudMetadata(t, ce)
  42. }
  43. func TestApplyInvalidCloudMetatada(t *testing.T) {
  44. ce := newCloudEnvironmentService()
  45. // No name (moniker) for the cloud
  46. bb := []byte(`
  47. [{
  48. "authentication": {
  49. "loginEndpoint": "https://login.docker.com/",
  50. "audiences": [
  51. "https://management.docker.com/",
  52. "https://management.cli.docker.com/"
  53. ],
  54. "tenant": "F5773994-FE88-482E-9E33-6E799D250416"
  55. },
  56. "suffixes": {
  57. "acrLoginServer": "azurecr.docker.io"
  58. },
  59. "resourceManager": "https://management.docker.com/"
  60. }]`)
  61. err := ce.applyCloudMetadata(bb)
  62. assert.ErrorContains(t, err, "no name")
  63. ensureWellKnownCloudMetadata(t, ce)
  64. // Invalid resource manager URL
  65. bb = []byte(`
  66. [{
  67. "authentication": {
  68. "loginEndpoint": "https://login.docker.com/",
  69. "audiences": [
  70. "https://management.docker.com/",
  71. "https://management.cli.docker.com/"
  72. ],
  73. "tenant": "F5773994-FE88-482E-9E33-6E799D250416"
  74. },
  75. "name": "DockerAzureCloud",
  76. "suffixes": {
  77. "acrLoginServer": "azurecr.docker.io"
  78. },
  79. "resourceManager": "123"
  80. }]`)
  81. err = ce.applyCloudMetadata(bb)
  82. assert.ErrorContains(t, err, "invalid resource manager URL")
  83. ensureWellKnownCloudMetadata(t, ce)
  84. // Invalid login endpoint
  85. bb = []byte(`
  86. [{
  87. "authentication": {
  88. "loginEndpoint": "456",
  89. "audiences": [
  90. "https://management.docker.com/",
  91. "https://management.cli.docker.com/"
  92. ],
  93. "tenant": "F5773994-FE88-482E-9E33-6E799D250416"
  94. },
  95. "name": "DockerAzureCloud",
  96. "suffixes": {
  97. "acrLoginServer": "azurecr.docker.io"
  98. },
  99. "resourceManager": "https://management.docker.com/"
  100. }]`)
  101. err = ce.applyCloudMetadata(bb)
  102. assert.ErrorContains(t, err, "invalid login endpoint")
  103. ensureWellKnownCloudMetadata(t, ce)
  104. // No audiences
  105. bb = []byte(`
  106. [{
  107. "authentication": {
  108. "loginEndpoint": "https://login.docker.com/",
  109. "audiences": [ ],
  110. "tenant": "F5773994-FE88-482E-9E33-6E799D250416"
  111. },
  112. "name": "DockerAzureCloud",
  113. "suffixes": {
  114. "acrLoginServer": "azurecr.docker.io"
  115. },
  116. "resourceManager": "https://management.docker.com/"
  117. }]`)
  118. err = ce.applyCloudMetadata(bb)
  119. assert.ErrorContains(t, err, "no authentication audiences")
  120. ensureWellKnownCloudMetadata(t, ce)
  121. }
  122. func TestApplyCloudMetadata(t *testing.T) {
  123. ce := newCloudEnvironmentService()
  124. bb := []byte(`
  125. [{
  126. "authentication": {
  127. "loginEndpoint": "https://login.docker.com/",
  128. "audiences": [
  129. "https://management.docker.com/",
  130. "https://management.cli.docker.com/"
  131. ],
  132. "tenant": "F5773994-FE88-482E-9E33-6E799D250416"
  133. },
  134. "name": "DockerAzureCloud",
  135. "suffixes": {
  136. "acrLoginServer": "azurecr.docker.io"
  137. },
  138. "resourceManager": "https://management.docker.com/"
  139. }]`)
  140. err := ce.applyCloudMetadata(bb)
  141. assert.NilError(t, err)
  142. env, err := ce.Get("DockerAzureCloud")
  143. assert.NilError(t, err)
  144. assert.Equal(t, env.Authentication.LoginEndpoint, "https://login.docker.com")
  145. ensureWellKnownCloudMetadata(t, ce)
  146. }
  147. func TestDefaultCloudMetadataPresent(t *testing.T) {
  148. ensureWellKnownCloudMetadata(t, CloudEnvironments)
  149. }
  150. func ensureWellKnownCloudMetadata(t *testing.T, ce CloudEnvironmentService) {
  151. // Make sure well-known public cloud information is still available
  152. _, err := ce.Get(AzurePublicCloudName)
  153. assert.NilError(t, err)
  154. _, err = ce.Get("AzureChinaCloud")
  155. assert.NilError(t, err)
  156. _, err = ce.Get("AzureUSGovernment")
  157. assert.NilError(t, err)
  158. }