File size: 24,212 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 |
package harness
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
stdlog "log"
"log/slog"
"net/http/httptest"
"net/smtp"
"net/url"
"os"
"path/filepath"
"reflect"
"sort"
"strings"
"sync"
"testing"
"text/template"
"time"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/pkg/errors"
sloglogrus "github.com/samber/slog-logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/target/goalert/alert"
"github.com/target/goalert/app"
"github.com/target/goalert/config"
"github.com/target/goalert/devtools/mockslack"
"github.com/target/goalert/devtools/mocktwilio"
"github.com/target/goalert/devtools/pgdump-lite"
"github.com/target/goalert/devtools/pgmocktime"
"github.com/target/goalert/expflag"
"github.com/target/goalert/migrate"
"github.com/target/goalert/notification/twilio"
"github.com/target/goalert/permission"
"github.com/target/goalert/user"
"github.com/target/goalert/user/notificationrule"
"github.com/target/goalert/util/log"
"github.com/target/goalert/util/sqlutil"
"github.com/target/goalert/util/timeutil"
)
var (
dbURLStr string
dbURL *url.URL
)
func init() {
dbURLStr = os.Getenv("DB_URL")
if dbURLStr == "" {
dbURLStr = "postgres://goalert@127.0.0.1:5432?sslmode=disable"
}
var err error
dbURL, err = url.Parse(dbURLStr)
if err != nil {
panic(err)
}
}
func DBURL(name string) string {
if name == "" {
return dbURLStr
}
u := *dbURL
u.Path = "/" + url.PathEscape(name)
return u.String()
}
// Harness is a helper for smoketests. It deals with assertions, database management, and backend monitoring during tests.
type Harness struct {
phoneCCG, uuidG, emailG *DataGen
t *testing.T
closing bool
appPool *pgxpool.Pool
rcsSenderID string
rcsMsgSvcID string
msgSvcID string
expFlags expflag.FlagSet
tw *twilioAssertionAPI
twS *httptest.Server
cfg config.Config
appCfg app.Config
email *emailServer
slack *slackServer
slackS *httptest.Server
slackApp mockslack.AppInfo
slackUser mockslack.UserInfo
pgTime *pgmocktime.Mocker
ignoreErrors []string
backend *app.App
backendLogs io.Closer
dbURL string
dbName string
mx sync.Mutex
userGeneratedIndex int
gqlSessions map[string]string
}
func (h *Harness) Config() config.Config {
return h.cfg
}
// NewHarness will create a new database, perform `migrateSteps` migrations, inject `initSQL` and return a new Harness bound to
// the result. It starts a backend process pre-configured to a mock twilio server for monitoring notifications as well.
func NewHarness(t *testing.T, initSQL, migrationName string) *Harness {
t.Helper()
return NewHarnessWithFlags(t, initSQL, migrationName, nil)
}
// NewHarnessWithFlags is the same as NewHarness, but allows passing in a set of experimental flags to be used for the test.
func NewHarnessWithFlags(t *testing.T, initSQL, migrationName string, fs expflag.FlagSet) *Harness {
stdlog.SetOutput(io.Discard)
t.Helper()
h := NewStoppedHarnessWithFlags(t, initSQL, nil, migrationName, fs)
h.Start()
return h
}
func (h *Harness) App() *app.App { return h.backend }
func NewHarnessWithData(t *testing.T, initSQL string, sqlData interface{}, migrationName string) *Harness {
t.Helper()
h := NewStoppedHarness(t, initSQL, sqlData, migrationName)
h.Start()
return h
}
// NewHarnessDebugDB works like NewHarness, but fails the test immediately after
// migrations have been run. It is used to debug data & queries from a smoketest.
//
// Note that the now() function will be locked to the init timestamp for inspection.
func NewHarnessDebugDB(t *testing.T, initSQL, migrationName string) *Harness {
t.Helper()
h := NewStoppedHarness(t, initSQL, nil, migrationName)
h.Migrate("")
t.Fatal("DEBUG DB ::", h.dbURL)
return nil
}
const (
twilioAuthToken = "11111111111111111111111111111111"
twilioAccountSID = "AC00000000000000000000000000000000"
mailgunAPIKey = "key-00000000000000000000000000000000"
)
// NewStoppedHarness will create a NewHarness, but will not call Start.
func NewStoppedHarness(t *testing.T, initSQL string, sqlData interface{}, migrationName string) *Harness {
t.Helper()
return NewStoppedHarnessWithFlags(t, initSQL, sqlData, migrationName, nil)
}
// NewStoppedHarnessWithFlags is the same as NewStoppedHarness, but allows
// passing in a set of experimental flags to be used for the test.
func NewStoppedHarnessWithFlags(t *testing.T, initSQL string, sqlData interface{}, migrationName string, expFlags expflag.FlagSet) *Harness {
t.Helper()
if testing.Short() {
t.Skip("skipping Harness tests for short mode")
}
t.Logf("Using DB URL: %s", dbURL)
name := strings.ReplaceAll("smoketest_"+time.Now().Format("2006_01_02_15_04_05")+uuid.New().String(), "-", "")
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
defer cancel()
conn, err := pgx.Connect(ctx, DBURL(""))
if err != nil {
t.Fatal("connect to db:", err)
}
defer conn.Close(ctx)
_, err = conn.Exec(ctx, "create database "+sqlutil.QuoteID(name))
if err != nil {
t.Fatal("create db:", err)
}
conn.Close(ctx)
t.Logf("created test database '%s': %s", name, dbURL)
twCfg := mocktwilio.Config{
AuthToken: twilioAuthToken,
AccountSID: twilioAccountSID,
MinQueueTime: 100 * time.Millisecond, // until we have a stateless backend for answering calls
}
pgTime, err := pgmocktime.New(ctx, DBURL(name))
if err != nil {
t.Fatal("create pgmocktime:", err)
}
h := &Harness{
uuidG: NewDataGen(t, "UUID", DataGenFunc(GenUUID)),
phoneCCG: NewDataGen(t, "Phone", DataGenArgFunc(GenPhoneCC)),
emailG: NewDataGen(t, "Email", DataGenFunc(func() string { return GenUUID() + "@example.com" })),
dbName: name,
dbURL: DBURL(name),
pgTime: pgTime,
gqlSessions: make(map[string]string),
expFlags: expFlags,
t: t,
}
h.email = newEmailServer(h)
h.tw = newTwilioAssertionAPI(func() {
h.FastForward(time.Minute)
h.Trigger()
}, func(num string) string {
id, ok := h.phoneCCG.names[num]
if !ok {
return num
}
return fmt.Sprintf("%s/Phone(%s)", num, id)
}, mocktwilio.NewServer(twCfg), h.phoneCCG.Get("twilio"))
h.twS = httptest.NewServer(h.tw)
err = h.pgTime.Inject(ctx)
if err != nil {
t.Fatal(err)
}
err = h.pgTime.SetSpeed(ctx, 0)
if err != nil {
t.Fatal(err)
}
h.Migrate(migrationName)
h.initSlack()
h.execQuery(initSQL, sqlData)
return h
}
func (h *Harness) Start() {
h.t.Helper()
h.StartWithAppCfgHook(nil)
}
func (h *Harness) StartWithAppCfgHook(fn func(*app.Config)) {
h.t.Helper()
var cfg config.Config
cfg.General.DisableMessageBundles = true
cfg.Slack.Enable = true
cfg.Slack.AccessToken = h.slackApp.AccessToken
cfg.Slack.ClientID = h.slackApp.ClientID
cfg.Slack.ClientSecret = h.slackApp.ClientSecret
cfg.Slack.SigningSecret = SlackTestSigningSecret
cfg.Twilio.Enable = true
cfg.Twilio.AccountSID = twilioAccountSID
cfg.Twilio.AuthToken = twilioAuthToken
cfg.Twilio.FromNumber = h.phoneCCG.Get("twilio")
cfg.SMTP.Enable = true
cfg.SMTP.Address = h.email.Addr()
cfg.SMTP.DisableTLS = true
cfg.SMTP.From = "goalert-test@localhost"
cfg.Webhook.Enable = true
cfg.Mailgun.Enable = true
cfg.Mailgun.APIKey = mailgunAPIKey
cfg.Mailgun.EmailDomain = "smoketest.example.com"
h.cfg = cfg
_, err := migrate.ApplyAll(context.Background(), h.dbURL)
if err != nil {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
h.t.Fatalf("failed to migrate backend: %#v\n", pgErr)
}
h.t.Fatalf("failed to migrate backend: %v\n", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
err = h.pgTime.SetSpeed(ctx, 1)
if err != nil {
h.t.Fatalf("resume flow of time: %v", err)
}
appCfg := app.Defaults()
appCfg.ExpFlags = h.expFlags
appCfg.LegacyLogger = log.NewLogger()
appCfg.Logger = slog.New(sloglogrus.Option{
Logger: appCfg.LegacyLogger.Logrus(),
}.NewLogrusHandler())
appCfg.ListenAddr = "localhost:0"
appCfg.Verbose = true
appCfg.JSON = true
appCfg.DBURL = h.dbURL
appCfg.TwilioBaseURL = h.twS.URL
appCfg.DBMaxOpen = 5
appCfg.SlackBaseURL = h.slackS.URL
appCfg.SMTPListenAddr = "localhost:0"
appCfg.EmailIntegrationDomain = "smoketest.example.com"
appCfg.InitialConfig = &h.cfg
h.appCfg = appCfg
r, w := io.Pipe()
h.backendLogs = w
appCfg.LegacyLogger.EnableJSON()
appCfg.LegacyLogger.SetOutput(w)
go h.watchBackendLogs(r)
poolCfg, err := pgxpool.ParseConfig(h.dbURL)
if err != nil {
h.t.Fatalf("failed to parse db url: %v", err)
}
poolCfg.MaxConns = 5
h.appPool, err = pgxpool.NewWithConfig(ctx, poolCfg)
require.NoError(h.t, err, "create pgx pool")
if fn != nil {
fn(&appCfg)
}
h.backend, err = app.NewApp(appCfg, h.appPool)
if err != nil {
h.t.Fatalf("failed to start backend: %v", err)
}
h.TwilioNumber("") // register default number
h.slack.SetActionURL(h.slackApp.ClientID, h.backend.URL()+"/api/v2/slack/message-action")
go func() {
assert.NoError(h.t, h.backend.Run(context.Background())) // can't use require.NoError because we're in the background
}()
err = h.backend.WaitForStartup(ctx)
if err != nil {
h.t.Fatalf("failed to start backend: %v", err)
}
}
// RestartGoAlertWithConfig will restart the backend with the provided config.
func (h *Harness) RestartGoAlertWithConfig(cfg config.Config) {
h.t.Helper()
h.RestartGoAlertWithAppCfgHook(func(c *app.Config) {
c.InitialConfig = &cfg
})
}
// RestartGoAlertWithAppCfgHook will restart the backend with the provided config hook.
func (h *Harness) RestartGoAlertWithAppCfgHook(reconfig func(*app.Config)) {
h.t.Helper()
h.t.Logf("Stopping backend for restart")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
err := h.backend.Shutdown(ctx)
if err != nil {
h.t.Error("failed to shutdown backend cleanly:", err)
}
h.t.Logf("Restarting backend")
reconfig(&h.appCfg)
h.backend, err = app.NewApp(h.appCfg, h.appPool)
if err != nil {
h.t.Fatalf("failed to start backend: %v", err)
}
h.slack.SetActionURL(h.slackApp.ClientID, h.backend.URL()+"/api/v2/slack/message-action")
go func() {
assert.NoError(h.t, h.backend.Run(context.Background())) // can't use require.NoError because we're in the background
}()
err = h.backend.WaitForStartup(ctx)
if err != nil {
h.t.Fatalf("failed to start backend: %v", err)
}
h.t.Logf("Backend restarted")
}
// URL returns the backend server's URL
func (h *Harness) URL() string {
return h.backend.URL()
}
// SendMail will send an email to the backend's SMTP server.
func (h *Harness) SendMail(from, to, subject, body string) {
h.t.Helper()
err := smtp.SendMail(h.App().SMTPAddr(), nil, from, []string{to}, []byte(fmt.Sprintf("Subject: %s\r\n\r\n%s", subject, body)))
require.NoError(h.t, err)
}
// Migrate will perform `steps` number of migrations.
func (h *Harness) Migrate(migrationName string) {
h.t.Helper()
h.t.Logf("Running migrations (target: %s)", migrationName)
_, err := migrate.Up(context.Background(), h.dbURL, migrationName)
if err != nil {
h.t.Fatalf("failed to run migration: %v", err)
}
}
// IgnoreErrorsWith will cause the Harness to ignore backend errors containing the specified substring.
func (h *Harness) IgnoreErrorsWith(substr string) {
h.mx.Lock()
defer h.mx.Unlock()
h.ignoreErrors = append(h.ignoreErrors, substr)
}
// Now returns the current time, as observed by the DB.
func (h *Harness) Now() time.Time {
h.t.Helper()
var now time.Time
err := h.appPool.QueryRow(context.Background(), "SELECT NOW()").Scan(&now)
require.NoError(h.t, err, "get now()")
return now
}
// FastForwardToTime will fast-forward the database time to the next occurrence of the provided clock time.
func (h *Harness) FastForwardToTime(t timeutil.Clock, zoneName string) {
h.t.Helper()
zone, err := time.LoadLocation(zoneName)
require.NoError(h.t, err, "load location")
now := h.Now()
y, m, d := now.In(zone).Date()
dst := time.Date(y, m, d, t.Hour(), t.Minute(), 0, 0, zone)
if !dst.After(now) {
dst = dst.AddDate(0, 0, 1)
}
h.FastForward(dst.Sub(now))
}
func (h *Harness) FastForward(d time.Duration) {
h.t.Helper()
h.t.Logf("Fast-forward %s", d.String())
err := h.pgTime.AdvanceTime(context.Background(), d)
if err != nil {
h.t.Fatalf("failed to fast-forward time: %v", err)
}
}
func (h *Harness) execQuery(sql string, data interface{}) {
h.t.Helper()
t := template.New("sql")
t.Funcs(template.FuncMap{
"uuidJSON": func(id string) string { return fmt.Sprintf(`"%s"`, h.uuidG.Get(id)) },
"uuid": func(id string) string { return fmt.Sprintf("'%s'", h.uuidG.Get(id)) },
"phone": func(id string) string { return fmt.Sprintf("'%s'", h.phoneCCG.Get(id)) },
"email": func(id string) string { return fmt.Sprintf("'%s'", h.emailG.Get(id)) },
"phoneCC": func(cc, id string) string { return fmt.Sprintf("'%s'", h.phoneCCG.GetWithArg(cc, id)) },
"slackChannelID": func(name string) string { return fmt.Sprintf("'%s'", h.Slack().Channel(name).ID()) },
"slackUserID": func(name string) string { return fmt.Sprintf("'%s'", h.Slack().User(name).ID()) },
"slackUserGroupID": func(name string) string { return fmt.Sprintf("'%s'", h.Slack().UserGroup(name).ID()) },
})
_, err := t.Parse(sql)
if err != nil {
h.t.Fatalf("failed to parse query template: %v", err)
}
b := new(bytes.Buffer)
err = t.Execute(b, data)
if err != nil {
h.t.Fatalf("failed to render query template: %v", err)
}
err = ExecSQLBatch(context.Background(), h.dbURL, b.String())
if err != nil {
h.t.Fatalf("failed to exec query: %v\n%s", err, b.String())
}
}
// CreateAlerts will create one or more unacknowledged alerts for a service.
func (h *Harness) CreateAlert(serviceID string, summary string) TestAlert {
h.t.Helper()
return h.CreateAlertWithDetails(serviceID, summary, "")
}
type TestAlert interface {
ID() int
Ack()
Escalate()
Close()
}
type testAlert struct {
h *Harness
a alert.Alert
}
func (t testAlert) setStatus(stat alert.Status) {
t.h.t.Helper()
permission.SudoContext(context.Background(), func(ctx context.Context) {
t.h.t.Helper()
tx, err := t.h.backend.DB().BeginTx(ctx, nil)
require.NoError(t.h.t, err, "begin tx")
defer SQLRollback(t.h.t, "harness: set alert status", tx)
t.a.Status = stat
result, isNew, err := t.h.backend.AlertStore.CreateOrUpdateTx(ctx, tx, &t.a)
require.NoErrorf(t.h.t, err, "set alert to %s", stat)
require.False(t.h.t, isNew, "not be new")
require.NotNil(t.h.t, result)
require.NoError(t.h.t, tx.Commit(), "commit tx")
})
}
func (t testAlert) ID() int { return t.a.ID }
func (t testAlert) Close() { t.setStatus(alert.StatusClosed) }
func (t testAlert) Ack() { t.setStatus(alert.StatusActive) }
func (t testAlert) Escalate() {
t.h.t.Helper()
permission.SudoContext(context.Background(), func(ctx context.Context) {
t.h.t.Helper()
err := t.h.backend.AlertStore.Escalate(ctx, t.a.ID, 0)
require.NoErrorf(t.h.t, err, "escalate alert %d", t.a.ID)
})
}
// CreateAlertWithDetails will create a single alert with summary and detailss.
func (h *Harness) CreateAlertWithDetails(serviceID, summary, details string) TestAlert {
h.t.Helper()
var newAlert alert.Alert
permission.SudoContext(context.Background(), func(ctx context.Context) {
h.t.Helper()
tx, err := h.backend.DB().BeginTx(ctx, nil)
if err != nil {
h.t.Fatalf("failed to start tx: %v", err)
}
defer SQLRollback(h.t, "harness: create alert", tx)
a := &alert.Alert{
ServiceID: serviceID,
Summary: summary,
Details: details,
}
h.t.Logf("insert alert: %v", a)
_newAlert, isNew, err := h.backend.AlertStore.CreateOrUpdateTx(ctx, tx, a)
if err != nil {
h.t.Fatalf("failed to insert alert: %v", err)
}
if !isNew {
h.t.Fatal("could not create duplicate alert with summary: " + summary)
}
newAlert = *_newAlert
err = tx.Commit()
if err != nil {
h.t.Fatalf("failed to commit tx: %v", err)
}
})
return testAlert{h: h, a: newAlert}
}
// AddNotificationRule will add a notification rule to the database.
func (h *Harness) AddNotificationRule(userID, cmID string, delayMinutes int) {
h.t.Helper()
nr := ¬ificationrule.NotificationRule{
DelayMinutes: delayMinutes,
UserID: userID,
ContactMethodID: uuid.MustParse(cmID),
}
h.t.Logf("insert notification rule: %v", nr)
permission.SudoContext(context.Background(), func(ctx context.Context) {
h.t.Helper()
_, err := h.backend.NotificationRuleStore.Insert(ctx, nr)
if err != nil {
h.t.Fatalf("failed to insert notification rule: %v", err)
}
})
}
// Trigger will trigger, and wait for, an engine cycle.
func (h *Harness) Trigger() {
id := h.backend.Engine.NextCycleID()
go h.backend.Engine.Trigger()
require.NoError(h.t, h.backend.Engine.WaitCycleID(context.Background(), id))
}
// Escalate will escalate an alert in the database, when 'level' matches.
func (h *Harness) Escalate(alertID, level int) {
h.t.Helper()
err := h.EscalateAlertErr(alertID)
require.NoError(h.t, err, "escalate alert")
}
func (h *Harness) EscalateAlertErr(alertID int) (err error) {
h.t.Helper()
h.t.Logf("escalate alert #%d", alertID)
permission.SudoContext(context.Background(), func(ctx context.Context) {
h.t.Helper()
err = h.backend.AlertStore.Escalate(ctx, alertID, -1)
})
return err
}
// Phone will return the generated phone number for the id provided.
func (h *Harness) Phone(id string) string { return h.phoneCCG.Get(id) }
// PhoneCC will return the generated phone number for the id provided.
func (h *Harness) PhoneCC(cc, id string) string { return h.phoneCCG.GetWithArg(cc, id) }
// UUID will return the generated UUID for the id provided.
func (h *Harness) UUID(id string) string { return h.uuidG.Get(id) }
func (h *Harness) isClosing() bool {
h.mx.Lock()
defer h.mx.Unlock()
return h.closing
}
func (h *Harness) dumpDB() {
testName := reflect.ValueOf(h.t).Elem().FieldByName("name").String()
file := filepath.Join("smoketest_db_dump", testName+".sql")
file, err := filepath.Abs(file)
if err != nil {
h.t.Fatalf("failed to get abs dump path: %v", err)
}
if err := os.MkdirAll(filepath.Dir(file), 0o755); err != nil {
h.t.Fatalf("failed to create abs dump path: %v", err)
}
conn, err := pgx.Connect(context.Background(), h.dbURL)
if err != nil {
h.t.Fatalf("failed to get db connection: %v", err)
}
defer conn.Close(context.Background())
var t time.Time
err = conn.QueryRow(context.Background(), "select now()").Scan(&t)
if err != nil {
h.t.Fatalf("failed to get current timestamp: %v", err)
}
fd, err := os.Create(file)
if err != nil {
h.t.Fatalf("failed to open dump file: %v", err)
}
defer fd.Close()
err = pgdump.DumpData(context.Background(), conn, fd, nil)
if err != nil {
h.t.Errorf("failed to dump database '%s': %v", h.dbName, err)
}
_, err = fmt.Fprintf(fd, "\n-- Last Timestamp: %s\n", t.Format(time.RFC3339Nano))
if err != nil {
h.t.Fatalf("failed to open DB dump: %v", err)
}
}
// Close terminates any background processes, and drops the testing database.
// It should be called at the end of all tests (usually with `defer h.Close()`).
func (h *Harness) Close() {
h.t.Helper()
if recErr := recover(); recErr != nil {
defer panic(recErr)
}
h.dumpDB() // early as possible
h.tw.WaitAndAssert(h.t)
h.slack.WaitAndAssert()
h.email.WaitAndAssert()
h.mx.Lock()
h.closing = true
h.mx.Unlock()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
err := h.backend.Shutdown(ctx)
if err != nil {
h.t.Error("failed to shutdown backend cleanly:", err)
}
h.backendLogs.Close()
h.slackS.Close()
h.twS.Close()
h.tw.Close()
h.pgTime.Close()
h.appPool.Close()
conn, err := pgx.Connect(ctx, DBURL(""))
if err != nil {
h.t.Error("failed to connect to DB:", err)
}
defer conn.Close(ctx)
_, err = conn.Exec(ctx, "drop database "+sqlutil.QuoteID(h.dbName))
if err != nil {
h.t.Errorf("failed to drop database '%s': %v", h.dbName, err)
}
}
// SetCarrierName will set the carrier name for the given phone number.
func (h *Harness) SetCarrierName(number, name string) {
h.tw.SetCarrierInfo(number, twilio.CarrierInfo{Name: name})
}
// TwilioNumber will return a registered (or register if missing) Twilio number for the given ID.
// The default FromNumber will always be the empty ID.
func (h *Harness) TwilioNumber(id string) string {
if id != "" {
id = ":" + id
}
num := h.phoneCCG.Get("twilio" + id)
err := h.tw.RegisterSMSCallback(num, h.URL()+"/v1/twilio/sms/messages")
if err != nil {
h.t.Fatalf("failed to init twilio (SMS callback): %v", err)
}
err = h.tw.RegisterVoiceCallback(num, h.URL()+"/v1/twilio/voice/call")
if err != nil {
h.t.Fatalf("failed to init twilio (voice callback): %v", err)
}
return num
}
// TwilioMessagingService will return the id and phone numbers for the mock messaging service.
func (h *Harness) TwilioMessagingService() string {
h.mx.Lock()
if h.msgSvcID != "" {
h.mx.Unlock()
return h.msgSvcID
}
defer h.mx.Unlock()
nums := []string{h.phoneCCG.Get("twilio:sid1"), h.phoneCCG.Get("twilio:sid2"), h.phoneCCG.Get("twilio:sid3")}
newID, err := h.tw.NewMessagingService(h.URL()+"/v1/twilio/sms/messages", nums...)
if err != nil {
panic(err)
}
h.msgSvcID = newID
return newID
}
// TwilioMessagingServiceRCS will return the id and phone numbers for the mock messaging service with RCS enabled.
func (h *Harness) TwilioMessagingServiceRCS() (rcs, msg string) {
h.mx.Lock()
defer h.mx.Unlock()
if h.rcsSenderID != "" {
return h.rcsSenderID, h.rcsMsgSvcID
}
nums := []string{h.phoneCCG.Get("twilio:rcs:sid1"), h.phoneCCG.Get("twilio:rcs:sid2"), h.phoneCCG.Get("twilio:rcs:sid3")}
newID, err := h.tw.NewMessagingService(h.URL()+"/v1/twilio/sms/messages", nums...)
if err != nil {
panic(err)
}
h.rcsMsgSvcID = newID
rcsID, err := h.tw.EnableRCS(newID)
require.NoError(h.t, err)
h.rcsSenderID = rcsID
return rcsID, newID
}
// CreateUser generates a random user.
func (h *Harness) CreateUser() (u *user.User) {
h.t.Helper()
var err error
permission.SudoContext(context.Background(), func(ctx context.Context) {
u, err = h.backend.UserStore.Insert(ctx, &user.User{
Name: fmt.Sprintf("Generated%d", h.userGeneratedIndex),
ID: uuid.New().String(),
Role: permission.RoleUser,
Email: fmt.Sprintf("generated%d@example.com", h.userGeneratedIndex),
})
})
if err != nil {
h.t.Fatal(errors.Wrap(err, "generate random user"))
}
h.userGeneratedIndex++
return u
}
// WaitAndAssertOnCallUsers will ensure the correct set of users as on-call for the given serviceID.
func (h *Harness) WaitAndAssertOnCallUsers(serviceID string, userIDs ...string) {
h.t.Helper()
doQL := func(query string, res interface{}) {
g := h.GraphQLQuery2(query)
for _, err := range g.Errors {
h.t.Error("GraphQL Error:", err.Message)
}
if len(g.Errors) > 0 {
h.t.Fatal("errors returned from GraphQL")
}
if res == nil {
return
}
err := json.Unmarshal(g.Data, &res)
if err != nil {
h.t.Fatal("failed to parse response:", err)
}
}
getUsers := func() []string {
var result struct {
Service struct {
OnCallUsers []struct {
UserID string
UserName string
}
}
}
doQL(fmt.Sprintf(`
query{
service(id: "%s"){
onCallUsers{
userID
userName
}
}
}
`, serviceID), &result)
var ids []string
for _, oc := range result.Service.OnCallUsers {
ids = append(ids, oc.UserID)
}
if len(ids) == 0 {
return nil
}
sort.Strings(ids)
uniq := ids[:1]
last := ids[0]
for _, id := range ids[1:] {
if id == last {
continue
}
uniq = append(uniq, id)
last = id
}
return uniq
}
sort.Strings(userIDs)
check := func(t *assert.CollectT) {
ids := getUsers()
require.Lenf(t, ids, len(userIDs), "number of on-call users")
require.EqualValuesf(t, userIDs, ids, "on-call users")
}
h.Trigger() // run engine cycle
assert.EventuallyWithT(h.t, check, 5*time.Second, 100*time.Millisecond)
}
|