001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.security.access;
019
020import static org.junit.jupiter.api.Assertions.assertThrows;
021import static org.mockito.Mockito.mock;
022
023import org.apache.hadoop.hbase.WriteAttemptedOnReadOnlyClusterException;
024import org.apache.hadoop.hbase.client.Mutation;
025import org.apache.hadoop.hbase.coprocessor.ObserverContext;
026import org.apache.hadoop.hbase.coprocessor.RegionServerCoprocessorEnvironment;
027import org.apache.hadoop.hbase.testclassification.SecurityTests;
028import org.apache.hadoop.hbase.testclassification.SmallTests;
029import org.junit.jupiter.api.AfterEach;
030import org.junit.jupiter.api.BeforeEach;
031import org.junit.jupiter.api.Tag;
032import org.junit.jupiter.api.Test;
033
034import org.apache.hbase.thirdparty.com.google.protobuf.ByteString;
035
036import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos;
037import org.apache.hadoop.hbase.shaded.protobuf.generated.WALProtos;
038
039// Tests methods of Region Server Observer which are implemented in ReadOnlyController,
040// by mocking the coprocessor environment and dependencies
041@Tag(SecurityTests.TAG)
042@Tag(SmallTests.TAG)
043public class TestReadOnlyControllerRegionServerObserver {
044
045  RegionServerReadOnlyController regionServerReadOnlyController;
046
047  // Region Server Coprocessor mocking variables
048  ObserverContext<RegionServerCoprocessorEnvironment> ctx;
049  AdminProtos.WALEntry walEntry;
050  Mutation mutation;
051
052  @BeforeEach
053  public void setup() throws Exception {
054    regionServerReadOnlyController = new RegionServerReadOnlyController();
055
056    // mocking variables initialization
057    ctx = mock(ObserverContext.class);
058    walEntry = AdminProtos.WALEntry.newBuilder()
059      .setKey(WALProtos.WALKey.newBuilder().setTableName(ByteString.copyFromUtf8("test"))
060        .setEncodedRegionName(ByteString.copyFromUtf8("regionA")).setLogSequenceNumber(100)
061        .setWriteTime(2).build())
062      .build();
063    mutation = mock(Mutation.class);
064  }
065
066  @AfterEach
067  public void tearDown() throws Exception {
068
069  }
070
071  @Test
072  public void testPreRollWALWriterRequestReadOnlyException() {
073    assertThrows(WriteAttemptedOnReadOnlyClusterException.class,
074      () -> regionServerReadOnlyController.preRollWALWriterRequest(ctx));
075  }
076
077  @Test
078  public void testPreReplicationSinkBatchMutateReadOnlyException() {
079    assertThrows(WriteAttemptedOnReadOnlyClusterException.class,
080      () -> regionServerReadOnlyController.preReplicationSinkBatchMutate(ctx, walEntry, mutation));
081  }
082
083  @Test
084  public void testPreReplicateLogEntriesReadOnlyException() {
085    assertThrows(WriteAttemptedOnReadOnlyClusterException.class,
086      () -> regionServerReadOnlyController.preReplicateLogEntries(ctx));
087  }
088}