Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deployment unit tests #236

Merged
merged 1 commit into from
Jan 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"

---
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this should be two files.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thinking is to have one fixture file per unit test file so it's clear what they're intended for, but admittedly I have no idea if that will meet our real needs when we have more of these. Another reason to keep them together is that they collectively represent a single deployed resource (the Deployment), including the cluster-side results (the RS) of shipping that resource. So they actually contain cross references (ownerRef, uid).

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