From b58d56ff5db7d364debb0077f7492d9a10c1e85d Mon Sep 17 00:00:00 2001 From: Daniel Turner Date: Thu, 11 Jan 2018 09:10:29 -0800 Subject: [PATCH] # This is a combination of 2 commits. # This is the 1st commit message: New tests # This is the commit message #2: Rework restart test --- test/fixtures/slow-cloud/web.yml.erb | 19 +++++++-------- test/integration/kubernetes_deploy_test.rb | 22 ++++++++++++++---- test/integration/restart_task_test.rb | 27 +++++++++++++--------- 3 files changed, 41 insertions(+), 27 deletions(-) diff --git a/test/fixtures/slow-cloud/web.yml.erb b/test/fixtures/slow-cloud/web.yml.erb index 12c233599..52f5ca8c9 100644 --- a/test/fixtures/slow-cloud/web.yml.erb +++ b/test/fixtures/slow-cloud/web.yml.erb @@ -6,32 +6,29 @@ metadata: shipit.shopify.io/restart: "true" kubernetes-deploy.shopify.io/required-rollout: maxUnavailable spec: - replicas: 3 + replicas: 2 + selector: + matchLabels: + name: web + app: slow-cloud strategy: type: RollingUpdate rollingUpdate: - maxSurge: 1 + maxSurge: 0 maxUnavailable: 1 template: metadata: labels: name: web app: slow-cloud + sha: "<%= current_sha %>" spec: + terminationGracePeriodSeconds: 0 containers: - name: app - readinessProbe: - exec: - command: - - sleep - - '7' - timeoutSeconds: 10 image: busybox imagePullPolicy: IfNotPresent command: ["tail", "-f", "/dev/null"] ports: - containerPort: 80 name: http - env: - - name: GITHUB_REV - value: <%= current_sha %> diff --git a/test/integration/kubernetes_deploy_test.rb b/test/integration/kubernetes_deploy_test.rb index 30ac70f7d..3239fb8b8 100644 --- a/test/integration/kubernetes_deploy_test.rb +++ b/test/integration/kubernetes_deploy_test.rb @@ -689,15 +689,27 @@ def test_can_deploy_deployment_with_zero_replicas end def test_deploy_successful_with_partial_availability - result = deploy_fixtures("slow-cloud") + result = deploy_fixtures("slow-cloud", sha: "deploy1") assert_deploy_success(result) - result = deploy_fixtures("slow-cloud") + result = deploy_fixtures("slow-cloud", sha: "deploy2") do |fixtures| + dep = fixtures["web.yml.erb"]["Deployment"].first + container = dep["spec"]["template"]["spec"]["containers"].first + container["readinessProbe"] = { + "exec" => { "command" => %w(sleep 5) }, + "timeoutSeconds" => 6 + } + end assert_deploy_success(result) - assert_logs_match_all( - [%r{Deployment\/web\s+[34] replicas, 3 updatedReplicas, 2 availableReplicas, [12] unavailableReplica}] - ) + new_pods = kubeclient.get_pods(namespace: @namespace, label_selector: 'name=web,app=slow-cloud,sha=deploy2') + assert new_pods.length >= 1, "Expected at least one new pod, saw #{new_pods.length}" + + new_ready_pods = new_pods.select do |pod| + pod.status.phase == "Running" && + pod.status.conditions.any? { |condition| condition["type"] == "Ready" && condition["status"] == "True" } + end + assert_equal 1, new_ready_pods.length, "Expected exactly one new pod to be ready, saw #{new_ready_pods.length}" end def test_deploy_aborts_immediately_if_metadata_name_missing diff --git a/test/integration/restart_task_test.rb b/test/integration/restart_task_test.rb index 4cb67dbb2..9626a17b3 100644 --- a/test/integration/restart_task_test.rb +++ b/test/integration/restart_task_test.rb @@ -209,23 +209,28 @@ def test_restart_failure def test_restart_successful_with_partial_availability result = deploy_fixtures("slow-cloud") do |fixtures| web = fixtures["web.yml.erb"]["Deployment"].first - web["spec"]["strategy"]['rollingUpdate']['maxUnavailable'] = '34%' + web["spec"]["strategy"]['rollingUpdate']['maxUnavailable'] = '50%' + container = web["spec"]["template"]["spec"]["containers"].first + container["readinessProbe"] = { + "exec" => { "command" => %w(sleep 5) }, + "timeoutSeconds" => 6 + } end assert_deploy_success(result) restart = build_restart_task assert_restart_success(restart.perform(["web"])) - assert_logs_match_all([ - "Configured to restart deployments by name: web", - "Triggered `web` restart", - "Waiting for rollout", - %r{Successfully restarted in \d+\.\d+s: Deployment/web}, - "Result: SUCCESS", - "Successfully restarted 1 resource", - %r{Deployment\/web\s+[34] replicas, 3 updatedReplicas, 2 availableReplicas, [12] unavailableReplica} - ], - in_order: true) + pods = kubeclient.get_pods(namespace: @namespace, label_selector: 'name=web,app=slow-cloud') + new_pods = pods.select { |pod| pod.spec.containers.select { |c| c["name"] == "app" && c.env&.find { |n| n.name == "RESTARTED_AT" } } } + assert new_pods.length >= 1, "Expected at least one new pod, saw #{new_pods.length}" + + new_ready_pods = new_pods.select do |pod| + pod.status.phase == "Running" && + pod.status.conditions.any? { |condition| condition["type"] == "Ready" && condition["status"] == "True" } && + pod.spec.containers.detect { |c| c["name"] == "app" && c.env&.find { |n| n.name == "RESTARTED_AT" } } + end + assert_equal 1, new_ready_pods.length, "Expected exactly one new pod to be ready, saw #{new_ready_pods.length}" assert fetch_restarted_at("web"), "RESTARTED_AT is present after the restart" end