summaryrefslogtreecommitdiff
path: root/lib/project_sdk_unittest.py
blob: 7859c02f69545013281cbdc25f18a896be1f31cd (plain)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# Copyright 2015 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Tests for the project_sdk library."""

from __future__ import print_function

import os

from chromite.cbuildbot import constants
from chromite.lib import cros_build_lib_unittest
from chromite.lib import cros_test_lib
from chromite.lib import osutils
from chromite.lib import project_sdk

# pylint: disable=protected-access

class ProjectSdkTest(cros_test_lib.TempDirTestCase):
  """Unittest for project_sdk.py"""

  def setUp(self):
    self.version = '1.2.3'

    # Define assorted paths to test against.
    self.bogus_dir = os.path.join(self.tempdir, 'bogus')
    self.repo_dir = os.path.join(self.tempdir, 'repo')
    self.nested_dir = os.path.join(self.repo_dir, 'foo', 'bar')

    # Create fake repo with sub-dirs.
    osutils.SafeMakedirs(os.path.join(self.repo_dir, '.repo'))
    osutils.SafeMakedirs(self.nested_dir)

    version_file = project_sdk.VersionFile(self.repo_dir)
    osutils.WriteFile(version_file, self.version)

  def testFindRepoRootCurrentRepo(self):
    """Test FindRepoRoot with default of CWD."""
    self.assertEqual(constants.SOURCE_ROOT, project_sdk.FindRepoRoot())

  def testFindRepoRootSpecifiedBogus(self):
    """Test FindRepoRoot with non-existent directory outside the repo."""
    self.assertIsNone(project_sdk.FindRepoRoot(self.bogus_dir))

  def testFindRepoRootSpecifiedRoot(self):
    """Test FindRepoRoot with top level of repo tree."""
    self.assertEqual(self.repo_dir, project_sdk.FindRepoRoot(self.repo_dir))

  def testFindRepoRootSpecifiedNested(self):
    """Test FindRepoRoot with nested inside repo tree."""
    self.assertEqual(self.repo_dir, project_sdk.FindRepoRoot(self.nested_dir))

  def testFindRepoRootSpecifiedNonexistent(self):
    """Test FindRepoRoot refuses to scan a nonexistent path."""
    self.assertIsNone(
        project_sdk.FindRepoRoot(os.path.join(self.nested_dir, 'not_there')))

  def testVersionFile(self):
    self.assertEqual('/foo/SDK_VERSION', project_sdk.VersionFile('/foo'))

  def testFindVersionDefault(self):
    """Test FindVersion with default of CWD."""
    # Expected results are undefined, just ensure we don't crash.
    project_sdk.FindVersion()

  def testFindVersionBogus(self):
    """Test FindVersion with non-existent directory outside the repo."""
    self.assertIsNone(project_sdk.FindVersion(self.bogus_dir))

  def testFindVersionSpecifiedRoot(self):
    """Test FindVersion with top level of repo tree."""
    self.assertEqual(self.version, project_sdk.FindVersion(self.repo_dir))

  def testFindVersionSpecifiedNested(self):
    """Test FindVersion with nested inside repo tree."""
    self.assertEqual(self.version, project_sdk.FindVersion(self.nested_dir))


class ProjectSdkVerifyFake(cros_test_lib.MockTempDirTestCase):
  """Test VerifyEnvironment with mocks."""

  def setUp(self):
    self.rc_mock = self.StartPatcher(cros_build_lib_unittest.RunCommandMock())

  def MockEnvironment(self, bash=True, git='2.2.0', curl=True, suid=True):
    """Mocks an environment for the specified checks.

    When adding a new environment check, add it to this function so that
    each test can isolate just the check it wants to test.

    Args:
      bash: True to mock a valid bash environment.
      git: If set, git version to mock.
      curl: True to mock a valid curl environment.
      suid: True to mock a valid suid environment.
    """
    if bash:
      self.rc_mock.AddCmdResult(['/bin/bash', '--version'])
    if git:
      self.rc_mock.AddCmdResult(['git', '--version'],
                                output='git version %s' % git)
    if curl:
      self.rc_mock.AddCmdResult(['curl', '--version'])
    if suid:
      self.PatchObject(os, 'statvfs', autospec=True).return_value.f_flag = 0x0

  def testMockEnvironment(self):
    """Test that MockEnvironment() by itself sets up a valid env."""
    self.MockEnvironment()

    self.assertTrue(project_sdk.VerifyEnvironment(self.tempdir))

  def testGTrusty(self):
    """Test with mock of 'gTrusty' distribution."""
    self.MockEnvironment(git='2.2.0.rc0.207.ga3a616c')

    self.assertTrue(project_sdk.VerifyEnvironment(self.tempdir))

  def testUbuntu14(self):
    """Test with mock of 'Ubuntu LTS 14' distribution."""
    self.MockEnvironment(git='2.1.0')

    self.assertTrue(project_sdk.VerifyEnvironment(self.tempdir))

  def testGitNewEnough(self):
    """Test with mock of git 1.8."""
    self.MockEnvironment(git='1.8.3.1')

    self.assertTrue(project_sdk.VerifyEnvironment(self.tempdir))

  def testFailNoBash(self):
    """Test with mock of no bash present."""
    self.MockEnvironment(bash=False)
    self.rc_mock.AddCmdResult(['/bin/bash', '--version'], returncode=127)

    self.assertFalse(project_sdk.VerifyEnvironment(self.tempdir))

  def testFailGitTooOld(self):
    """Test with mock of git too old to use."""
    self.MockEnvironment(git='1.7.10.4')

    self.assertFalse(project_sdk.VerifyEnvironment(self.tempdir))

  def testFailNoCurl(self):
    """Test with mock of no curl present."""
    self.MockEnvironment(curl=False)
    self.rc_mock.AddCmdResult(['curl', '--version'], returncode=127)

    self.assertFalse(project_sdk.VerifyEnvironment(self.tempdir))

  def testFailSuid(self):
    """Test with SUID is disabled."""
    self.MockEnvironment(suid=False)
    # The os.ST_NOSUID constant wasn't added until python-3.2.
    self.PatchObject(os, 'statvfs', autospec=True).return_value.f_flag = 0x2

    self.assertFalse(project_sdk.VerifyEnvironment(self.tempdir))

class ProjectSdkVerifyReal(cros_test_lib.TempDirTestCase):
  """Test VerifyEnvironment for real."""

  def testVerifyEnvironment(self):
    """Test, assuming production environment is valid."""
    self.assertTrue(project_sdk.VerifyEnvironment(self.tempdir))

  def testGetExecutableVersionNonExistent(self):
    """Tests _GetExecutableVersion() when the executable doesn't exist."""
    self.assertIsNone(project_sdk._GetExecutableVersion('/not/a/real/program'))