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

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