@@ -1569,31 +1569,42 @@ func TestInsertWorkspaceResource(t *testing.T) {
1569
1569
func TestNotifications (t * testing.T ) {
1570
1570
t .Parallel ()
1571
1571
1572
- t .Run ("Workspace deletion " , func (t * testing.T ) {
1572
+ t .Run ("Workspace Events " , func (t * testing.T ) {
1573
1573
t .Parallel ()
1574
1574
1575
1575
tests := []struct {
1576
- name string
1577
- deletionReason database.BuildReason
1578
- shouldNotify bool
1579
- shouldSelfInitiate bool
1576
+ name string
1577
+
1578
+ buildReason database.BuildReason
1579
+ buildFailed bool
1580
+ shouldNotify bool
1581
+ shouldSelfInitiate bool
1582
+ shouldDeleteWorkspace bool
1580
1583
}{
1581
1584
{
1582
- name : "initiated by autodelete" ,
1583
- deletionReason : database .BuildReasonAutodelete ,
1584
- shouldNotify : true ,
1585
+ name : "initiated by autodelete" ,
1586
+ buildReason : database .BuildReasonAutodelete ,
1587
+ shouldNotify : true ,
1588
+ shouldDeleteWorkspace : true ,
1589
+ },
1590
+ {
1591
+ name : "initiated by self" ,
1592
+ buildReason : database .BuildReasonInitiator ,
1593
+ shouldNotify : false ,
1594
+ shouldSelfInitiate : true ,
1595
+ shouldDeleteWorkspace : true ,
1585
1596
},
1586
1597
{
1587
- name : "initiated by self " ,
1588
- deletionReason : database .BuildReasonInitiator ,
1589
- shouldNotify : false ,
1590
- shouldSelfInitiate : true ,
1598
+ name : "initiated by someone else " ,
1599
+ buildReason : database .BuildReasonInitiator ,
1600
+ shouldNotify : true ,
1601
+ shouldDeleteWorkspace : true ,
1591
1602
},
1592
1603
{
1593
- name : "initiated by someone else " ,
1594
- deletionReason : database .BuildReasonInitiator ,
1595
- shouldNotify : true ,
1596
- shouldSelfInitiate : false ,
1604
+ name : "initiated by autostart but failed " ,
1605
+ buildReason : database .BuildReasonAutostart ,
1606
+ buildFailed : true ,
1607
+ shouldNotify : true ,
1597
1608
},
1598
1609
}
1599
1610
@@ -1604,7 +1615,11 @@ func TestNotifications(t *testing.T) {
1604
1615
ctx := context .Background ()
1605
1616
notifEnq := & fakeNotificationEnqueuer {}
1606
1617
1607
- srv , db , ps , pd := setup (t , false , & overrides {
1618
+ // Otherwise `(*Server).FailJob` fails with:
1619
+ // audit log - get build {"error": "sql: no rows in result set"}
1620
+ ignoreLogErrors := tc .buildFailed
1621
+
1622
+ srv , db , ps , pd := setup (t , ignoreLogErrors , & overrides {
1608
1623
notificationEnqueuer : notifEnq ,
1609
1624
})
1610
1625
@@ -1640,7 +1655,7 @@ func TestNotifications(t *testing.T) {
1640
1655
TemplateVersionID : version .ID ,
1641
1656
InitiatorID : initiator .ID ,
1642
1657
Transition : database .WorkspaceTransitionDelete ,
1643
- Reason : tc .deletionReason ,
1658
+ Reason : tc .buildReason ,
1644
1659
})
1645
1660
job := dbgen .ProvisionerJob (t , db , ps , database.ProvisionerJob {
1646
1661
FileID : file .ID ,
@@ -1660,23 +1675,34 @@ func TestNotifications(t *testing.T) {
1660
1675
})
1661
1676
require .NoError (t , err )
1662
1677
1663
- _ , err = srv .CompleteJob (ctx , & proto.CompletedJob {
1664
- JobId : job .ID .String (),
1665
- Type : & proto.CompletedJob_WorkspaceBuild_ {
1666
- WorkspaceBuild : & proto.CompletedJob_WorkspaceBuild {
1667
- State : []byte {},
1668
- Resources : []* sdkproto.Resource {{
1669
- Name : "example" ,
1670
- Type : "aws_instance" ,
1671
- }},
1678
+ if tc .buildFailed {
1679
+ _ , err = srv .FailJob (ctx , & proto.FailedJob {
1680
+ JobId : job .ID .String (),
1681
+ Type : & proto.FailedJob_WorkspaceBuild_ {
1682
+ WorkspaceBuild : & proto.FailedJob_WorkspaceBuild {
1683
+ State : []byte {},
1684
+ },
1672
1685
},
1673
- },
1674
- })
1686
+ })
1687
+ } else {
1688
+ _ , err = srv .CompleteJob (ctx , & proto.CompletedJob {
1689
+ JobId : job .ID .String (),
1690
+ Type : & proto.CompletedJob_WorkspaceBuild_ {
1691
+ WorkspaceBuild : & proto.CompletedJob_WorkspaceBuild {
1692
+ State : []byte {},
1693
+ Resources : []* sdkproto.Resource {{
1694
+ Name : "example" ,
1695
+ Type : "aws_instance" ,
1696
+ }},
1697
+ },
1698
+ },
1699
+ })
1700
+ }
1675
1701
require .NoError (t , err )
1676
1702
1677
1703
workspace , err = db .GetWorkspaceByID (ctx , workspace .ID )
1678
1704
require .NoError (t , err )
1679
- require .True ( t , workspace .Deleted )
1705
+ require .Equal ( t , tc . shouldDeleteWorkspace , workspace .Deleted )
1680
1706
1681
1707
if tc .shouldNotify {
1682
1708
// Validate that the notification was sent and contained the expected values.
@@ -1686,8 +1712,8 @@ func TestNotifications(t *testing.T) {
1686
1712
require .Contains (t , notifEnq .sent [0 ].targets , workspace .ID )
1687
1713
require .Contains (t , notifEnq .sent [0 ].targets , workspace .OrganizationID )
1688
1714
require .Contains (t , notifEnq .sent [0 ].targets , user .ID )
1689
- if tc .deletionReason == database .BuildReasonInitiator {
1690
- require .Equal (t , notifEnq .sent [0 ].labels ["initiatedBy " ], initiator .Username )
1715
+ if tc .buildReason == database .BuildReasonInitiator {
1716
+ require .Equal (t , notifEnq .sent [0 ].labels ["initiator " ], initiator .Username )
1691
1717
}
1692
1718
} else {
1693
1719
require .Len (t , notifEnq .sent , 0 )
0 commit comments