Commit 1c702f54 authored by Sean McGivern's avatar Sean McGivern

Merge branch 'helm_ls_parser' into 'master'

Add parser for helm list

See merge request gitlab-org/gitlab!31733
parents f8ed6ad8 9f40e3cb
# frozen_string_literal: true
module Gitlab
module Kubernetes
module Helm
module Parsers
# Parses Helm v2 list (JSON) output
class ListV2
ParserError = Class.new(StandardError)
attr_reader :contents, :json
def initialize(contents)
@contents = contents
@json = Gitlab::Json.parse(contents)
rescue JSON::ParserError => e
raise ParserError, e.message
end
def releases
@releases ||= json["Releases"] || []
end
end
end
end
end
end
# frozen_string_literal: true
require 'fast_spec_helper'
describe Gitlab::Kubernetes::Helm::Parsers::ListV2 do
let(:valid_file_contents) do
<<~EOF
{
"Next": "",
"Releases": [
{
"Name": "certmanager",
"Revision": 2,
"Updated": "Sun Mar 29 06:55:42 2020",
"Status": "DEPLOYED",
"Chart": "cert-manager-v0.10.1",
"AppVersion": "v0.10.1",
"Namespace": "gitlab-managed-apps"
},
{
"Name": "certmanager-crds",
"Revision": 2,
"Updated": "Sun Mar 29 06:55:32 2020",
"Status": "DEPLOYED",
"Chart": "cert-manager-crds-v0.2.0",
"AppVersion": "release-0.10",
"Namespace": "gitlab-managed-apps"
},
{
"Name": "certmanager-issuer",
"Revision": 1,
"Updated": "Tue Feb 18 10:04:04 2020",
"Status": "FAILED",
"Chart": "cert-manager-issuer-v0.1.0",
"AppVersion": "",
"Namespace": "gitlab-managed-apps"
},
{
"Name": "runner",
"Revision": 2,
"Updated": "Sun Mar 29 07:01:01 2020",
"Status": "DEPLOYED",
"Chart": "gitlab-runner-0.14.0",
"AppVersion": "12.8.0",
"Namespace": "gitlab-managed-apps"
}
]
}
EOF
end
describe '#initialize' do
it 'initializes without error' do
expect do
described_class.new(valid_file_contents)
end.not_to raise_error
end
it 'raises an error on invalid JSON' do
expect do
described_class.new('')
end.to raise_error(described_class::ParserError, 'A JSON text must at least contain two octets!')
end
end
describe '#releases' do
subject(:list_v2) { described_class.new(valid_file_contents) }
it 'returns list of releases' do
expect(list_v2.releases).to match([
a_hash_including('Name' => 'certmanager', 'Status' => 'DEPLOYED'),
a_hash_including('Name' => 'certmanager-crds', 'Status' => 'DEPLOYED'),
a_hash_including('Name' => 'certmanager-issuer', 'Status' => 'FAILED'),
a_hash_including('Name' => 'runner', 'Status' => 'DEPLOYED')
])
end
context 'empty Releases' do
let(:valid_file_contents) { '{}' }
it 'returns an empty array' do
expect(list_v2.releases).to eq([])
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment