Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chunhui-shi committed Nov 16, 2017
1 parent 6eeb933 commit 435ee17
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.drill.exec.store.mock;

import org.apache.calcite.schema.SchemaPlus;
import org.apache.drill.exec.server.DrillbitContext;
import org.apache.drill.exec.store.SchemaConfig;

import java.io.IOException;

public class MockBreakageStorage extends MockStorageEngine {

boolean breakRegister;
public MockBreakageStorage(MockStorageEngineConfig configuration, DrillbitContext context, String name) {
super(configuration, context, name);
breakRegister = false;
}

public void setBreakRegister(boolean breakRegister) {
this.breakRegister = breakRegister;
}

@Override
public void registerSchemas(SchemaConfig schemaConfig, SchemaPlus parent) throws IOException {
if (breakRegister) {
throw new IOException("mock breakRegister!");
}
super.registerSchemas(schemaConfig, parent);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class MockStorageEngine extends AbstractStoragePlugin {

public MockStorageEngine(MockStorageEngineConfig configuration, DrillbitContext context, String name) {
this.configuration = configuration;
this.schema = new MockSchema(this);
this.schema = new MockSchema(this, name);
}

@Override
Expand Down Expand Up @@ -120,6 +120,11 @@ public MockSchema(MockStorageEngine engine) {
this.engine = engine;
}

public MockSchema(MockStorageEngine engine, String name) {
super(ImmutableList.<String>of(), name);
this.engine = engine;
}

@Override
public Table getTable(String name) {
if (name.toLowerCase().endsWith(".json")) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.drill.exec.physical.impl;

import org.apache.drill.test.ClientFixture;
import org.apache.drill.test.ClusterFixture;
import org.apache.drill.test.ClusterMockStorageFixture;
import org.apache.drill.test.DrillTest;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import static org.junit.Assert.assertTrue;

public class TestSchema extends DrillTest {

private static ClusterMockStorageFixture cluster;
private static ClientFixture client;

@BeforeClass
public static void setup() throws Exception {
cluster = ClusterFixture.builder().buildCustomMockStorage();
boolean breakRegisterSchema = true;
cluster.insertMockStorage("mock_broken", breakRegisterSchema);
cluster.insertMockStorage("mock_good", !breakRegisterSchema);
client = cluster.clientFixture();
}

@Test
public void testQueryBrokenStorage() {
String sql = "SELECT id_i, name_s10 FROM `mock_broken`.`employees_5`";
try {
client.queryBuilder().sql(sql).printCsv();
} catch (Exception ex) {
assertTrue(ex.getMessage().contains("VALIDATION ERROR: Schema"));
}
}

@Test
public void testQueryGoodStorage() {
String sql = "SELECT id_i, name_s10 FROM `mock_good`.`employees_5`";
client.queryBuilder().sql(sql).printCsv();
}

@Test
public void testQueryGoodStorageWithDefaultSchema() throws Exception {
String use_dfs = "use dfs.tmp";
client.queryBuilder().sql(use_dfs).run();
String sql = "SELECT id_i, name_s10 FROM `mock_good`.`employees_5`";
client.queryBuilder().sql(sql).printCsv();
}

@Test
public void testUseBrokenStorage() throws Exception {
try {
String use_dfs = "use mock_broken";
client.queryBuilder().sql(use_dfs).run();
} catch(Exception ex) {
assertTrue(ex.getMessage().contains("VALIDATION ERROR: Schema"));
}
}

@AfterClass
public static void close() throws Exception {
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.drill.test;

import org.apache.drill.exec.server.Drillbit;
import org.apache.drill.exec.store.StoragePluginRegistry;
import org.apache.drill.exec.store.StoragePluginRegistryImpl;
import org.apache.drill.exec.store.mock.MockBreakageStorage;
import org.apache.drill.exec.store.mock.MockStorageEngineConfig;

public class ClusterMockStorageFixture extends ClusterFixture {
ClusterMockStorageFixture(FixtureBuilder builder) {
super(builder);

}

/**
* This should be called after bits are started
* @param name nthe mock storage name we are going to create
*/
public void insertMockStorage(String name, boolean breakRegisterSchema) {
for (Drillbit bit : drillbits()) {

// Bit name and registration.
final StoragePluginRegistry pluginRegistry = bit.getContext().getStorage();
MockStorageEngineConfig config = MockStorageEngineConfig.INSTANCE;
@SuppressWarnings("resource")
MockBreakageStorage plugin = new MockBreakageStorage(
MockStorageEngineConfig.INSTANCE, bit.getContext(), name);
((StoragePluginRegistryImpl) pluginRegistry).definePlugin(name, config, plugin);

plugin.setBreakRegister(breakRegisterSchema);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,8 @@ public FixtureBuilder saveProfiles() {
public ClusterFixture build() {
return new ClusterFixture(this);
}

public ClusterMockStorageFixture buildCustomMockStorage() {
return new ClusterMockStorageFixture(this);
}
}

0 comments on commit 435ee17

Please sign in to comment.