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

Add specs for tracks

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