Skip to content

Commit

Permalink
test(inspec): add golang archive unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
noelmcloughlin committed Jun 15, 2019
1 parent c76d7cd commit 6feafa9
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 18 deletions.
12 changes: 9 additions & 3 deletions pillar.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ golang:
winner: lookup
added_in_lookup: lookup_value

# Using bash package and udev service as an example. This allows us to
# test the golang formula itself. You should set these parameters to
# examples that make sense in the contexto of the formula you're writing.
# Using package archive as an example to test golang formula itself.
# You should set these parameters to values that make sense.
pkg:
archive:
uri: https://storage.googleapis.com/golang

linux:
#'Alternatives system' priority: zero disables (default)
altpriority: 1000

tofs:
# The files_switch key serves as a selector for alternative
Expand Down
50 changes: 50 additions & 0 deletions test/integration/default/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Default InSpec Profile

This shows the implementation of the Default InSpec [profile](https://github.com/inspec/inspec/blob/master/docs/profiles.md).

## Verify a profile

InSpec ships with built-in features to verify a profile structure.

```bash
$ inspec check default
Summary
-------
Location: default
Profile: profile
Controls: 4
Timestamp: 2019-06-24T23:09:01+00:00
Valid: true

Errors
------

Warnings
--------
```

## Execute a profile

To run all **supported** controls on a local machine use `inspec exec /path/to/profile`.

```bash
$ inspec exec default
..

Finished in 0.0025 seconds (files took 0.12449 seconds to load)
8 examples, 0 failures
```

## Execute a specific control from a profile

To run one control from the profile use `inspec exec /path/to/profile --controls name`.

```bash
$ inspec exec default --controls package
.

Finished in 0.0025 seconds (files took 0.12449 seconds to load)
1 examples, 0 failures
```

See an [example control here](https://github.com/inspec/inspec/blob/master/examples/profile/controls/example.rb).
45 changes: 45 additions & 0 deletions test/integration/default/controls/alternatives_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
title 'linux alternatives profile'

control 'golang linux alternatives' do
impact 1.0
title 'should be installed'
desc "Ensure golang linux alternatives are correct"
tag: package: "tarball archive"

describe file('/usr/local/go') do # golang-home alternative
it { should be_symlink }
it { should_not be_file }
it { should_not be_directory }
it { should be_owned_by 'root' }
it { should be_grouped_into 'root' }
its('shallow_link_path') { should eq '/etc/alternatives/golang-home' }
end

describe file('/usr/bin/go') do # go alternative
it { should be_symlink }
it { should_not be_file }
it { should_not be_directory }
it { should be_owned_by 'root' }
it { should be_grouped_into 'root' }
its('shallow_link_path') { should eq '/etc/alternatives/link-go' }
end

describe file('/usr/bin/godoc') do # godoc alternative
it { should be_symlink }
it { should_not be_file }
it { should_not be_directory }
it { should be_owned_by 'root' }
it { should be_grouped_into 'root' }
its('shallow_link_path') { should eq '/etc/alternatives/link-godoc' }
end

describe file('/usr/bin/gofmt') do # gofmt alternative
it { should be_symlink }
it { should_not be_file }
it { should_not be_directory }
it { should be_owned_by 'root' }
it { should be_grouped_into 'root' }
its('shallow_link_path') { should eq '/etc/alternatives/link-gofmt' }
end

end
36 changes: 36 additions & 0 deletions test/integration/default/controls/archives_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
title 'archives profile'

control 'golang tarball archive' do
impact 1.0
title 'should be installed'
desc "Ensure golang tarball archive was extracted correctly"
tag: package: "tarball archive"

describe file('/usr/local/go1.10.1.linux-amd64/go') do # another test
it { should be_directory }
it { should be_owned_by 'root' }
it { should be_grouped_into 'root' }
its('mode') { should cmp '0755' }
end

require 'digest'
binary = file('/usr/local/go1.10.1.linux-amd64/go/bin/go').content
sha256sum = Digest::SHA256.hexdigest(binary)
describe file('/usr/local/go1.10.1.linux-amd64/go/bin/go') do
its('sha256sum') { should eq '11438a2d41e257519e8c0ad098c287f9f73f1b8382a012a0c10e1dee5fb1e8ae' }
end

binary = file('/usr/local/go1.10.1.linux-amd64/go/bin/godoc').content
sha256sum = Digest::SHA256.hexdigest(binary)
describe file('/usr/local/go1.10.1.linux-amd64/go/bin/godoc') do
its('sha256sum') { should eq '4076bb73349f253c04e5ef214934938760eefb26529f22d2e6fdbd61b99bb4b5' }
end


binary = file('/usr/local/go1.10.1.linux-amd64/go/bin/gofmt').content
sha256sum = Digest::SHA256.hexdigest(binary)
describe file('/usr/local/go1.10.1.linux-amd64/go/bin/go') do
its('sha256sum') { should eq '5673f5914f195331322b20aee026f1882dac7c92b61c41bae23a04fb803b3e2c' }
end

end
9 changes: 0 additions & 9 deletions test/integration/default/controls/config_spec.rb

This file was deleted.

15 changes: 15 additions & 0 deletions test/integration/default/controls/environ_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
control 'template configuration environment' do
title 'should match desired lines'

describe file('/etc/default/golang.sh') do
it { should be_file }
it { should be_owned_by 'root' }
it { should be_grouped_into 'root' }
its('mode') { should cmp '0644' }
its('content') { should include 'Your changes may be overwritten' }
its('content') { should include 'export GOROOT=/usr/local/go' }
its('content') { should include 'export GOPATH=/usr/local/golang/packages' }
its('content') { should include 'export PATH=$PATH:$GOROOT/bin:$GOBASE/bin' }
its('content') { should include 'export PATH=${PATH}:/usr/local/go1.10.1.linux-amd64' }
end
end
12 changes: 6 additions & 6 deletions test/integration/default/inspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ maintainer: Your Name
license: Apache-2.0
summary: Verify that the golang formula is setup and configured correctly
supports:
- os-name: debian
- os-name: ubuntu
- os-name: centos
- os-name: fedora
- os-name: opensuse
- os-name: suse
- platform-name: debian
- platform-name: ubuntu
- platform-name: centos
- platform-name: fedora
- platform-name: opensuse
- platform-name: suse

0 comments on commit 6feafa9

Please sign in to comment.