Commit 5470f02c authored by Kamil Trzcinski's avatar Kamil Trzcinski

Add specs for tracks

parent 83b6714d
......@@ -10,15 +10,15 @@ module Gitlab
end
def labels
metadata['labels']
metadata.fetch('labels', {})
end
def track
labels.fetch('track', 'stable')
labels.fetch('track') || 'stable'
end
def stable?
track.nil? || track == 'stable'
track == 'stable'
end
def outdated?
......
......@@ -5,60 +5,70 @@ describe Gitlab::Kubernetes::Deployment do
describe '#name' do
let(:params) { named(:selected) }
it { expect(deployment.name).to eq(:selected) }
end
describe '#labels' do
let(:params) { make('metadata', 'labels' => :selected) }
it { expect(deployment.labels).to eq(:selected) }
end
describe '#outdated?' do
context 'when outdated' do
let(:params) { generation(2, 1) }
it { expect(deployment.outdated?).to be_truthy }
end
context 'when up to date' do
let(:params) { generation(2, 2) }
it { expect(deployment.outdated?).to be_falsy }
end
context 'when ahead of latest' do
let(:params) { generation(1, 2) }
it { expect(deployment.outdated?).to be_falsy }
end
end
describe '#wanted_replicas' do
let(:params) { make('spec', 'replicas' => :selected ) }
it { expect(deployment.wanted_replicas).to eq(:selected) }
end
describe '#finished_replicas' do
let(:params) { make('status', 'availableReplicas' => :selected) }
it { expect(deployment.finished_replicas).to eq(:selected) }
end
describe '#deploying_replicas' do
let(:params) { make('status', 'availableReplicas' => 2, 'updatedReplicas' => 4) }
it { expect(deployment.deploying_replicas).to eq(2) }
end
describe '#waiting_replicas' do
let(:params) { combine(make('spec', 'replicas' => 4), make('status', 'updatedReplicas' => 2)) }
it { expect(deployment.waiting_replicas).to eq(2) }
end
describe '#instances' do
context 'when unnamed' do
let(:params) { combine(generation(1, 1), instances) }
it 'returns all instances as unknown and waiting' do
expected = [
{ status: 'waiting', tooltip: 'unknown (pod 0) Waiting' },
{ status: 'waiting', tooltip: 'unknown (pod 1) Waiting' },
{ status: 'waiting', tooltip: 'unknown (pod 2) Waiting' },
{ status: 'waiting', tooltip: 'unknown (pod 3) Waiting' },
{ status: 'waiting', tooltip: 'unknown (pod 0) Waiting', track: 'stable', stable: true },
{ status: 'waiting', tooltip: 'unknown (pod 1) Waiting', track: 'stable', stable: true },
{ status: 'waiting', tooltip: 'unknown (pod 2) Waiting', track: 'stable', stable: true },
{ status: 'waiting', tooltip: 'unknown (pod 3) Waiting', track: 'stable', stable: true },
]
expect(deployment.instances).to eq(expected)
......@@ -67,12 +77,13 @@ describe Gitlab::Kubernetes::Deployment do
context 'when outdated' do
let(:params) { combine(named('foo'), generation(1, 0), instances) }
it 'returns all instances as named and waiting' do
expected = [
{ status: 'waiting', tooltip: 'foo (pod 0) Waiting' },
{ status: 'waiting', tooltip: 'foo (pod 1) Waiting' },
{ status: 'waiting', tooltip: 'foo (pod 2) Waiting' },
{ status: 'waiting', tooltip: 'foo (pod 3) Waiting' },
{ status: 'waiting', tooltip: 'foo (pod 0) Waiting', track: 'stable', stable: true },
{ status: 'waiting', tooltip: 'foo (pod 1) Waiting', track: 'stable', stable: true },
{ status: 'waiting', tooltip: 'foo (pod 2) Waiting', track: 'stable', stable: true },
{ status: 'waiting', tooltip: 'foo (pod 3) Waiting', track: 'stable', stable: true },
]
expect(deployment.instances).to eq(expected)
......@@ -84,15 +95,44 @@ describe Gitlab::Kubernetes::Deployment do
it 'returns all instances' do
expected = [
{ status: 'finished', tooltip: 'foo (pod 0) Finished' },
{ status: 'deploying', tooltip: 'foo (pod 1) Deploying' },
{ status: 'waiting', tooltip: 'foo (pod 2) Waiting' },
{ status: 'waiting', tooltip: 'foo (pod 3) Waiting' },
{ status: 'finished', tooltip: 'foo (pod 0) Finished', track: 'stable', stable: true },
{ status: 'deploying', tooltip: 'foo (pod 1) Deploying', track: 'stable', stable: true },
{ status: 'waiting', tooltip: 'foo (pod 2) Waiting', track: 'stable', stable: true },
{ status: 'waiting', tooltip: 'foo (pod 3) Waiting', track: 'stable', stable: true },
]
expect(deployment.instances).to eq(expected)
end
end
context 'with track label' do
let(:labels) { { 'track' => track } }
let(:params) { combine(named('foo'), generation(1, 0), instances(1, 1, 1, labels)) }
context 'when marked as stable' do
let(:track) { 'stable' }
it 'returns all instances' do
expected = [
{ status: 'waiting', tooltip: 'foo (pod 0) Waiting', track: 'stable', stable: true },
]
expect(deployment.instances).to eq(expected)
end
end
context 'when marked as canary' do
let(:track) { 'canary' }
it 'returns all instances' do
expected = [
{ status: 'waiting', tooltip: 'foo (pod 0) Waiting', track: 'canary', stable: false },
]
expect(deployment.instances).to eq(expected)
end
end
end
end
def generation(expected, observed)
......@@ -102,14 +142,15 @@ describe Gitlab::Kubernetes::Deployment do
)
end
def named(name = "foo")
make('metadata', 'name' => name)
def named(name = "foo", labels = {})
make('metadata', 'name' => name, 'labels' => labels)
end
def instances
def instances(replicas = 4, available = 1, updated = 2, labels = {})
combine(
make('spec', 'replicas' => 4),
make('status', 'availableReplicas' => 1, 'updatedReplicas' => 2),
named('foo', labels),
make('spec', 'replicas' => replicas),
make('status', 'availableReplicas' => available, 'updatedReplicas' => updated),
)
end
......
......@@ -2,16 +2,25 @@ require 'spec_helper'
describe Gitlab::Kubernetes::RolloutStatus do
include KubernetesHelpers
let(:specs_all_finished) { [kube_deployment(name: 'one'), kube_deployment(name: 'two')] }
let(:specs_half_finished) do
let(:track) { nil }
let(:specs) { specs_all_finished }
let(:specs_none) { [] }
let(:specs_all_finished) do
[
kube_deployment(name: 'one'),
kube_deployment(name: 'two').deep_merge('status' => { 'availableReplicas' => 0 })
kube_deployment(name: 'two', track: track)
]
end
let(:specs) { specs_all_finished }
let(:specs_none) { [] }
let(:specs_half_finished) do
[
kube_deployment(name: 'one'),
kube_deployment(name: 'two', track: track)
.deep_merge('status' => { 'availableReplicas' => 0 })
]
end
subject(:rollout_status) { described_class.from_specs(*specs) }
......@@ -24,17 +33,36 @@ describe Gitlab::Kubernetes::RolloutStatus do
end
describe '#instances' do
it 'stores the union of deployment instances' do
expected = [
{ status: 'finished', tooltip: 'one (pod 0) Finished' },
{ status: 'finished', tooltip: 'one (pod 1) Finished' },
{ status: 'finished', tooltip: 'one (pod 2) Finished' },
{ status: 'finished', tooltip: 'two (pod 0) Finished' },
{ status: 'finished', tooltip: 'two (pod 1) Finished' },
{ status: 'finished', tooltip: 'two (pod 2) Finished' },
]
expect(rollout_status.instances).to eq(expected)
context 'for stable track' do
it 'stores the union of deployment instances' do
expected = [
{ status: 'finished', tooltip: 'one (pod 0) Finished', track: 'stable', stable: true },
{ status: 'finished', tooltip: 'one (pod 1) Finished', track: 'stable', stable: true },
{ status: 'finished', tooltip: 'one (pod 2) Finished', track: 'stable', stable: true },
{ status: 'finished', tooltip: 'two (pod 0) Finished', track: 'stable', stable: true },
{ status: 'finished', tooltip: 'two (pod 1) Finished', track: 'stable', stable: true },
{ status: 'finished', tooltip: 'two (pod 2) Finished', track: 'stable', stable: true },
]
expect(rollout_status.instances).to eq(expected)
end
end
context 'for stable track' do
let(:track) { 'canary' }
it 'stores the union of deployment instances' do
expected = [
{ status: 'finished', tooltip: 'one (pod 0) Finished', track: 'stable', stable: true },
{ status: 'finished', tooltip: 'one (pod 1) Finished', track: 'stable', stable: true },
{ status: 'finished', tooltip: 'one (pod 2) Finished', track: 'stable', stable: true },
{ status: 'finished', tooltip: 'two (pod 0) Finished', track: 'canary', stable: false },
{ status: 'finished', tooltip: 'two (pod 1) Finished', track: 'canary', stable: false },
{ status: 'finished', tooltip: 'two (pod 2) Finished', track: 'canary', stable: false },
]
expect(rollout_status.instances).to eq(expected)
end
end
end
......@@ -47,6 +75,7 @@ describe Gitlab::Kubernetes::RolloutStatus do
context 'when half of the instances are finished' do
let(:specs) { specs_half_finished }
it { is_expected.to eq(50) }
end
end
......@@ -60,6 +89,7 @@ describe Gitlab::Kubernetes::RolloutStatus do
context 'when half of the instances are finished' do
let(:specs) { specs_half_finished }
it { is_expected.to be_falsy}
end
end
......
......@@ -85,12 +85,15 @@ module KubernetesHelpers
}
end
def kube_deployment(name: "kube-deployment", app: "valid-deployment-label")
def kube_deployment(name: "kube-deployment", app: "valid-deployment-label", track: nil)
{
"metadata" => {
"name" => name,
"generation" => 4,
"labels" => { "app" => app },
"labels" => {
"app" => app,
"track" => track
},
},
"spec" => { "replicas" => 3 },
"status" => {
......
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