github.com/mccv1r0/cni@v0.7.0-alpha1/plugins/test/noop/debug/debug.go (about)

     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  	"io/ioutil"
    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  	ReportStderr         string
    33  	ReportVersionSupport []string
    34  
    35  	// Command stores the CNI command that the plugin received
    36  	Command string
    37  
    38  	// CmdArgs stores the CNI Args and Env Vars that the plugin recieved
    39  	CmdArgs skel.CmdArgs
    40  }
    41  
    42  // ReadDebug will return a debug file recorded by the noop plugin
    43  func ReadDebug(debugFilePath string) (*Debug, error) {
    44  	debugBytes, err := ioutil.ReadFile(debugFilePath)
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  
    49  	var debug Debug
    50  	err = json.Unmarshal(debugBytes, &debug)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  
    55  	return &debug, nil
    56  }
    57  
    58  // WriteDebug will create a debug file to control the noop plugin
    59  func (debug *Debug) WriteDebug(debugFilePath string) error {
    60  	debugBytes, err := json.Marshal(debug)
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	err = ioutil.WriteFile(debugFilePath, debugBytes, 0600)
    66  	if err != nil {
    67  		return err
    68  	}
    69  
    70  	return nil
    71  }