k8s 中文文档 k8s 中文文档
指南
kubernetes.io (opens new window)
指南
kubernetes.io (opens new window)
  • k8s 是什么
  • 互动教程

  • Minikube 介绍

  • 概念

  • Kubectl CLI

  • Kubectl 命令表

  • 安装设置

  • API 使用

  • 集群管理

  • TASKS

Goss - Quick and Easy server validation



Goss in 45 seconds


Note:For an even faster way of doing this, see: autoadd

Note:For testing docker containers see the dgoss wrapper. Also, user submitted wrapper scripts for Kubernetes kgoss and Docker Compose dcgoss.

Note:For some Docker/Kubernetes healthcheck, health endpoint, and container ordering examples, see my blog post here.

Introduction


What is Goss?


Goss is a YAML based serverspec alternative tool for validating a server’s configuration. It eases the process of writing tests by allowing the user to generate tests from the current system state. Once the test suite is written they can be executed, waited-on, or served as a health endpoint.

Why use Goss?


Goss is EASY! - Goss in 45 seconds
Goss is FAST! - small-medium test suites are near instantaneous, see benchmarks
Goss is SMALL! - <10MB single self-contained binary

Installation


Note:For macOS and Windows, see: platform-feature-parity.

This will install goss and dgoss.

Note:Using curl | sh is not recommended for production systems, use manual installation below.

  1. ``` shell
  2. # Install latest version to /usr/local/bin
  3. curl -fsSL https://goss.rocks/install | sh

  4. # Install v0.3.16 version to ~/bin
  5. curl -fsSL https://goss.rocks/install | GOSS_VER=v0.3.16 GOSS_DST=~/bin sh
  6. ```

Manual installation


Latest


  1. ``` shell
  2. curl -L https://github.com/goss-org/goss/releases/latest/download/goss-linux-amd64 -o /usr/local/bin/goss
  3. chmod +rx /usr/local/bin/goss

  4. curl -L https://github.com/goss-org/goss/releases/latest/download/dgoss -o /usr/local/bin/dgoss
  5. # Alternatively, using the latest master
  6. # curl -L https://raw.githubusercontent.com/goss-org/goss/master/extras/dgoss/dgoss -o /usr/local/bin/dgoss
  7. chmod +rx /usr/local/bin/dgoss
  8. ```

Specific Version


  1. ``` shell
  2. # See https://github.com/goss-org/goss/releases for release versions
  3. VERSION=v0.3.10
  4. curl -L "https://github.com/goss-org/goss/releases/download/${VERSION}/goss-linux-amd64" -o /usr/local/bin/goss
  5. chmod +rx /usr/local/bin/goss

  6. # (optional) dgoss docker wrapper (use 'master' for latest version)
  7. VERSION=v0.3.10
  8. curl -L "https://github.com/goss-org/goss/releases/download/${VERSION}/dgoss" -o /usr/local/bin/dgoss
  9. chmod +rx /usr/local/bin/dgoss
  10. ```

Build it yourself


  1. ``` shell
  2. make build
  3. ```

Full Documentation


Documentation is available here: manual

Quick start


Writing a simple sshd test


An initial set of tests can be derived from the system state by using the add or autoadd commands.

Let's write a simple sshd test using autoadd.

  1. ``` adblock
  2. # Running it as root will allow it to also detect ports
  3. $ sudo goss autoadd sshd
  4. ```

Generated goss.yaml :

  1. ``` yaml
  2. $ cat goss.yaml
  3. port:
  4.   tcp:22:
  5.     listening: true
  6.     ip:
  7.     - 0.0.0.0
  8.   tcp6:22:
  9.     listening: true
  10.     ip:
  11.     - '::'
  12. service:
  13.   sshd:
  14.     enabled: true
  15.     running: true
  16. user:
  17.   sshd:
  18.     exists: true
  19.     uid: 74
  20.     gid: 74
  21.     groups:
  22.     - sshd
  23.     home: /var/empty/sshd
  24.     shell: /sbin/nologin
  25. group:
  26.   sshd:
  27.     exists: true
  28.     gid: 74
  29. process:
  30.   sshd:
  31.     running: true
  32. ```

Now that we have a test suite, we can:

Run it once

  1. ``` adblock
  2. goss validate
  3. ...............

  4. Total Duration: 0.021s # <- yeah, it's that fast..
  5. Count: 15, Failed: 0
  6. ```

Edit it to use templates, and run with a vars file

  1. ``` adblock
  2. goss --vars vars.yaml validate
  3. ```

keep running it until the system enters a valid state or we timeout

  1. ``` adblock
  2. goss validate --retry-timeout 30s --sleep 1s
  3. ```

serve the tests as a health endpoint

  1. ``` adblock
  2. goss serve &
  3. curl localhost:8080/healthz

  4. # JSON endpoint
  5. goss serve --format json &
  6. curl localhost:8080/healthz

  7. # rspecish response via content negotiation
  8. goss serve --format json &
  9. curl -H "Accept: application/vnd.goss-rspecish" localhost:8080/healthz
  10. ```

