smiklosovic commented on code in PR #4393:
URL: https://github.com/apache/cassandra/pull/4393#discussion_r2377895360


##########
test/unit/org/apache/cassandra/db/guardrails/GuardrailKeyspacePropertiesTest.java:
##########
@@ -0,0 +1,224 @@
+/*
+ * 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.db.guardrails;
+
+import static java.lang.String.format;
+import static org.apache.cassandra.db.guardrails.GuardrailTester.guardrails;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import org.apache.cassandra.db.guardrails.GuardrailTester;
+import org.apache.cassandra.db.guardrails.Guardrails;
+import org.apache.cassandra.schema.KeyspaceParams;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.google.common.collect.ImmutableSet;
+
+/**
+ * Tests the guardrail for keyspace properties, {@link 
Guardrails#keyspaceProperties}.
+ */
+public class GuardrailKeyspacePropertiesTest extends GuardrailTester
+{
+    private static final String CREATE_KEYSPACE = "CREATE KEYSPACE %s %s";
+    private static final String ALTER_KEYSPACE = "ALTER KEYSPACE %s WITH %s";
+
+    private static final String WARNED_PROPERTY_NAME = 
"keyspace_properties_warned";
+    private static final String IGNORED_PROPERTY_NAME = 
"keyspace_properties_ignored";
+    private static final String DISALLOWED_PROPERTY_NAME = 
"keyspace_properties_disallowed";
+
+    public GuardrailKeyspacePropertiesTest()
+    {
+        super(Guardrails.keyspaceProperties);
+    }
+
+    @Before
+    public void before()
+    {
+        // Set up basic test configuration - only allow "replication" by 
default
+        Set<String> allowed = new HashSet<>(Arrays.asList("replication"));
+        Set<String> allKeyspaceProperties = 
Arrays.stream(KeyspaceParams.Option.values())
+                                                  
.map(KeyspaceParams.Option::toString)
+                                                  .collect(Collectors.toSet());
+        guardrails().setKeyspacePropertiesDisallowed(allKeyspaceProperties
+                                                     .stream()
+                                                     .filter(p -> 
!allowed.contains(p))
+                                                     .map(String::toUpperCase)
+                                                     
.collect(Collectors.toSet()));
+        // Configure "durable_writes" to be warned about (tests will override 
as needed)
+        guardrails().setKeyspacePropertiesWarned("durable_writes");
+        guardrails().setKeyspacePropertiesIgnored(Collections.emptySet());
+    }
+
+    @Test
+    public void testConfigValidation()
+    {
+        String message = "Invalid value for %s: null is not allowed";
+        assertInvalidProperty(Guardrails::setKeyspacePropertiesWarned, 
(Set<String>) null, message, WARNED_PROPERTY_NAME);
+        assertInvalidProperty(Guardrails::setKeyspacePropertiesIgnored, 
(Set<String>) null, message, IGNORED_PROPERTY_NAME);
+        assertInvalidProperty(Guardrails::setKeyspacePropertiesDisallowed, 
(Set<String>) null, message, DISALLOWED_PROPERTY_NAME);
+
+        assertValidProperty(Collections.emptySet());
+        assertValidProperty(getValidKeyspaceProperties());
+
+        assertValidPropertyCSV("");
+        assertValidPropertyCSV(String.join(",", getValidKeyspaceProperties()));
+
+        assertInvalidProperty(Collections.singleton("invalid"), 
Collections.singleton("invalid"));
+        assertInvalidProperty(ImmutableSet.of("replication", "invalid1", 
"invalid2"), ImmutableSet.of("invalid1", "invalid2"));
+        assertInvalidProperty(ImmutableSet.of("invalid1", "invalid2", 
"replication"), ImmutableSet.of("invalid1", "invalid2"));
+        assertInvalidProperty(ImmutableSet.of("invalid1", "replication", 
"invalid2"), ImmutableSet.of("invalid1", "invalid2"));
+
+        assertInvalidPropertyCSV("invalid", "[invalid]");
+        assertInvalidPropertyCSV("replication,invalid1,invalid2", "[invalid1, 
invalid2]");
+        assertInvalidPropertyCSV("invalid1,invalid2,replication", "[invalid1, 
invalid2]");
+        assertInvalidPropertyCSV("invalid1,replication,invalid2", "[invalid1, 
invalid2]");
+    }
+
+    private void assertValidProperty(Set<String> properties)
+    {
+        assertValidProperty(Guardrails::setKeyspacePropertiesWarned, 
Guardrails::getKeyspacePropertiesWarned, properties);
+        assertValidProperty(Guardrails::setKeyspacePropertiesIgnored, 
Guardrails::getKeyspacePropertiesIgnored, properties);
+        assertValidProperty(Guardrails::setKeyspacePropertiesDisallowed, 
Guardrails::getKeyspacePropertiesDisallowed, properties);
+    }
+
+    private void assertValidPropertyCSV(String csv)
+    {
+        csv = sortCSV(csv);
+        assertValidProperty(Guardrails::setKeyspacePropertiesWarnedCSV, g -> 
sortCSV(g.getKeyspacePropertiesWarnedCSV()), csv);
+        assertValidProperty(Guardrails::setKeyspacePropertiesIgnoredCSV, g -> 
sortCSV(g.getKeyspacePropertiesIgnoredCSV()), csv);
+        assertValidProperty(Guardrails::setKeyspacePropertiesDisallowedCSV, g 
-> sortCSV(g.getKeyspacePropertiesDisallowedCSV()), csv);
+    }
+
+    private void assertInvalidProperty(Set<String> properties, Set<String> 
invalidProperties)
+    {
+        String message = format("Invalid value for %s: '%s' do not parse as 
valid keyspace properties",
+                                WARNED_PROPERTY_NAME, invalidProperties);
+        assertInvalidProperty(Guardrails::setKeyspacePropertiesWarned, 
properties, message, WARNED_PROPERTY_NAME);
+
+        message = format("Invalid value for %s: '%s' do not parse as valid 
keyspace properties",
+                         IGNORED_PROPERTY_NAME, invalidProperties);
+        assertInvalidProperty(Guardrails::setKeyspacePropertiesIgnored, 
properties, message, IGNORED_PROPERTY_NAME);
+
+        message = format("Invalid value for %s: '%s' do not parse as valid 
keyspace properties",
+                         DISALLOWED_PROPERTY_NAME, invalidProperties);
+        assertInvalidProperty(Guardrails::setKeyspacePropertiesDisallowed, 
properties, message, DISALLOWED_PROPERTY_NAME);
+    }
+
+    private void assertInvalidPropertyCSV(String properties, String rejected)
+    {
+        String message = "Invalid value for %s: '%s' do not parse as valid 
keyspace properties";
+        assertInvalidProperty(Guardrails::setKeyspacePropertiesWarnedCSV, 
properties, message, WARNED_PROPERTY_NAME, rejected);
+        assertInvalidProperty(Guardrails::setKeyspacePropertiesIgnoredCSV, 
properties, message, IGNORED_PROPERTY_NAME, rejected);
+        assertInvalidProperty(Guardrails::setKeyspacePropertiesDisallowedCSV, 
properties, message, DISALLOWED_PROPERTY_NAME, rejected);
+    }
+
+    @Test
+    public void testCreateKeyspaceWithWarned() throws Throwable
+    {
+        String ks = createKeyspaceName();
+        String statement = format(CREATE_KEYSPACE, ks,
+                                 "WITH replication = {'class': 
'NetworkTopologyStrategy', 'replication_factor': 1} AND durable_writes = 
false");
+        assertWarns(statement, "Keyspace Properties durable_writes is not 
recommended");
+    }
+
+    @Test
+    public void testCreateKeyspaceWithIgnored() throws Throwable
+    {
+        guardrails().setKeyspacePropertiesIgnored("durable_writes");
+        guardrails().setKeyspacePropertiesWarned(Collections.emptySet());
+        String ks = createKeyspaceName();
+        String statement = format(CREATE_KEYSPACE, ks,
+                                 "WITH replication = {'class': 
'NetworkTopologyStrategy', 'replication_factor': 1} AND durable_writes = 
false");
+        assertValid(statement);
+    }
+
+    @Test
+    public void testCreateKeyspaceWithDisallowed() throws Throwable
+    {
+        guardrails().setKeyspacePropertiesDisallowed("durable_writes");
+        String ks = createKeyspaceName();
+        String statement = format(CREATE_KEYSPACE, ks,
+                                 "WITH replication = {'class': 
'NetworkTopologyStrategy', 'replication_factor': 1} AND durable_writes = 
false");
+        assertFails(statement, "Keyspace Properties durable_writes is 
forbidden");

Review Comment:
   `Keyspace properties`? Why capitalized `P` in `Properties`? This is an 
opportunity to change it for table properties guardrail if it is same `P` 
there. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to