@@ -69,8 +69,8 @@ func testClient(t *testing.T) (*modelarmor.Client, context.Context) {
6969 return client , ctx
7070}
7171
72- // testModelArmorTemplate creates a new ModelArmor template for use in tests.
73- // It returns the created template or an error .
72+ // testModelArmorTemplate creates a new Model Armor template with default
73+ // filter settings for use in integration tests. Returns the created template .
7474func testModelArmorTemplate (t * testing.T , templateID string ) (* modelarmorpb.Template , error ) {
7575 t .Helper ()
7676 tc := testutil .SystemTest (t )
@@ -103,29 +103,19 @@ func testModelArmorTemplate(t *testing.T, templateID string) (*modelarmorpb.Temp
103103 return response , nil
104104}
105105
106- // testModelArmorEmptyTemplate creates a new ModelArmor template for use in tests.
107- // It returns the empty template or an error .
108- func testModelArmorEmptyTemplate (t * testing.T , templateID string ) ( * modelarmorpb. Template , error ) {
106+ // testCleanupTemplate deletes the specified Model Armor template if it exists,
107+ // ignoring the error if the template is already deleted .
108+ func testCleanupTemplate (t * testing.T , templateName string ) {
109109 t .Helper ()
110- tc := testutil .SystemTest (t )
111- locationID := testLocation (t )
112- client , ctx := testClient (t )
113-
114- template := & modelarmorpb.Template {
115- FilterConfig : & modelarmorpb.FilterConfig {}}
116110
117- req := & modelarmorpb. CreateTemplateRequest {
118- Parent : fmt . Sprintf ( "projects/%s/locations/%s" , tc . ProjectID , locationID ),
119- TemplateId : templateID ,
120- Template : template ,
111+ client , ctx := testClient ( t )
112+ err := client . DeleteTemplate ( ctx , & modelarmorpb. DeleteTemplateRequest { Name : templateName })
113+ if err == nil {
114+ return
121115 }
122-
123- response , err := client .CreateTemplate (ctx , req )
124- if err != nil {
125- return nil , fmt .Errorf ("failed to create template: %w" , err )
116+ if terr , ok := grpcstatus .FromError (err ); ! ok || terr .Code () != grpccodes .NotFound {
117+ t .Fatalf ("testCleanupTemplate: failed to delete template %v" , err )
126118 }
127-
128- return response , nil
129119}
130120
131121// testAllFilterTemplate creates a new ModelArmor template with all filters enabled.
@@ -203,6 +193,31 @@ func testAllFilterTemplate(t *testing.T, templateID string) (*modelarmorpb.Templ
203193 return response , filterConfig , nil
204194}
205195
196+ // testModelArmorEmptyTemplate creates a new ModelArmor template for use in tests.
197+ // It returns the empty template or an error.
198+ func testModelArmorEmptyTemplate (t * testing.T , templateID string ) (* modelarmorpb.Template , error ) {
199+ t .Helper ()
200+ tc := testutil .SystemTest (t )
201+ locationID := testLocation (t )
202+ client , ctx := testClient (t )
203+
204+ template := & modelarmorpb.Template {
205+ FilterConfig : & modelarmorpb.FilterConfig {}}
206+
207+ req := & modelarmorpb.CreateTemplateRequest {
208+ Parent : fmt .Sprintf ("projects/%s/locations/%s" , tc .ProjectID , locationID ),
209+ TemplateId : templateID ,
210+ Template : template ,
211+ }
212+
213+ response , err := client .CreateTemplate (ctx , req )
214+ if err != nil {
215+ return nil , fmt .Errorf ("failed to create template: %w" , err )
216+ }
217+
218+ return response , nil
219+ }
220+
206221// testSDPTemplate creates DLP inspect and deidentify templates for use in tests.
207222func testSDPTemplate (t * testing.T , projectID string , locationID string ) (string , string ) {
208223 inspectTemplateID := fmt .Sprintf ("model-armor-inspect-template-%s" , uuid .New ().String ())
@@ -383,21 +398,6 @@ func testModelArmorAdvancedSDPTemplate(t *testing.T, templateID string) (*modela
383398
384399}
385400
386- // testCleanupTemplate deletes the specified Model Armor template if it exists,
387- // ignoring the error if the template is already deleted.
388- func testCleanupTemplate (t * testing.T , templateName string ) {
389- t .Helper ()
390-
391- client , ctx := testClient (t )
392- err := client .DeleteTemplate (ctx , & modelarmorpb.DeleteTemplateRequest {Name : templateName })
393- if err == nil {
394- return
395- }
396- if terr , ok := grpcstatus .FromError (err ); ! ok || terr .Code () != grpccodes .NotFound {
397- t .Fatalf ("testCleanupTemplate: failed to delete template %v" , err )
398- }
399- }
400-
401401// TestCreateModelArmorTemplateWithAdvancedSDP tests creating a
402402// Model Armor template with advanced SDP using DLP templates.
403403func TestCreateModelArmorTemplateWithAdvancedSDP (t * testing.T ) {
@@ -1161,3 +1161,69 @@ func TestListModelArmorTemplatesWithFilter(t *testing.T) {
11611161 t .Errorf ("listModelArmorTemplatesWithFilter: expected %q to contain %q" , got , want )
11621162 }
11631163}
1164+
1165+ // TestUpdateTemplate verifies that the updateModelArmorTemplate function
1166+ // successfully updates the filter configuration of an existing template.
1167+ func TestUpdateTemplate (t * testing.T ) {
1168+ tc := testutil .SystemTest (t )
1169+ locationID := testLocation (t )
1170+ templateID := fmt .Sprintf ("test-model-armor-%s" , uuid .New ().String ())
1171+ templateName := fmt .Sprintf ("projects/%s/locations/%s/templates/%s" , tc .ProjectID , locationID , templateID )
1172+ var buf bytes.Buffer
1173+ if _ , err := testModelArmorTemplate (t , templateID ); err != nil {
1174+ t .Fatal (err )
1175+ }
1176+ defer testCleanupTemplate (t , templateName )
1177+
1178+ if err := updateModelArmorTemplate (& buf , tc .ProjectID , locationID , templateID ); err != nil {
1179+ t .Fatal (err )
1180+ }
1181+
1182+ if got , want := buf .String (), "Updated Filter Config: " ; ! strings .Contains (got , want ) {
1183+ t .Errorf ("updateModelArmorTemplate: expected %q to contain %q" , got , want )
1184+ }
1185+ }
1186+
1187+ // TestUpdateTemplate verifies that the updateModelArmorTemplate function
1188+ // successfully updates the filter configuration of an existing template.
1189+ func TestUpdateTemplateLabels (t * testing.T ) {
1190+ tc := testutil .SystemTest (t )
1191+ locationID := testLocation (t )
1192+ templateID := fmt .Sprintf ("test-model-armor-%s" , uuid .New ().String ())
1193+ templateName := fmt .Sprintf ("projects/%s/locations/%s/templates/%s" , tc .ProjectID , locationID , templateID )
1194+ var buf bytes.Buffer
1195+ if _ , err := testModelArmorTemplate (t , templateID ); err != nil {
1196+ t .Fatal (err )
1197+ }
1198+ defer testCleanupTemplate (t , templateName )
1199+
1200+ if err := updateModelArmorTemplateLabels (& buf , tc .ProjectID , locationID , templateID , map [string ]string {"testkey" : "testvalue" }); err != nil {
1201+ t .Fatal (err )
1202+ }
1203+
1204+ if got , want := buf .String (), "Updated Model Armor Template Labels: " ; ! strings .Contains (got , want ) {
1205+ t .Errorf ("updateModelArmorTemplateLabels: expected %q to contain %q" , got , want )
1206+ }
1207+ }
1208+
1209+ // TestUpdateTemplateMetadata verifies that the updateModelArmorTemplateMetadata function
1210+ // successfully updates the filter configuration of an existing template.
1211+ func TestUpdateTemplateMetadata (t * testing.T ) {
1212+ tc := testutil .SystemTest (t )
1213+ locationID := testLocation (t )
1214+ templateID := fmt .Sprintf ("test-model-armor-%s" , uuid .New ().String ())
1215+ templateName := fmt .Sprintf ("projects/%s/locations/%s/templates/%s" , tc .ProjectID , locationID , templateID )
1216+ var buf bytes.Buffer
1217+ if _ , err := testModelArmorTemplate (t , templateID ); err != nil {
1218+ t .Fatal (err )
1219+ }
1220+ defer testCleanupTemplate (t , templateName )
1221+
1222+ if err := updateModelArmorTemplateMetadata (& buf , tc .ProjectID , locationID , templateID ); err != nil {
1223+ t .Fatal (err )
1224+ }
1225+
1226+ if got , want := buf .String (), "Updated Model Armor Template Metadata: " ; ! strings .Contains (got , want ) {
1227+ t .Errorf ("updateModelArmorTemplateMetadata: expected %q to contain %q" , got , want )
1228+ }
1229+ }
0 commit comments