goju

package module
v0.0.0-...-10e7477 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 21, 2019 License: Apache-2.0 Imports: 10 Imported by: 1

README

Goju

Goju is for Go JSON UNIT tests.

The idea

It is intended to test JSON files using other json files

{
  "items": [ {
               "spec": {
                 "containers": [ {
                                 "image": "k8s.gcr.io/heapster-amd64:v1.5.0"
                                 }
                               ]
               }
             }
           ]
}

is checked for regular expression by the JSON file

{
  "items": {
    "length" :"1",
    "spec":{
      "containers":{
        "image":{
           "matches":"^(gcr.io/(google[-_]containers|k8s-minikube)|k8s.gcr.io)"
        }
      }
    }
  }
}

This means, the items array is checked for length 1 and any images in the items/spec/containers array is checked, if the string matches the regular expression. More examples are in the data directory.

The concept is to check configurations by other configurations, and implement checks adding additional leaves in JSON or YAML.

The executable checks are invoked by reflection on a Check object and must have the same name as define in the check.go file with a leading capital letter.

Installation

Install Goju by

go get github.com/endodoce/goju

Usage

Simply call

goju  -json=data/imagepod.json -rule=data/imagerule.json

to get an output like

I0624 13:24:16.733258    3384 main.go:29] Errors       : 0
I0624 13:24:16.733543    3384 main.go:30] Checks   true: 2
I0624 13:24:16.733573    3384 main.go:31] Checks  false: 0

The errors are parsing errors like unknown functions and the check results, which are true and false are reported

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Play

func Play(json, rule string) error

Play calls traverse check a json files by the rules in the second json file

func ReadFile

func ReadFile(f string, t interface{}) error

ReadFile reads file f and unmarshal it into t, reporting the error

func ToStringValue

func ToStringValue(i interface{}) reflect.Value

ToStringValue turns the input interface into a Value based on String

Types

type Check

type Check struct{}

Check is the class providing the methods for the checks Each method must return a (bool, error) pair

func (*Check) Equals

func (t *Check) Equals(ruleValue, treeValue string) (bool, error)

Equals tracks if both strings are equal

func (*Check) Eval

func (t *Check) Eval(r, s string) (bool, error)

Eval evaluates an expression

func (*Check) Length

func (t *Check) Length(length string, array []interface{}) (bool, error)

Length compares length to the len of the arry

func (*Check) Matches

func (t *Check) Matches(r, s string) (bool, error)

Matches tracks if r matches s as a regular expression

func (*Check) Max

func (t *Check) Max(max string, val int) (bool, error)

Max compares max to val

func (*Check) Min

func (t *Check) Min(min string, val int) (bool, error)

Min compares min to the val

type TreeCheck

type TreeCheck struct {
	Check                     *Check
	ErrorHistory              list.List
	TrueCounter, FalseCounter int
}

TreeCheck is the object collection all data on a traversal

func (*TreeCheck) AddError

func (t *TreeCheck) AddError(format string, args ...interface{})

AddError adds an error to the list of errors, format and args are format used to create a formatted error message

func (*TreeCheck) Traverse

func (t *TreeCheck) Traverse(tree interface{}, rules interface{})

Traverse check if tree complies according to rules Both are dictionaries with strings as keys and dictionaries or strings as value

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL