clean up of the codebase
This commit is contained in:
1 parent
7822529a38
commit
0ac61b4ece
52 files changed
+2545
-2274
No files matched your search
@@ -0,0 +1,154 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"slackerbot/internal/model"
|
||||
)
|
||||
|
||||
func TestParseScale(t *testing.T) {
|
||||
cur := model.Size{MemoryMB: 2048, CPUs: 1}
|
||||
cases := []struct {
|
||||
in string
|
||||
want model.Size
|
||||
}{
|
||||
{"", model.Size{MemoryMB: 4096, CPUs: 2}},
|
||||
{"up", model.Size{MemoryMB: 4096, CPUs: 2}},
|
||||
{"down", model.Size{MemoryMB: 1024, CPUs: 0.5}},
|
||||
{"8G 4cpu", model.Size{MemoryMB: 8192, CPUs: 4}},
|
||||
{"8gb", model.Size{MemoryMB: 8192, CPUs: 1}},
|
||||
{"+2G", model.Size{MemoryMB: 4096, CPUs: 1}},
|
||||
{"-1cpu", model.Size{MemoryMB: 2048, CPUs: 0}},
|
||||
{"512M +0.5cpu", model.Size{MemoryMB: 512, CPUs: 1.5}},
|
||||
{"1.5G 3 cores", model.Size{MemoryMB: 1536, CPUs: 3}},
|
||||
{"+2 GB", model.Size{MemoryMB: 4096, CPUs: 1}},
|
||||
{"+1cpus -512mb", model.Size{MemoryMB: 1536, CPUs: 2}},
|
||||
}
|
||||
for _, c := range cases {
|
||||
got, err := ParseScale(c.in, cur)
|
||||
if err != nil || got != c.want {
|
||||
t.Fatalf("scale %q = %v, %v; want %v", c.in, got, err, c.want)
|
||||
}
|
||||
}
|
||||
if _, err := ParseScale("the database to 3 replicas", cur); !errors.Is(err, ErrNotCommand) {
|
||||
t.Fatal("a normal sentence should go to the agent")
|
||||
}
|
||||
if _, err := ParseScale("4G lots", cur); err == nil || errors.Is(err, ErrNotCommand) {
|
||||
t.Fatal("a bad second word should be a usage error")
|
||||
}
|
||||
if _, err := ParseScale("4G 8G", cur); err == nil {
|
||||
t.Fatal("memory twice should fail")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseKeep(t *testing.T) {
|
||||
for in, want := range map[string]time.Duration{
|
||||
"24h": 24 * time.Hour, "3d": 72 * time.Hour, "1d12h": 36 * time.Hour, "2w": 336 * time.Hour,
|
||||
"5 hours": 5 * time.Hour, "2 days": 48 * time.Hour, "1d 6h": 30 * time.Hour, "90 mins": 90 * time.Minute,
|
||||
} {
|
||||
k, err := ParseKeep(in)
|
||||
if err != nil || k.Limit != want {
|
||||
t.Fatalf("keep %q = %+v, %v; want %s", in, k, err, want)
|
||||
}
|
||||
}
|
||||
for _, in := range []string{"indefinite", "Indefinitely", "forever"} {
|
||||
if k, _ := ParseKeep(in); !k.Indefinite {
|
||||
t.Fatalf("keep %q should be indefinite", in)
|
||||
}
|
||||
}
|
||||
if k, _ := ParseKeep("default"); !k.Reset {
|
||||
t.Fatal("keep default should reset")
|
||||
}
|
||||
if k, _ := ParseKeep(""); !k.Show {
|
||||
t.Fatal("bare keep should show")
|
||||
}
|
||||
if _, err := ParseKeep("the tests passing"); !errors.Is(err, ErrNotCommand) {
|
||||
t.Fatal("a normal sentence should go to the agent")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHuman(t *testing.T) {
|
||||
for d, want := range map[time.Duration]string{
|
||||
6 * time.Hour: "6h", 36 * time.Hour: "1d12h", 90 * time.Minute: "1h30m",
|
||||
45 * time.Minute: "45m", 168 * time.Hour: "7d", 0: "0m",
|
||||
} {
|
||||
if got := Human(d); got != want {
|
||||
t.Fatalf("Human(%s) = %q, want %q", d, got, want)
|
||||
}
|
||||
}
|
||||
if got := (model.Size{MemoryMB: 8192, CPUs: 4}).String(); got != "8 GB RAM, 4 CPUs" {
|
||||
t.Fatal(got)
|
||||
}
|
||||
if got := (model.Size{MemoryMB: 512, CPUs: 0.25}).String(); got != "512 MB RAM, 0.25 CPUs" {
|
||||
t.Fatal(got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseStorage(t *testing.T) {
|
||||
cases := []struct {
|
||||
in string
|
||||
min, max int64
|
||||
}{
|
||||
{"20G", 5120, 20480},
|
||||
{"+5G", 5120, 15360},
|
||||
{"-2G", 5120, 8192},
|
||||
{"max 30G", 5120, 30720},
|
||||
{"min 8G", 8192, 10240},
|
||||
{"min +1G max 30 GB", 6144, 30720},
|
||||
{"cap 1T", 5120, 1048576},
|
||||
}
|
||||
for _, c := range cases {
|
||||
min, max, err := ParseStorage(c.in, 5120, 10240)
|
||||
if err != nil || min != c.min || max != c.max {
|
||||
t.Fatalf("storage %q = %d/%d, %v; want %d/%d", c.in, min, max, err, c.min, c.max)
|
||||
}
|
||||
}
|
||||
if _, _, err := ParseStorage("is full, clean up the logs", 5120, 10240); !errors.Is(err, ErrNotCommand) {
|
||||
t.Fatal("a normal sentence should go to the agent")
|
||||
}
|
||||
for _, in := range []string{"min", "20G 30G", "min 5G min 6G", "20G lots"} {
|
||||
if _, _, err := ParseStorage(in, 5120, 10240); err == nil || errors.Is(err, ErrNotCommand) {
|
||||
t.Fatalf("%q should be a usage error, got %v", in, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNeededPermission(t *testing.T) {
|
||||
for in, want := range map[[2]string]model.Action{
|
||||
{"scale", "up"}: model.ActScale,
|
||||
{"scale", "8G 4cpu"}: model.ActScale,
|
||||
{"scale", "the db to 3 replicas"}: "",
|
||||
{"keep", ""}: "",
|
||||
{"keep", "24h"}: model.ActKeep,
|
||||
{"keep", "indefinite"}: model.ActKeep,
|
||||
{"keep", "the tests green"}: "",
|
||||
{"storage", ""}: "",
|
||||
{"storage", "20G"}: model.ActStorage,
|
||||
{"storage", "is full, clean it up"}: "",
|
||||
{"status", ""}: "",
|
||||
} {
|
||||
if got := NeededPermission(in[0], in[1]); got != want {
|
||||
t.Errorf("%s %q needs %q, want %q", in[0], in[1], got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseGrant(t *testing.T) {
|
||||
g, err := ParseGrant("<@U1> <@U2|bob>, move scale")
|
||||
if err != nil || len(g.Users) != 2 || g.Users[1] != "U2" || len(g.Actions) != 2 {
|
||||
t.Fatalf("%+v %v", g, err)
|
||||
}
|
||||
if g, _ := ParseGrant("default <@U1> all"); !g.ToDefault || g.Actions[0] != "all" {
|
||||
t.Fatalf("%+v", g)
|
||||
}
|
||||
for _, in := range []string{"me access to the repo", "all the things", ""} {
|
||||
if _, err := ParseGrant(in); !errors.Is(err, ErrNotCommand) {
|
||||
t.Errorf("%q should go to the agent, got %v", in, err)
|
||||
}
|
||||
}
|
||||
if _, err := ParseGrant("<@U1> teleport"); err == nil || errors.Is(err, ErrNotCommand) {
|
||||
t.Error("an unknown permission after a mention should be a usage error")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user