1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136

content / test / gpu / flake_suppressor / gpu_expectations_unittest.py [blame]

#!/usr/bin/env vpython3
# Copyright 2021 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import base64
from email.message import EmailMessage
import os
from typing import Any
import unittest
import unittest.mock as mock
import urllib.error

from flake_suppressor import gpu_expectations

from pyfakefs import fake_filesystem_unittest  # pylint:disable=import-error


class GetExpectationFileForSuiteUnittest(unittest.TestCase):
  def setUp(self) -> None:
    self.expectations = gpu_expectations.GpuExpectationProcessor()

  def testRegularExpectationFile(self) -> None:
    """Tests that a regular expectation file is found properly."""
    expected_filepath = os.path.join(
        gpu_expectations.ABSOLUTE_EXPECTATION_FILE_DIRECTORY,
        'pixel_expectations.txt')
    actual_filepath = self.expectations.GetExpectationFileForSuite(
        'pixel_integration_test', tuple())
    self.assertEqual(actual_filepath, expected_filepath)

  def testOverrideExpectationFile(self) -> None:
    """Tests that an overridden expectation file is found properly."""
    expected_filepath = os.path.join(
        gpu_expectations.ABSOLUTE_EXPECTATION_FILE_DIRECTORY,
        'info_collection_expectations.txt')
    actual_filepath = self.expectations.GetExpectationFileForSuite(
        'info_collection_test', tuple())
    self.assertEqual(actual_filepath, expected_filepath)


class GetOriginExpectationFileContentsUnittest(unittest.TestCase):
  class FakeRequestResult():
    def __init__(self):
      self.text = ''

    def read(self) -> str:
      return self.text

  def setUp(self) -> None:
    self.expectations = gpu_expectations.GpuExpectationProcessor()
    self._get_patcher = mock.patch(
        'flake_suppressor_common.expectations.urllib.request.urlopen')
    self._get_mock = self._get_patcher.start()
    self.addCleanup(self._get_patcher.stop)

  def testBasic(self) -> None:
    """Tests basic functionality along the happy path."""

    def SideEffect(
        url: str) -> GetOriginExpectationFileContentsUnittest.FakeRequestResult:
      request_result = (
          GetOriginExpectationFileContentsUnittest.FakeRequestResult())
      text = ''
      if url.endswith('test_expectations?format=TEXT'):
        text = """\
mode type hash foo_tests.txt
mode type hash bar_tests.txt"""
      elif url.endswith('foo_tests.txt?format=TEXT'):
        text = 'foo_tests.txt content'
      elif url.endswith('bar_tests.txt?format=TEXT'):
        text = 'bar_tests.txt content'
      else:
        self.fail('Given unhandled URL %s' % url)
      request_result.text = base64.b64encode(text.encode('utf-8'))
      return request_result

    self._get_mock.side_effect = SideEffect

    foo_tests_txt = (os.path.join(
        gpu_expectations.RELATIVE_EXPECTATION_FILE_DIRECTORY, 'foo_tests.txt'))
    bar_tests_txt = (os.path.join(
        gpu_expectations.RELATIVE_EXPECTATION_FILE_DIRECTORY, 'bar_tests.txt'))
    expected_contents = {
        foo_tests_txt: 'foo_tests.txt content',
        bar_tests_txt: 'bar_tests.txt content',
    }
    self.assertEqual(self.expectations.GetOriginExpectationFileContents(),
                     expected_contents)
    self.assertEqual(self._get_mock.call_count, 3)

  def testNonOkStatusCodesSurfaced(self) -> None:
    """Tests that getting a non-200 status code back results in a failure."""

    def SideEffect(_: Any) -> None:
      raise urllib.error.HTTPError('url', 404, 'No exist :(', EmailMessage(),
                                   None)

    self._get_mock.side_effect = SideEffect
    with self.assertRaises(urllib.error.HTTPError):
      self.expectations.GetOriginExpectationFileContents()


class GetLocalCheckoutExpectationFileContentsUnittest(
    fake_filesystem_unittest.TestCase):
  def setUp(self) -> None:
    self.expectations = gpu_expectations.GpuExpectationProcessor()
    self.setUpPyfakefs()

  def testBasic(self) -> None:
    """Tests basic functionality."""
    os.makedirs(gpu_expectations.ABSOLUTE_EXPECTATION_FILE_DIRECTORY)
    with open(
        os.path.join(gpu_expectations.ABSOLUTE_EXPECTATION_FILE_DIRECTORY,
                     'foo.txt'), 'w') as outfile:
      outfile.write('foo.txt contents')
    with open(
        os.path.join(gpu_expectations.ABSOLUTE_EXPECTATION_FILE_DIRECTORY,
                     'bar.txt'), 'w') as outfile:
      outfile.write('bar.txt contents')
    foo_txt = os.path.join(gpu_expectations.RELATIVE_EXPECTATION_FILE_DIRECTORY,
                           'foo.txt')
    bar_txt = os.path.join(gpu_expectations.RELATIVE_EXPECTATION_FILE_DIRECTORY,
                           'bar.txt')
    expected_contents = {
        foo_txt: 'foo.txt contents',
        bar_txt: 'bar.txt contents',
    }

    self.assertEqual(
        self.expectations.GetLocalCheckoutExpectationFileContents(),
        expected_contents)


if __name__ == '__main__':
  unittest.main(verbosity=2)