forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Webhooko API Coverage Tool: Coverage Calculation (kubernetes#426)
This changeset adds to support to perform coverage calculation based on the TypeCoverage type. This change also includes support to provide repos an ability to ignore certain fields based on their needs.
- Loading branch information
1 parent
2754247
commit 1a10543
Showing
18 changed files
with
343 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,23 @@ | ||
# Coverage Calculator | ||
|
||
`coveragecalculator` package contains types and helper methods pertaining to | ||
coverage calculation. Package includes type [TypeCoverage](coveragedata.go) | ||
to represent coverage data for a particular API object type. This is the | ||
wire contract between the webhook server running inside the K8 cluster and any | ||
client using the API-Coverage tool. All API calls into the webhook-server would | ||
return response containing this object to represent coverage data. | ||
coverage calculation. | ||
|
||
[TypeCoverage](coveragedata.go) is a type to represent coverage data for a | ||
particular API object type. This is the wire contract between the webhook | ||
server running inside the K8s cluster and any client using the API-Coverage | ||
tool. All API calls into the webhook-server would return response containing | ||
this object to represent coverage data. | ||
|
||
[IgnoredFields](ignorefields.go) type provides ability for individual repos to | ||
specify fields that they would like the API Coverage tool to ignore for | ||
coverage calculation. Individual repos are expected to provide a .yaml | ||
file providing fields that they would like to ignore and use helper method | ||
`ReadFromFile(filePath)` to read and intialize this type. `FieldIgnored()` can | ||
then be called by providing `packageName`, `typeName` and `FieldName` to check | ||
if the field needs to be ignored. | ||
|
||
[CalculateCoverage](calculator.go) method provides a capability to calculate | ||
coverage values. This method takes an array of [TypeCoverage](coveragedata.go) | ||
and iterates over them to aggreage coverage values. The aggregate result is | ||
encapsulated inside [CoverageValues](calculator.go) and returned. |
42 changes: 42 additions & 0 deletions
42
tools/webhook-apicoverage/coveragecalculator/calculator.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
Copyright 2019 The Knative Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package coveragecalculator | ||
|
||
// CoverageValues encapsulates all the coverage related values. | ||
type CoverageValues struct { | ||
TotalFields int | ||
CoveredFields int | ||
IgnoredFields int | ||
} | ||
|
||
// CalculateTypeCoverage calculates aggregate coverage values based on provided []TypeCoverage | ||
func CalculateTypeCoverage(typeCoverage []TypeCoverage) *CoverageValues { | ||
cv := CoverageValues{} | ||
for _, coverage := range typeCoverage { | ||
for _, field := range coverage.Fields { | ||
if field.Ignored { | ||
cv.IgnoredFields++ | ||
} else { | ||
cv.TotalFields++ | ||
if field.Coverage { | ||
cv.CoveredFields++ | ||
} | ||
} | ||
} | ||
} | ||
return &cv | ||
} |
17 changes: 17 additions & 0 deletions
17
tools/webhook-apicoverage/coveragecalculator/coveragedata.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
tools/webhook-apicoverage/coveragecalculator/ignorefields.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
Copyright 2019 The Knative Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package coveragecalculator | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"strings" | ||
|
||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
// IgnoredFields encapsulates fields to be ignored in a package for API coverage calculation. | ||
type IgnoredFields struct { | ||
ignoredFields map[string]map[string]bool | ||
} | ||
|
||
|
||
// This type is used for deserialization from the input .yaml file | ||
type inputIgnoredFields struct { | ||
Package string `yaml:"package"` | ||
Type string `yaml:"type"` | ||
Fields []string `yaml:"fields"` | ||
} | ||
|
||
// ReadFromFile is a utility method that can be used by repos to read .yaml input file into | ||
// IgnoredFields type. | ||
func (ig *IgnoredFields) ReadFromFile(filePath string) error { | ||
data, err := ioutil.ReadFile(filePath) | ||
if err != nil { | ||
return fmt.Errorf("Error reading file: %s Error : %v", filePath, err) | ||
} | ||
|
||
var inputEntries []inputIgnoredFields | ||
err = yaml.Unmarshal(data, &inputEntries) | ||
if err != nil { | ||
return fmt.Errorf("Error unmarshalling ignoredfields input yaml file: %s Content: %s Error: %v", filePath, string(data), err) | ||
} | ||
|
||
ig.ignoredFields = map[string]map[string]bool {} | ||
|
||
for _, entry := range inputEntries { | ||
if _, ok := ig.ignoredFields[entry.Package]; !ok { | ||
ig.ignoredFields[entry.Package] = map[string]bool{} | ||
} | ||
|
||
for _, field := range entry.Fields { | ||
ig.ignoredFields[entry.Package][entry.Type + "." + field] = true | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// FieldIgnored method given a package, type and field returns true if the field is marked ignored. | ||
func (ig *IgnoredFields) FieldIgnored(packageName string, typeName string, fieldName string) bool { | ||
if ig.ignoredFields != nil { | ||
for key, value := range ig.ignoredFields { | ||
if strings.HasSuffix(packageName, key) { | ||
if _, ok := value[typeName + "." + fieldName]; ok { | ||
return true | ||
} | ||
} | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.