getManager().copyQuotaSnapshots(); final Map<TableName, SpaceQuotaSnapshot> newSnapshots = fetchSnapshotsFromQuotaTable(); if (LOG.isTraceEnabled()) { LOG.trace(currentSnapshots.size() + " table quota snapshots are collected, "
@Test public void testMissingAllColumns() throws IOException { when(chore.fetchSnapshotsFromQuotaTable()).thenCallRealMethod(); ResultScanner scanner = mock(ResultScanner.class); Table quotaTable = mock(Table.class); when(conn.getTable(QuotaUtil.QUOTA_TABLE_NAME)).thenReturn(quotaTable); when(quotaTable.getScanner(any(Scan.class))).thenReturn(scanner); List<Result> results = new ArrayList<>(); results.add(Result.create(Collections.emptyList())); when(scanner.iterator()).thenReturn(results.iterator()); try { chore.fetchSnapshotsFromQuotaTable(); fail("Expected an IOException, but did not receive one."); } catch (IOException e) { // Expected an error because we had no cells in the row. // This should only happen due to programmer error. } }
@Test public void testMissingDesiredColumn() throws IOException { when(chore.fetchSnapshotsFromQuotaTable()).thenCallRealMethod(); ResultScanner scanner = mock(ResultScanner.class); Table quotaTable = mock(Table.class); when(conn.getTable(QuotaUtil.QUOTA_TABLE_NAME)).thenReturn(quotaTable); when(quotaTable.getScanner(any(Scan.class))).thenReturn(scanner); List<Result> results = new ArrayList<>(); // Give a column that isn't the one we want Cell c = new KeyValue(toBytes("t:inviolation"), toBytes("q"), toBytes("s"), new byte[0]); results.add(Result.create(Collections.singletonList(c))); when(scanner.iterator()).thenReturn(results.iterator()); try { chore.fetchSnapshotsFromQuotaTable(); fail("Expected an IOException, but did not receive one."); } catch (IOException e) { // Expected an error because we were missing the column we expected in this row. // This should only happen due to programmer error. } }
@Test public void testParsingError() throws IOException { when(chore.fetchSnapshotsFromQuotaTable()).thenCallRealMethod(); ResultScanner scanner = mock(ResultScanner.class); Table quotaTable = mock(Table.class); when(conn.getTable(QuotaUtil.QUOTA_TABLE_NAME)).thenReturn(quotaTable); when(quotaTable.getScanner(any(Scan.class))).thenReturn(scanner); List<Result> results = new ArrayList<>(); Cell c = new KeyValue(toBytes("t:inviolation"), toBytes("u"), toBytes("v"), new byte[0]); results.add(Result.create(Collections.singletonList(c))); when(scanner.iterator()).thenReturn(results.iterator()); try { chore.fetchSnapshotsFromQuotaTable(); fail("Expected an IOException, but did not receive one."); } catch (IOException e) { // We provided a garbage serialized protobuf message (empty byte array), this should // in turn throw an IOException } } }
@Test public void testNewPolicyOverridesOld() throws IOException { final Map<TableName,SpaceQuotaSnapshot> policiesToEnforce = new HashMap<>(); policiesToEnforce.put( TableName.valueOf("table1"), new SpaceQuotaSnapshot(new SpaceQuotaStatus(SpaceViolationPolicy.DISABLE), 1024L, 512L)); policiesToEnforce.put( TableName.valueOf("table2"), new SpaceQuotaSnapshot(new SpaceQuotaStatus(SpaceViolationPolicy.NO_WRITES), 2048L, 512L)); policiesToEnforce.put( TableName.valueOf("table3"), new SpaceQuotaSnapshot(new SpaceQuotaStatus(SpaceViolationPolicy.NO_INSERTS), 4096L, 512L)); final Map<TableName,SpaceQuotaSnapshot> previousPolicies = new HashMap<>(); previousPolicies.put( TableName.valueOf("table1"), new SpaceQuotaSnapshot(new SpaceQuotaStatus(SpaceViolationPolicy.NO_WRITES), 8192L, 512L)); // No active enforcements when(manager.getActivePoliciesAsMap()).thenReturn(previousPolicies); // Policies to enforce when(chore.fetchSnapshotsFromQuotaTable()).thenReturn(policiesToEnforce); chore.chore(); for (Entry<TableName,SpaceQuotaSnapshot> entry : policiesToEnforce.entrySet()) { verify(manager).enforceViolationPolicy(entry.getKey(), entry.getValue()); } verify(manager, never()).disableViolationPolicyEnforcement(TableName.valueOf("table1")); }
@Test public void testPoliciesAreEnforced() throws IOException { // Create a number of policies that should be enforced (usage > limit) final Map<TableName,SpaceQuotaSnapshot> policiesToEnforce = new HashMap<>(); policiesToEnforce.put( TableName.valueOf("table1"), new SpaceQuotaSnapshot(new SpaceQuotaStatus(SpaceViolationPolicy.DISABLE), 1024L, 512L)); policiesToEnforce.put( TableName.valueOf("table2"), new SpaceQuotaSnapshot(new SpaceQuotaStatus(SpaceViolationPolicy.NO_INSERTS), 2048L, 512L)); policiesToEnforce.put( TableName.valueOf("table3"), new SpaceQuotaSnapshot(new SpaceQuotaStatus(SpaceViolationPolicy.NO_WRITES), 4096L, 512L)); policiesToEnforce.put( TableName.valueOf("table4"), new SpaceQuotaSnapshot( new SpaceQuotaStatus(SpaceViolationPolicy.NO_WRITES_COMPACTIONS), 8192L, 512L)); // No active enforcements when(manager.copyQuotaSnapshots()).thenReturn(Collections.emptyMap()); // Policies to enforce when(chore.fetchSnapshotsFromQuotaTable()).thenReturn(policiesToEnforce); chore.chore(); for (Entry<TableName,SpaceQuotaSnapshot> entry : policiesToEnforce.entrySet()) { // Ensure we enforce the policy verify(manager).enforceViolationPolicy(entry.getKey(), entry.getValue()); // Don't disable any policies verify(manager, never()).disableViolationPolicyEnforcement(entry.getKey()); } }
when(chore.fetchSnapshotsFromQuotaTable()).thenReturn(policiesToEnforce);
@Test public void testMissingAllColumns() throws IOException { when(chore.fetchSnapshotsFromQuotaTable()).thenCallRealMethod(); ResultScanner scanner = mock(ResultScanner.class); Table quotaTable = mock(Table.class); when(conn.getTable(QuotaUtil.QUOTA_TABLE_NAME)).thenReturn(quotaTable); when(quotaTable.getScanner(any(Scan.class))).thenReturn(scanner); List<Result> results = new ArrayList<>(); results.add(Result.create(Collections.emptyList())); when(scanner.iterator()).thenReturn(results.iterator()); try { chore.fetchSnapshotsFromQuotaTable(); fail("Expected an IOException, but did not receive one."); } catch (IOException e) { // Expected an error because we had no cells in the row. // This should only happen due to programmer error. } }
@Test public void testMissingDesiredColumn() throws IOException { when(chore.fetchSnapshotsFromQuotaTable()).thenCallRealMethod(); ResultScanner scanner = mock(ResultScanner.class); Table quotaTable = mock(Table.class); when(conn.getTable(QuotaUtil.QUOTA_TABLE_NAME)).thenReturn(quotaTable); when(quotaTable.getScanner(any(Scan.class))).thenReturn(scanner); List<Result> results = new ArrayList<>(); // Give a column that isn't the one we want Cell c = new KeyValue(toBytes("t:inviolation"), toBytes("q"), toBytes("s"), new byte[0]); results.add(Result.create(Collections.singletonList(c))); when(scanner.iterator()).thenReturn(results.iterator()); try { chore.fetchSnapshotsFromQuotaTable(); fail("Expected an IOException, but did not receive one."); } catch (IOException e) { // Expected an error because we were missing the column we expected in this row. // This should only happen due to programmer error. } }
@Test public void testParsingError() throws IOException { when(chore.fetchSnapshotsFromQuotaTable()).thenCallRealMethod(); ResultScanner scanner = mock(ResultScanner.class); Table quotaTable = mock(Table.class); when(conn.getTable(QuotaUtil.QUOTA_TABLE_NAME)).thenReturn(quotaTable); when(quotaTable.getScanner(any(Scan.class))).thenReturn(scanner); List<Result> results = new ArrayList<>(); Cell c = new KeyValue(toBytes("t:inviolation"), toBytes("u"), toBytes("v"), new byte[0]); results.add(Result.create(Collections.singletonList(c))); when(scanner.iterator()).thenReturn(results.iterator()); try { chore.fetchSnapshotsFromQuotaTable(); fail("Expected an IOException, but did not receive one."); } catch (IOException e) { // We provided a garbage serialized protobuf message (empty byte array), this should // in turn throw an IOException } } }
when(chore.fetchSnapshotsFromQuotaTable()).thenReturn(policiesToEnforce);
@Test public void testNewPolicyOverridesOld() throws IOException { final Map<TableName,SpaceQuotaSnapshot> policiesToEnforce = new HashMap<>(); policiesToEnforce.put( TableName.valueOf("table1"), new SpaceQuotaSnapshot(new SpaceQuotaStatus(SpaceViolationPolicy.DISABLE), 1024L, 512L)); policiesToEnforce.put( TableName.valueOf("table2"), new SpaceQuotaSnapshot(new SpaceQuotaStatus(SpaceViolationPolicy.NO_WRITES), 2048L, 512L)); policiesToEnforce.put( TableName.valueOf("table3"), new SpaceQuotaSnapshot(new SpaceQuotaStatus(SpaceViolationPolicy.NO_INSERTS), 4096L, 512L)); final Map<TableName,SpaceQuotaSnapshot> previousPolicies = new HashMap<>(); previousPolicies.put( TableName.valueOf("table1"), new SpaceQuotaSnapshot(new SpaceQuotaStatus(SpaceViolationPolicy.NO_WRITES), 8192L, 512L)); // No active enforcements when(manager.getActivePoliciesAsMap()).thenReturn(previousPolicies); // Policies to enforce when(chore.fetchSnapshotsFromQuotaTable()).thenReturn(policiesToEnforce); chore.chore(); for (Entry<TableName,SpaceQuotaSnapshot> entry : policiesToEnforce.entrySet()) { verify(manager).enforceViolationPolicy(entry.getKey(), entry.getValue()); } verify(manager, never()).disableViolationPolicyEnforcement(TableName.valueOf("table1")); }
@Test public void testPoliciesAreEnforced() throws IOException { // Create a number of policies that should be enforced (usage > limit) final Map<TableName,SpaceQuotaSnapshot> policiesToEnforce = new HashMap<>(); policiesToEnforce.put( TableName.valueOf("table1"), new SpaceQuotaSnapshot(new SpaceQuotaStatus(SpaceViolationPolicy.DISABLE), 1024L, 512L)); policiesToEnforce.put( TableName.valueOf("table2"), new SpaceQuotaSnapshot(new SpaceQuotaStatus(SpaceViolationPolicy.NO_INSERTS), 2048L, 512L)); policiesToEnforce.put( TableName.valueOf("table3"), new SpaceQuotaSnapshot(new SpaceQuotaStatus(SpaceViolationPolicy.NO_WRITES), 4096L, 512L)); policiesToEnforce.put( TableName.valueOf("table4"), new SpaceQuotaSnapshot( new SpaceQuotaStatus(SpaceViolationPolicy.NO_WRITES_COMPACTIONS), 8192L, 512L)); // No active enforcements when(manager.copyQuotaSnapshots()).thenReturn(Collections.emptyMap()); // Policies to enforce when(chore.fetchSnapshotsFromQuotaTable()).thenReturn(policiesToEnforce); chore.chore(); for (Entry<TableName,SpaceQuotaSnapshot> entry : policiesToEnforce.entrySet()) { // Ensure we enforce the policy verify(manager).enforceViolationPolicy(entry.getKey(), entry.getValue()); // Don't disable any policies verify(manager, never()).disableViolationPolicyEnforcement(entry.getKey()); } }