github.com/nginxinc/kubernetes-ingress@v1.12.5/tests/suite/test_virtual_server_focused_canary.py (about)

     1  import pytest
     2  
     3  import requests
     4  import yaml
     5  
     6  from settings import TEST_DATA
     7  
     8  
     9  def get_weights_of_splitting(file) -> []:
    10      """
    11      Parse yaml file into an array of weights.
    12  
    13      :param file: an absolute path to file
    14      :return: []
    15      """
    16      weights = []
    17      with open(file) as f:
    18          docs = yaml.safe_load_all(f)
    19          for dep in docs:
    20              for item in dep['spec']['routes'][0]['matches'][0]['splits']:
    21                  weights.append(item['weight'])
    22      return weights
    23  
    24  
    25  def get_upstreams_of_splitting(file) -> []:
    26      """
    27      Parse yaml file into an array of upstreams.
    28  
    29      :param file: an absolute path to file
    30      :return: []
    31      """
    32      upstreams = []
    33      with open(file) as f:
    34          docs = yaml.safe_load_all(f)
    35          for dep in docs:
    36              for item in dep['spec']['routes'][0]['matches'][0]['splits']:
    37                  upstreams.append(item['action']['pass'])
    38      return upstreams
    39  
    40  
    41  @pytest.mark.vs
    42  @pytest.mark.parametrize('crd_ingress_controller, virtual_server_setup',
    43                           [({"type": "complete", "extra_args": [f"-enable-custom-resources"]},
    44                             {"example": "virtual-server-focused-canary", "app_type": "simple"})],
    45                           indirect=True)
    46  class TestVSFocusedCanaryRelease:
    47      def test_several_requests(self, kube_apis, crd_ingress_controller, virtual_server_setup):
    48          weights = get_weights_of_splitting(
    49              f"{TEST_DATA}/virtual-server-focused-canary/standard/virtual-server.yaml")
    50          upstreams = get_upstreams_of_splitting(
    51              f"{TEST_DATA}/virtual-server-focused-canary/standard/virtual-server.yaml")
    52          sum_weights = sum(weights)
    53          ratios = [round(i/sum_weights, 1) for i in weights]
    54  
    55          counter_v1, counter_v2 = 0, 0
    56          for _ in range(100):
    57              resp = requests.get(virtual_server_setup.backend_1_url,
    58                                  headers={"host": virtual_server_setup.vs_host, "x-version": "canary"})
    59              if upstreams[0] in resp.text in resp.text:
    60                  counter_v1 = counter_v1 + 1
    61              elif upstreams[1] in resp.text in resp.text:
    62                  counter_v2 = counter_v2 + 1
    63              else:
    64                  pytest.fail(f"An unexpected backend in response: {resp.text}")
    65  
    66          assert abs(round(counter_v1/(counter_v1 + counter_v2), 1) - ratios[0]) <= 0.2
    67          assert abs(round(counter_v2/(counter_v1 + counter_v2), 1) - ratios[1]) <= 0.2