Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing query with wrong platform in metadata, adding a unit test for it #2902 #2903

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
"category": "Access Control",
"descriptionText": "A non kube-system workload should not have hostPath mounted",
"descriptionUrl": "https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/pod",
"platform": "Cloudformation"
"platform": "Terraform"
}
34 changes: 20 additions & 14 deletions test/queries_content_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ var (
}

// TODO uncomment this test once all metadata are fixed
// availablePlatforms = map[string]string{
// "Ansible": "true",
// "CloudFormation": "true",
// "Common": "true",
// "Dockerfile": "true",
// "Kubernetes": "true",
// "OpenAPI": "true",
// "Terraform": "true",
// }
// platformKeys = MapToStringSlice(availablePlatforms)
availablePlatforms = map[string]string{
"Ansible": "ansible",
"CloudFormation": "cloudFormation",
"Common": "common",
"Dockerfile": "dockerfile",
"Kubernetes": "k8s",
"OpenAPI": "openAPI",
"Terraform": "terraform",
}
platformKeys = MapToStringSlice(availablePlatforms)

CategoriesKeys = MapToStringSlice(AvailableCategories)

Expand All @@ -98,7 +98,9 @@ var (
categoryValue := testMetadataFieldStringType(tb, value, "category", metadataPath)
require.NotEmpty(tb, categoryValue, "empty category in query metadata file %s", metadataPath)
_, ok := AvailableCategories[categoryValue]
require.True(tb, ok, "%s is not a valid category must be one of:\n%v", categoryValue, strings.Join(CategoriesKeys, "\n"))
require.True(tb, ok,
"%s in metadata: %s\nis not a valid category must be one of:\n%v",
categoryValue, metadataPath, strings.Join(CategoriesKeys, "\n"))
},
"descriptionText": func(tb testing.TB, value interface{}, metadataPath string) {
descriptionValue := testMetadataFieldStringType(tb, value, "descriptionText", metadataPath)
Expand All @@ -107,8 +109,12 @@ var (
"platform": func(tb testing.TB, value interface{}, metadataPath string) {
platformValue := testMetadataFieldStringType(tb, value, "platform", metadataPath)
require.NotEmpty(tb, platformValue, "empty platform text in query metadata file %s", metadataPath)
// _, ok := availablePlatforms[platformValue]
// require.True(tb, ok, "%s is not a valid platform must be one of:\n%v", platformValue, strings.Join(platformKeys, "\n"))
platformDir, ok := availablePlatforms[platformValue]
require.True(tb, ok,
"%s in metadata: %s\nis not a valid platform must be one of:\n%v",
platformValue, metadataPath, strings.Join(platformKeys, "\n"))
require.True(tb, strings.Contains(filepath.Clean(metadataPath), filepath.Join("assets", "queries", platformDir)),
"platform and query directory mismatch platform:\n%s\nmetadata:\n%s", platformValue, metadataPath)
},
"descriptionUrl": func(tb testing.TB, value interface{}, metadataPath string) {
switch urlValue := value.(type) {
Expand Down Expand Up @@ -261,7 +267,7 @@ func testMetadataURL(tb testing.TB, url, metadataPath string) {
}

func testUnmarshalMetadata(tb testing.TB, entry queryEntry) (meta map[string]interface{}, metadataPath string) {
metadataPath = path.Join(entry.dir, source.MetadataFileName)
metadataPath = filepath.Join(entry.dir, source.MetadataFileName)
content, err := os.ReadFile(metadataPath)
require.NoError(tb, err, "can't read query metadata file %s", metadataPath)

Expand Down