def startTestRun(self, event): # Create a mock for the `sudo snap prepare-image` command. This is an # expensive command which hits the actual snap store. We want this to # run at least once so we know our tests are valid. We can cache the # results in a test-suite-wide temporary directory and simulate future # calls by just recursively copying the contents to the specified # directories. # # It's a bit more complicated than that though, because it's possible # that the channel and model.assertion will be different, so we need # to make the cache dependent on those values. # # Finally, to enable full end-to-end tests, check an environment # variable to see if the mocking should even be done. This way, we # can make our Travis-CI job do at least one real end-to-end test. self.resources = ExitStack() # How should we mock `snap prepare-image`? If set to 'always' (case # insensitive), then use the sample data in the .zip file. Any other # truthy value says to use a second-and-onward mock. should_we_mock = os.environ.get('UBUNTUIMAGE_MOCK_SNAP', 'yes') if should_we_mock.lower() == 'always': mock_class = AlwaysMock elif as_bool(should_we_mock): mock_class = SecondAndOnwardMock else: mock_class = None if mock_class is not None: tmpdir = self.resources.enter_context(TemporaryDirectory()) self.resources.enter_context(mock_class(tmpdir))
def startTestRun(self, event): # Create a mock for the `sudo snap prepare-image` command. This is an # expensive command which hits the actual snap store. We want this to # run at least once so we know our tests are valid. We can cache the # results in a test-suite-wide temporary directory and simulate future # calls by just recursively copying the contents to the specified # directories. # # It's a bit more complicated than that though, because it's possible # that the channel and model.assertion will be different, so we need # to make the cache dependent on those values. # # Finally, to enable full end-to-end tests, check an environment # variable to see if the mocking should even be done. This way, we # can make our Travis-CI job do at least one real end-to-end test. self.resources = ExitStack() # How should we mock `snap prepare-image`? If set to 'always' (case # insensitive), then use the sample data in the .zip file. Any other # truthy value says to use a second-and-onward mock. should_we_mock = os.environ.get('UBUNTU_IMAGE_MOCK_SNAP', 'yes') if should_we_mock.lower() == 'always': mock_class = AlwaysMock elif as_bool(should_we_mock): mock_class = SecondAndOnwardMock else: mock_class = None if mock_class is not None: tmpdir = self.resources.enter_context(TemporaryDirectory()) # Record the actual snap mocker on the class so that other tests # can temporarily disable it. Some tests need to run the actual # snap() helper function. self.__class__.snap_mocker = self.resources.enter_context( mock_class(tmpdir))
def test_as_bool(self): for value in {'no', 'False', '0', 'DISABLE', 'DiSaBlEd'}: self.assertFalse(as_bool(value), value) for value in {'YES', 'tRUE', '1', 'eNaBlE', 'enabled'}: self.assertTrue(as_bool(value), value) self.assertRaises(ValueError, as_bool, 'anything else')