/* * 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.cassandra.config; import java.net.Inet4Address; import java.net.Inet6Address; import java.net.InetAddress; import java.net.NetworkInterface; import java.util.Enumeration; import junit.framework.Assert; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.apache.cassandra.OrderedJUnit4ClassRunner; import org.apache.cassandra.SchemaLoader; import org.apache.cassandra.db.Keyspace; import org.apache.cassandra.exceptions.ConfigurationException; import org.apache.cassandra.exceptions.InvalidRequestException; import org.apache.cassandra.gms.Gossiper; import org.apache.cassandra.locator.SimpleStrategy; import org.apache.cassandra.service.MigrationManager; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; @RunWith(OrderedJUnit4ClassRunner.class) public class DatabaseDescriptorTest { @Test public void testCFMetaDataSerialization() throws ConfigurationException, InvalidRequestException { // test serialization of all defined test CFs. for (String keyspaceName : Schema.instance.getNonSystemKeyspaces()) { for (CFMetaData cfm : Schema.instance.getKeyspaceMetaData(keyspaceName).values()) { CFMetaData cfmDupe = CFMetaData.fromThrift(cfm.toThrift()); assertNotNull(cfmDupe); assertEquals(cfm, cfmDupe); } } } @Test public void testKSMetaDataSerialization() throws ConfigurationException { for (KSMetaData ksm : Schema.instance.getKeyspaceDefinitions()) { // Not testing round-trip on the KsDef via serDe() because maps KSMetaData ksmDupe = KSMetaData.fromThrift(ksm.toThrift()); assertNotNull(ksmDupe); assertEquals(ksm, ksmDupe); } } // this came as a result of CASSANDRA-995 @Test public void testTransKsMigration() throws ConfigurationException { SchemaLoader.cleanupAndLeaveDirs(); DatabaseDescriptor.loadSchemas(); assertEquals(0, Schema.instance.getNonSystemKeyspaces().size()); Gossiper.instance.start((int)(System.currentTimeMillis() / 1000)); Keyspace.setInitialized(); try { // add a few. MigrationManager.announceNewKeyspace(KSMetaData.testMetadata("ks0", SimpleStrategy.class, KSMetaData.optsWithRF(3))); MigrationManager.announceNewKeyspace(KSMetaData.testMetadata("ks1", SimpleStrategy.class, KSMetaData.optsWithRF(3))); assertNotNull(Schema.instance.getKSMetaData("ks0")); assertNotNull(Schema.instance.getKSMetaData("ks1")); Schema.instance.clearKeyspaceDefinition(Schema.instance.getKSMetaData("ks0")); Schema.instance.clearKeyspaceDefinition(Schema.instance.getKSMetaData("ks1")); assertNull(Schema.instance.getKSMetaData("ks0")); assertNull(Schema.instance.getKSMetaData("ks1")); DatabaseDescriptor.loadSchemas(); assertNotNull(Schema.instance.getKSMetaData("ks0")); assertNotNull(Schema.instance.getKSMetaData("ks1")); } finally { Gossiper.instance.stop(); } } @Test public void testConfigurationLoader() throws Exception { // By default, we should load from the yaml Config config = DatabaseDescriptor.loadConfig(); assertEquals("Test Cluster", config.cluster_name); Keyspace.setInitialized(); // Now try custom loader ConfigurationLoader testLoader = new TestLoader(); System.setProperty("cassandra.config.loader", testLoader.getClass().getName()); config = DatabaseDescriptor.loadConfig(); assertEquals("ConfigurationLoader Test", config.cluster_name); } public static class TestLoader implements ConfigurationLoader { public Config loadConfig() throws ConfigurationException { Config testConfig = new Config(); testConfig.cluster_name = "ConfigurationLoader Test";; return testConfig; } } static NetworkInterface suitableInterface = null; static boolean hasIPv4andIPv6 = false; /* * Server only accepts interfaces by name if they have a single address * OS X seems to always have an ipv4 and ipv6 address on all interfaces which means some tests fail * if not checked for and skipped */ @BeforeClass public static void selectSuitableInterface() throws Exception { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while(interfaces.hasMoreElements()) { NetworkInterface intf = interfaces.nextElement(); System.out.println("Evaluating " + intf.getName()); if (intf.isLoopback()) { suitableInterface = intf; boolean hasIPv4 = false; boolean hasIPv6 = false; Enumeration<InetAddress> addresses = suitableInterface.getInetAddresses(); while (addresses.hasMoreElements()) { if (addresses.nextElement().getClass() == Inet6Address.class) hasIPv6 = true; else hasIPv4 = true; } hasIPv4andIPv6 = hasIPv4 && hasIPv6; return; } } } @Test public void testRpcInterface() throws Exception { Config testConfig = DatabaseDescriptor.loadConfig(); testConfig.rpc_interface = suitableInterface.getName(); testConfig.rpc_address = null; DatabaseDescriptor.applyAddressConfig(testConfig); /* * Confirm ability to select between IPv4 and IPv6 */ if (hasIPv4andIPv6) { testConfig = DatabaseDescriptor.loadConfig(); testConfig.rpc_interface = suitableInterface.getName(); testConfig.rpc_address = null; testConfig.rpc_interface_prefer_ipv6 = true; DatabaseDescriptor.applyAddressConfig(testConfig); assertEquals(DatabaseDescriptor.getRpcAddress().getClass(), Inet6Address.class); testConfig = DatabaseDescriptor.loadConfig(); testConfig.rpc_interface = suitableInterface.getName(); testConfig.rpc_address = null; testConfig.rpc_interface_prefer_ipv6 = false; DatabaseDescriptor.applyAddressConfig(testConfig); assertEquals(DatabaseDescriptor.getRpcAddress().getClass(), Inet4Address.class); } else { /* * Confirm first address of interface is selected */ assertEquals(DatabaseDescriptor.getRpcAddress(), suitableInterface.getInetAddresses().nextElement()); } } @Test public void testListenInterface() throws Exception { Config testConfig = DatabaseDescriptor.loadConfig(); testConfig.listen_interface = suitableInterface.getName(); testConfig.listen_address = null; DatabaseDescriptor.applyAddressConfig(testConfig); /* * Confirm ability to select between IPv4 and IPv6 */ if (hasIPv4andIPv6) { testConfig = DatabaseDescriptor.loadConfig(); testConfig.listen_interface = suitableInterface.getName(); testConfig.listen_address = null; testConfig.listen_interface_prefer_ipv6 = true; DatabaseDescriptor.applyAddressConfig(testConfig); assertEquals(DatabaseDescriptor.getListenAddress().getClass(), Inet6Address.class); testConfig = DatabaseDescriptor.loadConfig(); testConfig.listen_interface = suitableInterface.getName(); testConfig.listen_address = null; testConfig.listen_interface_prefer_ipv6 = false; DatabaseDescriptor.applyAddressConfig(testConfig); assertEquals(DatabaseDescriptor.getListenAddress().getClass(), Inet4Address.class); } else { /* * Confirm first address of interface is selected */ assertEquals(DatabaseDescriptor.getRpcAddress(), suitableInterface.getInetAddresses().nextElement()); } } @Test public void testListenAddress() throws Exception { Config testConfig = DatabaseDescriptor.loadConfig(); testConfig.listen_address = suitableInterface.getInterfaceAddresses().get(0).getAddress().getHostAddress(); testConfig.listen_interface = null; DatabaseDescriptor.applyAddressConfig(testConfig); } @Test public void testRpcAddress() throws Exception { Config testConfig = DatabaseDescriptor.loadConfig(); testConfig.rpc_address = suitableInterface.getInterfaceAddresses().get(0).getAddress().getHostAddress(); testConfig.rpc_interface = null; DatabaseDescriptor.applyAddressConfig(testConfig); } }