test(inspec): share library to access some minion informations
The `system.hostname` return the result of either `hostname -s` or `hostnamectl --static` depending of the availability of each command. The `system.platform` return a hash with tweaked `inspec.platform` values: - `system.platform[:family]` provides a family name for Arch - `system.platform[:name]` modify `amazon` to `amazonlinux` - `system.platform[:release]` tweak for Arch and Amazon Linux: - `Arch` is always `base-later` - `Amazon Linux` release `2018` became `1` - `system.platform[:finger]` is just the concatenation of the name and the first release number (except for Ubuntu which gives `20.04` for example)
This commit is contained in:
parent
6a882026d2
commit
a8d61f4307
|
@ -0,0 +1,20 @@
|
|||
# InSpec Profile: `share`
|
||||
|
||||
This shows the implementation of the `share` InSpec [profile](https://github.com/inspec/inspec/blob/master/docs/profiles.md).
|
||||
|
||||
It's goal is to share the libraries between all profiles.
|
||||
|
||||
## Libraries
|
||||
|
||||
### `system`
|
||||
|
||||
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`
|
||||
- `system.platform[:release]` tweak for Arch and Amazon Linux:
|
||||
- `Arch` is always `base-later`
|
||||
- `Amazon Linux` release `2018` became `1`
|
||||
- `system.platform[:finger]` is just the concatenation of the name and the first release number (except for Ubuntu which gives `20.04` for example)
|
|
@ -0,0 +1,18 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# vim: ft=yaml
|
||||
---
|
||||
name: share
|
||||
title: Inspec shared resources
|
||||
maintainer: SaltStack Formulas
|
||||
license: Apache-2.0
|
||||
summary: shared resources
|
||||
supports:
|
||||
- platform-name: debian
|
||||
- platform-name: ubuntu
|
||||
- platform-name: centos
|
||||
- platform-name: fedora
|
||||
- platform-name: opensuse
|
||||
- platform-name: suse
|
||||
- platform-name: freebsd
|
||||
- platform-name: amazon
|
||||
- platform-name: arch
|
|
@ -0,0 +1,99 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
# system.rb -- InSpec resources for system values
|
||||
# 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,
|
||||
name: build_platform_name,
|
||||
release: build_platform_release,
|
||||
finger: build_platform_finger
|
||||
}
|
||||
end
|
||||
|
||||
def build_platform_family
|
||||
case inspec.platform[:name]
|
||||
when 'arch'
|
||||
'arch'
|
||||
else
|
||||
inspec.platform[:family]
|
||||
end
|
||||
end
|
||||
|
||||
def build_platform_name
|
||||
case inspec.platform[:name]
|
||||
when 'amazon'
|
||||
'amazonlinux'
|
||||
else
|
||||
inspec.platform[:name]
|
||||
end
|
||||
end
|
||||
|
||||
def build_platform_release
|
||||
case inspec.platform[:name]
|
||||
when 'amazon'
|
||||
# `2018` relase is named `1` in kitchen.yaml
|
||||
inspec.platform[:release].gsub(/2018.*/, '1')
|
||||
when 'arch'
|
||||
'base-latest'
|
||||
else
|
||||
inspec.platform[:release]
|
||||
end
|
||||
end
|
||||
|
||||
def build_platform_finger
|
||||
"#{build_platform_name}-#{build_finger_release}"
|
||||
end
|
||||
|
||||
def build_finger_release
|
||||
case inspec.platform[:name]
|
||||
when 'ubuntu'
|
||||
build_platform_release.split('.').slice(0, 2).join('.')
|
||||
else
|
||||
build_platform_release.split('.')[0]
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue