github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/integration/sawtooth_integration/tests/test_xo_smoke.py (about)

     1  # Copyright 2017 Intel Corporation
     2  #
     3  # Licensed under the Apache License, Version 2.0 (the "License");
     4  # you may not use this file except in compliance with the License.
     5  # You may obtain a copy of the License at
     6  #
     7  #     http://www.apache.org/licenses/LICENSE-2.0
     8  #
     9  # Unless required by applicable law or agreed to in writing, software
    10  # distributed under the License is distributed on an "AS IS" BASIS,
    11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  # See the License for the specific language governing permissions and
    13  # limitations under the License.
    14  # ------------------------------------------------------------------------------
    15  
    16  import os
    17  import shlex
    18  import logging
    19  import unittest
    20  import subprocess
    21  
    22  from sawtooth_integration.tests.integration_tools import XoClient
    23  from sawtooth_integration.tests.integration_tools import wait_for_rest_apis
    24  
    25  
    26  LOGGER = logging.getLogger(__name__)
    27  LOGGER.setLevel(logging.DEBUG)
    28  
    29  
    30  WAIT = 300
    31  REST_API = 'rest-api:8008'
    32  
    33  
    34  class TestXoSmoke(unittest.TestCase):
    35      @classmethod
    36      def setUpClass(cls):
    37          cls.client = XoClient('http://' + REST_API)
    38          wait_for_rest_apis([REST_API])
    39  
    40      def test_xo_smoke(self):
    41          for username in ('nunzio', 'tony'):
    42              _send_cmd('sawtooth keygen {}'.format(username))
    43  
    44          game_cmds = (
    45              'xo create game-1 --username nunzio',
    46              'xo take game-1 1 --username nunzio',
    47              'xo take game-1 4 --username tony',
    48              'xo take game-1 2 --username nunzio',
    49              'xo take game-1 2 --username tony',
    50              'xo take game-1 5 --username tony',
    51              'xo create game-2 --username tony',
    52              'xo take game-2 9 --username nunzio',
    53              'xo take game-2 8 --username tony',
    54              'xo take game-1 3 --username tony',
    55              'xo take game-1 3 --username nunzio',
    56              'xo take game-1 7 --username tony',
    57              'xo take game-2 6 --username nunzio',
    58              'xo create blank --username tony',
    59          )
    60  
    61          for cmd in game_cmds:
    62              _send_cmd(
    63                  '{} --url {} --wait {}'.format(
    64                      cmd,
    65                      self.client.url,
    66                      WAIT))
    67  
    68          self.assert_number_of_games(3)
    69  
    70          self.verify_game('game-1', 'XXXOO----', 'P1-WIN')
    71          self.verify_game('game-2', '-----X-OX', 'P2-NEXT')
    72          self.verify_game('blank', '---------', 'P1-NEXT')
    73  
    74          LOGGER.info(
    75              "Verifying that XO CLI commands don't blow up (but nothing else)")
    76  
    77          cli_cmds = (
    78              'xo list',
    79              'xo show game-1',
    80              'xo show game-2',
    81              'xo show blank',
    82          )
    83  
    84          for cmd in cli_cmds:
    85              _send_cmd(
    86                  '{} --url {}'.format(
    87                      cmd,
    88                      self.client.url))
    89  
    90          if not _tp_supports_delete():
    91              LOGGER.warning('TP does not support state delete')
    92              return
    93  
    94          delete_cmds = (
    95              'xo delete game-1 --username nunzio',
    96              'xo delete blank --username tony',
    97          )
    98  
    99          for cmd in delete_cmds:
   100              _send_cmd(
   101                  '{} --url {} --wait {}'.format(
   102                      cmd,
   103                      self.client.url,
   104                      WAIT))
   105  
   106          _send_cmd('xo list --url {}'.format(self.client.url))
   107  
   108          self.assert_number_of_games(1)
   109  
   110          self.verify_game('game-2', '-----X-OX', 'P2-NEXT')
   111  
   112          self.assert_no_game('game-1')
   113          self.assert_no_game('blank')
   114  
   115      def verify_game(self, game_name, expected_board, expected_turn):
   116          LOGGER.info('Verifying game: %s', game_name)
   117  
   118          board, turn, _, _ = self.client.get_game(game_name)
   119  
   120          self.assertEqual(
   121              board,
   122              expected_board,
   123              'Wrong board -- expected: {} -- actual: {}'.format(
   124                  expected_board, board))
   125  
   126          self.assertEqual(
   127              turn,
   128              expected_turn,
   129              'Wrong turn -- expected: {} -- actual: {}'.format(
   130                  expected_turn, turn))
   131  
   132      def assert_number_of_games(self, number):
   133          self.assertEqual(
   134              len(self.client.get_data()),
   135              number)
   136  
   137      def assert_no_game(self, game_name):
   138          with self.assertRaises(Exception):
   139              self.client.get_game(game_name)
   140  
   141  
   142  def _send_cmd(cmd_str):
   143      LOGGER.info('Sending %s', cmd_str)
   144  
   145      subprocess.run(
   146          shlex.split(cmd_str),
   147          check=True)
   148  
   149  
   150  def _tp_supports_delete():
   151      supported_langs = 'python', 'javascript', 'go'
   152  
   153      return os.getenv('TP_LANG', False) in supported_langs