github.com/mkimuram/operator-sdk@v0.7.1-0.20190410172100-52ad33a4bda0/internal/pkg/scorecard/test_definitions.go (about) 1 // Copyright 2019 The Operator-SDK Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package scorecard 16 17 import ( 18 "context" 19 ) 20 21 // Type Definitions 22 23 // Test provides methods for running scorecard tests 24 type Test interface { 25 GetName() string 26 GetDescription() string 27 IsCumulative() bool 28 Run(context.Context) *TestResult 29 } 30 31 // TestResult contains a test's points, suggestions, and errors 32 type TestResult struct { 33 Test Test 34 EarnedPoints int 35 MaximumPoints int 36 Suggestions []string 37 Errors []error 38 } 39 40 // TestInfo contains information about the scorecard test 41 type TestInfo struct { 42 Name string 43 Description string 44 // If a test is set to cumulative, the scores of multiple runs of the same test on separate CRs are added together for the total score. 45 // If cumulative is false, if any test failed, the total score is 0/1. Otherwise 1/1. 46 Cumulative bool 47 } 48 49 // GetName return the test name 50 func (i TestInfo) GetName() string { return i.Name } 51 52 // GetDescription returns the test description 53 func (i TestInfo) GetDescription() string { return i.Description } 54 55 // IsCumulative returns true if the test's scores are intended to be cumulative 56 func (i TestInfo) IsCumulative() bool { return i.Cumulative } 57 58 // TestSuite contains a list of tests and results, along with the relative weights of each test 59 type TestSuite struct { 60 TestInfo 61 Tests []Test 62 TestResults []*TestResult 63 Weights map[string]float64 64 } 65 66 // Helper functions 67 68 // AddTest adds a new Test to a TestSuite along with a relative weight for the new Test 69 func (ts *TestSuite) AddTest(t Test, weight float64) { 70 ts.Tests = append(ts.Tests, t) 71 ts.Weights[t.GetName()] = weight 72 } 73 74 // TotalScore calculates and returns the total score of all run Tests in a TestSuite 75 func (ts *TestSuite) TotalScore() (score int) { 76 floatScore := 0.0 77 for _, result := range ts.TestResults { 78 if result.MaximumPoints != 0 { 79 floatScore += (float64(result.EarnedPoints) / float64(result.MaximumPoints)) * ts.Weights[result.Test.GetName()] 80 } 81 } 82 // scale to a percentage 83 addedWeights := 0.0 84 for _, weight := range ts.Weights { 85 addedWeights += weight 86 } 87 floatScore = floatScore * (100 / addedWeights) 88 return int(floatScore) 89 } 90 91 // Run runs all Tests in a TestSuite 92 func (ts *TestSuite) Run(ctx context.Context) { 93 for _, test := range ts.Tests { 94 ts.TestResults = append(ts.TestResults, test.Run(ctx)) 95 } 96 } 97 98 // NewTestSuite returns a new TestSuite with a given name and description 99 func NewTestSuite(name, description string) *TestSuite { 100 return &TestSuite{ 101 TestInfo: TestInfo{ 102 Name: name, 103 Description: description, 104 }, 105 Weights: make(map[string]float64), 106 } 107 }