-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
293 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
test/fixtures/for_unit_tests/pod_disruption_budget_test.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--- | ||
apiVersion: policy/v1beta1 | ||
kind: PodDisruptionBudget | ||
metadata: | ||
name: test | ||
generation: 2 | ||
spec: | ||
minAvailable: 2 | ||
selector: | ||
matchLabels: | ||
name: web | ||
app: hello-cloud | ||
status: | ||
observedGeneration: 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
apiVersion: extensions/v1beta1 | ||
kind: ReplicaSet | ||
metadata: | ||
name: test | ||
generation: 2 | ||
spec: | ||
replicas: 3 | ||
selector: | ||
matchLabels: | ||
app: hello-cloud | ||
name: test | ||
template: | ||
metadata: | ||
labels: | ||
app: hello-cloud | ||
name: test | ||
spec: | ||
containers: | ||
- name: app | ||
image: busybox | ||
imagePullPolicy: IfNotPresent | ||
command: ["tail", "-f", "/dev/null"] | ||
status: | ||
observedGeneration: 2 | ||
availableReplicas: 3 | ||
readyReplicas: 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
apiVersion: apps/v1beta1 | ||
kind: StatefulSet | ||
metadata: | ||
name: test-ss | ||
generation: 2 | ||
spec: | ||
selector: | ||
matchLabels: | ||
name: test-ss | ||
app: hello-cloud | ||
serviceName: "test-ss" | ||
updateStrategy: | ||
type: RollingUpdate | ||
replicas: 2 | ||
template: | ||
metadata: | ||
labels: | ||
app: hello-cloud | ||
name: test-ss | ||
spec: | ||
containers: | ||
- name: app | ||
image: busybox | ||
imagePullPolicy: IfNotPresent | ||
command: ["tail", "-f", "/dev/null"] | ||
status: | ||
replicas: 2 | ||
readyReplicas: 2 | ||
currentReplicas: 2 | ||
observedGeneration: 2 | ||
currentRevision: 2 | ||
updateRevision: 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
test/unit/kubernetes-deploy/kubernetes_resource/pod_disruption_budget_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
require 'test_helper' | ||
|
||
class PodDisruptionBudgetTest < KubernetesDeploy::TestCase | ||
def setup | ||
KubernetesDeploy::Kubectl.any_instance.expects(:run).never | ||
super | ||
end | ||
|
||
def test_deploy_succeeded_is_true_as_soon_as_controller_observes_new_version | ||
template = build_pdb_template(status: { "observedGeneration": 2 }) | ||
pdb = build_synced_pdb(template: template) | ||
assert pdb.deploy_succeeded? | ||
end | ||
|
||
def test_deploy_succeeded_not_fooled_by_stale_status | ||
template = build_pdb_template(status: { "observedGeneration": 1 }) | ||
pdb = build_synced_pdb(template: template) | ||
refute pdb.deploy_succeeded? | ||
end | ||
|
||
private | ||
|
||
def build_pdb_template(status: {}) | ||
pdb_fixture.dup.deep_merge("status" => status) | ||
end | ||
|
||
def build_synced_pdb(template:) | ||
pdb = KubernetesDeploy::PodDisruptionBudget.new(namespace: "test", context: "nope", | ||
logger: logger, definition: template) | ||
sync_mediator = KubernetesDeploy::SyncMediator.new(namespace: 'test', context: 'minikube', logger: logger) | ||
sync_mediator.kubectl.expects(:run).with("get", "PodDisruptionBudget", "test", "-a", "--output=json").returns( | ||
[template.to_json, "", SystemExit.new(0)] | ||
) | ||
pdb.sync(sync_mediator) | ||
pdb | ||
end | ||
|
||
def pdb_fixture | ||
@pdb_fixture ||= YAML.load_stream( | ||
File.read(File.join(fixture_path('for_unit_tests'), 'pod_disruption_budget_test.yml')) | ||
).find { |fixture| fixture["kind"] == "PodDisruptionBudget" } | ||
end | ||
end |
53 changes: 53 additions & 0 deletions
53
test/unit/kubernetes-deploy/kubernetes_resource/replica_set_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
require 'test_helper' | ||
|
||
class ReplicaSetTest < KubernetesDeploy::TestCase | ||
def setup | ||
KubernetesDeploy::Kubectl.any_instance.expects(:run).never | ||
super | ||
end | ||
|
||
def test_deploy_succeeded_is_true_when_generation_and_replica_counts_match | ||
template = build_rs_template(status: { "observedGeneration": 2 }) | ||
rs = build_synced_rs(template: template) | ||
assert rs.deploy_succeeded? | ||
end | ||
|
||
def test_deploy_succeeded_not_fooled_by_stale_status | ||
template = build_rs_template(status: { "observedGeneration": 1 }) | ||
rs = build_synced_rs(template: template) | ||
refute rs.deploy_succeeded? | ||
end | ||
|
||
def test_deploy_failed_ensures_controller_has_observed_deploy | ||
template = build_rs_template(status: { "observedGeneration": 1 }) | ||
rs = build_synced_rs(template: template) | ||
rs.stubs(:pods).returns([stub(deploy_failed?: true)]) | ||
refute rs.deploy_failed? | ||
end | ||
|
||
private | ||
|
||
def build_rs_template(status: {}) | ||
rs_fixture.dup.deep_merge("status" => status) | ||
end | ||
|
||
def build_synced_rs(template:) | ||
rs = KubernetesDeploy::ReplicaSet.new(namespace: "test", context: "nope", logger: logger, definition: template) | ||
sync_mediator = KubernetesDeploy::SyncMediator.new(namespace: 'test', context: 'minikube', logger: logger) | ||
sync_mediator.kubectl.expects(:run).with("get", "ReplicaSet", "test", "-a", "--output=json").returns( | ||
[template.to_json, "", SystemExit.new(0)] | ||
) | ||
sync_mediator.kubectl.expects(:run).with("get", "Pod", "-a", "--output=json", anything).returns( | ||
['{ "items": [] }', "", SystemExit.new(0)] | ||
) | ||
rs.sync(sync_mediator) | ||
rs | ||
end | ||
|
||
def rs_fixture | ||
@rs_fixture ||= YAML.load_stream( | ||
File.read(File.join(fixture_path('for_unit_tests'), 'replica_set_test.yml')) | ||
).find { |fixture| fixture["kind"] == "ReplicaSet" } | ||
end | ||
end |
Oops, something went wrong.