Unable to run serverspec test on GCP packer instance

I am struggling to run serverspec test on GCP packer instance from local machine. I have created a packer json config file as follows to create an image on 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"
    }
]

}

and i am executing rake spec TARGET_HOST=(ip of packer instance) to run serverspec test from local machine and spec_helper.rb is configured with ssh login as follows:

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

and Rakefile is configured to run test from specific folder.

after running packer build command packer build -var-file=variables.json sftp.json| tee build.log

it fails with

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

Packer build log

==> 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

Due to this error I am unable to run serverspec test from local machine using ssh on remote packer instance.

any answer will be appreciated. thanks very much.

1 answer

  • answered 2017-11-15 07:28 Rickard von Essen

    It seems like you are missing the password for centos user.

    Add this to spec_helper.rb:

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

    And ajust your shell-local to:

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