summaryrefslogtreecommitdiff
path: root/spec/factories/environments.rb
blob: 050cb8f8e6cbfd03dd1184715724f23978585f62 (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
# frozen_string_literal: true

FactoryBot.define do
  factory :environment, class: 'Environment' do
    sequence(:name) { |n| "environment#{n}" }

    association :project, :repository
    sequence(:external_url) { |n| "https://env#{n}.example.gitlab.com" }

    trait :available do
      state { :available }
    end

    trait :stopped do
      state { :stopped }
    end

    trait :with_review_app do |environment|
      transient do
        ref { 'master' }
      end

      # At this point `review app` is an ephemeral concept related to
      # deployments being deployed for given environment. There is no
      # first-class `review app` available so we need to create set of
      # interconnected objects to simulate a review app.
      #
      after(:create) do |environment, evaluator|
        pipeline = create(:ci_pipeline, project: environment.project)

        deployable = create(:ci_build, name: "#{environment.name}:deploy",
                                       pipeline: pipeline)

        deployment = create(:deployment,
                            :success,
                            environment: environment,
                            project: environment.project,
                            deployable: deployable,
                            ref: evaluator.ref,
                            sha: environment.project.commit(evaluator.ref).id)

        teardown_build = create(:ci_build, :manual,
                                name: "#{environment.name}:teardown",
                                pipeline: pipeline)

        deployment.update_column(:on_stop, teardown_build.name)
        environment.update_attribute(:deployments, [deployment])
      end
    end

    trait :non_playable do
      status { 'created' }
      self.when { 'manual' }
    end

    trait :auto_stoppable do
      auto_stop_at { 1.day.ago }
    end

    trait :will_auto_stop do
      auto_stop_at { 1.day.from_now }
    end
  end
end