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

     1  import pytest
     2  import requests
     3  import yaml
     4  
     5  from settings import TEST_DATA
     6  from suite.resources_utils import ensure_response_from_backend
     7  from suite.yaml_utils import get_paths_from_vsr_yaml
     8  
     9  
    10  def get_weights_of_splitting(file) -> []:
    11      """
    12      Parse VSR 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.load_all(f)
    20          for dep in docs:
    21              for item in dep['spec']['subroutes'][0]['splits']:
    22                  weights.append(item['weight'])
    23      return weights
    24  
    25  
    26  def get_upstreams_of_splitting(file) -> []:
    27      """
    28      Parse VSR 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.load_all(f)
    36          for dep in docs:
    37              for item in dep['spec']['subroutes'][0]['splits']:
    38                  upstreams.append(item['action']['pass'])
    39      return upstreams
    40  
    41  
    42  @pytest.mark.vsr
    43  @pytest.mark.parametrize('crd_ingress_controller, v_s_route_setup',
    44                           [({"type": "complete", "extra_args": [f"-enable-custom-resources"]},
    45                             {"example": "virtual-server-route-split-traffic"})],
    46                           indirect=True)
    47  class TestVSRTrafficSplitting:
    48      def test_several_requests(self, kube_apis, crd_ingress_controller, v_s_route_setup, v_s_route_app_setup):
    49          split_path = get_paths_from_vsr_yaml(f"{TEST_DATA}/virtual-server-route-split-traffic/route-multiple.yaml")
    50          req_url = f"http://{v_s_route_setup.public_endpoint.public_ip}:{v_s_route_setup.public_endpoint.port}{split_path[0]}"
    51          ensure_response_from_backend(req_url, v_s_route_setup.vs_host)
    52          weights = get_weights_of_splitting(
    53              f"{TEST_DATA}/virtual-server-route-split-traffic/route-multiple.yaml")
    54          upstreams = get_upstreams_of_splitting(
    55              f"{TEST_DATA}/virtual-server-route-split-traffic/route-multiple.yaml")
    56          sum_weights = sum(weights)
    57          ratios = [round(i/sum_weights, 1) for i in weights]
    58  
    59          counter_v1, counter_v2 = 0, 0
    60          for _ in range(100):
    61              resp = requests.get(req_url,
    62                                  headers={"host": v_s_route_setup.vs_host})
    63              if resp.status_code == 502:
    64                  print("Backend is not ready yet, skip.")
    65              if upstreams[0] in resp.text in resp.text:
    66                  counter_v1 = counter_v1 + 1
    67              elif upstreams[1] in resp.text in resp.text:
    68                  counter_v2 = counter_v2 + 1
    69              else:
    70                  pytest.fail(f"An unexpected response: {resp.text}")
    71  
    72          assert abs(round(counter_v1/(counter_v1 + counter_v2), 1) - ratios[0]) <= 0.2
    73          assert abs(round(counter_v2/(counter_v1 + counter_v2), 1) - ratios[1]) <= 0.2