@@ -1569,41 +1569,146 @@ func TestInsertWorkspaceResource(t *testing.T) {
1569
1569
func TestNotifications (t * testing.T ) {
1570
1570
t .Parallel ()
1571
1571
1572
- t .Run ("Workspace Events " , func (t * testing.T ) {
1572
+ t .Run ("Workspace deletion " , func (t * testing.T ) {
1573
1573
t .Parallel ()
1574
1574
1575
1575
tests := []struct {
1576
- name string
1577
-
1578
- buildReason database.BuildReason
1579
- buildFailed bool
1580
- shouldNotify bool
1581
- shouldSelfInitiate bool
1582
- shouldDeleteWorkspace bool
1576
+ name string
1577
+ deletionReason database.BuildReason
1578
+ shouldNotify bool
1579
+ shouldSelfInitiate bool
1583
1580
}{
1584
1581
{
1585
- name : "initiated by autodelete" ,
1586
- buildReason : database .BuildReasonAutodelete ,
1587
- shouldNotify : true ,
1588
- shouldDeleteWorkspace : true ,
1582
+ name : "initiated by autodelete" ,
1583
+ deletionReason : database .BuildReasonAutodelete ,
1584
+ shouldNotify : true ,
1589
1585
},
1590
1586
{
1591
- name : "initiated by self" ,
1592
- buildReason : database .BuildReasonInitiator ,
1593
- shouldNotify : false ,
1594
- shouldSelfInitiate : true ,
1595
- shouldDeleteWorkspace : true ,
1587
+ name : "initiated by self" ,
1588
+ deletionReason : database .BuildReasonInitiator ,
1589
+ shouldNotify : false ,
1590
+ shouldSelfInitiate : true ,
1596
1591
},
1597
1592
{
1598
- name : "initiated by someone else" ,
1599
- buildReason : database .BuildReasonInitiator ,
1600
- shouldNotify : true ,
1601
- shouldDeleteWorkspace : true ,
1593
+ name : "initiated by someone else" ,
1594
+ deletionReason : database .BuildReasonInitiator ,
1595
+ shouldNotify : true ,
1596
+ shouldSelfInitiate : false ,
1602
1597
},
1598
+ }
1599
+
1600
+ for _ , tc := range tests {
1601
+ t .Run (tc .name , func (t * testing.T ) {
1602
+ t .Parallel ()
1603
+
1604
+ ctx := context .Background ()
1605
+ notifEnq := & fakeNotificationEnqueuer {}
1606
+
1607
+ srv , db , ps , pd := setup (t , false , & overrides {
1608
+ notificationEnqueuer : notifEnq ,
1609
+ })
1610
+
1611
+ user := dbgen .User (t , db , database.User {})
1612
+ initiator := user
1613
+ if ! tc .shouldSelfInitiate {
1614
+ initiator = dbgen .User (t , db , database.User {})
1615
+ }
1616
+
1617
+ template := dbgen .Template (t , db , database.Template {
1618
+ Name : "template" ,
1619
+ Provisioner : database .ProvisionerTypeEcho ,
1620
+ OrganizationID : pd .OrganizationID ,
1621
+ })
1622
+ template , err := db .GetTemplateByID (ctx , template .ID )
1623
+ require .NoError (t , err )
1624
+ file := dbgen .File (t , db , database.File {CreatedBy : user .ID })
1625
+ workspace := dbgen .Workspace (t , db , database.Workspace {
1626
+ TemplateID : template .ID ,
1627
+ OwnerID : user .ID ,
1628
+ OrganizationID : pd .OrganizationID ,
1629
+ })
1630
+ version := dbgen .TemplateVersion (t , db , database.TemplateVersion {
1631
+ OrganizationID : pd .OrganizationID ,
1632
+ TemplateID : uuid.NullUUID {
1633
+ UUID : template .ID ,
1634
+ Valid : true ,
1635
+ },
1636
+ JobID : uuid .New (),
1637
+ })
1638
+ build := dbgen .WorkspaceBuild (t , db , database.WorkspaceBuild {
1639
+ WorkspaceID : workspace .ID ,
1640
+ TemplateVersionID : version .ID ,
1641
+ InitiatorID : initiator .ID ,
1642
+ Transition : database .WorkspaceTransitionDelete ,
1643
+ Reason : tc .deletionReason ,
1644
+ })
1645
+ job := dbgen .ProvisionerJob (t , db , ps , database.ProvisionerJob {
1646
+ FileID : file .ID ,
1647
+ Type : database .ProvisionerJobTypeWorkspaceBuild ,
1648
+ Input : must (json .Marshal (provisionerdserver.WorkspaceProvisionJob {
1649
+ WorkspaceBuildID : build .ID ,
1650
+ })),
1651
+ OrganizationID : pd .OrganizationID ,
1652
+ })
1653
+ _ , err = db .AcquireProvisionerJob (ctx , database.AcquireProvisionerJobParams {
1654
+ OrganizationID : pd .OrganizationID ,
1655
+ WorkerID : uuid.NullUUID {
1656
+ UUID : pd .ID ,
1657
+ Valid : true ,
1658
+ },
1659
+ Types : []database.ProvisionerType {database .ProvisionerTypeEcho },
1660
+ })
1661
+ require .NoError (t , err )
1662
+
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
+ }},
1672
+ },
1673
+ },
1674
+ })
1675
+ require .NoError (t , err )
1676
+
1677
+ workspace , err = db .GetWorkspaceByID (ctx , workspace .ID )
1678
+ require .NoError (t , err )
1679
+ require .True (t , workspace .Deleted )
1680
+
1681
+ if tc .shouldNotify {
1682
+ // Validate that the notification was sent and contained the expected values.
1683
+ require .Len (t , notifEnq .sent , 1 )
1684
+ require .Equal (t , notifEnq .sent [0 ].userID , user .ID )
1685
+ require .Contains (t , notifEnq .sent [0 ].targets , template .ID )
1686
+ require .Contains (t , notifEnq .sent [0 ].targets , workspace .ID )
1687
+ require .Contains (t , notifEnq .sent [0 ].targets , workspace .OrganizationID )
1688
+ require .Contains (t , notifEnq .sent [0 ].targets , user .ID )
1689
+ if tc .deletionReason == database .BuildReasonInitiator {
1690
+ require .Equal (t , notifEnq .sent [0 ].labels ["initiator" ], initiator .Username )
1691
+ }
1692
+ } else {
1693
+ require .Len (t , notifEnq .sent , 0 )
1694
+ }
1695
+ })
1696
+ }
1697
+ })
1698
+
1699
+ t .Run ("Workspace autobuild failed" , func (t * testing.T ) {
1700
+ t .Parallel ()
1701
+
1702
+ tests := []struct {
1703
+ name string
1704
+
1705
+ buildReason database.BuildReason
1706
+ shouldNotify bool
1707
+ shouldDeleteWorkspace bool
1708
+ }{
1603
1709
{
1604
1710
name : "initiated by autostart but failed" ,
1605
1711
buildReason : database .BuildReasonAutostart ,
1606
- buildFailed : true ,
1607
1712
shouldNotify : true ,
1608
1713
},
1609
1714
}
@@ -1617,17 +1722,13 @@ func TestNotifications(t *testing.T) {
1617
1722
1618
1723
// Otherwise `(*Server).FailJob` fails with:
1619
1724
// audit log - get build {"error": "sql: no rows in result set"}
1620
- ignoreLogErrors := tc .buildFailed
1621
-
1725
+ ignoreLogErrors := true
1622
1726
srv , db , ps , pd := setup (t , ignoreLogErrors , & overrides {
1623
1727
notificationEnqueuer : notifEnq ,
1624
1728
})
1625
1729
1626
1730
user := dbgen .User (t , db , database.User {})
1627
1731
initiator := user
1628
- if ! tc .shouldSelfInitiate {
1629
- initiator = dbgen .User (t , db , database.User {})
1630
- }
1631
1732
1632
1733
template := dbgen .Template (t , db , database.Template {
1633
1734
Name : "template" ,
@@ -1675,29 +1776,14 @@ func TestNotifications(t *testing.T) {
1675
1776
})
1676
1777
require .NoError (t , err )
1677
1778
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
- },
1685
- },
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
- },
1779
+ _ , err = srv .FailJob (ctx , & proto.FailedJob {
1780
+ JobId : job .ID .String (),
1781
+ Type : & proto.FailedJob_WorkspaceBuild_ {
1782
+ WorkspaceBuild : & proto.FailedJob_WorkspaceBuild {
1783
+ State : []byte {},
1698
1784
},
1699
- })
1700
- }
1785
+ },
1786
+ })
1701
1787
require .NoError (t , err )
1702
1788
1703
1789
workspace , err = db .GetWorkspaceByID (ctx , workspace .ID )
@@ -1712,12 +1798,7 @@ func TestNotifications(t *testing.T) {
1712
1798
require .Contains (t , notifEnq .sent [0 ].targets , workspace .ID )
1713
1799
require .Contains (t , notifEnq .sent [0 ].targets , workspace .OrganizationID )
1714
1800
require .Contains (t , notifEnq .sent [0 ].targets , user .ID )
1715
- if tc .buildReason == database .BuildReasonInitiator {
1716
- require .Equal (t , notifEnq .sent [0 ].labels ["initiator" ], initiator .Username )
1717
- } else {
1718
- _ , ok := notifEnq .sent [0 ].labels ["initiator" ]
1719
- require .False (t , ok , "initiator label not expected" )
1720
- }
1801
+ require .Equal (t , notifEnq .sent [0 ].labels ["initiator" ], "autobuild" )
1721
1802
} else {
1722
1803
require .Len (t , notifEnq .sent , 0 )
1723
1804
}
0 commit comments