Skip to content

Commit

Permalink
testman validate (#5)
Browse files Browse the repository at this point in the history
* validate: POST turns into PUT, returns 201 not 200

* harsher CI checks

* validate: thanks golang for that awkward actualMain func

* validate: bump to 0.4.0 from 0.3.0

* validate: note

* validate: thanks TravisCI
fenollp authored Nov 11, 2017

Verified

This commit was signed with the committer’s verified signature.
1 parent 8ccebb3 commit f9a7bcd
Showing 5 changed files with 79 additions and 37 deletions.
8 changes: 6 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -3,7 +3,9 @@ sudo: false
go: 1.x

before_install:
- set -e
- set -o errexit
- set -o errtrace
# - set -o nounset
- set -o pipefail
- go get github.com/mitchellh/gox

@@ -24,7 +26,9 @@ script:
- go test -v -race .
- gox -os 'linux darwin windows' -arch 'amd64' -output 'testman-{{.OS}}_{{.Arch}}' -ldflags "-X main.Rev=$(git describe --abbrev --dirty --always --tags)" -verbose .
- ./testman-linux_amd64 -h | grep testman
- ./testman-linux_amd64 --version | grep testman
- ./testman-linux_amd64 --help | grep testman
- ./testman-linux_amd64 -V | grep testman/
- ./testman-linux_amd64 --version | grep testman/
- ls -lha

after_script:
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -8,4 +8,5 @@ all: $(filter-out schemas.go,$(wildcard *.go)) $(wildcard misc/*.json)
go build -o $(EXE)

debug: all
DEBUG=1 ./$(EXE) test --slow
DEBUG=1 ./$(EXE) validate
DEBUG=1 ./$(EXE) test
9 changes: 8 additions & 1 deletion dialogue.go
Original file line number Diff line number Diff line change
@@ -32,7 +32,12 @@ type ymlCfg struct {
func initDialogue(apiKey string) (*ymlCfg, aCmd) {
yml := readYAML(localYML)

validationJSON := validateDocs(apiKey, yml)
validationJSON, errors := validateDocs(apiKey, yml)
if errors != nil {
reportValidationErrors(errors)
log.Fatal("Documentation validation failed")
}

cmdJSON, authToken := initPUT(apiKey, validationJSON)
cmd := unmarshalCmd(cmdJSON)

@@ -97,6 +102,7 @@ func initPUT(apiKey string, JSON []byte) ([]byte, string) {

r.Header.Set("Content-Type", mimeYAML)
r.Header.Set("Accept", mimeJSON)
r.Header.Set("User-Agent", pkgVersion)
r.Header.Set(xAPIKeyHeader, apiKey)
client := &http.Client{}

@@ -134,6 +140,7 @@ func nextPOST(cfg *ymlCfg, payload []byte) []byte {

r.Header.Set("content-type", mimeJSON)
r.Header.Set("Accept", mimeJSON)
r.Header.Set("User-Agent", pkgVersion)
r.Header.Set(xAuthTokenHeader, cfg.AuthToken)
client := &http.Client{}
start := time.Now()
72 changes: 50 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"log"
"os"

@@ -10,8 +11,9 @@ import (
//go:generate go run misc/include_jsons.go

const (
pkgVersion = "0.3.0"
pkgVersion = "0.4.0"
pkgTitle = "testman/" + pkgVersion
envAPIKey = "COVEREDCI_API_KEY"
)

var (
@@ -28,58 +30,76 @@ func init() {
isDebug = "1" == os.Getenv("DEBUG")

if isDebug {
apiRoot = "http://localhost:1042"
docsURL = "http://localhost:2042/blob"
apiRoot = "http://localhost:1042/1"
docsURL = "http://localhost:2042/1/blob"
} else {
apiRoot = "https://test.coveredci.com"
docsURL = "https://lint.coveredci.com/blob"
apiRoot = "https://test.coveredci.com/1"
docsURL = "https://lint.coveredci.com/1/blob"
}
initURL = apiRoot + "/1/init"
nextURL = apiRoot + "/1/next"
initURL = apiRoot + "/init"
nextURL = apiRoot + "/next"

unstacheInit()
}

func main() {
os.Exit(actualMain())
}

func usage() (map[string]interface{}, error) {
usage := `testman
Usage:
testman test [--slow]
testman test
testman validate
testman -h | --help
testman --version
testman -V | --version
Options:
--slow Don't phone home using Websockets
-h --help Show this screen
--version Show version`
-h, --help Show this screen
-V, --version Show version`

return docopt.Parse(usage, nil, true, pkgTitle, false)
}

func main() {
func actualMain() int {
args, err := usage()
if err != nil {
log.Fatal("!args: ", err)
log.Println("!args: ", err)
return 1
}
log.Println(args) //FIXME: use args
log.Println(args)

if !isDebug {
latest := getLatestRelease()
if isOutOfDate(pkgVersion, latest) {
log.Fatalf("A newer version of %s is available: %s\n", pkgTitle, latest)
log.Printf("A newer version of %s is available: %s\n", pkgTitle, latest)
return 3
}
}

if _, err := os.Stat(shell()); os.IsNotExist(err) {
log.Fatal(shell() + " is required")
log.Println(shell() + " is required")
return 5
}

apiKey := os.Getenv("COVEREDCI_API_KEY")
if isDebug {
apiKey = "42"
apiKey := getAPIKey()
if args["validate"].(bool) {
yml := readYAML(localYML)
_, errors := validateDocs(apiKey, yml)
if errors != nil {
reportValidationErrors(errors)
return 2
} else {
fmt.Println("No validation errors found.")
//TODO: make it easy to use returned token
return 0
}
}

if apiKey == "" {
log.Fatal("$COVEREDCI_API_KEY is unset")
log.Println("$" + envAPIKey + " is unset")
return 4
}

envSerializedPath := uniquePath()
@@ -93,7 +113,7 @@ func main() {
cmd = next(cfg, cmd)
if nil == cmd {
log.Println("We're done!")
break
return 0
}
}
}
@@ -103,3 +123,11 @@ func ensureDeleted(path string) {
log.Fatal(err)
}
}

func getAPIKey() string {
apiKey := os.Getenv(envAPIKey)
if isDebug {
apiKey = "42"
}
return apiKey
}
24 changes: 13 additions & 11 deletions validate_docs.go
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ import (
"gopkg.in/yaml.v2"
)

func validateDocs(apiKey string, yml []byte) []byte {
func validateDocs(apiKey string, yml []byte) ([]byte, []byte) {
docs := struct {
V uint `json:"v"`
Blobs map[string]string `json:"blobs"`
@@ -26,19 +26,22 @@ func validateDocs(apiKey string, yml []byte) []byte {
log.Fatal(err)
}

return validationPOST(apiKey, payload)
return validationReq(apiKey, payload)
}

func validationPOST(apiKey string, JSON []byte) []byte {
r, err := http.NewRequest(http.MethodPost, docsURL, bytes.NewBuffer(JSON))
func validationReq(apiKey string, JSON []byte) ([]byte, []byte) {
r, err := http.NewRequest(http.MethodPut, docsURL, bytes.NewBuffer(JSON))
if err != nil {
log.Fatal(err)
}

r.Header.Set("Content-Type", mimeJSON)
r.Header.Set("Accept", mimeJSON)
r.Header.Set("Accept-Encoding", "gzip, deflate, br")
r.Header.Set(xAPIKeyHeader, apiKey)
r.Header.Set("User-Agent", pkgVersion)
if apiKey != "" {
r.Header.Set(xAPIKeyHeader, apiKey)
}
client := &http.Client{}

start := time.Now()
@@ -53,15 +56,14 @@ func validationPOST(apiKey string, JSON []byte) []byte {
if err != nil {
log.Fatal("!read body: ", err)
}
log.Printf("🡱 %vμs POST %s\n 🡱 %s\n 🡳 %s\n", us, docsURL, JSON, body)
log.Printf("🡱 %vμs PUT %s\n 🡱 %s\n 🡳 %s\n", us, docsURL, JSON, body)

if resp.StatusCode == 400 {
reportValidationErrors(body)
log.Fatal("Documentation validation failed")
return nil, body
}

if resp.StatusCode != 200 {
log.Fatal("!200: ", resp.Status)
if resp.StatusCode != 201 {
log.Fatal("!201: ", resp.Status)
}

var validated struct {
@@ -75,7 +77,7 @@ func validationPOST(apiKey string, JSON []byte) []byte {
log.Fatal("Could not acquire a validation token")
}

return body
return body, nil
}

func reportValidationErrors(errors []byte) {

0 comments on commit f9a7bcd

Please sign in to comment.