Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds output_file as a attribute to redirect goss output to the specified file #100

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
example/packer_cache
example/packer_cache
.envrc
packer-plugin-goss
tests/debug-goss-spec.yaml
tests/goss-spec.yaml
tests/goss.json
32 changes: 32 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
default: help

.PHONY: help
help: ## list makefile targets
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

.PHONY: fmt
fmt: ## format go files
gofumpt -w .
gci write .
packer fmt -recursive -write .

.PHONY: build
build: ## build the plugin
go build -ldflags="-X github.com/YaleUniversity/packer-provisioner-goss/version.VersionPrerelease=dev" -o packer-plugin-goss

.PHONY: install
install: ## install the plugin
packer plugins install --path packer-plugin-goss github.com/YaleUniversity/goss

.PHONY: local
local: clean build install ## build and install the plugin locally
cd example && packer init .
cd example && packer build alpine.pkr.hcl

.PHONY: clean
clean: ## remove tmp files
rm -f example/alpine.tar example/goss_test_results.xml example/debug-goss-spec.yaml example/goss-spec.yaml packer-plugin-goss

.PHONY: generate
generate: ## go generate
go generate ./...
72 changes: 54 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Wouldn't it be nice if you could run [goss](https://github.com/aelsabbahy/goss)
Well, I thought it would, so now you can!

This runs during the provisioning process since the machine being provisioned is only available at that time.
There is an example packer build with goss tests in the `example/` directory.
There is an example packer build with `goss` tests in the [`example/`](https://github.com/YaleUniversity/packer-plugin-goss/tree/master/example) directory.

## Configuration

Expand All @@ -20,6 +20,58 @@ packer {
}
```

## Quickstart Example
**This example assumes you have a file `goss.yaml` in your `PWD` containing goss tests**:
```hcl
# example/alpine.pkr.hcl
packer {
required_version = ">= 1.9.0"

required_plugins {
goss = {
version = "v0.0.1"
source = "github.com/YaleUniversity/goss"
}
docker = {
source = "github.com/hashicorp/docker"
version = "v1.0.10"
}
}
}

# fetch a normal alpine container and export the image as alpine.tar
source "docker" "alpine" {
image = "alpine"
export_path = "alpine.tar"
}

build {
# apply build params against the alpine container
sources = ["docker.alpine"]

# run goss tests using goss provisioner
provisioner "goss" {
# download and install goss to /tmp/goss_install within the target system
download_path = "/tmp/goss_install"

# run goss tests in goss.yaml
tests = ["./goss.yaml"]

# output results as junit
format = "junit"

# write results to /tmp/goss_test_results.xml, which will then be copied to the hosts CWD that is running packer
output_file = "/tmp/goss_test_results.xml"
}


# output the test results just for demo purposes
provisioner "shell-local" {
inline = ["cat goss_test_results.xml"]
}
}
```

### Additional (optional) properties

```hcl
Expand All @@ -30,7 +82,7 @@ build {
# Provisioner Args
arch ="amd64"
download_path = "/tmp/goss-VERSION-linux-ARCH"
inspect = "{{ inspect_mode }}",
inspect = "{{ inspect_mode }}"
password = ""
skip_install = false
url = "https://github.com/aelsabbahy/goss/releases/download/vVERSION/goss-linux-ARCH"
Expand Down Expand Up @@ -74,22 +126,6 @@ Goss spec file and debug spec file (`goss render -d`) are downloaded to `/tmp` f

This now has support for Windows. Set the optional parameter `target_os` to `Windows`. Currently, the `vars_env` parameter must include `GOSS_USE_ALPHA=1` as specified in [goss's feature parity document](https://github.com/aelsabbahy/goss/blob/master/docs/platform-feature-parity.md#platform-feature-parity). In the future when goss come of of alpha for Windows this parameter will not be required.

## Build

### Using Golang docker image

```bash
docker run --rm -it -v "$PWD":/usr/src/packer-provisioner-goss -w /usr/src/packer-provisioner-goss -e 'VERSION=v1.0.0' golang:1.13 bash
go test ./...
for GOOS in darwin linux windows; do
for GOARCH in 386 amd64; do
export GOOS GOARCH
go get -v ./...
go build -v -o packer-provisioner-goss-${VERSION}-$GOOS-$GOARCH
done
done
```

## Author

E. Camden Fisher <[email protected]>
Expand Down
46 changes: 46 additions & 0 deletions example/alpine.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
packer {
required_version = ">= 1.9.0"

required_plugins {
goss = {
version = "v0.0.1"
source = "github.com/YaleUniversity/goss"
}
docker = {
source = "github.com/hashicorp/docker"
version = "v1.0.10"
}
}
}

# fetch a normal alpine container and export the image as alpine.tar
source "docker" "alpine" {
image = "alpine"
export_path = "alpine.tar"
}

build {
# apply build params against the alpine container
sources = ["docker.alpine"]

# run goss tests using goss provisioner
provisioner "goss" {
# download and install goss to /tmp/goss_install
download_path = "/tmp/goss_install"

# run goss tests in goss.yaml
tests = ["./goss.yaml"]

# output results as junit
format = "junit"

# write results to /tmp/goss_test_results.xml, which will be copied to the host
output_file = "/tmp/goss_test_results.xml"
}


# output the test results just for demo purposes
provisioner "shell-local" {
inline = ["cat goss_test_results.xml"]
}
}
3 changes: 3 additions & 0 deletions example/goss.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
process:
sshd:
running: true
91 changes: 0 additions & 91 deletions example/packer.json

This file was deleted.

File renamed without changes.
2 changes: 1 addition & 1 deletion example/http/ks7.cfg → example/vbox/http/ks7.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,4 @@ chkconfig yum-updateonboot off

echo 'useDNS no' >> /etc/ssh/sshd_config
yum clean all
%end
%end
91 changes: 91 additions & 0 deletions example/vbox/packer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
{
"builders": [
{
"boot_command": [
"<tab> text ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/ks7.cfg<enter>"
],
"boot_wait": "10s",
"communicator": "ssh",
"disk_size": 10240,
"guest_os_type": "RedHat_64",
"hard_drive_interface": "scsi",
"headless": true,
"http_directory": "http",
"iso_checksum": "sha256:{{user `iso_checksum`}}",
"iso_url": "{{user `iso_url`}}",
"name": "centos-7-x86_64",
"output_directory": "img_centos_7_virtualbox",
"shutdown_command": "echo 'packer'|sudo -S /sbin/halt -h -p",
"ssh_handshake_attempts": 50,
"ssh_password": "changeme",
"ssh_port": 22,
"ssh_timeout": "20000s",
"ssh_username": "root",
"type": "virtualbox-iso",
"vboxmanage": [
[
"modifyvm",
"{{.Name}}",
"--paravirtprovider",
"kvm"
],
[
"modifyvm",
"{{.Name}}",
"--nictype1",
"virtio"
],
[
"modifyvm",
"{{.Name}}",
"--memory",
"1024"
],
[
"modifyvm",
"{{.Name}}",
"--cpus",
"1"
]
],
"virtualbox_version_file": ".vbox_version"
}
],
"post-processors": [
{
"type": "vagrant"
}
],
"provisioners": [
{
"execute_command": "{{ .Vars }} /bin/sh '{{.Path}}'",
"scripts": [
"scripts/vagrant.sh"
],
"type": "shell"
},
{
"goss_file": "goss.yaml",
"retry_timeout": "5s",
"sleep": "2s",
"tests": [
"goss/goss.yaml"
],
"type": "goss"
},
{
"execute_command": "{{ .Vars }} /bin/sh '{{.Path}}'",
"scripts": [
"scripts/cleanup.sh",
"scripts/zerodisk.sh"
],
"type": "shell"
}
],
"variables": {
"iso_checksum": "b79079ad71cc3c5ceb3561fff348a1b67ee37f71f4cddfec09480d4589c191d6",
"iso_checksum_type ": "sha256",
"iso_url": "http://mirror.net.cen.ct.gov/centos/7.9.2009/isos/x86_64/CentOS-7-x86_64-NetInstall-2009.iso"
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ rm -rf VBoxGuestAdditions_*.iso

# Remove traces of mac address from network configuration
sed -i /HWADDR/d /etc/sysconfig/network-scripts/ifcfg-eth0
rm -f /etc/udev/rules.d/70-persistent-net.rules
rm -f /etc/udev/rules.d/70-persistent-net.rules
File renamed without changes.
Loading
Loading