From 712f7a25ff081b43abb02b46684b5b428c1136d3 Mon Sep 17 00:00:00 2001 From: Aya Igarashi Date: Fri, 17 Jul 2020 09:53:59 +0900 Subject: [PATCH] Add validation test --- cmd/cmd_test.go | 63 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 cmd/cmd_test.go diff --git a/cmd/cmd_test.go b/cmd/cmd_test.go new file mode 100644 index 0000000..abdf339 --- /dev/null +++ b/cmd/cmd_test.go @@ -0,0 +1,63 @@ +package cmd + +import ( + "testing" + + "github.com/Ladicle/kubectl-rolesum/pkg/util/subject" +) + +func TestValidate(t *testing.T) { + tests := []struct { + desc string + args []string + option Option + wantError bool + }{ + { + desc: "Use Service Account", + args: []string{"ci-bot"}, + option: Option{ + SubjectKind: subject.KindSA, + }, + }, + { + desc: "Use User", + args: []string{"alice"}, + option: Option{ + SubjectKind: subject.KindUser, + }, + }, + { + desc: "Use Group", + args: []string{"developer"}, + option: Option{ + SubjectKind: subject.KindGroup, + }, + }, + { + desc: "No arguments", + wantError: true, + }, + { + desc: "Unknown subject kind", + option: Option{ + SubjectKind: "unknown", + }, + wantError: true, + }, + } + for _, tt := range tests { + t.Run(tt.desc, func(t *testing.T) { + err := tt.option.Validate(nil, tt.args) + if err != nil { + if !tt.wantError { + t.Fatalf("o.Validate(): %v", err) + } + return + } + if tt.wantError { + t.Fatal("o.Validate() should fail") + } + }) + } +}