github.com/zoomfoo/nomad@v0.8.5-0.20180907175415-f28fd3a1a056/plugins/base/testing.go (about)

     1  package base
     2  
     3  import (
     4  	"github.com/hashicorp/nomad/plugins/shared/hclspec"
     5  )
     6  
     7  var (
     8  	// TestSpec is an hcl Spec for testing
     9  	TestSpec = &hclspec.Spec{
    10  		Block: &hclspec.Spec_Object{
    11  			Object: &hclspec.Object{
    12  				Attributes: map[string]*hclspec.Spec{
    13  					"foo": {
    14  						Block: &hclspec.Spec_Attr{
    15  							Attr: &hclspec.Attr{
    16  								Type:     "string",
    17  								Required: false,
    18  							},
    19  						},
    20  					},
    21  					"bar": {
    22  						Block: &hclspec.Spec_Attr{
    23  							Attr: &hclspec.Attr{
    24  								Type:     "number",
    25  								Required: true,
    26  							},
    27  						},
    28  					},
    29  					"baz": {
    30  						Block: &hclspec.Spec_Attr{
    31  							Attr: &hclspec.Attr{
    32  								Type: "bool",
    33  							},
    34  						},
    35  					},
    36  				},
    37  			},
    38  		},
    39  	}
    40  )
    41  
    42  // TestConfig is used to decode a config from the TestSpec
    43  type TestConfig struct {
    44  	Foo string `cty:"foo" codec:"foo"`
    45  	Bar int64  `cty:"bar" codec:"bar"`
    46  	Baz bool   `cty:"baz" codec:"baz"`
    47  }
    48  
    49  // MockPlugin is used for testing.
    50  // Each function can be set as a closure to make assertions about how data
    51  // is passed through the base plugin layer.
    52  type MockPlugin struct {
    53  	PluginInfoF   func() (*PluginInfoResponse, error)
    54  	ConfigSchemaF func() (*hclspec.Spec, error)
    55  	SetConfigF    func([]byte) error
    56  }
    57  
    58  func (p *MockPlugin) PluginInfo() (*PluginInfoResponse, error) { return p.PluginInfoF() }
    59  func (p *MockPlugin) ConfigSchema() (*hclspec.Spec, error)     { return p.ConfigSchemaF() }
    60  func (p *MockPlugin) SetConfig(data []byte) error              { return p.SetConfigF(data) }