• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

garethr/kubetest: Write unit tests for your Kubernetes configurations

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

garethr/kubetest

开源软件地址(OpenSource Url):

https://github.com/garethr/kubetest

开源编程语言(OpenSource Language):

Go 79.7%

开源软件介绍(OpenSource Introduction):

Kubetest was an interesting experiment, but I've mainly moved my focus to Conftest which has a similar, but broader, goal. Conftest also uses the much more powerful, and supported, Rego language from Open Polciy Agent. Given this, I'm archiving this repository.

Kubetest

kubetest is a tool for running tests against a Kubernetes YAML or JSON configuration file. These tests can be used to enforce local or global best-practices, for example:

  • Ensuring certain labels are set
  • Prevent usage of images with the latest tag
  • Prohibit privileged containers
  • Enforce a naming convention for different resources

Build Status Go Report Card GoDoc Coverage Status

kubetest is currently alpha quality and undoutedly has a few issues. Things will change, hopefully for the better. Please open issues if you have feedback when trying it out.

Writing tests

Tests are written in Skylark, which is a small dialect of Python suitable for embedding in other programmes. This means you do not need an additional interpreter installed to run tests with kubetest. kubetest prioritises interopability over flexibility in this regard. Tests for Kubetest just require the kubetest binary to run. Let's take a look at an example test:

#// vim: set ft=python:
def test_for_team_label():
    if spec["kind"] == "Deployment":
        labels = spec["spec"]["template"]["metadata"]["labels"]
        assert_contains(labels, "team", "should indicate which team owns the deployment")

test_for_team_label()

Save the test file in a directory called tests, with an extension of .sky. You can change the default directory name using the --tests flag. You can now run kubetest against your configuration files.

$ kubetest my-deployment.yaml
WARN my-deployment.yaml Deployment should have at least 4 replicas
$ echo $?
1

If any of the tests fail then kubetest will return a non-zero exit code.

By default kubetest outputs information about failing tests only, but you can pass --verbose to get information about passing tests as well.

$ kubetest rc.yaml --verbose 
INFO rc.yaml should not use latest images
WARN rc.yaml ReplicationController should have at least 4 replicas

The spec variable

spec is a global variable passed into the Skylark code which contains the structure of the Kubernetes configuration passed in to kubetest. You'll need to be reasonably familiar with the structure of the Kubernetes API objects to write tests, but it is possible to write helper methods for common assertions.

Assertions

kubetest automatically makes available a set of assertions to make writing tests in Skylark more pleasant. A failed assertion results in kubetest exiting with a non-zero exit code, and assertions output results as shown above.

  • assert_equal
  • assert_contains
  • assert_not_contains
  • assert_not_equal
  • assert_nil
  • assert_not_nil
  • fail
  • fail_now
  • assert_empty
  • assert_not_empty
  • assert_true
  • assert_false

Assertions take zero, one or two arguments (noted above) depending on what they are comparing. They then take an additional message argument which is output when the assertion runs. For example the following assertion checks whether the variable labels contains the value team.

assert_contains(labels, "team", "should indicate which team owns the deployment")

Installation

Tagged versions of kubetest are built by Travis and automatically uploaded to GitHub. This means you should find tar.gz files under the release tab. These should contain a single kubetest binary for platform in the filename (ie. windows, linux, darwin). Either execute that binary directly or place it on your path.

wget https://github.com/garethr/kubetest/releases/download/0.1.0/kubetest-darwin-amd64.tar.gz
tar xf kubetest-darwin-amd64.tar.gz
cp kubetest /usr/local/bin

Windows users can download tar or zip files from the releases tab.

CLI

$ kubetest --help
Run tests against a Kubernetes YAML file

Usage:
  kubetest <file> [file...] [flags]

Flags:
  -h, --help           help for kubetest
      --json           Output results as JSON
  -t, --tests string   Test directory (default "tests")
      --verbose        Output passes as well as failures

Thanks

A big thank you goes to the authors of stretchr/testify from where much of the assertion code has been ported.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
intridea/multi_json: A generic swappable back-end for JSON handling.发布时间:2022-07-08
下一篇:
junbaor/json: json 在线格式化工具发布时间:2022-07-08
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap