def testRunInitializeCommandsTwice(self):
     # The SDK.RunInitializationCommands function should be called automatically
     # by the factory functions, but running them again manually should be a
     # no-op.
     with driver.Manager():
         sdk = driver.DefaultSDK()
         sdk.RunInitializationCommands()
         _, _, ret = sdk.RunGcloud(['config', 'list'])
         self.assertEqual(0, ret)
 def testHelpCommand(self):
     # The help commands do not return JSON results unlike almost all normal
     # commands. Verify that they're still handled properly.
     with driver.Manager():
         sdk = driver.DefaultSDK()
         out, _, ret = sdk.RunGcloud(['config', '--help'])
         self.assertEqual(0, ret)
         # Just verify that whatever RunGcloud returns is valid as a JSON object.
         json.dumps(out)
    def testDidntSpecifyPythonPath(self):
        paths = ['path4', 'path5', 'path6']
        self.StartObjectPatch(sys, 'path', new=paths)
        sdk = driver.DefaultSDK()

        sdk.Run(['config', 'list'])
        _, kwargs = self.popen_patch.call_args
        self.assertEqual(os.pathsep.join(paths),
                         kwargs['env'][constants.PYTHON_PATH])
 def testInstallPreviousVersion(self):
     # Make sure the driver can install from one of the archived versions.
     with driver.Manager(
             tar_location=
             'https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-140.0.0-linux-x86_64.tar.gz'
     ):
         sdk = driver.DefaultSDK()
         sdk.RunInitializationCommands()
         _, _, ret = sdk.RunGcloud(['config', 'list'])
         self.assertEqual(0, ret)
    def testNoPath(self):
        sdk = driver.DefaultSDK()

        sdk.Run(['config', 'list'])
        _, kwargs = self.popen_patch.call_args
        self.assertEqual(
            os.pathsep.join([
                os.path.join(self.expected_sdk_dir, constants.BIN_FOLDER),
                'env_path'
            ]), kwargs['env']['PATH'])
    def setUp(self):
        self.out = {'a': 'b', 'c': 'd'}
        self.err = 'error'
        self.code = 0

        self.run_patch = self.StartObjectPatch(
            driver.SDK,
            'Run',
            return_value=(json.dumps(self.out), self.err, self.code))
        self.MockSDKFactoryDependencies()

        self.sdk = driver.DefaultSDK()
    def testSDKDefaultConfig(self):
        sdk = driver.DefaultSDK()
        calls = self.popen_patch.mock_calls
        # This should generate no shell calls as none of the config values that
        # would cause commands to be run during creation are set.
        self.assertEqual(0, len(calls))

        sdk.Run(['config', 'list'])
        _, kwargs = self.popen_patch.call_args
        self.assertIn(os.path.join(self.expected_sdk_dir, 'bin'),
                      kwargs['env']['PATH'])
        self.assertEqual(
            os.path.join(self.expected_sdk_dir, self.expected_config_name),
            kwargs['env']['CLOUDSDK_CONFIG'])
    def testRunTwiceNoDestroy(self):
        # Forgetting to call Destroy mainly means that the installation location is
        # not cleaned up. By default this would leave files in the temp directory.
        # It's best not to leave such files around taking up disk space, but they
        # shouldn't hurt anything if the disk's not full.

        # After Init, imitate forgetting to call Destroy by removing the environment
        # variable tracking the installation location.
        driver.Init()
        self.location = os.environ[constants.DRIVER_LOCATION_ENV]
        del os.environ[constants.DRIVER_LOCATION_ENV]
        self.addCleanup(self.cleanup)

        # Verify that things still work.
        with driver.Manager():
            sdk = driver.DefaultSDK()
            _, _, ret = sdk.RunGcloud(['config', 'list'])
            self.assertEqual(0, ret)
    def testRunTwiceNoDestroyFixedPath(self):
        # If you're using a fixed root directory, not calling Destroy will behave a
        # little differently. Instead of leaking the previous installation, it will
        # just reuse it.
        root_directory = tempfile.mkdtemp()

        # After Init, imitate forgetting to call Destroy by removing the environment
        # variable tracking the installation location.
        driver.Init(root_directory=root_directory)
        self.location = os.environ[constants.DRIVER_LOCATION_ENV]
        del os.environ[constants.DRIVER_LOCATION_ENV]
        self.addCleanup(self.cleanup)

        # If the root directory was set, the driver should be using it
        self.assertEqual(root_directory, self.location)

        # Verify that things still work.
        with driver.Manager(root_directory=root_directory):
            sdk = driver.DefaultSDK()
            _, _, ret = sdk.RunGcloud(['config', 'list'])
            self.assertEqual(0, ret)

            self.assertEqual(root_directory,
                             os.environ[constants.DRIVER_LOCATION_ENV])
 def testConfigList(self):
     sdk = driver.DefaultSDK()
     _, _, ret = sdk.RunGcloud(['config', 'list'])
     self.assertEqual(0, ret)
    def setUp(self):
        self.MockSDKFactoryDependencies()

        self.sdk = driver.DefaultSDK()
 def testNoPythonExecutable(self):
     self.StartObjectPatch(sys, 'executable', new=None)
     with self.assertRaises(error.SDKError):
         driver.DefaultSDK()
 def testNotInitialized(self):
     del os.environ[constants.DRIVER_LOCATION_ENV]
     with self.assertRaises(error.SDKError):
         driver.DefaultSDK()
 def testInitialized(self):
     sdk = driver.DefaultSDK()
     sdk.Run(['config', 'list'])
     _, kwargs = self.popen_patch.call_args
     self.assertEqual(self.expected_cwd, kwargs['cwd'])
 def testNoInit(self):
     with self.assertRaises(error.SDKError):
         driver.DefaultSDK()