registrycredentials_test.go 6.1 KB

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