Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

fix: exclude defaultSchemaLocations in offline k8s validation #643

Merged
merged 15 commits into from
May 25, 2022
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
try fixing lint
  • Loading branch information
royhadad committed May 25, 2022
commit 46b341d547d99da9805d9b1151fcfd6f414c6d2b
39 changes: 18 additions & 21 deletions bl/validation/k8sValidator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func TestValidateResources(t *testing.T) {
test_get_all_schema_locations_offline(t)
test_get_datree_crd_schema_by_name(t)
t.Run("test empty file", test_empty_file)
t.Run("test no internet connection", test_offline_without_custom_schema_locations)
t.Run("test no internet connection", test_offline_with_remote_custom_schema_location)
t.Run("test_offline_without_custom_schema_locations", test_offline_without_custom_schema_locations)
t.Run("test_offline_with_remote_custom_schema_location", test_offline_with_remote_custom_schema_location)
t.Run("test missing schema skipped", test_missing_schema_skipped)
}

Expand Down Expand Up @@ -130,14 +130,14 @@ func test_empty_file(t *testing.T) {
}
}

func test_offline_without_custom_schema_locations(t *testing.T) {
func test_offline_with_remote_custom_schema_location(t *testing.T) {
validationClient := &mockValidationClient{}
validationClient.On("Validate", mock.Anything, mock.Anything).Return([]kubeconformValidator.Result{
{Status: kubeconformValidator.Error, Err: fmt.Errorf("no such host")},
})
k8sValidator := K8sValidator{
validationClient: validationClient,
areThereCustomSchemaLocations: false,
areThereCustomSchemaLocations: true,
isOffline: true,
}

Expand All @@ -149,25 +149,22 @@ func test_offline_without_custom_schema_locations(t *testing.T) {
Configurations: []extractor.Configuration{},
}
close(filesConfigurationsChan)
k8sValidationWarningPerValidFile := make(K8sValidationWarningPerValidFile)

_, _, filesWithWarningsChan := k8sValidator.ValidateResources(filesConfigurationsChan, 1)
_, invalidFilesChan, filesWithWarningsChan := k8sValidator.ValidateResources(filesConfigurationsChan, 1)
for p := range invalidFilesChan {
assert.Equal(t, 1, len(p.ValidationErrors))
assert.Equal(t, "k8s schema validation error: no such host\n", p.ValidationErrors[0].Error())
}
for p := range filesWithWarningsChan {
k8sValidationWarningPerValidFile[p.Filename] = *p
panic("expected 0 warnings when custom --schema-location provided, instead got warning: " + p.Warning)
}

assert.Equal(t, 1, len(k8sValidationWarningPerValidFile))
assert.Equal(t, "k8s schema validation skipped: no internet connection", k8sValidationWarningPerValidFile[path].Warning)
}

func test_offline_with_remote_custom_schema_location(t *testing.T) {
func test_offline_without_custom_schema_locations(t *testing.T) {
validationClient := &mockValidationClient{}
validationClient.On("Validate", mock.Anything, mock.Anything).Return([]kubeconformValidator.Result{
{Status: kubeconformValidator.Error, Err: fmt.Errorf("no such host")},
})
k8sValidator := K8sValidator{
validationClient: validationClient,
areThereCustomSchemaLocations: true,
areThereCustomSchemaLocations: false,
isOffline: true,
}

Expand All @@ -179,15 +176,15 @@ func test_offline_with_remote_custom_schema_location(t *testing.T) {
Configurations: []extractor.Configuration{},
}
close(filesConfigurationsChan)
k8sValidationWarningPerValidFile := make(K8sValidationWarningPerValidFile)

_, invalidFilesChan, filesWithWarningsChan := k8sValidator.ValidateResources(filesConfigurationsChan, 1)
for p := range invalidFilesChan {
assert.Equal(t, 1, len(p.ValidationErrors))
assert.Equal(t, "k8s schema validation error: no such host\n", p.ValidationErrors[0].Error())
}
_, _, filesWithWarningsChan := k8sValidator.ValidateResources(filesConfigurationsChan, 1)
for p := range filesWithWarningsChan {
panic("expected 0 warnings when custom --schema-location provided, instead got warning: " + p.Warning)
k8sValidationWarningPerValidFile[p.Filename] = *p
}

assert.Equal(t, 1, len(k8sValidationWarningPerValidFile))
assert.Equal(t, "k8s schema validation skipped: no internet connection", k8sValidationWarningPerValidFile[path].Warning)
}

func test_missing_schema_skipped(t *testing.T) {
Expand Down