无法在GCP打包程序实例上运行serverspec测试

时间:2017-11-14 23:25:52

标签: rspec packer gcp serverspec

我正在努力在本地计算机上对GCP打包程序实例运行serverspec测试。 我创建了一个packer json配置文件,如下所示在GCP上创建一个图像:

{
"variables": {
    "project": "gcp-project",
    "image_family": "centos-7",
    "username": "centos",
    "zone": "us-central1-c",
    "version": "latest"
},

"builders": [
    {
        "type": "googlecompute",
        "account_file": "account.json",
        "project_id": "{{user `project`}}",
        "zone": "{{user `zone`}}",
        "source_image":"centos-7-v20171025",
        "image_name": "sftp-{{user `image_family`}}-{{user `version`}}-{{timestamp}}",
        "image_family": "{{user `image_family`}}",
        "image_description": "sftp - from packer",
        "ssh_username": "{{user `username`}}",
        "machine_type": "g1-small"
    }
],

"provisioners": [
    {
        "type": "shell-local",
        "command": "rake spec TARGET_HOST=remotehost"
    }
]

}

我执行rake spec TARGET_HOST=(ip of packer instance)从本地计算机运行serverspec test,spec_helper.rb使用ssh login配置如下:

host = ENV['TARGET_HOST']
options = Net::SSH::Config.for(host)
options[:user] = 'centos'
set :host,        options[:host_name] || host
set :ssh_options, options

并且Rakefile配置为从特定文件夹运行测试。

运行packer build命令packer build -var-file=variables.json sftp.json| tee build.log

之后

它以

失败
Net::SSH::AuthenticationFailed:
Authentication failed for user centos@1.2.3.4

Packer构建日志

==> googlecompute: Checking image does not exist...
==> googlecompute: Creating temporary SSH key for instance...
==> googlecompute: Using image: centos-7-v20171025
==> googlecompute: Creating instance...
    googlecompute: Loading zone: us-central1-c
    googlecompute: Loading machine type: g1-small
    googlecompute: Loading network: default
    googlecompute: Requesting instance creation...
    googlecompute: Waiting for creation operation to complete...
    googlecompute: Instance has been created!
==> googlecompute: Waiting for the instance to become running...
    googlecompute: IP: 1.2.3.4
==> googlecompute: Waiting for SSH to become available...
==> googlecompute: Connected to SSH!
==> googlecompute: Executing local command: rake spec TARGET_HOST=1.2.3.4
    googlecompute: An error occurred while loading ./spec/1.2.3.4/sftp_spec.rb.
    googlecompute: On host '1.2.3.4'
    googlecompute: Failure/Error:
    googlecompute:   describe service('sshd'), :if => os[:family] == 'redhat' do
    googlecompute:     it { should be_enabled }
    googlecompute:     it { should be_running }
    googlecompute:   end
    googlecompute: Net::SSH::AuthenticationFailed:
    googlecompute:   Authentication failed for user centos@1.2.3.4

由于此错误,我无法使用远程打包程序实例上的ssh从本地计算机运行serverspec测试。

任何答案都将不胜感激。非常感谢。

1 个答案:

答案 0 :(得分:0)

您似乎错过了centos用户的密码。

将此添加到spec_helper.rb

options[:password] = ENV['LOGIN_PASSWORD']

并将您的shell-local调整为:

{
    "type": "shell-local",
    "command": "LOGIN_PASSWORD='{{ user `sftp_password` }}' rake spec TARGET_HOST=remotehost"
}