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

Conversation

FalcoSuessgott
Copy link
Collaborator

@FalcoSuessgott FalcoSuessgott commented Jun 29, 2024

merge after #99
fixes #89

Changes

  • updated README.md with a new more up-to-date example showcasing how to use the provisioner easily locally
  • added a docker example to examples/
  • moved previous virtualbox examples to examples/vbox
  • added output_file attribute for outputting the results using tee (Linux) and Tee-Object (Windows)

Example

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"]
  }
}

running make local will build & install the plugin and execute it:

docker.alpine: output will be in this color.

==> docker.alpine: Creating a temporary directory for sharing data...
==> docker.alpine: Pulling Docker image: alpine
    docker.alpine: Using default tag: latest
    docker.alpine: latest: Pulling from library/alpine
    docker.alpine: Digest: sha256:b89d9c93e9ed3597455c90a0b88a8bbb5cb7188438f70953fede212a0c4394e0
    docker.alpine: Status: Image is up to date for alpine:latest
    docker.alpine: docker.io/library/alpine:latest
==> docker.alpine: Starting docker container...
    docker.alpine: Run command: docker run -v /home/morelly_t1/.config/packer/tmp3486027672:/packer-files -d -i -t --entrypoint=/bin/sh -- alpine
    docker.alpine: Container ID: d13395b14141df26c8a5e9e700fc72fd8dc6cf0c3060d8c34cea5ec5c5b95ab6
==> docker.alpine: Using docker communicator to connect: 172.17.0.3
==> docker.alpine: Provisioning with Goss
==> docker.alpine: Configured to run on Linux
    docker.alpine: Creating directory: /tmp/goss
    docker.alpine: Installing Goss from, https://github.com/goss-org/goss/releases/download/v0.4.7/goss-linux-amd64
    docker.alpine: Downloading Goss to /tmp/goss_install
==> docker.alpine: /bin/sh: curl: not found
    docker.alpine: goss version v0.4.7
==> docker.alpine: Uploading goss tests...
    docker.alpine: Uploading ./goss.yaml
==> docker.alpine: 
==> docker.alpine: 
==> docker.alpine: 
==> docker.alpine: Running goss tests...
==> docker.alpine: Running GOSS render command: cd /tmp/goss &&  /tmp/goss_install    render > /tmp/goss-spec.yaml
==> docker.alpine: Goss render ran successfully
==> docker.alpine: Running GOSS render debug command: cd /tmp/goss &&  /tmp/goss_install    render -d > /tmp/debug-goss-spec.yaml
==> docker.alpine: Goss render debug ran successfully
==> docker.alpine: Running GOSS validate command: cd /tmp/goss &&   /tmp/goss_install    validate --retry-timeout 0s --sleep 1s -f junit  | tee "/tmp/goss_test_results.xml"
    docker.alpine: <?xml version="1.0" encoding="UTF-8"?>
    docker.alpine: <testsuite name="goss" errors="0" tests="1" failures="1" skipped="0" time="0.000" timestamp="2024-07-06T05:14:34Z">
    docker.alpine: <testcase name="Process sshd running" time="0.000">
    docker.alpine: <system-err>Process: sshd: running: Expected false to equal true</system-err>
    docker.alpine: <failure>Process: sshd: running: Expected false to equal true</failure>
    docker.alpine: </testcase>
    docker.alpine: </testsuite>
==> docker.alpine: Goss validate ran successfully
==> docker.alpine: 
==> docker.alpine: 
==> docker.alpine: 
==> docker.alpine: Downloading Goss test result file
    docker.alpine: Downloading Goss test results from /tmp/goss_test_results.xml to current dir
==> docker.alpine: 
==> docker.alpine: 
==> docker.alpine: 
==> docker.alpine: Downloading spec file and debug info
    docker.alpine: Downloading Goss specs from, /tmp/goss-spec.yaml and /tmp/debug-goss-spec.yaml to current dir
==> docker.alpine: Running local shell script: /tmp/packer-shell2227336962
    docker.alpine: <?xml version="1.0" encoding="UTF-8"?>
    docker.alpine: <testsuite name="goss" errors="0" tests="1" failures="1" skipped="0" time="0.000" timestamp="2024-07-06T05:14:34Z">
    docker.alpine: <testcase name="Process sshd running" time="0.000">
    docker.alpine: <system-err>Process: sshd: running: Expected false to equal true</system-err>
    docker.alpine: <failure>Process: sshd: running: Expected false to equal true</failure>
    docker.alpine: </testcase>
    docker.alpine: </testsuite>
==> docker.alpine: Exporting the container
==> docker.alpine: Killing the container: d13395b14141df26c8a5e9e700fc72fd8dc6cf0c3060d8c34cea5ec5c5b95ab6
Build 'docker.alpine' finished after 11 seconds 770 milliseconds.

==> Wait completed after 11 seconds 770 milliseconds

==> Builds finished. The artifacts of successful builds are:
--> docker.alpine: Exported Docker file: alpine.tar

@FalcoSuessgott FalcoSuessgott changed the title Dest dir 2 Adds output_file as a attribute to redirect goss output to the specified file Jun 29, 2024
@FalcoSuessgott FalcoSuessgott force-pushed the dest_dir_2 branch 3 times, most recently from c1eac1c to d7c0606 Compare June 29, 2024 12:14
@FalcoSuessgott FalcoSuessgott marked this pull request as draft June 29, 2024 13:34
@mattlorimor
Copy link

I couldn't find anything in the goss-org/goss code nor documentation that allows one to tell Goss itself to write the test results to an output file.

I like that this provisioner has a possible workaround for that, but this also seems like something that should be an argument on goss validate that this provisioner simply has to inject if configured.

I may take a look and see how hard it'd be to add that functionality to goss. In the event that it does get added, I'd expect that this provisioner would just have to switch from using tee to injecting the argument instead.

@mattlorimor
Copy link

You could look at utilizing Tee-Object for the Windows use case.

@FalcoSuessgott
Copy link
Collaborator Author

Thanks for the tip. I will have a look at it on the weekend. I agree with this, should have already been an option in upstream goss. Maybe you can have a look at it :D

@mattlorimor
Copy link

Oh this actually is a request in goss already: goss-org/goss#875.

@FalcoSuessgott
Copy link
Collaborator Author

You could look at utilizing Tee-Object for the Windows use case.

Is this included in the default powershell installation in all Windows Versions?

@mattlorimor
Copy link

I think so. It's part of Microsoft.PowerShell.Utility. Which, I'm fairly certain, has been included on Windows installations for a while - especially server OS flavors.

Microsoft is even nice enough to include a tee alias.

@mattlorimor
Copy link

I guess I wouldn't say all Windows versions. But it would appear to be present on pretty much anything anybody would be building today. The documentation links I included show it going as far back as PowerShell 5.0, which was released in February of 2016.

@mattlorimor
Copy link

If you want to be cautious, you could simply include in the documentation for the new output argument/parameter that, when using it on Windows, PowerShell is expected to be present at a version of at least 5.0 or higher due to the dependency on Tee-Object.

@FalcoSuessgott
Copy link
Collaborator Author

Thanks for the information. Will update the PR accordingly and hopefully get it merged very soon.

@FalcoSuessgott FalcoSuessgott marked this pull request as ready for review July 6, 2024 05:16
@FalcoSuessgott
Copy link
Collaborator Author

Alright, added some more examples/docs and implemented the Windows Version using Tee-Object, now waiting for some reviews

@FalcoSuessgott
Copy link
Collaborator Author

superseded by #101

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Specify Results directory
2 participants