Add an actual delete of an asset from the test instance of Snipe-IT

* Add delete InSpec test
* Add purge via SSH to pipeline
* Add exists? and deleted? methods to Asset class
* Update delete ChefSpec
* Update azure-pipelines.yml
* Use macOS pool to enable support for vagrant.
* Update kitchen.yml
* Remove macos platforms for now.
* Fix indentation for always condition for the SSH command
* Utilize Linux pool since we're not testing on macOS yet.
* Enable batching of changes in the pipeline
This commit is contained in:
Eammon Hanlon 2018-11-26 08:13:44 -08:00
Родитель fd915af2ac
Коммит f86d0d79c6
10 изменённых файлов: 124 добавлений и 45 удалений

Просмотреть файл

@ -2,6 +2,9 @@ resources:
- repo: self
clean: true
trigger:
batch: true
name: $(SourceBranchName)_$(Date:yyyyMMdd)$(Rev:.r)
jobs:
@ -75,4 +78,12 @@ jobs:
tkAzureEndpoint: 'Apex Lab - CorpNet'
tkCommand: destroy
tkKitchenFile: kitchen.yml
condition: always()
condition: always()
- task: SSH@0
displayName: 'Purge the soft deletes from the Snipe-IT test instance'
condition: always()
inputs:
sshEndpoint: snipeit-test-endpoint
runOptions: inline
inline: 'cd /var/www/snipeit; php artisan snipeit:purge -n --force=true'

Просмотреть файл

@ -4,15 +4,19 @@ driver:
provider: parallels
provisioner:
product_name: chef
product_version: latest
always_update_cookbooks: true
multiple_converge: 2
enforce_idempotency: true
data_bags_path: ./test/fixtures/data_bags
attributes:
snipeit:
api:
instance: <%= ENV['SNIPEIT_URL'] %>
token: <%= ENV['SNIPEIT_TOKEN'] %>
verifier:
name: inspec
inspec_tests:
- test/integration/default
reporter:
- cli
- junit:/tmp/inspec.xml
@ -31,17 +35,21 @@ platforms:
vnet_id: /subscriptions/<%= ENV['AZURE_SUBSCRIPTION_ID'] %>/resourceGroups/<%= ENV['AZURE_ER_RESOURCE_GROUP'] %>/providers/Microsoft.Network/virtualNetworks/<%= ENV['AZURE_ER_VNET_NAME'] %>
subnet_id: Subnet-1
- name: macos-10.13
- name: macos-10.14
suites:
- name: default
- name: configure
run_list:
- recipe[snipeit_api_test::configure_snipeit]
includes:
- ubuntu-16.04
attributes:
snipeit:
api:
instance: <%= ENV['SNIPEIT_URL'] %>
token: <%= ENV['SNIPEIT_TOKEN'] %>
verifier:
controls:
- create-manufacturers
- create-categories
- create-models
- create-locations
- create-assets
- name: delete
run_list:
- recipe[snipeit_api_test::delete_items_from_snipeit]
verifier:
controls:
- delete-assets

Просмотреть файл

@ -12,15 +12,16 @@ class Asset
@endpoint_url = endpoint.snipeit_url
end
class DoesNotExistError < StandardError
def exist?
!@asset.response['rows'].empty?
end
def deleted?
exist? && current_value['deleted_at']
end
def current_value
if @asset.response['rows'].empty?
raise Asset::DoesNotExistError, "#{@serial_number} does not exist in the database!"
else
@asset.response['rows'].first
end
@asset.response['rows'].first
end
def asset_tag

Просмотреть файл

@ -53,8 +53,9 @@ describe 'snipeit_api::asset - create action' do
end
context 'when the asset does not exist' do
serial_number = 'W81123456789'
before do
stub_request(:get, "#{hardware_endpoint}/byserial/W81123456789")
stub_request(:get, "#{hardware_endpoint}/byserial/#{serial_number}")
.to_return(body: empty_response)
end
@ -62,7 +63,7 @@ describe 'snipeit_api::asset - create action' do
asset 'create a machine' do
machine_name 'Does Not Exist'
asset_tag '0000000'
serial_number 'W81123456789'
serial_number serial_number
model 'MacPro4,1'
location 'Building 1'
token chef_vault_item('snipe-it', 'api')['key']
@ -74,7 +75,7 @@ describe 'snipeit_api::asset - create action' do
rtd_location_id: 1,
name: 'Does Not Exist',
asset_tag: '0000000',
serial: 'W81123456789',
serial: serial_number,
status_id: 1,
model_id: 4,
}

Просмотреть файл

@ -2,20 +2,20 @@ require 'spec_helper'
describe 'snipeit_api::asset - delete action' do
step_into :asset
context 'when the asset exists' do
before do
stub_request(:get, "#{hardware_endpoint}/byserial/W80123456789")
.to_return(body: {
total: 1,
rows: [
{
id: 1,
serial: 'W80123456789',
},
],
}.to_json
)
.to_return(
body: {
total: 1,
rows: [
{
id: 1,
serial: 'W80123456789',
},
],
}.to_json
)
end
recipe do
@ -36,4 +36,43 @@ describe 'snipeit_api::asset - delete action' do
)
}
end
context 'when the asset exists and is already marked as deleted' do
before do
stub_request(:get, "#{hardware_endpoint}/byserial/W11123456789")
.to_return(
body: {
total: 1,
rows: [
{
id: 1,
serial: 'W11123456789',
deleted_at: {
date_time: '2018-11-24 12:30:12',
formatted: '2018-11-24 12:30 PM',
},
},
],
}.to_json
)
end
recipe do
asset 'delete' do
serial_number 'W11123456789'
token chef_vault_item('snipe-it', 'api')['key']
url node['snipeit']['api']['instance']
action :delete
end
end
it {
is_expected.to_not delete_http_request('delete W11123456789')
.with(
url: ::File.join(hardware_endpoint, '1'),
message: 'delete W11123456789',
headers: headers
)
}
end
end

Просмотреть файл

@ -0,0 +1,9 @@
api_token = node['snipeit']['api']['token']
url = node['snipeit']['api']['instance']
asset 'delete asset' do
serial_number 'HALAEK123123'
token api_token
url url
action :delete
end

Просмотреть файл

@ -1,3 +1 @@
# Example InSpec Profile
This example shows the implementation of an InSpec profile.
# Snipe-IT API InSpec Profile

Просмотреть файл

@ -2,7 +2,7 @@ url = attribute('url', decription: 'The Snipe-IT URL')
token = attribute('api_token', description: 'The API token for Snipe-IT')
snipeit = snipeit_api(url, token)
control 'manufacturers' do
control 'create-manufacturers' do
impact 0.7
title 'Create manufacturers'
describe json(content: snipeit.response('manufacturers', 'Apple').body) do
@ -12,7 +12,7 @@ control 'manufacturers' do
end
end
control 'categories' do
control 'create-categories' do
impact 0.7
title 'Create categories'
categories = ['macOS - Desktop', 'macOS - Portable']
@ -25,7 +25,7 @@ control 'categories' do
end
end
control 'models' do
control 'create-models' do
impact 0.7
title 'Create models'
describe json(content: snipeit.response('models', 'Mac Pro (Early 2009)').body) do
@ -35,7 +35,7 @@ control 'models' do
end
end
control 'locations' do
control 'create-locations' do
impact 0.7
title 'Create locations'
describe json(content: snipeit.response('locations', 'Building 1').body) do
@ -49,14 +49,14 @@ control 'locations' do
end
end
control 'assets' do
control 'create-assets' do
impact 0.7
title 'Create assets'
describe json(content: snipeit.response('hardware', 'HALAEK123123').body) do
its('total') { should cmp 1 }
its('rows.first') { should include 'asset_tag' => '1234567' }
its('rows.first') { should include 'serial' => 'HALAEK123123' }
its(['rows', 0, 'status_label']) { should include 'name' => 'Pending' }
its('rows.first') { should include 'model_number' => 'MacPro4,1' }
its('rows.first') { should include 'deleted_at' => nil }
end
end

Просмотреть файл

@ -0,0 +1,12 @@
url = attribute('url', decription: 'The Snipe-IT URL')
token = attribute('api_token', description: 'The API token for Snipe-IT')
snipeit = snipeit_api(url, token)
control 'delete-assets' do
impact 0.7
title 'Delete assets'
describe json(content: snipeit.response('hardware', 'HALAEK123123').body) do
its('total') { should cmp 0 }
its('rows.first') { should cmp nil }
end
end

Просмотреть файл

@ -5,4 +5,4 @@ copyright: Microsoft
copyright_email: chef@microsoft.com
license: MIT
summary: Verify that Snipe-IT is configured via the API
version: 0.1.0
version: 0.2.0