Пример #1
0
    def test_c_put_string_with_async_request_timeout_ok(self):
        """Example writes a string value asynchronously then checks for it.

    The request is asynchronous so its final status is not immediately known.
    We'll inform citest of the application protocol so it can wait for the
    request to complete, then observe the results.
    """
        key = self.make_key('MyTimeoutKey')
        expect_value = 'My Timeout Value'

        operation = http_agent.HttpPostOperation(
            title='Writing Key Value Asynchronously',
            status_class=KeystoreStatus,
            data=json.JSONEncoder().encode(expect_value),
            path='/put/' + key + '?async&delay=2.5',
            max_wait_secs=2)

        builder = st.HttpContractBuilder(self.scenario.agent)
        (builder.new_clause_builder(
            'Check Key Value',
            retryable_for_secs=1).get_url_path('/lookup/' +
                                               key).contains_path_eq(
                                                   '', expect_value))
        contract = builder.build()

        test = st.OperationContract(operation, contract)
        self.run_test_case(test, timeout_ok=True)
Пример #2
0
  def test_d_use_context_for_unknown_value(self):
    """Example uses dynamic return result from operation to check value."""
    key = self.make_key('MyRandomKey')
    operation = http_agent.HttpPostOperation(
        title='Generating Key Value',
        data=json.JSONEncoder().encode(''),
        path='/put_random/' + key)
    builder = st.HttpContractBuilder(self.scenario.agent)
    (builder.new_clause_builder('Check Key Value')
     .get_url_path('/lookup/' + key)
     .contains_path_eq('', lambda context: context['EXPECT']))
    contract = builder.build()

    def extractor(operation_status, context):
      """Helper function that populates context with value from status.

      This is so we can write our contract in terms of a value not known
      until we perform the actual operation.
      """
      response = operation_status.raw_http_response
      context.set_snapshotable('EXPECT', response.output)

    test = st.OperationContract(operation, contract,
                                status_extractor=extractor)
    self.run_test_case(test)
Пример #3
0
    def test_b_put_string_with_async_request(self):
        """Example writes a string value asynchronously then checks for it.

    The request is asynchronous so its final status is not immediately known.
    We'll inform citest of the application protocol so it can wait for the
    request to complete, then observe the results.
    """
        key = self.make_key('MyAsyncKey')
        expect_value = 'My Async Value'

        operation = http_agent.HttpPostOperation(
            title='Writing Key Value Asynchronously',
            status_class=KeystoreStatus,
            data=json.JSONEncoder().encode(expect_value),
            path='/put/' + key + '?async&delay=1.5')

        # For our server, we expect that the observer will be able to see
        # the value immediately once the operation finishes even though the
        # operation itself will take 1.5 seconds to complete (after the original
        # HTTP POST operation completes).
        builder = st.HttpContractBuilder(self.scenario.agent)
        (builder.new_clause_builder('Check Key Value').get_url_path(
            '/lookup/' + key).contains_path_eq('', expect_value))
        contract = builder.build()

        test = st.OperationContract(operation, contract)
        self.run_test_case(test)
Пример #4
0
    def test_a_put_string_eventual_correctness(self):
        """Example writes a value that is not immediately observable.

    The request itself is synchronous, but the server does not make
    the value immediately available so observer may need to retry.
    """
        key = self.make_key('MyEventualCorrectnessKey')
        expect_value = 'My Eventual-Correctness Value'

        # Our server is really going to use an asynchronous protocol,
        # but this test is ignoring that and treating the put at face value.
        # It will attempt to observe a few times until eventually it sees what
        # we were expecting to eventually see.
        operation = http_agent.HttpPostOperation(
            title='Writing Key Value',
            data=json.JSONEncoder().encode(expect_value),
            path='/put/' + key + '?async')
        builder = st.HttpContractBuilder(self.scenario.agent)
        (builder.new_clause_builder(
            'Check Key Value',
            retryable_for_secs=3).get_url_path('/lookup/' +
                                               key).contains_path_eq(
                                                   '', expect_value))
        contract = builder.build()

        test = st.OperationContract(operation, contract)
        self.run_test_case(test)
  def new_post_operation(cls, title, data, path, **kwargs):
    """Creates an operation that posts data to the given path when executed.

    The base_url will come from the agent that the operation is eventually
    executed on.

    Args:
      title: [string] The name of the operation for reporting purposes.
      data: [string] The payload to send in the HTTP POST.
      path: [string] The path relative to the base url provided later.
      kwargs: [kwargs] Additional kwargs for HttpPostOperation
    """
    return http_agent.HttpPostOperation(title=title, data=data, path=path,
                                        **kwargs)
  def new_post_operation(cls, title, data, path, status_class=None):
    """Creates an operation that posts data to the given path when executed.

    The base_url will come from the agent that the operation is eventually
    executed on.

    Args:
      title: [string] The name of the operation for reporting purposes.
      data: [string] The payload to send in the HTTP POST.
      path: [string] The path relative to the base url provided later.
      status_class: [class AgentOperationStatus] If provided, a specialization
         of the AgentOperationStatus to use for tracking the execution.
    """
    return http_agent.HttpPostOperation(title=title, data=data, path=path,
                                        status_class=status_class)
