@@ -12,7 +12,6 @@ import (
1212)
1313
1414func TestAccProjectShareRepository_full (t * testing.T ) {
15- t .Skip ("project API is not returning/setting read_only field correctly" )
1615
1716 client := acctest .GetTestResty (t )
1817 version , err := util .GetArtifactoryVersion (client )
@@ -48,7 +47,7 @@ func TestAccProjectShareRepository_full(t *testing.T) {
4847 key = "{{ .repo_key }}"
4948
5049 lifecycle {
51- ignore_changes = ["project_key"]
50+ ignore_changes = ["project_key", "project_environments" ]
5251 }
5352 }
5453
@@ -80,7 +79,7 @@ func TestAccProjectShareRepository_full(t *testing.T) {
8079 key = "{{ .repo_key }}"
8180
8281 lifecycle {
83- ignore_changes = ["project_key"]
82+ ignore_changes = ["project_key", "project_environments" ]
8483 }
8584 }
8685
@@ -147,3 +146,170 @@ func TestAccProjectShareRepository_full(t *testing.T) {
147146 },
148147 })
149148}
149+
150+ func TestAccProjectShareRepositoryWithMultipleProjects (t * testing.T ) {
151+ // The goal of this test is to simulate the race condition, when the repository was only shared with one project (first in the list) if
152+ // the loop was used in "project_share_repository" resource
153+ client := acctest .GetTestResty (t )
154+ version , err := util .GetArtifactoryVersion (client )
155+ if err != nil {
156+ t .Fatal (err )
157+ }
158+ valid , err := util .CheckVersion (version , "7.90.1" )
159+ if err != nil {
160+ t .Fatal (err )
161+ }
162+ if ! valid {
163+ t .Skipf ("Artifactory version %s is earlier than 7.90.1" , version )
164+ }
165+
166+ projectKey1 := strings .ToLower (acctest .RandSeq (10 ))
167+ projectKey2 := strings .ToLower (acctest .RandSeq (10 ))
168+ projectKey3 := strings .ToLower (acctest .RandSeq (10 ))
169+ projectKey4 := strings .ToLower (acctest .RandSeq (10 ))
170+ projectName1 := fmt .Sprintf ("tftestprojects1_%s" , projectKey1 )
171+ projectName2 := fmt .Sprintf ("tftestprojects2_%s" , projectKey2 )
172+ projectName3 := fmt .Sprintf ("tftestprojects3_%s" , projectKey3 )
173+ projectName4 := fmt .Sprintf ("tftestprojects4_%s" , projectKey4 )
174+
175+ repoKey := fmt .Sprintf ("repo%d" , testutil .RandomInt ())
176+
177+ fqrn := "project_share_repository.share_repo"
178+
179+ params := map [string ]string {
180+ "project_name_1" : projectName1 ,
181+ "project_key_1" : projectKey1 ,
182+ "project_name_2" : projectName2 ,
183+ "project_key_2" : projectKey2 ,
184+ "project_name_3" : projectName3 ,
185+ "project_key_3" : projectKey3 ,
186+ "project_name_4" : projectName4 ,
187+ "project_key_4" : projectKey4 ,
188+ "repo_key" : repoKey ,
189+ }
190+ // Creating projects without for each loop, they are not supported in tf tests
191+ temp := `
192+ resource "artifactory_local_generic_repository" "repo" {
193+ key = "{{ .repo_key }}"
194+ description = "Lab repository for troubleshooting - {{ .repo_key }}"
195+
196+ lifecycle {
197+ ignore_changes = ["project_key", "project_environments"]
198+ }
199+ }
200+
201+ # Create 4 projects
202+ resource "project" "{{ .project_name_1 }}" {
203+ key = "{{ .project_key_1 }}"
204+ display_name = "{{ .project_name_1 }}"
205+ description = "test description"
206+ admin_privileges {
207+ manage_members = true
208+ manage_resources = true
209+ index_resources = true
210+ }
211+ max_storage_in_gibibytes = 1
212+ block_deployments_on_limit = true
213+ email_notification = false
214+ }
215+
216+ resource "project" "{{ .project_name_2 }}" {
217+ key = "{{ .project_key_2 }}"
218+ display_name = "{{ .project_name_2 }}"
219+ description = "test description"
220+ admin_privileges {
221+ manage_members = true
222+ manage_resources = true
223+ index_resources = true
224+ }
225+ max_storage_in_gibibytes = 1
226+ block_deployments_on_limit = true
227+ email_notification = false
228+ }
229+
230+ resource "project" "{{ .project_name_3 }}" {
231+ key = "{{ .project_key_3 }}"
232+ display_name = "{{ .project_name_3 }}"
233+ description = "test description"
234+ admin_privileges {
235+ manage_members = true
236+ manage_resources = true
237+ index_resources = true
238+ }
239+ max_storage_in_gibibytes = 1
240+ block_deployments_on_limit = true
241+ email_notification = false
242+ }
243+
244+ resource "project" "{{ .project_name_4 }}" {
245+ key = "{{ .project_key_4 }}"
246+ display_name = "{{ .project_name_4 }}"
247+ description = "test description"
248+ admin_privileges {
249+ manage_members = true
250+ manage_resources = true
251+ index_resources = true
252+ }
253+ max_storage_in_gibibytes = 1
254+ block_deployments_on_limit = true
255+ email_notification = false
256+ }
257+
258+ # Add repository to {{ .project_name_1 }}
259+ resource "project_repository" "add_to_project1" {
260+ project_key = project.{{ .project_name_1 }}.key
261+ key = artifactory_local_generic_repository.repo.key
262+ }
263+
264+ # Share ONE repo with the other three projects
265+ resource "project_share_repository" "share_repo" {
266+ count = 3
267+
268+ repo_key = artifactory_local_generic_repository.repo.key
269+ target_project_key = element(
270+ [
271+ project.{{ .project_name_2 }}.key,
272+ project.{{ .project_name_3 }}.key,
273+ project.{{ .project_name_4 }}.key
274+ ],
275+ count.index
276+ )
277+ read_only = true
278+ depends_on = [project_repository.add_to_project1]
279+ }
280+ `
281+
282+ config := util .ExecuteTemplate ("TestAccProjectShareRepository" , temp , params )
283+
284+ resource .Test (t , resource.TestCase {
285+ PreCheck : func () { acctest .PreCheck (t ) },
286+ ProtoV6ProviderFactories : acctest .ProtoV6ProviderFactories ,
287+ ExternalProviders : map [string ]resource.ExternalProvider {
288+ "artifactory" : {
289+ Source : "jfrog/artifactory" ,
290+ },
291+ },
292+ Steps : []resource.TestStep {
293+ {
294+ Config : config ,
295+ Check : resource .ComposeTestCheckFunc (
296+ resource .TestCheckResourceAttr (fmt .Sprintf ("%s.0" , fqrn ), "repo_key" , params ["repo_key" ]),
297+ resource .TestCheckResourceAttr (fmt .Sprintf ("%s.0" , fqrn ), "target_project_key" , params ["project_key_2" ]),
298+ resource .TestCheckResourceAttr (fmt .Sprintf ("%s.0" , fqrn ), "read_only" , "true" ),
299+ resource .TestCheckResourceAttr (fmt .Sprintf ("%s.1" , fqrn ), "repo_key" , params ["repo_key" ]),
300+ resource .TestCheckResourceAttr (fmt .Sprintf ("%s.1" , fqrn ), "target_project_key" , params ["project_key_3" ]),
301+ resource .TestCheckResourceAttr (fmt .Sprintf ("%s.1" , fqrn ), "read_only" , "true" ),
302+ resource .TestCheckResourceAttr (fmt .Sprintf ("%s.2" , fqrn ), "repo_key" , params ["repo_key" ]),
303+ resource .TestCheckResourceAttr (fmt .Sprintf ("%s.2" , fqrn ), "target_project_key" , params ["project_key_4" ]),
304+ resource .TestCheckResourceAttr (fmt .Sprintf ("%s.2" , fqrn ), "read_only" , "true" ),
305+ ),
306+ },
307+ {
308+ ResourceName : fmt .Sprintf ("%s[0]" , fqrn ),
309+ ImportStateId : fmt .Sprintf ("%s:%s" , params ["repo_key" ], params ["project_key_2" ]),
310+ ImportState : true ,
311+ ImportStateVerify : false ,
312+ },
313+ },
314+ })
315+ }
0 commit comments