Skip to content

Commit

Permalink
Unit testing with simulated sync for Deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
KnVerey committed Jan 10, 2018
1 parent 8f46ab3 commit d6004c1
Show file tree
Hide file tree
Showing 3 changed files with 368 additions and 89 deletions.
19 changes: 11 additions & 8 deletions lib/kubernetes-deploy/kubernetes_resource/deployment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ def deploy_succeeded?
@latest_rs.ready_replicas >= minimum_needed &&
@latest_rs.available_replicas >= minimum_needed
else
raise "#{REQUIRED_ROLLOUT_ANNOTATION}:#{required_rollout} is invalid "\
" Acceptable options: #{REQUIRED_ROLLOUT_TYPES.join(',')}"
raise FatalDeploymentError, rollout_annotation_err_msg
end
end

Expand Down Expand Up @@ -104,21 +103,25 @@ def validate_definition
super

unless REQUIRED_ROLLOUT_TYPES.include?(required_rollout)
@validation_errors << "#{REQUIRED_ROLLOUT_ANNOTATION}:#{required_rollout} is invalid "\
"Acceptable options: #{REQUIRED_ROLLOUT_TYPES.join(',')}"
@validation_errors << rollout_annotation_err_msg
end

if required_rollout.downcase == 'maxunavailable' && @definition.dig('spec', 'strategy').respond_to?(:downcase) &&
@definition.dig('spec', 'strategy').downcase == 'recreate'
@validation_errors << "#{REQUIRED_ROLLOUT_ANNOTATION}:#{required_rollout} is invalid "\
"with strategy 'rollingUpdate'"
strategy = @definition.dig('spec', 'strategy', 'type').to_s
if required_rollout.downcase == 'maxunavailable' && strategy.downcase != 'rollingupdate'
@validation_errors << "'#{REQUIRED_ROLLOUT_ANNOTATION}: #{required_rollout}' is incompatible "\
"with strategy '#{strategy}'"
end

@validation_errors.empty?
end

private

def rollout_annotation_err_msg
"'#{REQUIRED_ROLLOUT_ANNOTATION}: #{required_rollout}' is invalid. "\
"Acceptable values: #{REQUIRED_ROLLOUT_TYPES.join(', ')}"
end

def deploy_failing_to_progress?
return false unless @progress_condition.present?

Expand Down
62 changes: 62 additions & 0 deletions test/fixtures/for_unit_tests/deployment_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: web
uid: foobar
annotations:
"deployment.kubernetes.io/revision": "1"
spec:
replicas: 3
progressDeadlineSeconds: 10
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
selector:
matchLabels:
name: web
app: hello-cloud
template:
metadata:
labels:
name: web
app: hello-cloud
spec:
containers:
- name: app
image: busybox
status:
replicas: 3
conditions:
- type: Progressing
status: True
lastUpdateTime: "2018-01-09 22:56:45 UTC"

---
apiVersion: apps/v1beta1
kind: ReplicaSet
metadata:
name: web-1
annotations:
"deployment.kubernetes.io/revision": "1"
ownerReferences:
- uid: foobar
spec:
replicas: 3
selector:
matchLabels:
name: web
app: hello-cloud
template:
metadata:
labels:
name: web
app: hello-cloud
spec:
containers:
- name: app
image: busybox
status:
replicas: 3
Loading

0 comments on commit d6004c1

Please sign in to comment.