This pull request refactors how patch policy SQL queries are generated and validated, with the main goal of simplifying and correcting the construction of `NOT EXISTS` queries for version checks. The changes ensure that the generated queries are more accurate, especially in cases involving SQL `OR` conditions and platform-specific version columns. The update also adapts related test cases to match the new query structure. **Patch policy query generation improvements:** * Refactored the SQL generation logic in `GenerateQueryForManifest` to append the `version_compare` clause directly inside the original `WHERE` clause, rather than wrapping the entire query in extra parentheses. This results in simpler, more standard SQL queries. * Added logic to detect `OR` conditions in the `WHERE` clause and wrap them in parentheses to ensure correct SQL precedence when appending the `AND version_compare(...)` clause. * Improved selection of the version column (e.g., `bundle_short_version`, `version`, or `file_version`) based on platform and table name, ensuring correct queries for both macOS and Windows policies. **Test updates:** * Updated all relevant test cases in `patch_policy_test.go` to expect the new, simplified query format, removing the extra parentheses and validating correct handling of SQL with `OR` and platform-specific columns. [[1]](diffhunk://#diff-a770c8e2c3066123079c660322e318014a7c4870429e091a6e48d4acb222c340L23-R23) [[2]](diffhunk://#diff-a770c8e2c3066123079c660322e318014a7c4870429e091a6e48d4acb222c340L32-R32) [[3]](diffhunk://#diff-a770c8e2c3066123079c660322e318014a7c4870429e091a6e48d4acb222c340L41-R41) [[4]](diffhunk://#diff-a770c8e2c3066123079c660322e318014a7c4870429e091a6e48d4acb222c340L50-R61) * Adjusted a Homebrew ingester test to match the new query formatting, ensuring consistency across the codebase.
72 lines
2.6 KiB
Go
72 lines
2.6 KiB
Go
package patch_policy_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/fleetdm/fleet/v4/pkg/patch_policy"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestGenerateQueryForManifest(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
want string
|
|
p patch_policy.PolicyData
|
|
}{
|
|
{
|
|
name: "darwin from exists query",
|
|
p: patch_policy.PolicyData{
|
|
Platform: "darwin",
|
|
Version: "1.0",
|
|
ExistsQuery: "SELECT 1 FROM apps WHERE bundle_identifier = 'com.foo';",
|
|
},
|
|
want: "SELECT 1 WHERE NOT EXISTS (SELECT 1 FROM apps WHERE bundle_identifier = 'com.foo' AND version_compare(bundle_short_version, '1.0') < 0);",
|
|
},
|
|
{
|
|
name: "windows from exists query",
|
|
p: patch_policy.PolicyData{
|
|
Platform: "windows",
|
|
Version: "1.0",
|
|
ExistsQuery: "SELECT 1 FROM programs WHERE name = 'Foo x64' AND publisher = 'Bar, Inc.';",
|
|
},
|
|
want: "SELECT 1 WHERE NOT EXISTS (SELECT 1 FROM programs WHERE name = 'Foo x64' AND publisher = 'Bar, Inc.' AND version_compare(version, '1.0') < 0);",
|
|
},
|
|
{
|
|
name: "windows from exists query with LIKE percent wildcard",
|
|
p: patch_policy.PolicyData{
|
|
Platform: "windows",
|
|
Version: "12.5.6",
|
|
ExistsQuery: "SELECT 1 FROM programs WHERE name LIKE 'Postman x64 %' AND publisher = 'Postman';",
|
|
},
|
|
want: "SELECT 1 WHERE NOT EXISTS (SELECT 1 FROM programs WHERE name LIKE 'Postman x64 %' AND publisher = 'Postman' AND version_compare(version, '12.5.6') < 0);",
|
|
},
|
|
{
|
|
name: "windows from exists query with multiple LIKE percent wildcards",
|
|
p: patch_policy.PolicyData{
|
|
Platform: "windows",
|
|
Version: "139.0.0",
|
|
ExistsQuery: "SELECT 1 FROM programs WHERE name LIKE 'Mozilla Firefox % ESR %' AND publisher = 'Mozilla';",
|
|
},
|
|
want: "SELECT 1 WHERE NOT EXISTS (SELECT 1 FROM programs WHERE name LIKE 'Mozilla Firefox % ESR %' AND publisher = 'Mozilla' AND version_compare(version, '139.0.0') < 0);",
|
|
},
|
|
{
|
|
name: "codex-cli portable install OR precedence and file_version",
|
|
p: patch_policy.PolicyData{
|
|
Platform: "windows",
|
|
Version: "0.130.0",
|
|
ExistsQuery: "SELECT 1 FROM file WHERE path = 'C:\\Program Files\\Codex CLI\\codex.exe' " +
|
|
"OR path LIKE '%\\AppData\\Local\\Programs\\Codex CLI\\codex.exe';",
|
|
},
|
|
want: "SELECT 1 WHERE NOT EXISTS (SELECT 1 FROM file WHERE (path = 'C:\\Program Files\\Codex CLI\\codex.exe' " +
|
|
"OR path LIKE '%\\AppData\\Local\\Programs\\Codex CLI\\codex.exe') AND version_compare(file_version, '0.130.0') < 0);",
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
query, err := patch_policy.GenerateQueryForManifest(tt.p)
|
|
require.NoError(t, err)
|
|
require.Equal(t, tt.want, query)
|
|
})
|
|
}
|
|
}
|