93 lines
2.4 KiB
Go
93 lines
2.4 KiB
Go
package bot
|
|
|
|
import (
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/slack-go/slack"
|
|
|
|
"slackerbot/internal/acp"
|
|
"slackerbot/internal/model"
|
|
"slackerbot/internal/testutil"
|
|
"slackerbot/internal/testutil/clustertest"
|
|
)
|
|
|
|
func updateBot(t *testing.T) (*Bot, *clustertest.Cluster, *slackLog) {
|
|
c := clustertest.New(t)
|
|
c.AddOwner(t, testutil.HomeData(), true)
|
|
log := &slackLog{text: map[string]string{}, under: map[string]string{}}
|
|
srv := httptest.NewServer(log)
|
|
t.Cleanup(srv.Close)
|
|
b := &Bot{cfg: c.Cfg, store: c.Store, mgr: c.Mgr, api: slack.New("xoxb", slack.OptionAPIURL(srv.URL+"/")),
|
|
acps: map[string]*acp.ACP{}, busy: map[string]int{}}
|
|
return b, c, log
|
|
}
|
|
|
|
func setOutdated(c *clustertest.Cluster, outdated bool) {
|
|
c.Master.Mu.Lock()
|
|
defer c.Master.Mu.Unlock()
|
|
c.Master.Outdated["c-c1"] = outdated
|
|
}
|
|
|
|
func (s *slackLog) prompts() int {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
n := 0
|
|
for _, text := range s.text {
|
|
if strings.Contains(text, "A system update is waiting") {
|
|
n++
|
|
}
|
|
}
|
|
return n
|
|
}
|
|
|
|
func TestUpdatePromptOncePerSystem(t *testing.T) {
|
|
b, c, log := updateBot(t)
|
|
u := c.Store.Owner("C1")
|
|
|
|
b.offerUpdate(c.Ctx, u, "C1", "100.0")
|
|
if log.prompts() != 0 {
|
|
t.Fatal("no prompt for a container on the current system")
|
|
}
|
|
setOutdated(c, true)
|
|
b.offerUpdate(c.Ctx, u, "C1", "100.0")
|
|
b.offerUpdate(c.Ctx, u, "C1", "200.0")
|
|
if log.prompts() != 1 {
|
|
t.Fatalf("want one prompt for one pending update, got %d", log.prompts())
|
|
}
|
|
setOutdated(c, false)
|
|
b.offerUpdate(c.Ctx, u, "C1", "100.0")
|
|
setOutdated(c, true)
|
|
b.offerUpdate(c.Ctx, u, "C1", "100.0")
|
|
if log.prompts() != 2 {
|
|
t.Fatalf("a newer update should be offered again, got %d prompts", log.prompts())
|
|
}
|
|
}
|
|
|
|
func TestRestartWaitsForReply(t *testing.T) {
|
|
b, c, log := updateBot(t)
|
|
setOutdated(c, true)
|
|
b.setBusy("c-c1", +1)
|
|
|
|
cb := slack.InteractionCallback{Channel: slack.Channel{GroupConversation: slack.GroupConversation{
|
|
Conversation: slack.Conversation{ID: "C1"}}}}
|
|
cb.Message.Timestamp, cb.Message.ThreadTimestamp = "5.0", "100.0"
|
|
b.restartFor(c.Ctx, cb, "C1", model.OwnerChannel)
|
|
|
|
c.Master.Mu.Lock()
|
|
still := c.Master.Outdated["c-c1"]
|
|
c.Master.Mu.Unlock()
|
|
if !still {
|
|
t.Fatal("a busy container must not be restarted")
|
|
}
|
|
log.mu.Lock()
|
|
defer log.mu.Unlock()
|
|
for _, text := range log.text {
|
|
if strings.Contains(text, "still replying") {
|
|
return
|
|
}
|
|
}
|
|
t.Fatal("the user should be told to try again once the reply is done")
|
|
}
|