|
| 1 | +# shellcheck shell=bash |
| 2 | + |
| 3 | +Describe "MongoDB sharding topology provision order" |
| 4 | + |
| 5 | + verify_sharding_provision_order() { |
| 6 | + local chart_dir rendered render_rc |
| 7 | + |
| 8 | + chart_dir=$(cd .. && pwd) |
| 9 | + rendered=$(helm template kb-addon-mongodb "$chart_dir" --dependency-update) |
| 10 | + render_rc=$? |
| 11 | + if [ "$render_rc" -ne 0 ]; then |
| 12 | + echo "helm template failed with status $render_rc" >&2 |
| 13 | + return "$render_rc" |
| 14 | + fi |
| 15 | + |
| 16 | + # shellcheck disable=SC2016 |
| 17 | + printf '%s\n' "$rendered" | ruby -ryaml -e ' |
| 18 | + documents = YAML.load_stream($stdin.read).compact |
| 19 | + definitions = documents.select do |document| |
| 20 | + document["kind"] == "ClusterDefinition" && |
| 21 | + document.dig("metadata", "name") == "mongodb" |
| 22 | + end |
| 23 | + abort "expected one MongoDB ClusterDefinition, got #{definitions.length}" unless definitions.length == 1 |
| 24 | +
|
| 25 | + topologies = definitions.first.dig("spec", "topologies") || [] |
| 26 | + shardings = topologies.select { |topology| topology["name"] == "sharding" } |
| 27 | + abort "expected one MongoDB sharding topology, got #{shardings.length}" unless shardings.length == 1 |
| 28 | + sharding = shardings.first |
| 29 | +
|
| 30 | + component_names = (sharding["components"] || []).map { |component| component["name"] } |
| 31 | + sharding_names = (sharding["shardings"] || []).map { |entry| entry["name"] } |
| 32 | + expected_members = ["config-server", "mongos", "shard"] |
| 33 | + actual_members = (component_names + sharding_names).sort |
| 34 | + abort "unexpected sharding members: #{actual_members.inspect}" unless actual_members == expected_members.sort |
| 35 | +
|
| 36 | + expected_order = ["config-server", "mongos", "shard"] |
| 37 | + actual_order = sharding.dig("orders", "provision") |
| 38 | + abort "expected provision order #{expected_order.inspect}, got #{actual_order.inspect}" unless actual_order == expected_order |
| 39 | +
|
| 40 | + expected_terminate = ["shard", "mongos,config-server"] |
| 41 | + actual_terminate = sharding.dig("orders", "terminate") |
| 42 | + abort "expected terminate order #{expected_terminate.inspect}, got #{actual_terminate.inspect}" unless actual_terminate == expected_terminate |
| 43 | +
|
| 44 | + expected_update = ["mongos,config-server", "shard"] |
| 45 | + actual_update = sharding.dig("orders", "update") |
| 46 | + abort "expected update order #{expected_update.inspect}, got #{actual_update.inspect}" unless actual_update == expected_update |
| 47 | + ' |
| 48 | + } |
| 49 | + |
| 50 | + verify_renderer_failure_is_propagated() { |
| 51 | + local fake_bin test_status |
| 52 | + |
| 53 | + fake_bin=$(mktemp -d) |
| 54 | + cat > "$fake_bin/helm" <<'FAKE_HELM' |
| 55 | +#!/bin/sh |
| 56 | +cat <<'YAML' |
| 57 | +apiVersion: apps.kubeblocks.io/v1 |
| 58 | +kind: ClusterDefinition |
| 59 | +metadata: |
| 60 | + name: mongodb |
| 61 | +spec: |
| 62 | + topologies: |
| 63 | + - name: sharding |
| 64 | + components: |
| 65 | + - name: mongos |
| 66 | + - name: config-server |
| 67 | + shardings: |
| 68 | + - name: shard |
| 69 | + orders: |
| 70 | + provision: |
| 71 | + - config-server |
| 72 | + - mongos |
| 73 | + - shard |
| 74 | + terminate: |
| 75 | + - shard |
| 76 | + - mongos,config-server |
| 77 | + update: |
| 78 | + - mongos,config-server |
| 79 | + - shard |
| 80 | +YAML |
| 81 | +exit 42 |
| 82 | +FAKE_HELM |
| 83 | + chmod +x "$fake_bin/helm" |
| 84 | + |
| 85 | + PATH="$fake_bin:$PATH" verify_sharding_provision_order |
| 86 | + test_status=$? |
| 87 | + rm -rf "$fake_bin" |
| 88 | + return "$test_status" |
| 89 | + } |
| 90 | + |
| 91 | + verify_duplicate_sharding_topology_is_rejected() { |
| 92 | + local fake_bin test_status |
| 93 | + |
| 94 | + fake_bin=$(mktemp -d) |
| 95 | + cat > "$fake_bin/helm" <<'FAKE_HELM' |
| 96 | +#!/bin/sh |
| 97 | +cat <<'YAML' |
| 98 | +apiVersion: apps.kubeblocks.io/v1 |
| 99 | +kind: ClusterDefinition |
| 100 | +metadata: |
| 101 | + name: mongodb |
| 102 | +spec: |
| 103 | + topologies: |
| 104 | + - name: sharding |
| 105 | + components: |
| 106 | + - name: mongos |
| 107 | + - name: config-server |
| 108 | + shardings: |
| 109 | + - name: shard |
| 110 | + orders: |
| 111 | + provision: |
| 112 | + - config-server |
| 113 | + - mongos |
| 114 | + - shard |
| 115 | + terminate: |
| 116 | + - shard |
| 117 | + - mongos,config-server |
| 118 | + update: |
| 119 | + - mongos,config-server |
| 120 | + - shard |
| 121 | + - name: sharding |
| 122 | + components: |
| 123 | + - name: unexpected |
| 124 | + orders: |
| 125 | + provision: |
| 126 | + - unexpected |
| 127 | +YAML |
| 128 | +FAKE_HELM |
| 129 | + chmod +x "$fake_bin/helm" |
| 130 | + |
| 131 | + PATH="$fake_bin:$PATH" verify_sharding_provision_order |
| 132 | + test_status=$? |
| 133 | + rm -rf "$fake_bin" |
| 134 | + return "$test_status" |
| 135 | + } |
| 136 | + |
| 137 | + It "provisions config server, mongos, then shards" |
| 138 | + When call verify_sharding_provision_order |
| 139 | + The status should be success |
| 140 | + End |
| 141 | + |
| 142 | + It "propagates a renderer failure after valid output" |
| 143 | + When call verify_renderer_failure_is_propagated |
| 144 | + The stderr should include "helm template failed with status 42" |
| 145 | + The status should equal 42 |
| 146 | + End |
| 147 | + |
| 148 | + It "rejects duplicate sharding topologies" |
| 149 | + When call verify_duplicate_sharding_topology_is_rejected |
| 150 | + The stderr should include "expected one MongoDB sharding topology, got 2" |
| 151 | + The status should be failure |
| 152 | + End |
| 153 | +End |
0 commit comments