25
25
import com .google .cloud .spanner .Database ;
26
26
import com .google .cloud .spanner .DatabaseAdminClient ;
27
27
import com .google .cloud .spanner .DatabaseClient ;
28
+ import com .google .cloud .spanner .DatabaseId ;
28
29
import com .google .cloud .spanner .ErrorCode ;
29
30
import com .google .cloud .spanner .IntegrationTestEnv ;
30
31
import com .google .cloud .spanner .ParallelIntegrationTest ;
35
36
import com .google .cloud .spanner .Statement ;
36
37
import com .google .cloud .spanner .testing .RemoteSpannerHelper ;
37
38
import com .google .common .collect .ImmutableList ;
39
+ import java .util .ArrayList ;
38
40
import java .util .Arrays ;
41
+ import java .util .List ;
39
42
import java .util .concurrent .TimeUnit ;
40
- import org .junit .Before ;
43
+ import org .junit .AfterClass ;
44
+ import org .junit .BeforeClass ;
41
45
import org .junit .ClassRule ;
42
46
import org .junit .Test ;
43
47
import org .junit .experimental .categories .Category ;
49
53
@ RunWith (JUnit4 .class )
50
54
public class ITDatabaseRolePermissionTest {
51
55
@ ClassRule public static IntegrationTestEnv env = new IntegrationTestEnv ();
52
- private DatabaseAdminClient dbAdminClient ;
53
- private RemoteSpannerHelper testHelper ;
56
+ private static DatabaseAdminClient dbAdminClient ;
57
+ private static RemoteSpannerHelper testHelper ;
54
58
55
- @ Before
56
- public void setUp () {
59
+ private static List <DatabaseId > databasesToDrop ;
60
+
61
+ @ BeforeClass
62
+ public static void setUp () {
57
63
assumeFalse ("Emulator does not support database roles" , isUsingEmulator ());
58
64
testHelper = env .getTestHelper ();
59
65
dbAdminClient = testHelper .getClient ().getDatabaseAdminClient ();
66
+ databasesToDrop = new ArrayList <>();
67
+ }
68
+
69
+ @ AfterClass
70
+ public static void cleanup () throws Exception {
71
+ if (databasesToDrop != null ) {
72
+ for (DatabaseId id : databasesToDrop ) {
73
+ try {
74
+ dbAdminClient .dropDatabase (id .getInstanceId ().getInstance (), id .getDatabase ());
75
+ } catch (Exception e ) {
76
+ System .err .println ("Failed to drop database " + id + ", skipping...: " + e .getMessage ());
77
+ }
78
+ }
79
+ }
60
80
}
61
81
62
82
@ Test
@@ -84,7 +104,6 @@ public void grantAndRevokeDatabaseRolePermissions() throws Exception {
84
104
85
105
Spanner spanner = options .getService ();
86
106
DatabaseClient dbClient = spanner .getDatabaseClient (createdDatabase .getId ());
87
- DatabaseAdminClient dbAdminClient = spanner .getDatabaseAdminClient ();
88
107
89
108
// Test SELECT permissions to role dbRoleParent on table T.
90
109
// Query using dbRoleParent should return result.
@@ -103,7 +122,7 @@ public void grantAndRevokeDatabaseRolePermissions() throws Exception {
103
122
dbAdminClient
104
123
.updateDatabaseDdl (
105
124
instanceId , databaseId , Arrays .asList (revokeSelectOnTableFromParent ), null )
106
- .get ();
125
+ .get (5 , TimeUnit . MINUTES );
107
126
108
127
// Test SELECT permissions to role dbRoleParent on table T.
109
128
// Query using dbRoleParent should return PermissionDeniedException.
@@ -118,7 +137,8 @@ public void grantAndRevokeDatabaseRolePermissions() throws Exception {
118
137
final String dropRoleParent = String .format ("DROP ROLE %s" , dbRoleParent );
119
138
dbAdminClient
120
139
.updateDatabaseDdl (instanceId , databaseId , Arrays .asList (dropTableT , dropRoleParent ), null )
121
- .get ();
140
+ .get (5 , TimeUnit .MINUTES );
141
+ databasesToDrop .add (createdDatabase .getId ());
122
142
}
123
143
124
144
@ Test
@@ -141,7 +161,6 @@ public void roleWithNoPermissions() throws Exception {
141
161
142
162
Spanner spanner = options .getService ();
143
163
DatabaseClient dbClient = spanner .getDatabaseClient (createdDatabase .getId ());
144
- DatabaseAdminClient dbAdminClient = spanner .getDatabaseAdminClient ();
145
164
146
165
// Test SELECT permissions to role dbRoleOrphan on table T.
147
166
// Query using dbRoleOrphan should return PermissionDeniedException.
@@ -156,6 +175,7 @@ public void roleWithNoPermissions() throws Exception {
156
175
final String dropRoleParent = String .format ("DROP ROLE %s" , dbRoleOrphan );
157
176
dbAdminClient
158
177
.updateDatabaseDdl (instanceId , databaseId , Arrays .asList (dropTableT , dropRoleParent ), null )
159
- .get ();
178
+ .get (5 , TimeUnit .MINUTES );
179
+ databasesToDrop .add (createdDatabase .getId ());
160
180
}
161
181
}
0 commit comments