github.com/fabianvf/ocp-release-operator-sdk@v0.0.0-20190426141702-57620ee2f090/cmd/operator-sdk/scorecard/basic_tests.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 "encoding/json" 20 "fmt" 21 "strings" 22 23 "k8s.io/apimachinery/pkg/types" 24 ) 25 26 // Run - implements Test interface 27 func (t *CheckSpecTest) Run(ctx context.Context) *TestResult { 28 res := &TestResult{Test: t, MaximumPoints: 1} 29 err := t.Client.Get(ctx, types.NamespacedName{Namespace: t.CR.GetNamespace(), Name: t.CR.GetName()}, t.CR) 30 if err != nil { 31 res.Errors = append(res.Errors, fmt.Errorf("error getting custom resource: %v", err)) 32 return res 33 } 34 if t.CR.Object["spec"] != nil { 35 res.EarnedPoints++ 36 } 37 if res.EarnedPoints != 1 { 38 res.Suggestions = append(res.Suggestions, "Add a 'spec' field to your Custom Resource") 39 } 40 return res 41 } 42 43 // Run - implements Test interface 44 func (t *CheckStatusTest) Run(ctx context.Context) *TestResult { 45 res := &TestResult{Test: t, MaximumPoints: 1} 46 err := t.Client.Get(ctx, types.NamespacedName{Namespace: t.CR.GetNamespace(), Name: t.CR.GetName()}, t.CR) 47 if err != nil { 48 res.Errors = append(res.Errors, fmt.Errorf("error getting custom resource: %v", err)) 49 return res 50 } 51 if t.CR.Object["status"] != nil { 52 res.EarnedPoints++ 53 } 54 if res.EarnedPoints != 1 { 55 res.Suggestions = append(res.Suggestions, "Add a 'status' field to your Custom Resource") 56 } 57 return res 58 } 59 60 // Run - implements Test interface 61 func (t *WritingIntoCRsHasEffectTest) Run(ctx context.Context) *TestResult { 62 res := &TestResult{Test: t, MaximumPoints: 1} 63 logs, err := getProxyLogs(t.ProxyPod) 64 if err != nil { 65 res.Errors = append(res.Errors, fmt.Errorf("error getting proxy logs: %v", err)) 66 return res 67 } 68 msgMap := make(map[string]interface{}) 69 for _, msg := range strings.Split(logs, "\n") { 70 if err := json.Unmarshal([]byte(msg), &msgMap); err != nil { 71 continue 72 } 73 method, ok := msgMap["method"].(string) 74 if !ok { 75 continue 76 } 77 if method == "PUT" || method == "POST" { 78 res.EarnedPoints = 1 79 break 80 } 81 } 82 if res.EarnedPoints != 1 { 83 res.Suggestions = append(res.Suggestions, "The operator should write into objects to update state. No PUT or POST requests from the operator were recorded by the scorecard.") 84 } 85 return res 86 }