如何通过机器人框架运行AWS CLI命令?

时间:2020-08-28 09:25:07

标签: amazon-s3 robotframework aws-cli

更新我的问题:我的测试场景是获取特定S3存储桶中的文件大小。为此,我安装了robotframework-aws库。现在,我不确定要使用哪个关键字来获取文件大小。这是我到目前为止编写的代码:

protected function _getCategoriesID($field_value)
        {
            global $Database;
            $products_categories = array();
            $separator           = $this->_getSeparator($field_value);
            $category_array      = array();
            if ($separator != '') {
                $category_array = explode($separator, $field_value);
                $category_array = array_reverse($category_array);
            } else {
                $category_array[0] = $field_value;
            }
            unset($separator);
            $Qcategory = $Database->query('select c0.categories_id from :table_categories as c0 inner join :table_categories_description as cd on c0.categories_id=cd.categories_id where cd.categories_name = :categories_name and cd.language_id=:language_id');
            $Qcategory->bindTable(':table_categories', TABLE_CATEGORIES);
            $Qcategory->bindTable(':table_categories_description', TABLE_CATEGORIES_DESCRIPTION);
            $Qcategory->bindValue(':categories_name', $category_array[0]);
            $Qcategory->bindInt(':language_id', $this->language);
            $count = 1;
            if (count($category_array) > 1) {
                while ($count < count($category_array)) {
                    $Qcategory->appendQuery(' and c' . ($count - 1) . '.parent_id in (select c' . $count . '.categories_id from :table_categories' . $count . ' as c' . $count . ' inner join :table_categories_description' . $count . ' as cd' . $count . ' on c' . $count . '.categories_id=cd' . $count . '.categories_id where cd' . $count . '.categories_name = :categories_name' . $count . ' and cd' . $count . '.language_id=:language_id' . $count . ' ');
                    $Qcategory->bindTable(':table_categories' . $count, TABLE_CATEGORIES);
                    $Qcategory->bindTable(':table_categories_description' . $count, TABLE_CATEGORIES_DESCRIPTION);
                    $Qcategory->bindValue(':categories_name' . $count, $category_array[$count]);
                    $Qcategory->bindInt(':language_id' . $count, $this->language);
                    $count++;
                }
                for ($i = 0; $i < $count - 1; $i++) {
                    $Qcategory->appendQuery(')');
                }
            }
            $Qcategory->execute();
            while ($Qcategory->next()) {
                $products_categories[] = $Qcategory->value('categories_id');
            }
            unset($Qcategories);
            return $products_categories;
        }

使用此代码,我得到以下错误:

Run AWS CLI Command to capture file size of S3 MRL Hub Source
Create Session With Keys    region: us-east-1    access_key: xxxx   secret_key: xxxx    aws_session_token: str
Read File From S3    bucket_name: com-abc-def-ghi    key: name/of/the/file/i/am/looking/for.parquet

1 个答案:

答案 0 :(得分:1)

您可以使用Run命令,该命令是OperatingSystem库的一部分,默认情况下在安装RobotFramework时已包含该命令。

使用此方法,您可以使Robotframework运行您要运行的任何命令提示符。例如:

*** Settings ***
Library  OperatingSystem

*** Test Cases ***
Test run command
    ${output}=  Run  aws --version
    log  ${output}

当您要使用aws configure的交互功能时,就会出现问题。我的意思是,您不能期望Robotframework测试用例需要您的输入。在这种情况下,您需要事先提供所有aws configure options。这意味着您需要为测试用例准备一个profile文件,然后可以连接更多命令,例如:

*** Test Cases ***
Test run command
    ${output}=  Run  aws configure --profile <profilename> && set https_proxy http://webproxy.xyz.com:8080
    log  ${output}

或者最好直接将profile文件与s3一起使用,例如aws s3 ls --profile <profilename>

请记住,最好的方法是使用某种外部库,例如AWSLibrary或使用custom library创建自己的boto3 Python library