summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/config_spec.rb
blob: 57354e12aa397dfe8eb0689dc046aad6a0614b16 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
require 'fast_spec_helper'

require_dependency 'active_model'

describe Gitlab::Ci::Config do
  let(:project) { create(:project, :repository) }
  let(:config) do
    described_class.new(yml, project)
  end

  context 'when config is valid' do
    let(:yml) do
      <<-EOS
        image: ruby:2.2

        rspec:
          script:
            - gem install rspec
            - rspec
      EOS
    end

    describe '#to_hash' do
      it 'returns hash created from string' do
        hash = {
          image: 'ruby:2.2',
          rspec: {
            script: ['gem install rspec',
                     'rspec']
          }
        }

        expect(config.to_hash).to eq hash
      end

      describe '#valid?' do
        it 'is valid' do
          expect(config).to be_valid
        end

        it 'has no errors' do
          expect(config.errors).to be_empty
        end
      end
    end
  end

  context 'when using extendable hash' do
    let(:yml) do
      <<-EOS
        image: ruby:2.2

        rspec:
          script: rspec

        test:
          extends: rspec
          image: ruby:alpine
      EOS
    end

    it 'correctly extends the hash' do
      hash = {
        image: 'ruby:2.2',
        rspec: { script: 'rspec' },
        test: {
          extends: 'rspec',
          image: 'ruby:alpine',
          script: 'rspec'
        }
      }

      expect(config).to be_valid
      expect(config.to_hash).to eq hash
    end
  end

  context 'when config is invalid' do
    context 'when yml is incorrect' do
      let(:yml) { '// invalid' }

      describe '.new' do
        it 'raises error' do
          expect { config }.to raise_error(
            described_class::ConfigError,
            /Invalid configuration format/
          )
        end
      end
    end

    context 'when config logic is incorrect' do
      let(:yml) { 'before_script: "ls"' }

      describe '#valid?' do
        it 'is not valid' do
          expect(config).not_to be_valid
        end

        it 'has errors' do
          expect(config.errors).not_to be_empty
        end
      end

      describe '#errors' do
        it 'returns an array of strings' do
          expect(config.errors).to all(be_an_instance_of(String))
        end
      end
    end

    context 'when invalid extended hash has been provided' do
      let(:yml) do
        <<-EOS
          test:
            extends: test
            script: rspec
        EOS
      end

      it 'raises an error' do
        expect { config }.to raise_error(
          described_class::ConfigError, /circular dependency detected/
        )
      end
    end
  end

  context "when yml has valid 'includes' defined" do
    let(:yml) do
      <<-EOS
        includes:
          - /spec/fixtures/gitlab/ci/external_files/.gitlab-ci-template-1.yml
          - /spec/fixtures/gitlab/ci/external_files/.gitlab-ci-template-2.yml
          - https://gitlab.com/gitlab-org/gitlab-ce/blob/1234/.gitlab-ci-1.yml

        image: ruby:2.2
      EOS
    end

    before do
      allow_any_instance_of(Kernel).to receive_message_chain(:open, :read).and_return(yml)
    end

    it 'should return a composed hash' do
      before_script_values = [
        "apt-get update -qq && apt-get install -y -qq sqlite3 libsqlite3-dev nodejs", "ruby -v",
        "which ruby",
        "gem install bundler --no-ri --no-rdoc",
        "bundle install --jobs $(nproc)  \"${FLAGS[@]}\""
      ]
      variables = {
        AUTO_DEVOPS_DOMAIN: "domain.example.com",
        POSTGRES_USER: "user",
        POSTGRES_PASSWORD: "testing-password",
        POSTGRES_ENABLED: "true",
        POSTGRES_DB: "$CI_ENVIRONMENT_SLUG"
      }
      composed_hash = {
        before_script: before_script_values,
        image: "ruby:2.2",
        rspec: { script: ["bundle exec rspec"] },
        variables: variables
      }

      expect(config.to_hash).to eq(composed_hash)
    end
  end

  context "when config has invalid 'includes' defined"  do
    let(:yml) do
      <<-EOS
      includes: invalid
      EOS
    end

    it 'raises error' do
      expect { config }.to raise_error(
        ::Gitlab::Ci::ExternalFiles::Processor::ExternalFileError,
        /External files should be a valid local or remote file/
      )
    end
  end
end