Skip to content
This repository has been archived by the owner on Jul 7, 2021. It is now read-only.

Commit

Permalink
Merge branch 'devsbb-env-with-templates'
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaslorentz committed Nov 30, 2019
2 parents 25acc2e + d7ae31c commit effa5aa
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 7 deletions.
4 changes: 2 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

set -e

go get -v
go test -race -v
go vet $(go list ./... | grep -v vendor)
go test -race -v $(go list ./... | grep -v vendor)

CGO_ENABLED=0 go build -o caddy

Expand Down
3 changes: 2 additions & 1 deletion examples/Caddyfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ localhost {
restart_policy always
}

supervisor echo "Hello World {{.Replica}}" {
supervisor sh -c "echo Hello World {{.Replica}} Number = $NUMBER" {
redirect_stdout stdout
env NUMBER "{{ add 0 .Replica }}"
restart_policy on_failure
replicas 3
}
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ require (
github.com/Masterminds/semver v1.5.0 // indirect
github.com/Masterminds/sprig v2.22.0+incompatible
github.com/caddyserver/caddy v1.0.4
github.com/go-errors/errors v1.0.1
github.com/huandu/xstrings v1.2.1 // indirect
github.com/imdario/mergo v0.3.8 // indirect
github.com/mitchellh/copystructure v1.0.0 // indirect
github.com/onsi/ginkgo v1.8.0 // indirect
github.com/onsi/gomega v1.5.0 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ github.com/go-acme/lego/v3 v3.1.0/go.mod h1:074uqt+JS6plx+c9Xaiz6+L+GBb+7itGtzfc
github.com/go-acme/lego/v3 v3.2.0 h1:z0zvNlL1niv/1qA06V5X1BRC5PeLoGKAlVaWthXQz9c=
github.com/go-acme/lego/v3 v3.2.0/go.mod h1:074uqt+JS6plx+c9Xaiz6+L+GBb+7itGtzfcDM2AhEE=
github.com/go-cmd/cmd v1.0.5/go.mod h1:y8q8qlK5wQibcw63djSl/ntiHUHXHGdCkPk0j4QeW4s=
github.com/go-errors/errors v1.0.1 h1:LUHzmkK3GUKUrL/1gfBUxAHzcev3apQlezX/+O7ma6w=
github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q=
github.com/go-ini/ini v1.44.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
Expand Down Expand Up @@ -197,8 +198,12 @@ github.com/olekukonko/tablewriter v0.0.1/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXW
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs=
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.8.0 h1:VkHVNpR4iVnU8XQR6DBm8BqYjN7CRzw+xKUbVVbbW9w=
github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.4.3 h1:RE1xgDvH7imwFD45h+u2SgIfERHlS2yNG4DObb5BSKU=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/onsi/gomega v1.5.0 h1:izbySO9zDPmjJ8rDjLvkA2zJHIo+HkYXHnf7eN7SSyo=
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/openzipkin/zipkin-go v0.1.6/go.mod h1:QgAqvLzwWbR/WpD4A3cGpPtJrZXNIiJc5AZX7/PBEpw=
github.com/oracle/oci-go-sdk v7.0.0+incompatible/go.mod h1:VQb79nF8Z2cwLkLS35ukwStZIg5F66tcBccjip/j888=
github.com/ovh/go-ovh v0.0.0-20181109152953-ba5adb4cf014/go.mod h1:joRatxRJaZBsY3JAOEMcoOp05CnZzsx4scTxi95DHyQ=
Expand Down Expand Up @@ -328,6 +333,7 @@ golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190312170243-e65039ee4138/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c h1:97SnQk1GYRXJgvwZ8fadnxDOWfKvkNQHH3CtZntPSrM=
golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
google.golang.org/api v0.3.1/go.mod h1:6wY9I6uQWHQ8EM57III9mq/AjF+i8G65rmVagqKMtkk=
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
Expand Down
21 changes: 17 additions & 4 deletions supervisor/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package supervisor
import (
"log"
"strconv"
"strings"
"time"

"github.com/caddyserver/caddy"
)

// ParseOption parses supervisor options
func ParseOption(c *caddy.Controller, options *Options) {
func ParseOption(c *caddy.Controller, options *Options) bool {
v := c.Val()
switch v {
case "command":
Expand All @@ -18,6 +19,7 @@ func ParseOption(c *caddy.Controller, options *Options) {
options.Command = args[0]
} else {
log.Printf("Option 'command' expects 1 argument\n")
return false
}
break
case "args":
Expand All @@ -29,6 +31,7 @@ func ParseOption(c *caddy.Controller, options *Options) {
options.Dir = args[0]
} else {
log.Printf("Option 'dir' expects 1 argument\n")
return false
}
break
case "redirect_stdout":
Expand Down Expand Up @@ -61,10 +64,11 @@ func ParseOption(c *caddy.Controller, options *Options) {
default:
options.RestartPolicy = RestartNever
log.Printf("Invalid 'restart' option %v\n", options.RestartPolicy)
break
return false
}
} else {
log.Printf("Option 'restart' expects 1 argument\n")
return false
}
case "termination_grace_period":
args := c.RemainingArgs()
Expand All @@ -74,16 +78,21 @@ func ParseOption(c *caddy.Controller, options *Options) {
options.TerminationGracePeriod = period
} else {
log.Printf("Invalid 'termination_grace_period' value %v\n", args[0])
return false
}
} else {
log.Printf("Option 'termination_grace_period' expects 1 argument\n")
return false
}
case "env":
args := c.RemainingArgs()
if len(args) == 1 {
if len(args) == 2 {
options.Env = append(options.Env, args[0]+"="+args[1])
} else if len(args) == 1 && strings.Contains(args[0], "=") {
options.Env = append(options.Env, args[0])
} else {
log.Printf("Option 'env' expects 1 argument in format KEY=VALUE\n")
log.Printf("Option 'env' expects 2 argument in format KEY VALUE or 1 argument in format KEY=VALUE\n")
return false
}
break
case "replicas":
Expand All @@ -94,10 +103,14 @@ func ParseOption(c *caddy.Controller, options *Options) {
options.Replicas = replicas
} else {
log.Printf("Invalid 'replicas' value %v\n", args[0])
return false
}
} else {
log.Printf("Option 'replicas' expects 1 argument\n")
return false
}
break
}

return true
}
89 changes: 89 additions & 0 deletions supervisor/parser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package supervisor

import (
"testing"

"github.com/caddyserver/caddy"
"github.com/go-errors/errors"
)

func TestParseOption(t *testing.T) {
type args struct {
c *caddy.Controller
options *Options
}
tests := []struct {
name string
args args
expectedParseOptionReturn bool
assertOptions func(*Options) error
}{
{"env with variable 2 args", args{caddy.NewTestController("http", "supervisor {\nenv NUMBER \"{{ add 1 .Replica }}\"\n}"), CreateOptions()}, true, func(options *Options) error {
if len(options.Env) != 1 {
return errors.Errorf("Env should be exactly one and returned %v", len(options.Env))
}

expected := "NUMBER={{ add 1 .Replica }}"
returned := options.Env[0]
if expected != returned {
return errors.Errorf("env should be %v but returned %v", expected, returned)
}

supervisors := CreateSupervisors(options)
expected = "NUMBER=1"
returned = supervisors[0].options.Env[0]
if expected != returned {
return errors.Errorf("env should be %v but returned %v", expected, returned)
}

return nil
}},
{"env with variable 1 arg", args{caddy.NewTestController("http", "supervisor {\nenv \"NUMBER={{ add 1 .Replica }}\"\n}"), CreateOptions()}, true, func(options *Options) error {
if len(options.Env) != 1 {
return errors.Errorf("Env should be exactly one and returned %v", len(options.Env))
}

expected := "NUMBER={{ add 1 .Replica }}"
returned := options.Env[0]
if expected != returned {
return errors.Errorf("env should be %v but returned %v", expected, returned)
}

supervisors := CreateSupervisors(options)
expected = "NUMBER=1"
returned = supervisors[0].options.Env[0]
if expected != returned {
return errors.Errorf("env should be %v but returned %v", expected, returned)
}

return nil
}},
{"env variable 1 arg without =", args{caddy.NewTestController("http", "supervisor {\nenv \"NUMBER {{ add 1 .Replica }}\"\n}"), CreateOptions()}, false, func(options *Options) error {
if len(options.Env) != 0 {
return errors.Errorf("Env should be exactly zero and returned %v", len(options.Env))
}

return nil
}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.args.c.RemainingArgs()
processedBlocks := 0
for tt.args.c.NextBlock() {
processedBlocks++
if parseOptionReturn := ParseOption(tt.args.c, tt.args.options); parseOptionReturn != tt.expectedParseOptionReturn {
t.Errorf("ParseOption should return %v and returned %v", tt.expectedParseOptionReturn, parseOptionReturn)
}

if err := tt.assertOptions(tt.args.options); err != nil {
t.Error(err)
}
}

if processedBlocks == 0 {
t.Errorf("this test case seems to be empty as no block was processed")
}
})
}
}

0 comments on commit effa5aa

Please sign in to comment.