Manually editing Goss files


Goss files can be manually edited to improve readability and expressiveness of tests.

A Json draft 7 schema available in docs/goss-json-schema.yaml makes it easier to edit simple goss.yaml files in IDEs, providing usual coding assistance such as inline documentation, completion and static analysis. See PR 793 for screenshots.

For example, to configure the Json schema in JetBrains intellij IDEA, follow documented instructions, with arguments such as schema url=https://raw.githubusercontent.com/goss-org/goss/master/docs/goss-json-schema.yaml, schema version=Json schema version 7, file path pattern=*/goss.yaml

In addition, Goss files can also be further manually edited (without yet full json support) to use:

Patterns
Advanced Matchers
Templates
title and meta (arbitrary data) attributes are persisted when adding other resources with goss add

Some examples:

  1. ``` yaml
  2. user:
  3.   sshd:
  4.     title: UID must be between 50-100, GID doesn't matter. home is flexible
  5.     meta:
  6.       desc: Ensure sshd is enabled and running since it's needed for system management
  7.       sev: 5
  8.     exists: true
  9.     uid:
  10.       # Validate that UID is between 50 and 100
  11.       and:
  12.         gt: 50
  13.         lt: 100
  14.     home:
  15.       # Home can be any of the following
  16.       or:
  17.       - /var/empty/sshd
  18.       - /var/run/sshd

  19. package:
  20.   kernel:
  21.     installed: true
  22.     versions:
  23.       # Must have 3 kernels and none of them can be 4.4.0
  24.       and:
  25.       - have-len: 3
  26.       - not:
  27.           contain-element: 4.4.0

  28.   # Loaded from --vars YAML/JSON file
  29.   {{.Vars.package}}:
  30.     installed: true

  31. {{if eq .Env.OS "centos"}}
  32.   # This test is only when $OS environment variable is set to "centos"
  33.   libselinux:
  34.     installed: true
  35. {{end}}
  36. ```

Goss.yaml files with templates can still be validated through the Json schema after being rendered using the goss render command. See example below

  1. ``` shell
  2. cd docs
  3. goss --vars ./vars.yaml render > rendered_goss.yaml
  4. # proceed with json schema validation of rendered_goss.yaml in your favorite IDE
  5. # or in one of the Json schema validator listed in https://json-schema.org/implementations.html
  6. # The following example is for a Linux AMD64 host
  7. curl -LO https://github.com/neilpa/yajsv/releases/download/v1.4.1/yajsv.linux.amd64
  8. chmod a+x yajsv.linux.amd64
  9. sudo mv yajsv.linux.amd64 /usr/sbin/yajsv

  10. yajsv -s goss-json-schema.yaml rendered_goss.yaml

  11. rendered_goss.yaml: fail: process.chrome: skip is required
  12. rendered_goss.yaml: fail: service.sshd: skip is required
  13. 1 of 1 failed validation
  14. rendered_goss.yaml: fail: process.chrome: skip is required
  15. rendered_goss.yaml: fail: service.sshd: skip is required
  16. ```

Full list of available Json schema validators can be found in https://json-schema.org/implementations.html#validator-command%20line

Supported resources


package - add new package
file - add new file
addr - add new remote address:port - ex: google.com:80
port - add new listening [protocol]:port - ex: 80 or udp:123
service - add new service
user - add new user
group - add new group
command - add new command
dns - add new dns
process - add new process name
kernel-param - add new kernel-param
mount - add new mount
interface - add new network interface
http - add new network http url with proxy support
goss - add new goss file, it will be imported from this one
matching - test for matches in supplied content

Supported output formats


rspecish - (default)Similar to rspec output
documentation - Verbose test results
json - JSON, detailed test result
tap - TAP style
junit - JUnit style
nagios - Nagios/Sensu compatible output /w exit code 2 for failures.
prometheus - Prometheus compatible output.
silent - No output. Avoids exposing system information (e.g. when serving tests as a healthcheck endpoint).

Community Contributions


goss-ansible - Ansible module for Goss.
degoss - Ansible role for installing, running, and removing Goss in a single go.
kitchen-goss - A test-kitchen verifier plugin for Goss.
goss-fpm-files - Might be useful for building goss system packages.
molecule - Automated testing for Ansible roles, with native Goss support.
packer-provisioner-goss - A packer plugin to run Goss as a provision step.
gossboss - Collect and view aggregated Goss test results from multiple remote Goss servers.

Limitations


goss works well on Linux, but support on Windows & macOS is alpha. See platform-feature-parity.

The following tests have limitations.

Package:

rpm
deb
Alpine apk
pacman

Service:

systemd
sysV init
OpenRC init
Upstart
Last Updated: 2023-09-03 19:17:54