@@ -1687,14 +1687,125 @@ func TestNotifications(t *testing.T) {
1687
1687
require .Contains (t ,notifEnq .sent [0 ].targets ,workspace .OrganizationID )
1688
1688
require .Contains (t ,notifEnq .sent [0 ].targets ,user .ID )
1689
1689
if tc .deletionReason == database .BuildReasonInitiator {
1690
- require .Equal (t ,notifEnq .sent [0 ].labels ["initiatedBy" ], initiator . Username )
1690
+ require .Equal (t ,initiator . Username , notifEnq .sent [0 ].labels ["initiator" ] )
1691
1691
}
1692
1692
}else {
1693
1693
require .Len (t ,notifEnq .sent ,0 )
1694
1694
}
1695
1695
})
1696
1696
}
1697
1697
})
1698
+
1699
+ t .Run ("Workspace build failed" ,func (t * testing.T ) {
1700
+ t .Parallel ()
1701
+
1702
+ tests := []struct {
1703
+ name string
1704
+
1705
+ buildReason database.BuildReason
1706
+ shouldNotify bool
1707
+ }{
1708
+ {
1709
+ name :"initiated by owner" ,
1710
+ buildReason :database .BuildReasonInitiator ,
1711
+ shouldNotify :false ,
1712
+ },
1713
+ {
1714
+ name :"initiated by autostart" ,
1715
+ buildReason :database .BuildReasonAutostart ,
1716
+ shouldNotify :true ,
1717
+ },
1718
+ }
1719
+
1720
+ for _ ,tc := range tests {
1721
+ t .Run (tc .name ,func (t * testing.T ) {
1722
+ t .Parallel ()
1723
+
1724
+ ctx := context .Background ()
1725
+ notifEnq := & fakeNotificationEnqueuer {}
1726
+
1727
+ //Otherwise `(*Server).FailJob` fails with:
1728
+ // audit log - get build {"error": "sql: no rows in result set"}
1729
+ ignoreLogErrors := true
1730
+ srv ,db ,ps ,pd := setup (t ,ignoreLogErrors ,& overrides {
1731
+ notificationEnqueuer :notifEnq ,
1732
+ })
1733
+
1734
+ user := dbgen .User (t ,db , database.User {})
1735
+ initiator := user
1736
+
1737
+ template := dbgen .Template (t ,db , database.Template {
1738
+ Name :"template" ,
1739
+ Provisioner :database .ProvisionerTypeEcho ,
1740
+ OrganizationID :pd .OrganizationID ,
1741
+ })
1742
+ template ,err := db .GetTemplateByID (ctx ,template .ID )
1743
+ require .NoError (t ,err )
1744
+ file := dbgen .File (t ,db , database.File {CreatedBy :user .ID })
1745
+ workspace := dbgen .Workspace (t ,db , database.Workspace {
1746
+ TemplateID :template .ID ,
1747
+ OwnerID :user .ID ,
1748
+ OrganizationID :pd .OrganizationID ,
1749
+ })
1750
+ version := dbgen .TemplateVersion (t ,db , database.TemplateVersion {
1751
+ OrganizationID :pd .OrganizationID ,
1752
+ TemplateID : uuid.NullUUID {
1753
+ UUID :template .ID ,
1754
+ Valid :true ,
1755
+ },
1756
+ JobID :uuid .New (),
1757
+ })
1758
+ build := dbgen .WorkspaceBuild (t ,db , database.WorkspaceBuild {
1759
+ WorkspaceID :workspace .ID ,
1760
+ TemplateVersionID :version .ID ,
1761
+ InitiatorID :initiator .ID ,
1762
+ Transition :database .WorkspaceTransitionDelete ,
1763
+ Reason :tc .buildReason ,
1764
+ })
1765
+ job := dbgen .ProvisionerJob (t ,db ,ps , database.ProvisionerJob {
1766
+ FileID :file .ID ,
1767
+ Type :database .ProvisionerJobTypeWorkspaceBuild ,
1768
+ Input :must (json .Marshal (provisionerdserver.WorkspaceProvisionJob {
1769
+ WorkspaceBuildID :build .ID ,
1770
+ })),
1771
+ OrganizationID :pd .OrganizationID ,
1772
+ })
1773
+ _ ,err = db .AcquireProvisionerJob (ctx , database.AcquireProvisionerJobParams {
1774
+ OrganizationID :pd .OrganizationID ,
1775
+ WorkerID : uuid.NullUUID {
1776
+ UUID :pd .ID ,
1777
+ Valid :true ,
1778
+ },
1779
+ Types : []database.ProvisionerType {database .ProvisionerTypeEcho },
1780
+ })
1781
+ require .NoError (t ,err )
1782
+
1783
+ _ ,err = srv .FailJob (ctx ,& proto.FailedJob {
1784
+ JobId :job .ID .String (),
1785
+ Type :& proto.FailedJob_WorkspaceBuild_ {
1786
+ WorkspaceBuild :& proto.FailedJob_WorkspaceBuild {
1787
+ State : []byte {},
1788
+ },
1789
+ },
1790
+ })
1791
+ require .NoError (t ,err )
1792
+
1793
+ if tc .shouldNotify {
1794
+ // Validate that the notification was sent and contained the expected values.
1795
+ require .Len (t ,notifEnq .sent ,1 )
1796
+ require .Equal (t ,notifEnq .sent [0 ].userID ,user .ID )
1797
+ require .Contains (t ,notifEnq .sent [0 ].targets ,template .ID )
1798
+ require .Contains (t ,notifEnq .sent [0 ].targets ,workspace .ID )
1799
+ require .Contains (t ,notifEnq .sent [0 ].targets ,workspace .OrganizationID )
1800
+ require .Contains (t ,notifEnq .sent [0 ].targets ,user .ID )
1801
+ require .Equal (t ,"autobuild" ,notifEnq .sent [0 ].labels ["initiator" ])
1802
+ require .Equal (t ,string (tc .buildReason ),notifEnq .sent [0 ].labels ["reason" ])
1803
+ }else {
1804
+ require .Len (t ,notifEnq .sent ,0 )
1805
+ }
1806
+ })
1807
+ }
1808
+ })
1698
1809
}
1699
1810
1700
1811
type overrides struct {