Пример #7
0
  def test_a_put_string(self):
    """Example writes a string value then checks for it."""
    key = self.make_key('MyStringKey')
    expect_value = 'My Key Value'

    operation = http_agent.HttpPostOperation(
        title='Writing Key Value',
        data=json.JSONEncoder().encode(expect_value),
        path='/put/' + key)
    builder = st.HttpContractBuilder(self.scenario.agent)
    (builder.new_clause_builder('Check Key Value')
     .get_url_path('/lookup/' + key)
     .contains_path_eq('', expect_value))
    contract = builder.build()

    test = st.OperationContract(operation, contract)
    self.run_test_case(test)
Пример #8
0
  def test_m_failure(self):
    """Example showing a typical test failure."""
    key = self.make_key('MyRandomKey')
    expect_value = 'My Key Value'

    operation = http_agent.HttpPostOperation(
        title='Writing Key Value',
        data=json.JSONEncoder().encode(expect_value),
        path='/put/' + key)
    builder = st.HttpContractBuilder(self.scenario.agent)
    (builder.new_clause_builder('Check For Wrong Value')
     .get_url_path('/lookup/' + key)
     .contains_path_eq('', 'Not ' + expect_value))
    contract = builder.build()

    test = st.OperationContract(operation, contract)
    self.run_test_case(test)
Пример #9
0
  def test_multiple(self):
    key = self.make_key('MyDictKey')
    expect_value = {'a': 'A', 'b': 'B', 'c': 'C'}
    operation = http_agent.HttpPostOperation(
        title='Writing Key Value',
        data=json.JSONEncoder().encode(expect_value),
        path='/put/' + key)

    context = citest.base.ExecutionContext()
    builder = st.HttpContractBuilder(self.scenario.agent)
    (builder.new_clause_builder('Check Multiple Key Values using contains_path_eq')
     .get_url_path('/lookup/' + key)
     .contains_path_eq('a', 'A')
     .contains_path_eq('b', 'B'))
    contract = builder.build()
    test = st.OperationContract(operation, contract)
    self.run_test_case(test)
Пример #10
0
  def test_b_put_dict(self):
    """Example writes a dict value then checks for parts of it."""
    key = self.make_key('MyDictKey')
    expect_value = {'a': 'A', 'b': 'B', 'c': 'C'}

    operation = http_agent.HttpPostOperation(
        title='Writing Key Value',
        data=json.JSONEncoder().encode(expect_value),
        path='/put/' + key)
    builder = st.HttpContractBuilder(self.scenario.agent)
    (builder.new_clause_builder('Check Key Value')
     .get_url_path('/lookup/' + key)
     .contains_match({'a': jp.EQUIVALENT('A'),
                      'b': jp.EQUIVALENT('B')}))
    contract = builder.build()

    test = st.OperationContract(operation, contract)
    self.run_test_case(test)
Пример #11
0
  def test_c_put_array(self):
    """Example writes an array value then shows many ways to check values."""
    key = self.make_key('MyArrayKey')
    expect_value = [{'a': 1, 'b': 1}, 2, {'a': 3, 'b': 3}]

    operation = http_agent.HttpPostOperation(
        title='Writing Key Value',
        data=json.JSONEncoder().encode(expect_value),
        path='/put/' + key)
    # Examples of different ways to express things
    builder = st.HttpContractBuilder(self.scenario.agent)
    (builder.new_clause_builder('Contains a=1 and contains b=3')
     .get_url_path('/lookup/' + key)
     .contains_path_value('a', 1)
     .contains_path_value('b', 3))
    (builder.new_clause_builder('Contains (a=1 and b=1))')
     .get_url_path('/lookup/' + key)
     .contains_pred_list([jp.PathPredicate('a', jp.NUM_EQ(1)),
                          jp.PathPredicate('b', jp.NUM_EQ(1))]))
    (builder.new_clause_builder('Does not contain (a=1 and b=3)')
     .get_url_path('/lookup/' + key)
     .excludes_pred_list([jp.PathPredicate('a', jp.NUM_EQ(1)),
                          jp.PathPredicate('b', jp.NUM_EQ(3))]))
    (builder.new_clause_builder('Contains List')
     .get_url_path('/lookup/' + key)
     .contains_match([jp.EQUIVALENT(2),
                      jp.DICT_MATCHES({'a': jp.EQUIVALENT(3),
                                       'b': jp.DIFFERENT(1)})]))
    (builder.new_clause_builder("Contains Multiple A's >= 0")
     .get_url_path('/lookup/' + key)
     .contains_path_pred('a', jp.NUM_GE(0), min=2))
    (builder.new_clause_builder("Contains only 1 A >= 2")
     .get_url_path('/lookup/' + key)
     .contains_path_pred('a', jp.NUM_GE(2), min=1, max=1))
    (builder.new_clause_builder("Contains no A >= 10")
     .get_url_path('/lookup/' + key)
     .excludes_path_pred('a', jp.NUM_GE(10)))

    contract = builder.build()

    test = st.OperationContract(operation, contract)
    self.run_test_case(test)