yaml_processor_spec.rb 80.2 KB
Newer Older
2001
2002
2003
2004

        it "returns errors if job stage is not a defined stage" do
          config = YAML.dump({ types: %w(build test), rspec: { script: "test", type: "acceptance" } })
          expect do
2005
            Gitlab::Ci::YamlProcessor.new(config)
2006
          end.to raise_error(Gitlab::Ci::YamlProcessor::ValidationError, "rspec job: stage parameter should be .pre, build, test, .post")
2007
2008
2009
2010
2011
        end

        it "returns errors if stages is not an array" do
          config = YAML.dump({ stages: "test", rspec: { script: "test" } })
          expect do
2012
            Gitlab::Ci::YamlProcessor.new(config)
2013
2014
2015
2016
2017
2018
          end.to raise_error(Gitlab::Ci::YamlProcessor::ValidationError, "stages config should be an array of strings")
        end

        it "returns errors if stages is not an array of strings" do
          config = YAML.dump({ stages: [true, "test"], rspec: { script: "test" } })
          expect do
2019
            Gitlab::Ci::YamlProcessor.new(config)
2020
2021
2022
2023
2024
2025
          end.to raise_error(Gitlab::Ci::YamlProcessor::ValidationError, "stages config should be an array of strings")
        end

        it "returns errors if variables is not a map" do
          config = YAML.dump({ variables: "test", rspec: { script: "test" } })
          expect do
2026
            Gitlab::Ci::YamlProcessor.new(config)
2027
2028
2029
2030
2031
2032
          end.to raise_error(Gitlab::Ci::YamlProcessor::ValidationError, "variables config should be a hash of key value pairs")
        end

        it "returns errors if variables is not a map of key-value strings" do
          config = YAML.dump({ variables: { test: false }, rspec: { script: "test" } })
          expect do
2033
            Gitlab::Ci::YamlProcessor.new(config)
2034
2035
2036
2037
2038
2039
          end.to raise_error(Gitlab::Ci::YamlProcessor::ValidationError, "variables config should be a hash of key value pairs")
        end

        it "returns errors if job when is not on_success, on_failure or always" do
          config = YAML.dump({ rspec: { script: "test", when: 1 } })
          expect do
2040
            Gitlab::Ci::YamlProcessor.new(config)
2041
          end.to raise_error(Gitlab::Ci::YamlProcessor::ValidationError, "jobs:rspec when should be one of: #{Gitlab::Ci::Config::Entry::Job::ALLOWED_WHEN.join(', ')}")
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
        end

        it "returns errors if job artifacts:name is not an a string" do
          config = YAML.dump({ types: %w(build test), rspec: { script: "test", artifacts: { name: 1 } } })
          expect do
            Gitlab::Ci::YamlProcessor.new(config)
          end.to raise_error(Gitlab::Ci::YamlProcessor::ValidationError, "jobs:rspec:artifacts name should be a string")
        end

        it "returns errors if job artifacts:when is not an a predefined value" do
          config = YAML.dump({ types: %w(build test), rspec: { script: "test", artifacts: { when: 1 } } })
          expect do
            Gitlab::Ci::YamlProcessor.new(config)
          end.to raise_error(Gitlab::Ci::YamlProcessor::ValidationError, "jobs:rspec:artifacts when should be on_success, on_failure or always")
        end

        it "returns errors if job artifacts:expire_in is not an a string" do
          config = YAML.dump({ types: %w(build test), rspec: { script: "test", artifacts: { expire_in: 1 } } })
          expect do
            Gitlab::Ci::YamlProcessor.new(config)
          end.to raise_error(Gitlab::Ci::YamlProcessor::ValidationError, "jobs:rspec:artifacts expire in should be a duration")
        end

        it "returns errors if job artifacts:expire_in is not an a valid duration" do
          config = YAML.dump({ types: %w(build test), rspec: { script: "test", artifacts: { expire_in: "7 elephants" } } })
          expect do
            Gitlab::Ci::YamlProcessor.new(config)
          end.to raise_error(Gitlab::Ci::YamlProcessor::ValidationError, "jobs:rspec:artifacts expire in should be a duration")
        end

        it "returns errors if job artifacts:untracked is not an array of strings" do
          config = YAML.dump({ types: %w(build test), rspec: { script: "test", artifacts: { untracked: "string" } } })
          expect do
            Gitlab::Ci::YamlProcessor.new(config)
          end.to raise_error(Gitlab::Ci::YamlProcessor::ValidationError, "jobs:rspec:artifacts untracked should be a boolean value")
        end

        it "returns errors if job artifacts:paths is not an array of strings" do
          config = YAML.dump({ types: %w(build test), rspec: { script: "test", artifacts: { paths: "string" } } })
          expect do
            Gitlab::Ci::YamlProcessor.new(config)
          end.to raise_error(Gitlab::Ci::YamlProcessor::ValidationError, "jobs:rspec:artifacts paths should be an array of strings")
        end

        it "returns errors if cache:untracked is not an array of strings" do
          config = YAML.dump({ cache: { untracked: "string" }, rspec: { script: "test" } })
          expect do
            Gitlab::Ci::YamlProcessor.new(config)
          end.to raise_error(Gitlab::Ci::YamlProcessor::ValidationError, "cache:untracked config should be a boolean value")
        end

        it "returns errors if cache:paths is not an array of strings" do
          config = YAML.dump({ cache: { paths: "string" }, rspec: { script: "test" } })
          expect do
            Gitlab::Ci::YamlProcessor.new(config)
          end.to raise_error(Gitlab::Ci::YamlProcessor::ValidationError, "cache:paths config should be an array of strings")
        end

        it "returns errors if cache:key is not a string" do
          config = YAML.dump({ cache: { key: 1 }, rspec: { script: "test" } })
          expect do
            Gitlab::Ci::YamlProcessor.new(config)
2104
          end.to raise_error(Gitlab::Ci::YamlProcessor::ValidationError, "cache:key should be a hash, a string or a symbol")
2105
2106
2107
2108
2109
2110
        end

        it "returns errors if job cache:key is not an a string" do
          config = YAML.dump({ types: %w(build test), rspec: { script: "test", cache: { key: 1 } } })
          expect do
            Gitlab::Ci::YamlProcessor.new(config)
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
          end.to raise_error(Gitlab::Ci::YamlProcessor::ValidationError, "jobs:rspec:cache:key should be a hash, a string or a symbol")
        end

        it 'returns errors if job cache:key:files is not an array of strings' do
          config = YAML.dump({ types: %w(build test), rspec: { script: "test", cache: { key: { files: [1] } } } })
          expect do
            Gitlab::Ci::YamlProcessor.new(config)
          end.to raise_error(Gitlab::Ci::YamlProcessor::ValidationError, 'jobs:rspec:cache:key:files config should be an array of strings')
        end

        it 'returns errors if job cache:key:files is an empty array' do
          config = YAML.dump({ types: %w(build test), rspec: { script: "test", cache: { key: { files: [] } } } })
          expect do
            Gitlab::Ci::YamlProcessor.new(config)
          end.to raise_error(Gitlab::Ci::YamlProcessor::ValidationError, 'jobs:rspec:cache:key:files config requires at least 1 item')
        end

        it 'returns errors if job defines only cache:key:prefix' do
          config = YAML.dump({ types: %w(build test), rspec: { script: "test", cache: { key: { prefix: 'prefix-key' } } } })
          expect do
            Gitlab::Ci::YamlProcessor.new(config)
          end.to raise_error(Gitlab::Ci::YamlProcessor::ValidationError, 'jobs:rspec:cache:key config missing required keys: files')
        end

        it 'returns errors if job cache:key:prefix is not an a string' do
          config = YAML.dump({ types: %w(build test), rspec: { script: "test", cache: { key: { prefix: 1, files: ['file'] } } } })
          expect do
            Gitlab::Ci::YamlProcessor.new(config)
          end.to raise_error(Gitlab::Ci::YamlProcessor::ValidationError, 'jobs:rspec:cache:key:prefix config should be a string or symbol')
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
        end

        it "returns errors if job cache:untracked is not an array of strings" do
          config = YAML.dump({ types: %w(build test), rspec: { script: "test", cache: { untracked: "string" } } })
          expect do
            Gitlab::Ci::YamlProcessor.new(config)
          end.to raise_error(Gitlab::Ci::YamlProcessor::ValidationError, "jobs:rspec:cache:untracked config should be a boolean value")
        end

        it "returns errors if job cache:paths is not an array of strings" do
          config = YAML.dump({ types: %w(build test), rspec: { script: "test", cache: { paths: "string" } } })
          expect do
            Gitlab::Ci::YamlProcessor.new(config)
          end.to raise_error(Gitlab::Ci::YamlProcessor::ValidationError, "jobs:rspec:cache:paths config should be an array of strings")
        end

        it "returns errors if job dependencies is not an array of strings" do
          config = YAML.dump({ types: %w(build test), rspec: { script: "test", dependencies: "string" } })
          expect do
            Gitlab::Ci::YamlProcessor.new(config)
          end.to raise_error(Gitlab::Ci::YamlProcessor::ValidationError, "jobs:rspec dependencies should be an array of strings")
        end
2162

2163
        it 'returns errors if pipeline variables expression policy is invalid' do
2164
2165
2166
2167
          config = YAML.dump({ rspec: { script: 'test', only: { variables: ['== null'] } } })

          expect { Gitlab::Ci::YamlProcessor.new(config) }
            .to raise_error(Gitlab::Ci::YamlProcessor::ValidationError,
2168
                            'jobs:rspec:only variables invalid expression syntax')
2169
        end
2170

2171
2172
2173
2174
2175
2176
2177
2178
        it 'returns errors if pipeline changes policy is invalid' do
          config = YAML.dump({ rspec: { script: 'test', only: { changes: [1] } } })

          expect { Gitlab::Ci::YamlProcessor.new(config) }
            .to raise_error(Gitlab::Ci::YamlProcessor::ValidationError,
                            'jobs:rspec:only changes should be an array of strings')
        end

2179
2180
2181
2182
2183
        it 'returns errors if extended hash configuration is invalid' do
          config = YAML.dump({ rspec: { extends: 'something', script: 'test' } })

          expect { Gitlab::Ci::YamlProcessor.new(config) }
            .to raise_error(Gitlab::Ci::YamlProcessor::ValidationError,
Wolphin's avatar
Wolphin committed
2184
                            'rspec: unknown keys in `extends` (something)')
2185
        end
2186
2187
2188
      end

      describe "#validation_message" do
2189
2190
        subject { Gitlab::Ci::YamlProcessor.validation_message(content) }

2191
        context "when the YAML could not be parsed" do
2192
          let(:content) { YAML.dump("invalid: yaml: test") }
2193

2194
          it { is_expected.to eq "Invalid configuration format" }
2195
2196
2197
        end

        context "when the tags parameter is invalid" do
2198
          let(:content) { YAML.dump({ rspec: { script: "test", tags: "mysql" } }) }
2199

2200
          it { is_expected.to eq "jobs:rspec:tags config should be an array of strings" }
2201
2202
2203
        end

        context "when YAML content is empty" do
2204
          let(:content) { '' }
2205

2206
2207
2208
2209
2210
2211
2212
          it { is_expected.to eq "Please provide content of .gitlab-ci.yml" }
        end

        context 'when the YAML contains an unknown alias' do
          let(:content) { 'steps: *bad_alias' }

          it { is_expected.to eq "Unknown alias: bad_alias" }
2213
2214
2215
        end

        context "when the YAML is valid" do
2216
          let(:content) { File.read(Rails.root.join('spec/support/gitlab_stubs/gitlab_ci.yml')) }
2217

2218
          it { is_expected.to be_nil }
2219
2220
2221
2222
2223
        end
      end
    end
  end
end