Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
小 白蛋
Datree
Commits
7ca1a6e0
Commit
7ca1a6e0
authored
3 years ago
by
Dima Brusilovsky
Browse files
Options
Download
Email Patches
Plain Diff
refactor: move only-k8s-files logic
parent
2bf95671
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
bl/validation/k8sValidator.go
+25
-11
bl/validation/k8sValidator.go
bl/validation/k8sValidator_test.go
+5
-5
bl/validation/k8sValidator_test.go
cmd/test/main.go
+12
-8
cmd/test/main.go
cmd/test/main_test.go
+22
-13
cmd/test/main_test.go
cmd/test/testCommandHelper.go
+3
-2
cmd/test/testCommandHelper.go
with
67 additions
and
39 deletions
+67
-39
bl/validation/k8sValidator.go
+
25
-
11
View file @
7ca1a6e0
...
...
@@ -33,25 +33,17 @@ func (val *K8sValidator) InitClient(k8sVersion string, ignoreMissingSchemas bool
val
.
validationClient
=
newKubconformValidator
(
k8sVersion
,
ignoreMissingSchemas
,
schemaLocations
)
}
func
(
val
*
K8sValidator
)
ValidateResources
(
filesConfigurationsChan
chan
*
extractor
.
FileConfigurations
,
concurrency
int
,
onlyK8sFiles
bool
)
(
chan
*
extractor
.
FileConfigurations
,
chan
*
InvalidK8sFile
,
chan
*
string
)
{
func
(
val
*
K8sValidator
)
ValidateResources
(
filesConfigurationsChan
chan
*
extractor
.
FileConfigurations
,
concurrency
int
)
(
chan
*
extractor
.
FileConfigurations
,
chan
*
InvalidK8sFile
)
{
validK8sFilesConfigurationsChan
:=
make
(
chan
*
extractor
.
FileConfigurations
,
concurrency
)
invalidK8sFilesChan
:=
make
(
chan
*
InvalidK8sFile
,
concurrency
)
ignoredFilesChan
:=
make
(
chan
*
string
,
concurrency
)
go
func
()
{
defer
func
()
{
close
(
invalidK8sFilesChan
)
close
(
validK8sFilesConfigurationsChan
)
close
(
ignoredFilesChan
)
}()
for
fileConfigurations
:=
range
filesConfigurationsChan
{
if
onlyK8sFiles
{
if
ok
:=
val
.
validatePotentialK8sFile
(
fileConfigurations
.
Configurations
);
!
ok
{
ignoredFilesChan
<-
&
fileConfigurations
.
FileName
continue
}
}
isValid
,
validationErrors
,
err
:=
val
.
validateResource
(
fileConfigurations
.
FileName
)
if
err
!=
nil
{
...
...
@@ -71,10 +63,32 @@ func (val *K8sValidator) ValidateResources(filesConfigurationsChan chan *extract
}
}
}()
return
validK8sFilesConfigurationsChan
,
invalidK8sFilesChan
,
ignoredFilesChan
return
validK8sFilesConfigurationsChan
,
invalidK8sFilesChan
}
func
(
val
*
K8sValidator
)
GetK8sFiles
(
filesConfigurationsChan
chan
*
extractor
.
FileConfigurations
,
concurrency
int
)
(
chan
*
extractor
.
FileConfigurations
,
chan
*
extractor
.
FileConfigurations
)
{
k8sFilesChan
:=
make
(
chan
*
extractor
.
FileConfigurations
,
concurrency
)
ignoredYamlFilesChan
:=
make
(
chan
*
extractor
.
FileConfigurations
,
concurrency
)
go
func
()
{
defer
func
()
{
close
(
k8sFilesChan
)
close
(
ignoredYamlFilesChan
)
}()
for
fileConfigurations
:=
range
filesConfigurationsChan
{
if
ok
:=
val
.
isK8sFile
(
fileConfigurations
.
Configurations
);
ok
{
k8sFilesChan
<-
fileConfigurations
}
else
{
ignoredYamlFilesChan
<-
fileConfigurations
}
}
}()
return
k8sFilesChan
,
ignoredYamlFilesChan
}
func
(
val
*
K8sValidator
)
validatePotential
K8sFile
(
fileConfigurations
[]
extractor
.
Configuration
)
bool
{
func
(
val
*
K8sValidator
)
is
K8sFile
(
fileConfigurations
[]
extractor
.
Configuration
)
bool
{
for
_
,
configuration
:=
range
fileConfigurations
{
_
,
has_apiVersion
:=
configuration
[
"apiVersion"
]
_
,
has_kind
:=
configuration
[
"kind"
]
...
...
This diff is collapsed.
Click to expand it.
bl/validation/k8sValidator_test.go
+
5
-
5
View file @
7ca1a6e0
...
...
@@ -43,7 +43,7 @@ func test_valid_multiple_configurations(t *testing.T) {
Configurations
:
[]
extractor
.
Configuration
{},
}
close
(
filesConfigurationsChan
)
validConfigurationsChan
,
_
,
_
:=
k8sValidator
.
ValidateResources
(
filesConfigurationsChan
,
1
,
false
)
validConfigurationsChan
,
_
:=
k8sValidator
.
ValidateResources
(
filesConfigurationsChan
,
1
)
for
p
:=
range
validConfigurationsChan
{
assert
.
Equal
(
t
,
path
,
p
.
FileName
)
...
...
@@ -67,10 +67,10 @@ func test_valid_multiple_configurations_only_k8s_files(t *testing.T) {
Configurations
:
[]
extractor
.
Configuration
{},
}
close
(
filesConfigurationsChan
)
_
,
_
,
ignoredConfiguration
sChan
:=
k8sValidator
.
ValidateResourc
es
(
filesConfigurationsChan
,
1
,
true
)
validK8sFile
sChan
,
_
:=
k8sValidator
.
GetK8sFil
es
(
filesConfigurationsChan
,
1
)
for
p
:=
range
ignoredConfiguration
sChan
{
assert
.
Equal
(
t
,
path
,
p
)
for
p
:=
range
validK8sFile
sChan
{
assert
.
Equal
(
t
,
path
,
p
.
FileName
)
}
}
...
...
@@ -91,7 +91,7 @@ func test_invalid_file(t *testing.T) {
Configurations
:
[]
extractor
.
Configuration
{},
}
close
(
filesConfigurationsChan
)
_
,
invalidFilesChan
,
_
:=
k8sValidator
.
ValidateResources
(
filesConfigurationsChan
,
1
,
false
)
_
,
invalidFilesChan
:=
k8sValidator
.
ValidateResources
(
filesConfigurationsChan
,
1
)
for
p
:=
range
invalidFilesChan
{
assert
.
Equal
(
t
,
path
,
p
.
Path
)
...
...
This diff is collapsed.
Click to expand it.
cmd/test/main.go
+
12
-
8
View file @
7ca1a6e0
...
...
@@ -33,8 +33,9 @@ type Messager interface {
}
type
K8sValidator
interface
{
ValidateResources
(
filesConfigurations
chan
*
extractor
.
FileConfigurations
,
concurrency
int
,
onlyK8sFiles
bool
)
(
chan
*
extractor
.
FileConfigurations
,
chan
*
validation
.
InvalidK8sFile
,
chan
*
string
)
ValidateResources
(
filesConfigurations
chan
*
extractor
.
FileConfigurations
,
concurrency
int
)
(
chan
*
extractor
.
FileConfigurations
,
chan
*
validation
.
InvalidK8sFile
)
InitClient
(
k8sVersion
string
,
ignoreMissingSchemas
bool
,
schemaLocations
[]
string
)
GetK8sFiles
(
filesConfigurationsChan
chan
*
extractor
.
FileConfigurations
,
concurrency
int
)
(
chan
*
extractor
.
FileConfigurations
,
chan
*
extractor
.
FileConfigurations
)
}
type
TestCommandFlags
struct
{
...
...
@@ -211,20 +212,23 @@ func test(ctx *TestCommandContext, paths []string, flags TestCommandFlags) error
}
ctx
.
K8sValidator
.
InitClient
(
createEvaluationResponse
.
K8sVersion
,
flags
.
IgnoreMissingSchemas
,
flags
.
SchemaLocations
)
validK8sFilesConfigurationsChan
,
invalidK8sFilesChan
,
ignoredYamlFilesChan
:=
ctx
.
K8sValidator
.
ValidateResources
(
validYamlFilesConfigurationsChan
,
concurrency
,
flags
.
OnlyK8sFiles
)
invalidYamlFiles
:=
aggregateInvalidYamlFiles
(
invalidYamlFilesChan
)
ignoredYamlFiles
:=
aggregateIgnoredYamlFiles
(
ignoredYamlFilesChan
)
invalidYamlFilesLen
:=
len
(
invalidYamlFiles
)
ignoredYamlFilesLen
:=
len
(
ignoredYamlFiles
)
ignoredYamlFiles
:=
[]
extractor
.
FileConfigurations
{}
if
flags
.
OnlyK8sFiles
{
filesPathsLen
=
filesPathsLen
-
invalidYamlFilesLen
-
ignoredYamlFilesLen
invalidYamlFilesLen
=
0
var
ignoredYamlFilesChan
chan
*
extractor
.
FileConfigurations
validYamlFilesConfigurationsChan
,
ignoredYamlFilesChan
=
ctx
.
K8sValidator
.
GetK8sFiles
(
validYamlFilesConfigurationsChan
,
concurrency
)
ignoredYamlFiles
=
aggregateIgnoredYamlFiles
(
ignoredYamlFilesChan
)
filesPathsLen
=
filesPathsLen
-
len
(
invalidYamlFiles
)
-
len
(
ignoredYamlFiles
)
invalidYamlFiles
=
[]
*
validation
.
InvalidYamlFile
{}
}
invalidYamlFilesLen
:=
len
(
invalidYamlFiles
)
validK8sFilesConfigurationsChan
,
invalidK8sFilesChan
:=
ctx
.
K8sValidator
.
ValidateResources
(
validYamlFilesConfigurationsChan
,
concurrency
)
ignoredYamlFilesLen
:=
len
(
ignoredYamlFiles
)
stopEvaluation
:=
invalidYamlFilesLen
+
ignoredYamlFilesLen
==
filesPathsLen
err
=
ctx
.
Evaluator
.
UpdateFailedYamlValidation
(
invalidYamlFiles
,
createEvaluationResponse
.
EvaluationId
,
stopEvaluation
)
if
err
!=
nil
{
...
...
This diff is collapsed.
Click to expand it.
cmd/test/main_test.go
+
22
-
13
View file @
7ca1a6e0
...
...
@@ -62,9 +62,14 @@ type K8sValidatorMock struct {
mock
.
Mock
}
func
(
kv
*
K8sValidatorMock
)
ValidateResources
(
filesConfigurationsChan
chan
*
extractor
.
FileConfigurations
,
concurrency
int
,
onlyK8sFiles
bool
)
(
chan
*
extractor
.
FileConfigurations
,
chan
*
validation
.
InvalidK8sFile
,
chan
*
string
)
{
args
:=
kv
.
Called
(
filesConfigurationsChan
,
concurrency
,
onlyK8sFiles
)
return
args
.
Get
(
0
)
.
(
chan
*
extractor
.
FileConfigurations
),
args
.
Get
(
1
)
.
(
chan
*
validation
.
InvalidK8sFile
),
args
.
Get
(
2
)
.
(
chan
*
string
)
func
(
kv
*
K8sValidatorMock
)
ValidateResources
(
filesConfigurationsChan
chan
*
extractor
.
FileConfigurations
,
concurrency
int
)
(
chan
*
extractor
.
FileConfigurations
,
chan
*
validation
.
InvalidK8sFile
)
{
args
:=
kv
.
Called
(
filesConfigurationsChan
,
concurrency
)
return
args
.
Get
(
0
)
.
(
chan
*
extractor
.
FileConfigurations
),
args
.
Get
(
1
)
.
(
chan
*
validation
.
InvalidK8sFile
)
}
func
(
kv
*
K8sValidatorMock
)
GetK8sFiles
(
filesConfigurationsChan
chan
*
extractor
.
FileConfigurations
,
concurrency
int
)
(
chan
*
extractor
.
FileConfigurations
,
chan
*
extractor
.
FileConfigurations
)
{
args
:=
kv
.
Called
(
filesConfigurationsChan
,
concurrency
)
return
args
.
Get
(
0
)
.
(
chan
*
extractor
.
FileConfigurations
),
args
.
Get
(
1
)
.
(
chan
*
extractor
.
FileConfigurations
)
}
func
(
kv
*
K8sValidatorMock
)
InitClient
(
k8sVersion
string
,
ignoreMissingSchemas
bool
,
schemaLocations
[]
string
)
{
...
...
@@ -144,7 +149,8 @@ func TestTestCommand(t *testing.T) {
invelidK8sFilesChan
:=
newInvalidK8sFilesChan
()
ignoredFilesChan
:=
newIgnoredYamlFilesChan
()
k8sValidatorMock
.
On
(
"ValidateResources"
,
mock
.
Anything
,
mock
.
Anything
,
mock
.
Anything
)
.
Return
(
filesConfigurationsChan
,
invelidK8sFilesChan
,
ignoredFilesChan
,
newErrorsChan
())
k8sValidatorMock
.
On
(
"ValidateResources"
,
mock
.
Anything
,
mock
.
Anything
)
.
Return
(
filesConfigurationsChan
,
invelidK8sFilesChan
,
newErrorsChan
())
k8sValidatorMock
.
On
(
"GetK8sFiles"
,
mock
.
Anything
,
mock
.
Anything
)
.
Return
(
filesConfigurationsChan
,
ignoredFilesChan
,
newErrorsChan
())
k8sValidatorMock
.
On
(
"InitClient"
,
mock
.
Anything
,
mock
.
Anything
,
mock
.
Anything
)
.
Return
()
printerMock
:=
&
PrinterMock
{}
...
...
@@ -180,7 +186,7 @@ func TestTestCommand(t *testing.T) {
func
test_testCommand_no_flags
(
t
*
testing
.
T
,
evaluator
*
mockEvaluator
,
k8sValidator
*
K8sValidatorMock
,
filesConfigurations
[]
*
extractor
.
FileConfigurations
,
evaluationId
int
,
ctx
*
TestCommandContext
)
{
test
(
ctx
,
[]
string
{
"8/*"
},
TestCommandFlags
{
K8sVersion
:
"1.18.0"
,
Output
:
""
,
PolicyName
:
"Default"
})
k8sValidator
.
AssertCalled
(
t
,
"ValidateResources"
,
mock
.
Anything
,
100
,
false
)
k8sValidator
.
AssertCalled
(
t
,
"ValidateResources"
,
mock
.
Anything
,
100
)
evaluator
.
AssertCalled
(
t
,
"CreateEvaluation"
,
"134kh"
,
""
,
"1.18.0"
,
"Default"
)
evaluator
.
AssertCalled
(
t
,
"Evaluate"
,
filesConfigurations
,
evaluationId
)
}
...
...
@@ -188,28 +194,29 @@ func test_testCommand_no_flags(t *testing.T, evaluator *mockEvaluator, k8sValida
func
test_testCommand_json_output
(
t
*
testing
.
T
,
evaluator
*
mockEvaluator
,
k8sValidator
*
K8sValidatorMock
,
filesConfigurations
[]
*
extractor
.
FileConfigurations
,
evaluationId
int
,
ctx
*
TestCommandContext
)
{
test
(
ctx
,
[]
string
{
"8/*"
},
TestCommandFlags
{
Output
:
"json"
})
k8sValidator
.
AssertCalled
(
t
,
"ValidateResources"
,
mock
.
Anything
,
100
,
false
)
k8sValidator
.
AssertCalled
(
t
,
"ValidateResources"
,
mock
.
Anything
,
100
)
evaluator
.
AssertCalled
(
t
,
"Evaluate"
,
filesConfigurations
,
evaluationId
)
}
func
test_testCommand_yaml_output
(
t
*
testing
.
T
,
evaluator
*
mockEvaluator
,
k8sValidator
*
K8sValidatorMock
,
filesConfigurations
[]
*
extractor
.
FileConfigurations
,
evaluationId
int
,
ctx
*
TestCommandContext
)
{
test
(
ctx
,
[]
string
{
"8/*"
},
TestCommandFlags
{
Output
:
"yaml"
})
k8sValidator
.
AssertCalled
(
t
,
"ValidateResources"
,
mock
.
Anything
,
100
,
false
)
k8sValidator
.
AssertCalled
(
t
,
"ValidateResources"
,
mock
.
Anything
,
100
)
evaluator
.
AssertCalled
(
t
,
"Evaluate"
,
filesConfigurations
,
evaluationId
)
}
func
test_testCommand_xml_output
(
t
*
testing
.
T
,
evaluator
*
mockEvaluator
,
k8sValidator
*
K8sValidatorMock
,
filesConfigurations
[]
*
extractor
.
FileConfigurations
,
evaluationId
int
,
ctx
*
TestCommandContext
)
{
test
(
ctx
,
[]
string
{
"8/*"
},
TestCommandFlags
{
Output
:
"xml"
})
k8sValidator
.
AssertCalled
(
t
,
"ValidateResources"
,
mock
.
Anything
,
100
,
false
)
k8sValidator
.
AssertCalled
(
t
,
"ValidateResources"
,
mock
.
Anything
,
100
)
evaluator
.
AssertCalled
(
t
,
"Evaluate"
,
filesConfigurations
,
evaluationId
)
}
func
test_testCommand_only_k8s_files
(
t
*
testing
.
T
,
k8sValidator
*
K8sValidatorMock
,
filesConfigurations
[]
*
extractor
.
FileConfigurations
,
evaluationId
int
,
ctx
*
TestCommandContext
)
{
test
(
ctx
,
[]
string
{
"8/*"
},
TestCommandFlags
{
OnlyK8sFiles
:
true
})
k8sValidator
.
AssertCalled
(
t
,
"ValidateResources"
,
mock
.
Anything
,
100
,
true
)
k8sValidator
.
AssertCalled
(
t
,
"ValidateResources"
,
mock
.
Anything
,
100
)
k8sValidator
.
AssertCalled
(
t
,
"GetK8sFiles"
,
mock
.
Anything
,
100
)
}
func
newFilesConfigurationsChan
(
path
string
)
chan
*
extractor
.
FileConfigurations
{
...
...
@@ -249,12 +256,14 @@ func newInvalidK8sFilesChan() chan *validation.InvalidK8sFile {
return
invalidFilesChan
}
func
newIgnoredYamlFilesChan
()
chan
*
string
{
ignoredFilesChan
:=
make
(
chan
*
string
)
ignoredFile
:=
"path/to/ignored/file"
func
newIgnoredYamlFilesChan
()
chan
*
extractor
.
FileConfigurations
{
ignoredFilesChan
:=
make
(
chan
*
extractor
.
FileConfigurations
)
ignoredFile
:=
&
extractor
.
FileConfigurations
{
FileName
:
"path/to/ignored/file"
,
}
go
func
()
{
ignoredFilesChan
<-
&
ignoredFile
ignoredFilesChan
<-
ignoredFile
close
(
ignoredFilesChan
)
}()
...
...
This diff is collapsed.
Click to expand it.
cmd/test/testCommandHelper.go
+
3
-
2
View file @
7ca1a6e0
...
...
@@ -31,8 +31,9 @@ func aggregateInvalidYamlFiles(invalidFilesChan chan *validation.InvalidYamlFile
}
return
invalidFiles
}
func
aggregateIgnoredYamlFiles
(
ignoredFilesChan
chan
*
string
)
[]
string
{
var
ignoredFiles
[]
string
func
aggregateIgnoredYamlFiles
(
ignoredFilesChan
chan
*
extractor
.
FileConfigurations
)
[]
extractor
.
FileConfigurations
{
var
ignoredFiles
[]
extractor
.
FileConfigurations
for
ignoredFile
:=
range
ignoredFilesChan
{
ignoredFiles
=
append
(
ignoredFiles
,
*
ignoredFile
)
}
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment