summaryrefslogtreecommitdiff
path: root/qa/qa/specs/features/api/1_manage/migration/gitlab_migration_group_spec.rb
blob: f721b3326a0718ace456da4cb9fea0970392bc57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# frozen_string_literal: true

module QA
  RSpec.describe 'Manage', :reliable, :requires_admin do
    describe 'Gitlab migration' do
      let(:import_wait_duration) { { max_duration: 300, sleep_interval: 2 } }
      let(:admin_api_client) { Runtime::API::Client.as_admin }
      let(:user) do
        Resource::User.fabricate_via_api! do |usr|
          usr.api_client = admin_api_client
          usr.hard_delete_on_api_removal = true
        end
      end

      let(:api_client) { Runtime::API::Client.new(user: user) }

      let(:sandbox) do
        Resource::Sandbox.fabricate_via_api! do |group|
          group.api_client = admin_api_client
        end
      end

      let(:destination_group) do
        Resource::Group.fabricate_via_api! do |group|
          group.api_client = api_client
          group.sandbox = sandbox
          group.path = "destination-group-for-import-#{SecureRandom.hex(4)}"
        end
      end

      let(:source_group) do
        Resource::Group.fabricate_via_api! do |group|
          group.api_client = api_client
          group.sandbox = sandbox
          group.path = "source-group-for-import-#{SecureRandom.hex(4)}"
          group.avatar = File.new('qa/fixtures/designs/tanuki.jpg', 'r')
        end
      end

      let(:imported_group) do
        Resource::BulkImportGroup.fabricate_via_api! do |group|
          group.api_client = api_client
          group.sandbox = destination_group
          group.source_group = source_group
        end
      end

      let(:import_failures) do
        imported_group.import_details.sum([]) { |details| details[:failures] }
      end

      before do
        sandbox.add_member(user, Resource::Members::AccessLevel::MAINTAINER)
      end

      after do |example|
        # Checking for failures in the test currently makes test very flaky due to catching unrelated failures
        # Log failures for easier debugging
        Runtime::Logger.warn("Import failures: #{import_failures}") if example.exception && !import_failures.empty?
      ensure
        user.remove_via_api!
      end

      context 'with subgroups and labels' do
        let(:subgroup) do
          Resource::Group.fabricate_via_api! do |group|
            group.api_client = api_client
            group.sandbox = source_group
            group.path = "subgroup-for-import-#{SecureRandom.hex(4)}"
          end
        end

        let(:imported_subgroup) do
          Resource::Group.init do |group|
            group.api_client = api_client
            group.sandbox = imported_group
            group.path = subgroup.path
          end
        end

        before do
          Resource::GroupLabel.fabricate_via_api! do |label|
            label.api_client = api_client
            label.group = source_group
            label.title = "source-group-#{SecureRandom.hex(4)}"
          end
          Resource::GroupLabel.fabricate_via_api! do |label|
            label.api_client = api_client
            label.group = subgroup
            label.title = "subgroup-#{SecureRandom.hex(4)}"
          end

          imported_group # trigger import
        end

        it(
          'successfully imports groups and labels',
          testcase: 'https://gitlab.com/gitlab-org/gitlab/-/quality/test_cases/347674'
        ) do
          expect { imported_group.import_status }.to eventually_eq('finished').within(import_wait_duration)

          aggregate_failures do
            expect(imported_group.reload!).to eq(source_group)
            expect(imported_group.labels).to include(*source_group.labels)

            expect(imported_subgroup.reload!).to eq(subgroup)
            expect(imported_subgroup.labels).to include(*subgroup.labels)
          end
        end
      end

      context 'with milestones and badges' do
        let(:source_milestone) do
          Resource::GroupMilestone.fabricate_via_api! do |milestone|
            milestone.api_client = api_client
            milestone.group = source_group
          end
        end

        before do
          source_milestone

          Resource::GroupBadge.fabricate_via_api! do |badge|
            badge.api_client = api_client
            badge.group = source_group
            badge.link_url = "http://example.com/badge"
            badge.image_url = "http://shields.io/badge"
          end

          imported_group # trigger import
        end

        it(
          'successfully imports group milestones and badges',
          testcase: 'https://gitlab.com/gitlab-org/gitlab/-/quality/test_cases/347628'
        ) do
          expect { imported_group.import_status }.to eventually_eq('finished').within(import_wait_duration)

          imported_milestone = imported_group.reload!.milestones.find { |ml| ml.title == source_milestone.title }
          aggregate_failures do
            expect(imported_milestone).to eq(source_milestone)
            expect(imported_milestone.iid).to eq(source_milestone.iid)
            expect(imported_milestone.created_at).to eq(source_milestone.created_at)
            expect(imported_milestone.updated_at).to eq(source_milestone.updated_at)

            expect(imported_group.badges).to eq(source_group.badges)
          end
        end
      end
    end
  end
end