Debian Codegraph @

plugins/test/noop/debug/debug.go

raw
1 // Copyright 2016 CNI 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 // debug supports tests that use the noop plugin
16 package debug
17
18 import (
19 "encoding/json"
20 "os"
21
22 "github.com/containernetworking/cni/pkg/skel"
23 )
24
25 const EmptyReportResultMessage = "set debug.ReportResult and call debug.WriteDebug() before calling this plugin"
26
27 // Debug is used to control and record the behavior of the noop plugin
28 type Debug struct {
29 // Report* fields allow the test to control the behavior of the no-op plugin
30 ReportResult string
31 ReportError string
32 ReportErrorCode uint
33 ReportStderr string
34 ReportVersionSupport []string
35 ExitWithCode int
36
37 // Command stores the CNI command that the plugin received
38 Command string
39
40 // CmdArgs stores the CNI Args and Env Vars that the plugin received
41 CmdArgs skel.CmdArgs
42 }
43
44 // CmdLogEntry records a single CNI command as well as its args
45 type CmdLogEntry struct {
46 Command string
47 CmdArgs skel.CmdArgs
48 }
49
50 // CmdLog records a list of CmdLogEntry received by the noop plugin
51 type CmdLog []CmdLogEntry
52
53 // ReadDebug will return a debug file recorded by the noop plugin
54 func ReadDebug(debugFilePath string) (*Debug, error) {
55 debugBytes, err := os.ReadFile(debugFilePath)
56 if err != nil {
57 return nil, err
58 }
59
60 var debug Debug
61 err = json.Unmarshal(debugBytes, &debug)
62 if err != nil {
63 return nil, err
64 }
65
66 return &debug, nil
67 }
68
69 // WriteDebug will create a debug file to control the noop plugin
70 func (debug *Debug) WriteDebug(debugFilePath string) error {
71 debugBytes, err := json.Marshal(debug)
72 if err != nil {
73 return err
74 }
75
76 err = os.WriteFile(debugFilePath, debugBytes, 0o600)
77 if err != nil {
78 return err
79 }
80
81 return nil
82 }
83
84 // WriteCommandLog appends the executed cni command to the record file
85 func WriteCommandLog(path string, entry CmdLogEntry) error {
86 buf, err := os.ReadFile(path)
87 if err != nil {
88 return err
89 }
90 var cmds CmdLog
91 if len(buf) > 0 {
92 if err = json.Unmarshal(buf, &cmds); err != nil {
93 return err
94 }
95 }
96 cmds = append(cmds, entry)
97 if buf, err = json.Marshal(&cmds); err != nil {
98 return nil
99 }
100 return os.WriteFile(path, buf, 0o644)
101 }
102
103 func ReadCommandLog(path string) (CmdLog, error) {
104 buf, err := os.ReadFile(path)
105 if err != nil {
106 return nil, err
107 }
108 var cmds CmdLog
109 if err = json.Unmarshal(buf, &cmds); err != nil {
110 return nil, err
111 }
112 return cmds, nil
113 }