Skip to content

Commit

Permalink
Fixing wrong platform and adding unit tests #2902 (#2903)
Browse files Browse the repository at this point in the history
Co-authored-by: Felipe Avelar <[email protected]>
  • Loading branch information
rogeriopeixotocx and felipe-avelar authored Apr 20, 2021
1 parent fed9903 commit bf14ee2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
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

0 comments on commit bf14ee2

Please sign in to comment.