def test_get_client_secrets_location(self):
     location = AppScaleState.get_client_secrets_location(
         self.default_keyname)
     expected = os.path.join(
         AppScaleState.config_path(),
         "{0}-secrets.json".format(self.default_keyname))
     self.assertEqual(expected, location)
 def test_generate_rsa_key(self):
     AppScaleState.generate_rsa_key(self.default_keyname, False)
     dest_file = os.path.join(self.default_configdir, self.default_keyname)
     self.assertTrue(os.path.exists(dest_file),
                     "dest file doesn't exist: {0}".format(dest_file))
     self.assertTrue(
         os.path.exists(dest_file + '.key'),
         "dest_file doesn't exist: {0}".format(dest_file + '.key'))
    def test_get_storage_account(self):
        builtins = flexmock(sys.modules['__builtin__'])
        fake_locations_json = flexmock(name='fake_locations_json')
        fake_locations_json.should_receive('read').and_return(
            self.default_locations_content)

        builtins.should_call('open')
        (builtins.should_receive('open').with_args(
            AppScaleState.locations_json_location(self.default_keyname),
            'r').and_return(fake_locations_json))

        actual = AppScaleState.get_storage_account(self.default_keyname)
        expected = 'UNIT_TEST_STORAGE'
        self.assertEqual(expected, actual)
    def test_get_resource_group(self):
        builtins = flexmock(sys.modules['__builtin__'])
        fake_locations_json = flexmock(name='fake_locations_json')
        fake_locations_json.should_receive('read').and_return(
            self.default_locations_content)

        builtins.should_call('open')
        (builtins.should_receive('open').with_args(
            AppScaleState.locations_json_location(self.default_keyname),
            'r').and_return(fake_locations_json))

        actual = AppScaleState.get_project(self.default_keyname)
        expected = 'appscale-staging'
        self.assertEqual(expected, actual)
    def test_get_group(self):
        builtins = flexmock(sys.modules['__builtin__'])
        fake_locations_json = flexmock(name='fake_locations_json')
        fake_locations_json.should_receive('read').and_return(
            self.default_locations_content)

        builtins.should_call('open')
        (builtins.should_receive('open').with_args(
            AppScaleState.locations_json_location(self.default_keyname),
            'r').and_return(fake_locations_json))

        group = AppScaleState.get_group(self.default_keyname)
        expected = 'sgrahamappgroup'
        self.assertEqual(expected, group)
    def test_upgrade_json_file(self):
        json_loc = AppScaleState.locations_json_location(self.default_keyname)
        yaml_loc = AppScaleState.locations_yaml_location(self.default_keyname)

        with open(json_loc, 'wc') as json_fh:
            json_fh.write(self.old_config_json)

        with open(yaml_loc, 'wc') as yaml_fh:
            yaml_fh.write(self.old_config_yaml)

        AppScaleState.upgrade_json_file(self.default_keyname)

        # JSON content should be the
        expected = """{"node_info": {"node_info": [{"public_ip": "172.16.10.11", "jobs": ["load_balancer", "compute", "database", "zookeeper", "taskqueue_master", "db_master", "taskqueue", "memcache", "shadow", "login"], "ssh_key": "/etc/appscale/keys/cloud1/appscale3cc1f78769994c6ab909d278ff18d0e3.key", "instance_id": "dummyappgroup-9c3dc2be-a1ca-4dca-a32a-3dd58c407031", "instance_type": "n1-standard-1", "private_ip": "10.240.0.2", "disk": null, "cloud": "cloud1"}]}, "infrastructure_info": {"infrastructure_info": {"azure_app_secret_key": "SSSSHITSASECRET", "infrastructure": "gce", "group": "sgrahamappgroup", "zone": "us-central1-a", "project": "appscale-staging", "azure_resource_group": "UNIT_TEST_RESOURCE", "azure_tenant_id": "UNIT_TEST_TENANT_ID", "azure_subscription_id": "UNIT_TEST_SUBSCRIPTION_ID", "azure_storage_account": "UNIT_TEST_STORAGE", "azure_app_id": "UNIT_TEST_APPID"}}}"""
        actual = "DIDNTREAD"
        with open(json_loc, 'r') as json_fh:
            actual = json_fh.read()
        self.assertEqual(expected, actual)
        # yaml file should now be gone
        self.assertFalse(os.path.exists(yaml_loc))
    def test_write_key_file(self):

        # Write the test key into the /tmp directory for unit testing
        filename = '/tmp/unit_test_key.key'
        contents = "TEST CONTENT"
        AppScaleState.write_key_file(filename, contents)
        actual_contents = open(filename).read()
        s = os.stat(filename)

        # expected octal 0600
        expected_mode = stat.S_IWUSR | stat.S_IRUSR

        # we are only interested in the user/group/other bits
        perm_bits = (stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
        mode = s.st_mode & perm_bits

        # If the xor is 0, then the permissions are as expected
        # otherwise the permission bits are off.
        self.assertEqual(0, mode ^ expected_mode)
        os.remove(filename)

        # assert if the contents aren't the same.
        self.assertEqual(contents, actual_contents)
    def test_appscale_in_one_node_cloud_deployment_auto_spot_price(self):
        # let's say that appscale isn't already running
        local_appscale_path = os.path.expanduser("~") + os.sep + ".appscale" + \
          os.sep + self.keyname + ".key"
        self.local_state.should_receive(
            'ensure_appscale_isnt_running').and_return()
        self.local_state.should_receive('make_appscale_directory').and_return()
        self.local_state.should_receive('get_key_path_from_name').and_return(
            local_appscale_path)

        # mock out talking to logs.appscale.com
        fake_connection = flexmock(name='fake_connection')
        fake_connection.should_receive('request').with_args(
            'POST', '/upload', str, AppScaleLogger.HEADERS).and_return()

        flexmock(httplib)
        httplib.should_receive('HTTPConnection').with_args('logs.appscale.com') \
          .and_return(fake_connection)

        # mock out generating the secret key
        flexmock(uuid)
        uuid.should_receive('uuid4').and_return('the secret')

        # mock out writing the secret key to ~/.appscale, as well as reading it
        # later
        secret_key_location = AppScaleState.ssh_key(self.keyname)
        fake_secret = flexmock(name="fake_secret")
        fake_secret.should_receive('read').and_return('the secret')
        fake_secret.should_receive('write').and_return()
        self.builtins.should_receive('open').with_args(secret_key_location, 'r') \
          .and_return(fake_secret)
        self.builtins.should_receive('open').with_args(secret_key_location, 'w') \
          .and_return(fake_secret)

        # mock out writing the secret key to ~/.appscale, as well as reading it
        # later
        secret_key_location = LocalState.get_secret_key_location(self.keyname)
        fake_secret = flexmock(name="fake_secret")
        fake_secret.should_receive('read').and_return('the secret')
        fake_secret.should_receive('write').and_return()
        self.builtins.should_receive('open').with_args(secret_key_location, 'r') \
          .and_return(fake_secret)
        self.builtins.should_receive('open').with_args(secret_key_location, 'w') \
          .and_return(fake_secret)

        self.setup_ec2_mocks()

        # slip in some fake spot instance info
        fake_entry = flexmock(name='fake_entry', price=1)
        self.fake_ec2.should_receive('get_spot_price_history').with_args(
            start_time=str,
            end_time=str,
            product_description='Linux/UNIX',
            instance_type='m3.medium',
            availability_zone='my-zone-1b').and_return([fake_entry])

        # also mock out acquiring a spot instance
        self.fake_ec2.should_receive('request_spot_instances').with_args(
            '1.1',
            'ami-ABCDEFG',
            key_name=self.keyname,
            network_interfaces=None,
            security_groups=[self.group],
            instance_type='m3.medium',
            count=1,
            placement='my-zone-1b')

        # Don't write local metadata files.
        flexmock(LocalState).should_receive('update_local_metadata')

        # assume that root login is not enabled
        self.local_state.should_receive('shell').with_args(
            re.compile('ssh'), None, 5, stdin='ls').and_return(
                'Please login as the user "ubuntu" rather than the user "root"'
            )

        # assume that we can enable root login
        self.local_state.should_receive('shell').with_args(
            re.compile('ssh'),
            None,
            5,
            stdin='sudo touch /root/.ssh/authorized_keys').and_return()

        self.local_state.should_receive('shell').with_args(
            re.compile('ssh'),
            None,
            5,
            stdin='sudo chmod 600 /root/.ssh/authorized_keys').and_return()

        self.local_state.should_receive('shell').with_args(
            re.compile('ssh'), None, 5, stdin='mktemp').and_return()

        self.local_state.should_receive('shell').with_args(
            re.compile('ssh'),
            None,
            5,
            stdin=re.compile(
                'sudo sort -u ~/.ssh/authorized_keys /root/.ssh/authorized_keys -o '
            )).and_return()

        self.local_state.should_receive('shell').with_args(
            re.compile('ssh'),
            None,
            5,
            stdin=re.compile(
                'sudo sed -n '
                '\'\/\.\*Please login\/d; w\/root\/\.ssh\/authorized_keys\' ')
        ).and_return()

        self.local_state.should_receive('shell').with_args(
            re.compile('ssh'), None, 5,
            stdin=re.compile('rm -f ')).and_return()
        self.local_state.should_receive('shell').with_args(
            re.compile('ssh'), None, 5,
            stdin=re.compile('rm -rf ')).and_return()

        # and assume that we can copy over our ssh keys fine
        self.local_state.should_receive('shell').\
          with_args(re.compile('scp .*[r|d]sa'), None, 5).and_return()
        self.local_state.should_receive('shell').\
          with_args(re.compile('scp .*{0}'.format(self.keyname)), None, 5).\
          and_return()

        self.setup_appscale_compatibility_mocks()

        # mock out generating the private key
        self.local_state.should_receive('shell').with_args(
            re.compile('openssl'), stdin=None)

        self.local_state.should_receive('shell').with_args(
            re.compile('^ssh'),
            None,
            5,
            stdin='systemctl start appscale-controller')

        self.setup_socket_mocks('elastic-ip')
        self.setup_appcontroller_mocks('elastic-ip', 'private1')

        # mock out reading the locations.json file, and slip in our own json
        self.local_state.should_receive('get_local_nodes_info').and_return(
            json.loads(
                json.dumps([{
                    "public_ip": "elastic-ip",
                    "private_ip": "private1",
                    "roles": ["shadow"]
                }])))

        # copying over the locations yaml and json files should be fine
        self.local_state.should_receive('shell').with_args(
            re.compile('scp'),
            None,
            5,
            stdin=re.compile('locations-{0}'.format(self.keyname)))

        # same for the secret key
        self.local_state.should_receive('shell').with_args(
            re.compile('scp'),
            None,
            5,
            stdin=re.compile('{0}.secret'.format(self.keyname)))

        flexmock(RemoteHelper).should_receive('copy_deployment_credentials')
        flexmock(AppControllerClient)
        AppControllerClient.should_receive('does_user_exist').and_return(True)
        AppControllerClient.should_receive('get_property').\
          and_return({'login': '******'})

        # Let's mock the call to describe_instances when checking for old
        # instances to re-use, and then to start the headnode.
        pending_instance = flexmock(name='pending_instance',
                                    state='pending',
                                    key_name=self.keyname,
                                    id='i-ABCDEFG')
        pending_reservation = flexmock(name='pending_reservation',
                                       instances=[pending_instance])

        no_instances = flexmock(name='no_instances', instances=[])
        running_instance = flexmock(name='running_instance',
                                    state='running',
                                    key_name=self.keyname,
                                    id='i-ABCDEFG',
                                    ip_address='public1',
                                    private_ip_address='private1')
        running_reservation = flexmock(name='running_reservation',
                                       instances=[running_instance])

        self.fake_ec2.should_receive('get_all_instances') \
          .and_return(no_instances) \
          .and_return(pending_reservation) \
          .and_return(running_reservation)

        argv = [
            "--min", "1", "--max", "1", "--infrastructure", "ec2", "--machine",
            "ami-ABCDEFG", "--instance_type", "m3.medium",
            "--use_spot_instances", "--keyname", self.keyname, "--group",
            self.group, "--test", "--zone", "my-zone-1b", "--static_ip",
            "elastic-ip", "--EC2_ACCESS_KEY", "baz", "--EC2_SECRET_KEY", "baz"
        ]

        options = ParseArgs(argv, self.function).args
        AppScaleTools.run_instances(options)
 def test_locations_yaml_location(self):
     location = AppScaleState.locations_yaml_location(self.default_keyname)
     expected = os.path.join(
         AppScaleState.config_path(),
         "locations-{0}.yaml".format(self.default_keyname))
     self.assertEqual(expected, location)
 def test_get_oauth2_storage_location(self):
     location = AppScaleState.get_oauth2_storage_location(
         self.default_keyname)
     expected = os.path.join(AppScaleState.config_path(),
                             "{0}-oauth2.dat".format(self.default_keyname))
     self.assertEqual(expected, location)
 def test_ssh_key(self):
     actual = AppScaleState.ssh_key(self.default_keyname)
     expected = os.path.join(self.default_configdir,
                             "{0}.key".format(self.default_keyname))
     self.assertEqual(expected, actual)
 def test_public_key(self):
     actual = AppScaleState.public_key(self.default_keyname)
     expected = os.path.join(self.user_dir, self.appscale_dir,
                             "{0}.pub".format(self.default_keyname))
     self.assertEqual(expected, actual)
 def test_private_key(self):
     actual = AppScaleState.private_key(self.default_keyname)
     expected = os.path.join(self.default_configdir, self.default_keyname)
     self.assertEqual(expected, actual)
 def test_config_path(self):
     actual = AppScaleState.config_path()
     expected = self.default_configdir
     self.assertEqual(expected, actual)