Skip to content

Commit

Permalink
Merge pull request kubernetes#12371 from yashbhutwala/add-hook-unit-t…
Browse files Browse the repository at this point in the history
…ests

add unit tests for hook commands in scope of kubernetes#12137
  • Loading branch information
k8s-ci-robot authored Apr 26, 2019
2 parents 8174202 + 010a46e commit 3004dce
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 7 deletions.
7 changes: 6 additions & 1 deletion prow/cmd/hook/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ go_test(
"//prow:configs",
],
embed = [":go_default_library"],
deps = ["//prow/plugins:go_default_library"],
deps = [
"//prow/config:go_default_library",
"//prow/flagutil:go_default_library",
"//prow/plugins:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/sets:go_default_library",
],
)

go_library(
Expand Down
11 changes: 5 additions & 6 deletions prow/cmd/hook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ func (o *options) Validate() error {
return nil
}

func gatherOptions() options {
o := options{}
fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
func gatherOptions(fs *flag.FlagSet, args ...string) options {
var o options
fs.IntVar(&o.port, "port", 8888, "Port to listen on.")

fs.StringVar(&o.configPath, "config-path", "", "Path to config.yaml.")
Expand All @@ -86,15 +85,15 @@ func gatherOptions() options {

fs.StringVar(&o.webhookSecretFile, "hmac-secret-file", "/etc/webhook/hmac", "Path to the file containing the GitHub HMAC secret.")
fs.StringVar(&o.slackTokenFile, "slack-token-file", "", "Path to the file containing the Slack token to use.")
fs.Parse(os.Args[1:])
fs.Parse(args)
o.configPath = config.ConfigPath(o.configPath)
return o
}

func main() {
o := gatherOptions()
o := gatherOptions(flag.NewFlagSet(os.Args[0], flag.ExitOnError), os.Args[1:]...)
if err := o.Validate(); err != nil {
logrus.Fatalf("Invalid options: %v", err)
logrus.WithError(err).Fatal("Invalid options")
}
logrus.SetFormatter(logrusutil.NewDefaultFieldsFormatter(nil, logrus.Fields{"component": "hook"}))

Expand Down
110 changes: 110 additions & 0 deletions prow/cmd/hook/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ limitations under the License.
package main

import (
"flag"
"reflect"
"testing"
"time"

"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/test-infra/prow/config"
"k8s.io/test-infra/prow/flagutil"
"k8s.io/test-infra/prow/plugins"
)

Expand All @@ -29,3 +35,107 @@ func TestPlugins(t *testing.T) {
t.Fatalf("Could not load plugins: %v.", err)
}
}

func Test_gatherOptions(t *testing.T) {
cases := []struct {
name string
args map[string]string
del sets.String
expected func(*options)
err bool
}{
{
name: "minimal flags work",
},
{
name: "explicitly set --config-path",
args: map[string]string{
"--config-path": "/random/value",
},
expected: func(o *options) {
o.configPath = "/random/value"
},
},
{
name: "empty config-path defaults to old value",
args: map[string]string{
"--config-path": "",
},
expected: func(o *options) {
o.configPath = config.DefaultConfigPath
},
},
{
name: "expicitly set --dry-run=false",
args: map[string]string{
"--dry-run": "false",
},
expected: func(o *options) {
o.dryRun = false
},
},
{
name: "--dry-run=true requires --deck-url",
args: map[string]string{
"--dry-run": "true",
"--deck-url": "",
},
err: true,
},
{
name: "explicitly set --plugin-config",
args: map[string]string{
"--plugin-config": "/random/value",
},
expected: func(o *options) {
o.pluginConfig = "/random/value"
},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
expected := &options{
port: 8888,
configPath: "yo",
pluginConfig: "/etc/plugins/plugins.yaml",
dryRun: true,
gracePeriod: 180 * time.Second,
kubernetes: flagutil.ExperimentalKubernetesOptions{DeckURI: "http://whatever"},
webhookSecretFile: "/etc/webhook/hmac",
}
expectedfs := flag.NewFlagSet("fake-flags", flag.PanicOnError)
expected.github.AddFlags(expectedfs)
if tc.expected != nil {
tc.expected(expected)
}

argMap := map[string]string{
"--config-path": "yo",
"--deck-url": "http://whatever",
}
for k, v := range tc.args {
argMap[k] = v
}
for k := range tc.del {
delete(argMap, k)
}

var args []string
for k, v := range argMap {
args = append(args, k+"="+v)
}
fs := flag.NewFlagSet("fake-flags", flag.PanicOnError)
actual := gatherOptions(fs, args...)
switch err := actual.Validate(); {
case err != nil:
if !tc.err {
t.Errorf("unexpected error: %v", err)
}
case tc.err:
t.Errorf("failed to receive expected error")
case !reflect.DeepEqual(*expected, actual):
t.Errorf("%#v != expected %#v", actual, *expected)
}
})
}
}

0 comments on commit 3004dce

Please sign in to comment.