@@ -4714,7 +4714,7 @@ func TestWorkspaceAgentNameUniqueTrigger(t *testing.T) {
4714
4714
t .Skip ("This test makes use of a database trigger not implemented in dbmem" )
4715
4715
}
4716
4716
4717
- createWorkspaceWithAgent := func (t * testing.T ,db database.Store ,org database.Organization ,agentName string ) (database.WorkspaceTable , database.WorkspaceResource , database.WorkspaceAgent ) {
4717
+ createWorkspaceWithAgent := func (t * testing.T ,db database.Store ,org database.Organization ,agentName string ) (database.WorkspaceBuild , database.WorkspaceResource , database.WorkspaceAgent ) {
4718
4718
t .Helper ()
4719
4719
4720
4720
user := dbgen .User (t ,db , database.User {})
@@ -4750,19 +4750,18 @@ func TestWorkspaceAgentNameUniqueTrigger(t *testing.T) {
4750
4750
Name :agentName ,
4751
4751
})
4752
4752
4753
- return workspace ,resource ,agent
4753
+ return build ,resource ,agent
4754
4754
}
4755
4755
4756
- t .Run ("DuplicateNamesInSameWorkspaceBuild " ,func (t * testing.T ) {
4756
+ t .Run ("DuplicateNamesInSameWorkspaceResource " ,func (t * testing.T ) {
4757
4757
t .Parallel ()
4758
4758
4759
4759
db ,_ := dbtestutil .NewDB (t )
4760
4760
org := dbgen .Organization (t ,db , database.Organization {})
4761
4761
ctx := testutil .Context (t ,testutil .WaitShort )
4762
4762
4763
4763
// Given: A workspace with an agent
4764
- _ ,resource ,agent := createWorkspaceWithAgent (t ,db ,org ,"duplicate-agent" )
4765
- require .Equal (t ,"duplicate-agent" ,agent .Name )
4764
+ _ ,resource ,_ := createWorkspaceWithAgent (t ,db ,org ,"duplicate-agent" )
4766
4765
4767
4766
// When: Another agent is created for that workspace with the same name.
4768
4767
_ ,err := db .InsertWorkspaceAgent (ctx , database.InsertWorkspaceAgentParams {
@@ -4785,6 +4784,82 @@ func TestWorkspaceAgentNameUniqueTrigger(t *testing.T) {
4785
4784
require .Contains (t ,pqErr .Message ,`workspace agent name "duplicate-agent" already exists in this workspace resource` )
4786
4785
})
4787
4786
4787
+ t .Run ("DuplicateNamesInSameProvisionerJob" ,func (t * testing.T ) {
4788
+ t .Parallel ()
4789
+
4790
+ db ,_ := dbtestutil .NewDB (t )
4791
+ org := dbgen .Organization (t ,db , database.Organization {})
4792
+ ctx := testutil .Context (t ,testutil .WaitShort )
4793
+
4794
+ // Given: A workspace with an agent
4795
+ _ ,resource ,agent := createWorkspaceWithAgent (t ,db ,org ,"duplicate-agent" )
4796
+
4797
+ // When: A child agent is created for that workspace with the same name.
4798
+ _ ,err := db .InsertWorkspaceAgent (ctx , database.InsertWorkspaceAgentParams {
4799
+ ID :uuid .New (),
4800
+ CreatedAt :time .Now (),
4801
+ UpdatedAt :time .Now (),
4802
+ Name :agent .Name ,
4803
+ ResourceID :resource .ID ,
4804
+ AuthToken :uuid .New (),
4805
+ Architecture :"amd64" ,
4806
+ OperatingSystem :"linux" ,
4807
+ APIKeyScope :database .AgentKeyScopeEnumAll ,
4808
+ })
4809
+
4810
+ // Then: We expect it to fail.
4811
+ require .Error (t ,err )
4812
+ var pqErr * pq.Error
4813
+ require .True (t ,errors .As (err ,& pqErr ))
4814
+ require .Equal (t ,pq .ErrorCode ("23505" ),pqErr .Code )// unique_violation
4815
+ require .Contains (t ,pqErr .Message ,`workspace agent name "duplicate-agent" already exists in this workspace resource` )
4816
+ })
4817
+
4818
+ t .Run ("DuplicateChildNamesOverMultipleResources" ,func (t * testing.T ) {
4819
+ t .Parallel ()
4820
+
4821
+ db ,_ := dbtestutil .NewDB (t )
4822
+ org := dbgen .Organization (t ,db , database.Organization {})
4823
+ ctx := testutil .Context (t ,testutil .WaitShort )
4824
+
4825
+ // Given: A workspace with two agents
4826
+ _ ,resource1 ,agent1 := createWorkspaceWithAgent (t ,db ,org ,"parent-agent-1" )
4827
+
4828
+ resource2 := dbgen .WorkspaceResource (t ,db , database.WorkspaceResource {JobID :resource1 .JobID })
4829
+ agent2 := dbgen .WorkspaceAgent (t ,db , database.WorkspaceAgent {
4830
+ ResourceID :resource2 .ID ,
4831
+ Name :"parent-agent-2" ,
4832
+ })
4833
+
4834
+ // Given: One agent has a child agent
4835
+ agent1Child := dbgen .WorkspaceAgent (t ,db , database.WorkspaceAgent {
4836
+ ParentID : uuid.NullUUID {Valid :true ,UUID :agent1 .ID },
4837
+ Name :"child-agent" ,
4838
+ ResourceID :resource1 .ID ,
4839
+ })
4840
+
4841
+ // When: A child agent is inserted for the other parent.
4842
+ _ ,err := db .InsertWorkspaceAgent (ctx , database.InsertWorkspaceAgentParams {
4843
+ ID :uuid .New (),
4844
+ ParentID : uuid.NullUUID {Valid :true ,UUID :agent2 .ID },
4845
+ CreatedAt :time .Now (),
4846
+ UpdatedAt :time .Now (),
4847
+ Name :agent1Child .Name ,
4848
+ ResourceID :resource2 .ID ,
4849
+ AuthToken :uuid .New (),
4850
+ Architecture :"amd64" ,
4851
+ OperatingSystem :"linux" ,
4852
+ APIKeyScope :database .AgentKeyScopeEnumAll ,
4853
+ })
4854
+
4855
+ // Then: We expect it to fail.
4856
+ require .Error (t ,err )
4857
+ var pqErr * pq.Error
4858
+ require .True (t ,errors .As (err ,& pqErr ))
4859
+ require .Equal (t ,pq .ErrorCode ("23505" ),pqErr .Code )// unique_violation
4860
+ require .Contains (t ,pqErr .Message ,`workspace agent name "child-agent" already exists in this workspace resource` )
4861
+ })
4862
+
4788
4863
t .Run ("SameNamesInDifferentWorkspaces" ,func (t * testing.T ) {
4789
4864
t .Parallel ()
4790
4865