rob05c commented on a change in pull request #3173: Fix Traffic Ops API Tests to delete on t.Fatal or panic URL: https://github.com/apache/trafficcontrol/pull/3173#discussion_r245165527
########## File path: traffic_ops/testing/api/v14/withobjs.go ########## @@ -0,0 +1,93 @@ +/* + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package v14 + +import ( + "testing" +) + +// WithObjs creates the objs in order, runs f, and defers deleting the objs in the same order. +// +// Because deletion is deferred, using this ensures objects will be cleaned up if f panics or calls t.Fatal, as much as possible. +// +// Note that f itself may still create things which are not cleaned up properly, and likewise, the object creation and deletion tests themselves may fail. +// All tests in the Traffic Ops API Testing framework use the same Traffic Ops instance, with persistent data. Because of this, when any test fails, all subsequent tests should be considered invalid, irrespective whether they pass or fail. Users are encouraged to use `go test -failfast`. +func WithObjs(t *testing.T, objs []TCObj, f func()) { + for _, obj := range objs { + defer withFuncs[obj].Delete(t) + withFuncs[obj].Create(t) + } + f() +} + +type TCObj int + +const ( + CacheGroups TCObj = iota + CDNs + CDNFederations + Coordinates + DeliveryServices + DeliveryServiceRequests + DeliveryServiceRequestComments + Divisions + Origins + Parameters + PhysLocations + Profiles + ProfileParameters + Regions + Roles + Servers + Statuses + StaticDNSEntries + SteeringTargets + Tenants + Types + Users + UsersDeliveryServices +) + +type TCObjFuncs struct { + Create func(t *testing.T) + Delete func(t *testing.T) +} + +var withFuncs = map[TCObj]TCObjFuncs{ + CacheGroups: {CreateTestCacheGroups, DeleteTestCacheGroups}, + CDNs: {CreateTestCDNs, DeleteTestCDNs}, + CDNFederations: {CreateTestCDNFederations, DeleteTestCDNFederations}, + Coordinates: {CreateTestCoordinates, DeleteTestCoordinates}, + DeliveryServices: {CreateTestDeliveryServices, DeleteTestDeliveryServices}, + DeliveryServiceRequests: {CreateTestDeliveryServiceRequests, DeleteTestDeliveryServiceRequests}, + DeliveryServiceRequestComments: {CreateTestDeliveryServiceRequestComments, DeleteTestDeliveryServiceRequestComments}, + Divisions: {CreateTestDivisions, DeleteTestDivisions}, + Origins: {CreateTestOrigins, DeleteTestOrigins}, + Parameters: {CreateTestParameters, DeleteTestParameters}, + PhysLocations: {CreateTestPhysLocations, DeleteTestPhysLocations}, + Profiles: {CreateTestProfiles, DeleteTestProfiles}, + ProfileParameters: {CreateTestProfileParameters, DeleteTestProfileParameters}, + Regions: {CreateTestRegions, DeleteTestRegions}, + Roles: {CreateTestRoles, DeleteTestRoles}, + Servers: {CreateTestServers, DeleteTestServers}, + Statuses: {CreateTestStatuses, DeleteTestStatuses}, + StaticDNSEntries: {CreateTestStaticDNSEntries, DeleteTestStaticDNSEntries}, + SteeringTargets: {CreateTestSteeringTargets, DeleteTestSteeringTargets}, + Tenants: {CreateTestTenants, DeleteTestTenants}, + Types: {CreateTestTypes, DeleteTestTypes}, + Users: {CreateTestUsers, ForceDeleteTestUsers}, + UsersDeliveryServices: {CreateTestUsersDeliveryServices, DeleteTestUsersDeliveryServices}, Review comment: I'll try upgrading ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services