registrycredentials_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. Copyright 2020 Docker, Inc.
  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 convert
  14. import (
  15. "strconv"
  16. "testing"
  17. "github.com/Azure/go-autorest/autorest/to"
  18. "github.com/compose-spec/compose-go/types"
  19. cliconfigtypes "github.com/docker/cli/cli/config/types"
  20. "github.com/docker/api/compose"
  21. "github.com/Azure/azure-sdk-for-go/profiles/latest/containerinstance/mgmt/containerinstance"
  22. . "github.com/onsi/gomega"
  23. "github.com/stretchr/testify/mock"
  24. "github.com/stretchr/testify/suite"
  25. )
  26. const getAllCredentials = "getAllRegistryCredentials"
  27. type RegistryConvertTestSuite struct {
  28. suite.Suite
  29. loader *MockRegistryLoader
  30. }
  31. func (suite *RegistryConvertTestSuite) BeforeTest(suiteName, testName string) {
  32. suite.loader = &MockRegistryLoader{}
  33. }
  34. func (suite *RegistryConvertTestSuite) TestHubPrivateImage() {
  35. suite.loader.On(getAllCredentials).Return(registry("https://index.docker.io", userPwdCreds("toto", "pwd")), nil)
  36. creds, err := getRegistryCredentials(composeServices("gtardif/privateimg"), suite.loader)
  37. Expect(err).To(BeNil())
  38. Expect(creds).To(Equal([]containerinstance.ImageRegistryCredential{
  39. {
  40. Server: to.StringPtr(dockerHub),
  41. Username: to.StringPtr("toto"),
  42. Password: to.StringPtr("pwd"),
  43. },
  44. }))
  45. }
  46. func (suite *RegistryConvertTestSuite) TestRegistryNameWithoutProtocol() {
  47. suite.loader.On(getAllCredentials).Return(registry("index.docker.io", userPwdCreds("toto", "pwd")), nil)
  48. creds, err := getRegistryCredentials(composeServices("gtardif/privateimg"), suite.loader)
  49. Expect(err).To(BeNil())
  50. Expect(creds).To(Equal([]containerinstance.ImageRegistryCredential{
  51. {
  52. Server: to.StringPtr(dockerHub),
  53. Username: to.StringPtr("toto"),
  54. Password: to.StringPtr("pwd"),
  55. },
  56. }))
  57. }
  58. func (suite *RegistryConvertTestSuite) TestImageWithDotInName() {
  59. suite.loader.On(getAllCredentials).Return(registry("index.docker.io", userPwdCreds("toto", "pwd")), nil)
  60. creds, err := getRegistryCredentials(composeServices("my.image"), suite.loader)
  61. Expect(err).To(BeNil())
  62. Expect(creds).To(Equal([]containerinstance.ImageRegistryCredential{
  63. {
  64. Server: to.StringPtr(dockerHub),
  65. Username: to.StringPtr("toto"),
  66. Password: to.StringPtr("pwd"),
  67. },
  68. }))
  69. }
  70. func (suite *RegistryConvertTestSuite) TestAcrPrivateImage() {
  71. suite.loader.On(getAllCredentials).Return(registry("https://mycontainerregistrygta.azurecr.io", tokenCreds("123456")), nil)
  72. creds, err := getRegistryCredentials(composeServices("mycontainerregistrygta.azurecr.io/privateimg"), suite.loader)
  73. Expect(err).To(BeNil())
  74. Expect(creds).To(Equal([]containerinstance.ImageRegistryCredential{
  75. {
  76. Server: to.StringPtr("mycontainerregistrygta.azurecr.io"),
  77. Username: to.StringPtr(tokenUsername),
  78. Password: to.StringPtr("123456"),
  79. },
  80. }))
  81. }
  82. func (suite *RegistryConvertTestSuite) TestAcrPrivateImageLinux() {
  83. token := tokenCreds("123456")
  84. token.Username = tokenUsername
  85. suite.loader.On(getAllCredentials).Return(registry("https://mycontainerregistrygta.azurecr.io", token), nil)
  86. creds, err := getRegistryCredentials(composeServices("mycontainerregistrygta.azurecr.io/privateimg"), suite.loader)
  87. Expect(err).To(BeNil())
  88. Expect(creds).To(Equal([]containerinstance.ImageRegistryCredential{
  89. {
  90. Server: to.StringPtr("mycontainerregistrygta.azurecr.io"),
  91. Username: to.StringPtr(tokenUsername),
  92. Password: to.StringPtr("123456"),
  93. },
  94. }))
  95. }
  96. func (suite *RegistryConvertTestSuite) TestNoMoreRegistriesThanImages() {
  97. configs := map[string]cliconfigtypes.AuthConfig{
  98. "https://mycontainerregistrygta.azurecr.io": tokenCreds("123456"),
  99. "https://index.docker.io": userPwdCreds("toto", "pwd"),
  100. }
  101. suite.loader.On(getAllCredentials).Return(configs, nil)
  102. creds, err := getRegistryCredentials(composeServices("mycontainerregistrygta.azurecr.io/privateimg"), suite.loader)
  103. Expect(err).To(BeNil())
  104. Expect(creds).To(Equal([]containerinstance.ImageRegistryCredential{
  105. {
  106. Server: to.StringPtr("mycontainerregistrygta.azurecr.io"),
  107. Username: to.StringPtr(tokenUsername),
  108. Password: to.StringPtr("123456"),
  109. },
  110. }))
  111. creds, err = getRegistryCredentials(composeServices("someuser/privateimg"), suite.loader)
  112. Expect(err).To(BeNil())
  113. Expect(creds).To(Equal([]containerinstance.ImageRegistryCredential{
  114. {
  115. Server: to.StringPtr(dockerHub),
  116. Username: to.StringPtr("toto"),
  117. Password: to.StringPtr("pwd"),
  118. },
  119. }))
  120. }
  121. func (suite *RegistryConvertTestSuite) TestHubAndSeveralACRRegistries() {
  122. configs := map[string]cliconfigtypes.AuthConfig{
  123. "https://mycontainerregistry1.azurecr.io": tokenCreds("123456"),
  124. "https://mycontainerregistry2.azurecr.io": tokenCreds("456789"),
  125. "https://mycontainerregistry3.azurecr.io": tokenCreds("123456789"),
  126. "https://index.docker.io": userPwdCreds("toto", "pwd"),
  127. "https://other.registry.io": userPwdCreds("user", "password"),
  128. }
  129. suite.loader.On(getAllCredentials).Return(configs, nil)
  130. creds, err := getRegistryCredentials(composeServices("mycontainerregistry1.azurecr.io/privateimg", "someuser/privateImg2", "mycontainerregistry2.azurecr.io/privateimg"), suite.loader)
  131. Expect(err).To(BeNil())
  132. Expect(creds).To(ContainElement(containerinstance.ImageRegistryCredential{
  133. Server: to.StringPtr("mycontainerregistry1.azurecr.io"),
  134. Username: to.StringPtr(tokenUsername),
  135. Password: to.StringPtr("123456"),
  136. }))
  137. Expect(creds).To(ContainElement(containerinstance.ImageRegistryCredential{
  138. Server: to.StringPtr("mycontainerregistry2.azurecr.io"),
  139. Username: to.StringPtr(tokenUsername),
  140. Password: to.StringPtr("456789"),
  141. }))
  142. Expect(creds).To(ContainElement(containerinstance.ImageRegistryCredential{
  143. Server: to.StringPtr(dockerHub),
  144. Username: to.StringPtr("toto"),
  145. Password: to.StringPtr("pwd"),
  146. }))
  147. }
  148. func composeServices(images ...string) compose.Project {
  149. var services []types.ServiceConfig
  150. for index, name := range images {
  151. service := types.ServiceConfig{
  152. Name: "service" + strconv.Itoa(index),
  153. Image: name,
  154. }
  155. services = append(services, service)
  156. }
  157. return compose.Project{
  158. Config: types.Config{
  159. Services: services,
  160. },
  161. }
  162. }
  163. func registry(host string, configregistryData cliconfigtypes.AuthConfig) map[string]cliconfigtypes.AuthConfig {
  164. return map[string]cliconfigtypes.AuthConfig{
  165. host: configregistryData,
  166. }
  167. }
  168. func userPwdCreds(user string, password string) cliconfigtypes.AuthConfig {
  169. return cliconfigtypes.AuthConfig{
  170. Username: user,
  171. Password: password,
  172. }
  173. }
  174. func tokenCreds(token string) cliconfigtypes.AuthConfig {
  175. return cliconfigtypes.AuthConfig{
  176. IdentityToken: token,
  177. }
  178. }
  179. func TestRegistryConvertTestSuite(t *testing.T) {
  180. RegisterTestingT(t)
  181. suite.Run(t, new(RegistryConvertTestSuite))
  182. }
  183. type MockRegistryLoader struct {
  184. mock.Mock
  185. }
  186. func (s *MockRegistryLoader) getAllRegistryCredentials() (map[string]cliconfigtypes.AuthConfig, error) {
  187. args := s.Called()
  188. return args.Get(0).(map[string]cliconfigtypes.AuthConfig), args.Error(1)
  189. }