Commit 97225551 authored by Kamil Trzcinski's avatar Kamil Trzcinski

Finish tests

parent bfa42940
......@@ -7,6 +7,7 @@ class Projects::EnvironmentsController < Projects::ApplicationController
before_action :authorize_admin_environment!, only: [:terminal, :terminal_websocket_authorize]
before_action :environment, only: [:show, :edit, :update, :stop, :terminal, :terminal_websocket_authorize, :metrics]
before_action :verify_api_request!, only: :terminal_websocket_authorize
before_action :refresh_rollout_status, only: [:index]
def index
@environments = project.environments
......@@ -147,6 +148,10 @@ class Projects::EnvironmentsController < Projects::ApplicationController
Gitlab::Workhorse.verify_api_request!(request.headers)
end
def refresh_rollout_status
environment.rollout_status
end
def environment_params
params.require(:environment).permit(:name, :external_url)
end
......
......@@ -59,6 +59,9 @@ module ReactiveCaching
raise NotImplementedError
end
def reactive_cache_updated(*args)
end
def with_reactive_cache(*args, &blk)
within_reactive_cache_lifetime(*args) do
data = Rails.cache.read(full_reactive_cache_key(*args))
......@@ -79,6 +82,7 @@ module ReactiveCaching
enqueuing_update(*args) do
value = calculate_reactive_cache(*args)
Rails.cache.write(full_reactive_cache_key(*args), value)
reactive_cache_updated(*args)
end
end
end
......
......@@ -6,7 +6,7 @@ module EE
::Gitlab::Kubernetes::RolloutStatus.from_specs(*specs)
end
result || ::Gitlab::Kubernetes::RolloutStatus.loading_status
result || ::Gitlab::Kubernetes::RolloutStatus.loading
end
def calculate_reactive_cache
......@@ -16,6 +16,15 @@ module EE
result
end
def reactive_cache_updated
super
Gitlab::EtagCaching::Store.new.tap do |store|
store.touch(Gitlab::Routing.url_helpers.project_environments_path(
project, format: :json))
end
end
def read_deployments
kubeclient = build_kubeclient!(api_path: 'apis/extensions', api_version: 'v1beta1')
......
......@@ -36,7 +36,7 @@ module Gitlab
new(deployments)
end
def self.loading_rollout
def self.loading
new([], status: :loading)
end
......
......@@ -52,6 +52,10 @@ describe Projects::EnvironmentsController do
get :index, environment_params(format: :json, scope: :available)
end
it 'responds with matching schema' do
expect(response).to match_response_schema('environments')
end
it 'responds with a payload describing available environments' do
expect(environments.count).to eq 2
expect(environments.first['name']).to eq 'production'
......
......@@ -25,5 +25,30 @@ describe KubernetesService, models: true, use_clean_rails_memory_store_caching:
expect(rollout_status.deployments.map(&:labels)).to eq([{ 'app' => 'env-000000' }])
end
end
context 'with empty list of deployments' do
before do
stub_reactive_cache(
service,
deployments: []
)
end
it 'creates a matching RolloutStatus' do
expect(rollout_status).to be_kind_of(::Gitlab::Kubernetes::RolloutStatus)
expect(rollout_status).to be_not_found
end
end
context 'not yet loaded deployments' do
before do
stub_reactive_cache
end
it 'creates a matching RolloutStatus' do
expect(rollout_status).to be_kind_of(::Gitlab::Kubernetes::RolloutStatus)
expect(rollout_status).to be_loading
end
end
end
end
{
"additionalProperties": false,
"properties": {
"created_at": {
"type": "string"
},
"id": {
"type": "integer"
},
"iid": {
"type": "integer"
},
"last?": {
"type": "boolean"
},
"ref": {
"additionalProperties": false,
"properties": {
"name": {
"type": "string"
}
},
"required": [
"name"
],
"type": "object"
},
"sha": {
"type": "string"
},
"tag": {
"type": "boolean"
}
},
"required": [
"sha",
"created_at",
"iid",
"tag",
"last?",
"ref",
"id"
],
"type": "object"
}
......@@ -3,49 +3,7 @@
"properties": {
"deployments": {
"items": {
"additionalProperties": false,
"properties": {
"created_at": {
"type": "string"
},
"id": {
"type": "integer"
},
"iid": {
"type": "integer"
},
"last?": {
"type": "boolean"
},
"ref": {
"additionalProperties": false,
"properties": {
"name": {
"type": "string"
}
},
"required": [
"name"
],
"type": "object"
},
"sha": {
"type": "string"
},
"tag": {
"type": "boolean"
}
},
"required": [
"sha",
"created_at",
"iid",
"tag",
"last?",
"ref",
"id"
],
"type": "object"
"$ref": "deployment.json"
},
"minItems": 1,
"type": "array"
......
{
"type": "object",
"additionalProperties": false,
"required": [
"id",
"name",
"state",
"last_deployment",
"environment_path",
"created_at",
"updated_at"
],
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"state": {
"type": "string"
},
"external_url": {
"type": "string"
},
"environment_type": {
"type": [
"string",
"null"
]
},
"last_deployment": {
"oneOf": [
{
"$ref": "deployment.json"
},
{
"type": ["null"]
}
]
},
"stop_action?": {
"type": "boolean"
},
"rollout_status": {
"$ref": "rollout_status.json"
},
"environment_path": {
"type": "string"
},
"stop_path": {
"type": "string"
},
"terminal_path": {
"type": "string"
},
"folder_path": {
"type": "string"
},
"created_at": {
"type": "date"
},
"updated_at": {
"type": "date"
}
}
}
{
"additionalProperties": false,
"properties": {
"environments": {
"items": {
"$ref": "environments_group.json"
},
"minItems": 1,
"type": "array"
},
"available_count": {
"type": "integer"
},
"stopped_count": {
"type": "integer"
}
},
"required": [
"environments",
"available_count",
"stopped_count"
],
"type": "object"
}
{
"type": "object",
"required": [
"name",
"size",
"latest"
],
"additionalProperties": false,
"properties": {
"name": {
"type": "string"
},
"size": {
"type": "integer"
},
"latest": {
"$ref": "environment.json"
}
}
}
{
"type": "object",
"additionalProperties": false,
"required": [
"status"
],
"properties": {
"status": {
"type": "string"
},
"completion": {
"type": "integer"
},
"is_completed": {
"type": "boolean"
},
"instances": {
"type": "array",
"items": {
"additionalProperties": false,
"type": "object",
"required": [
"status",
"tooltip",
"track",
"stable"
],
"properties": {
"status": {
"type": "string"
},
"tooltip": {
"type": "string"
},
"track": {
"type": "string"
},
"stable": {
"type": "boolean"
}
}
}
}
}
}
......@@ -94,12 +94,24 @@ describe Gitlab::Kubernetes::RolloutStatus do
end
end
describe '#not_found?' do
context 'when the specs are passed' do
it { is_expected.not_to be_not_found }
end
context 'when list of specs is empty' do
let(:specs) { specs_none }
it { is_expected.to be_not_found }
end
end
describe '#found?' do
context 'when the specs are passed' do
it { is_expected.to be_found }
end
context 'when no specs are passed' do
context 'when list of specs is empty' do
let(:specs) { specs_none }
it { is_expected.not_to be_found }
......
......@@ -115,6 +115,12 @@ describe ReactiveCaching, :use_clean_rails_memory_store_caching do
go!
end
it "calls a reactive_cache_updated after updated" do
expect(instance).to receive(:reactive_cache_updated)
go!
end
context 'and #calculate_reactive_cache raises an exception' do
before do
stub_reactive_cache(instance, "preexisting")
......
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