diff --git a/cats/cats-sql/cats-sql.gradle b/cats/cats-sql/cats-sql.gradle index fa07cbc3018..ebc53a17bf6 100644 --- a/cats/cats-sql/cats-sql.gradle +++ b/cats/cats-sql/cats-sql.gradle @@ -62,3 +62,9 @@ dependencies { testImplementation "org.testcontainers:mysql" testImplementation "mysql:mysql-connector-java" } + +test { + useJUnitPlatform { + includeEngines "junit-vintage", "junit-jupiter" + } +} diff --git a/cats/cats-sql/src/main/kotlin/com/netflix/spinnaker/cats/sql/cache/SqlCache.kt b/cats/cats-sql/src/main/kotlin/com/netflix/spinnaker/cats/sql/cache/SqlCache.kt index 5c96a26d9ca..70d4e3d2b36 100644 --- a/cats/cats-sql/src/main/kotlin/com/netflix/spinnaker/cats/sql/cache/SqlCache.kt +++ b/cats/cats-sql/src/main/kotlin/com/netflix/spinnaker/cats/sql/cache/SqlCache.kt @@ -1431,10 +1431,10 @@ class SqlCache( var relWhere: Condition = noCondition() if (relationshipPrefixes.isNotEmpty() && !relationshipPrefixes.contains("ALL")) { - relWhere = field("rel_type").like(relationshipPrefixes[0]) + relWhere = field("rel_type").like("${relationshipPrefixes[0]}%") for (i in 1 until relationshipPrefixes.size) { - relWhere = relWhere.or(field("rel_type").like(relationshipPrefixes[i])) + relWhere = relWhere.or(field("rel_type").like("${relationshipPrefixes[i]}%")) } } diff --git a/cats/cats-sql/src/main/kotlin/com/netflix/spinnaker/cats/sql/cache/SqlNames.kt b/cats/cats-sql/src/main/kotlin/com/netflix/spinnaker/cats/sql/cache/SqlNames.kt index 4de0c1ebba2..290b7a65fde 100644 --- a/cats/cats-sql/src/main/kotlin/com/netflix/spinnaker/cats/sql/cache/SqlNames.kt +++ b/cats/cats-sql/src/main/kotlin/com/netflix/spinnaker/cats/sql/cache/SqlNames.kt @@ -17,6 +17,7 @@ package com.netflix.spinnaker.cats.sql.cache import com.google.common.hash.Hashing import com.netflix.spinnaker.config.SqlConstraints +import com.netflix.spinnaker.kork.annotations.VisibleForTesting /** * Provides utility methods for clouddriver's SQL naming conventions. @@ -47,7 +48,8 @@ class SqlNames( * It always keeps prefix with tableNamespace but can shorten name and suffix in that order. * @return computed table name */ - private fun checkTableName(prefix: String, name: String, suffix: String): String { + @VisibleForTesting + internal fun checkTableName(prefix: String, name: String, suffix: String): String { var base = prefix if (tableNamespace != null) { base = "${prefix + tableNamespace}_" diff --git a/cats/cats-sql/src/test/groovy/com/netflix/spinnaker/cats/sql/SqlCacheSpec.groovy b/cats/cats-sql/src/test/groovy/com/netflix/spinnaker/cats/sql/SqlCacheSpec.groovy index 617b09d709a..00a472d00ee 100644 --- a/cats/cats-sql/src/test/groovy/com/netflix/spinnaker/cats/sql/SqlCacheSpec.groovy +++ b/cats/cats-sql/src/test/groovy/com/netflix/spinnaker/cats/sql/SqlCacheSpec.groovy @@ -13,6 +13,7 @@ import com.netflix.spinnaker.kork.sql.config.SqlRetryProperties import com.netflix.spinnaker.kork.sql.test.SqlTestUtil import com.zaxxer.hikari.HikariDataSource import org.jooq.DSLContext +import org.jooq.impl.DSL import spock.lang.AutoCleanup import spock.lang.Shared import spock.lang.Unroll @@ -86,32 +87,15 @@ class SqlCacheSpec extends WriteableCacheSpec { def where = ((SqlCache) cache).getRelWhere(relPrefixes, queryPrefix) then: - where == expected + where.toString() == expected where: - filter || queryPrefix || expected - RelationshipCacheFilter.none() || "meowdy=partner" || "meowdy=partner" - null || "meowdy=partner" || "meowdy=partner" - RelationshipCacheFilter.include("instances", "images") || null || "(rel_type LIKE 'instances%' OR rel_type LIKE 'images%')" - RelationshipCacheFilter.include("images") || "meowdy=partner" || "meowdy=partner AND (rel_type LIKE 'images%')" - null || null || "1=1" - } - - @Unroll - def 'max length of table name is checked'() { - when: - def realName = ((SqlCache) cache).checkTableName("cats_v1_", name, suffix) - - then: - realName == expected - - where: - name || expected || suffix - "foo" || "cats_v1_test_foo" || "" - "abcdefghij" * 10 || "cats_v1_test_abcdefghijabcdefghijabcdefghijabcdeaa7d0fee7e891a66" || "" - "abcdefghij" * 10 || "cats_v1_test_abcdefghijabcdefghijabcdefghija9246690b33571ecc_rel" || "_rel" - "abcdefghij" * 10 || "cats_v1_test_abcdefghijabcdefghijabcdefghijabcdefe546a736182e553" || "suffix"*10 - + filter || queryPrefix || expected + RelationshipCacheFilter.none() || DSL.field("meowdy").eq("partner") || "meowdy = 'partner'" + null || DSL.field("meowdy").eq("partner") || "meowdy = 'partner'" + RelationshipCacheFilter.include("instances", "images") || null || "(\n rel_type like 'instances%'\n or rel_type like 'images%'\n)" + RelationshipCacheFilter.include("images") || DSL.field("meowdy").eq("partner") || "(\n meowdy = 'partner'\n and rel_type like 'images%'\n)" + null || null || "1 = 1" } @Override diff --git a/cats/cats-sql/src/test/kotlin/com/netflix/spinnaker/cats/sql/cache/SqlNamesTest.kt b/cats/cats-sql/src/test/kotlin/com/netflix/spinnaker/cats/sql/cache/SqlNamesTest.kt new file mode 100644 index 00000000000..00023b1d173 --- /dev/null +++ b/cats/cats-sql/src/test/kotlin/com/netflix/spinnaker/cats/sql/cache/SqlNamesTest.kt @@ -0,0 +1,49 @@ +/* + * Copyright 2020 Netflix, Inc. + * + * Licensed 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 com.netflix.spinnaker.cats.sql.cache + +import dev.minutest.junit.JUnit5Minutests +import dev.minutest.rootContext +import strikt.api.expectThat +import strikt.assertions.isEqualTo + +class SqlNamesTest : JUnit5Minutests { + + fun tests() = rootContext { + fixture { + SqlNames() + } + + listOf( + TableName("hello", "", "cats_v1_hello"), + TableName("hello", "world", "cats_v1_helloworld"), + TableName("abcdefghij".repeat(10), "", "cats_v1_abcdefghijabcdefghijabcdefghijabcdefghijaa7d0fee7e891a66"), + TableName("abcdefghij".repeat(10), "_rel", "cats_v1_abcdefghijabcdefghijabcdefghijabcdef9246690b33571ecc_rel"), + TableName("abcdefghij".repeat(10), "suffix".repeat(10), "cats_v1_abcdefghijabcdefghijabcdefghijabcdefghijfe546a736182e553") + ).forEach { table -> + test("max length of table name is checked: $table") { + expectThat(checkTableName("cats_v1_", table.name, table.suffix)) + .isEqualTo(table.expected) + } + } + } + + private inner class TableName( + val name: String, + val suffix: String, + val expected: String + ) +}