Skip to content

Commit

Permalink
test(inspec): no more need to mangle mapdata for hostname
Browse files Browse the repository at this point in the history
We force the hostname in `kitchen` so the `_mapdata` files can be
static.
  • Loading branch information
baby-gnu committed Sep 8, 2020
1 parent 208f873 commit 8cb31c6
Show file tree
Hide file tree
Showing 17 changed files with 15 additions and 51 deletions.
7 changes: 1 addition & 6 deletions test/integration/default/controls/_mapdata_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
# frozen_string_literal: true

# Replace per minion strings
replacement = {
hostname: system.hostname
}

mapdata_file = "_mapdata/#{system.platform[:finger].split('.').first}.yaml"
mapdata_dump = inspec.profile.file(mapdata_file) % replacement
mapdata_dump = inspec.profile.file(mapdata_file)

control '`map.jinja` YAML dump' do
title 'should contain the lines'
Expand Down
2 changes: 1 addition & 1 deletion test/integration/default/files/_mapdata/amazonlinux-1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ openssh:
present: false
banner: /etc/ssh/banner
banner_src: banner
banner_string: 'Welcome to %{hostname}!
banner_string: 'Welcome to example.net!
'
client: openssh-clients
client_version: latest
Expand Down
2 changes: 1 addition & 1 deletion test/integration/default/files/_mapdata/amazonlinux-2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ openssh:
present: false
banner: /etc/ssh/banner
banner_src: banner
banner_string: 'Welcome to %{hostname}!
banner_string: 'Welcome to example.net!
'
client: openssh-clients
client_version: latest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ openssh:
present: false
banner: /etc/ssh/banner
banner_src: banner
banner_string: 'Welcome to %{hostname}!
banner_string: 'Welcome to example.net!
'
client: openssh
client_version: latest
Expand Down
2 changes: 1 addition & 1 deletion test/integration/default/files/_mapdata/centos-6.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ openssh:
present: false
banner: /etc/ssh/banner
banner_src: banner
banner_string: 'Welcome to %{hostname}!
banner_string: 'Welcome to example.net!
'
client: openssh-clients
client_version: latest
Expand Down
2 changes: 1 addition & 1 deletion test/integration/default/files/_mapdata/centos-7.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ openssh:
present: false
banner: /etc/ssh/banner
banner_src: banner
banner_string: 'Welcome to %{hostname}!
banner_string: 'Welcome to example.net!
'
client: openssh-clients
client_version: latest
Expand Down
2 changes: 1 addition & 1 deletion test/integration/default/files/_mapdata/centos-8.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ openssh:
present: false
banner: /etc/ssh/banner
banner_src: banner
banner_string: 'Welcome to %{hostname}!
banner_string: 'Welcome to example.net!
'
client: openssh-clients
client_version: latest
Expand Down
2 changes: 1 addition & 1 deletion test/integration/default/files/_mapdata/debian-10.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ openssh:
present: false
banner: /etc/ssh/banner
banner_src: banner
banner_string: 'Welcome to %{hostname}!
banner_string: 'Welcome to example.net!
'
client: openssh-client
client_version: latest
Expand Down
2 changes: 1 addition & 1 deletion test/integration/default/files/_mapdata/debian-9.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ openssh:
present: false
banner: /etc/ssh/banner
banner_src: banner
banner_string: 'Welcome to %{hostname}!
banner_string: 'Welcome to example.net!
'
client: openssh-client
client_version: latest
Expand Down
2 changes: 1 addition & 1 deletion test/integration/default/files/_mapdata/fedora-31.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ openssh:
present: false
banner: /etc/ssh/banner
banner_src: banner
banner_string: 'Welcome to %{hostname}!
banner_string: 'Welcome to example.net!
'
client: openssh-clients
client_version: latest
Expand Down
2 changes: 1 addition & 1 deletion test/integration/default/files/_mapdata/fedora-32.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ openssh:
present: false
banner: /etc/ssh/banner
banner_src: banner
banner_string: 'Welcome to %{hostname}!
banner_string: 'Welcome to example.net!
'
client: openssh-clients
client_version: latest
Expand Down
2 changes: 1 addition & 1 deletion test/integration/default/files/_mapdata/opensuse-15.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ openssh:
present: false
banner: /etc/ssh/banner
banner_src: banner
banner_string: 'Welcome to %{hostname}!
banner_string: 'Welcome to example.net!
'
client: openssh
client_version: latest
Expand Down
2 changes: 1 addition & 1 deletion test/integration/default/files/_mapdata/ubuntu-16.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ openssh:
present: false
banner: /etc/ssh/banner
banner_src: banner
banner_string: 'Welcome to %{hostname}!
banner_string: 'Welcome to example.net!
'
client: openssh-client
client_version: latest
Expand Down
2 changes: 1 addition & 1 deletion test/integration/default/files/_mapdata/ubuntu-18.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ openssh:
present: false
banner: /etc/ssh/banner
banner_src: banner
banner_string: 'Welcome to %{hostname}!
banner_string: 'Welcome to example.net!
'
client: openssh-client
client_version: latest
Expand Down
2 changes: 1 addition & 1 deletion test/integration/default/files/_mapdata/ubuntu-20.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ openssh:
present: false
banner: /etc/ssh/banner
banner_src: banner
banner_string: 'Welcome to %{hostname}!
banner_string: 'Welcome to example.net!
'
client: openssh-client
client_version: latest
Expand Down
1 change: 0 additions & 1 deletion test/integration/share/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ It's goal is to share the libraries between all profiles.

The `system` library provides easy access to system dependents informations:

- `system.hostname`: return the result of `hostname -s` or `hostnamectl --static` based on the availability of each commans
- `system.platform`: take `inspec.platform` and mangle things a bit to be useful
- `system.platform[:family]` provides a family name for Arch
- `system.platform[:name]` modify `amazon` to `amazonlinux`
Expand Down
30 changes: 0 additions & 30 deletions test/integration/share/libraries/system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,17 @@
# Author: Daniel Dehennin <daniel.dehennin@ac-dijon.fr>
# Copyright (C) 2020 Daniel Dehennin <daniel.dehennin@ac-dijon.fr>

HOSTNAME_CMDS = %w[hostname hostnamectl].freeze
HOSTNAME_CMDS_OPT = {
'hostname' => '-s',
'hostnamectl' => '--static'
}.freeze

class SystemResource < Inspec.resource(1)
name 'system'

attr_reader :platform
attr_reader :hostname

def initialize
@platform = build_platform
@hostname = found_hostname
end

private

def found_hostname
cmd = guess_hostname_cmd

unless cmd.exit_status.zero?
raise Inspec::Exceptions::ResourceSkipped,
"Error running '#{cmd}': #{cmd.stderr}"
end

cmd.stdout.chomp
end

def guess_hostname_cmd
HOSTNAME_CMDS.each do |cmd|
if inspec.command(cmd).exist?
return inspec.command("#{cmd} #{HOSTNAME_CMDS_OPT[cmd]}")
end
end

raise Inspec::Exceptions::ResourceSkipped,
"Error: #{@platform[:finger]}} has none of #{HOSTNAME_CMDS.join(', ')}"
end

def build_platform
{
family: build_platform_family,
Expand Down

0 comments on commit 8cb31c6

Please sign in to comment.