github.com/ferranbt/nomad@v0.9.3-0.20190607002617-85c449b7667c/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: false,
    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  type PluginInfoFn func() (*PluginInfoResponse, error)
    50  type ConfigSchemaFn func() (*hclspec.Spec, error)
    51  type SetConfigFn func(*Config) error
    52  
    53  // MockPlugin is used for testing.
    54  // Each function can be set as a closure to make assertions about how data
    55  // is passed through the base plugin layer.
    56  type MockPlugin struct {
    57  	PluginInfoF   PluginInfoFn
    58  	ConfigSchemaF ConfigSchemaFn
    59  	SetConfigF    SetConfigFn
    60  }
    61  
    62  func (p *MockPlugin) PluginInfo() (*PluginInfoResponse, error) { return p.PluginInfoF() }
    63  func (p *MockPlugin) ConfigSchema() (*hclspec.Spec, error)     { return p.ConfigSchemaF() }
    64  func (p *MockPlugin) SetConfig(cfg *Config) error {
    65  	return p.SetConfigF(cfg)
    66  }
    67  
    68  // Below are static implementations of the base plugin functions
    69  
    70  // StaticInfo returns the passed PluginInfoResponse with no error
    71  func StaticInfo(out *PluginInfoResponse) PluginInfoFn {
    72  	return func() (*PluginInfoResponse, error) {
    73  		return out, nil
    74  	}
    75  }
    76  
    77  // StaticConfigSchema returns the passed Spec with no error
    78  func StaticConfigSchema(out *hclspec.Spec) ConfigSchemaFn {
    79  	return func() (*hclspec.Spec, error) {
    80  		return out, nil
    81  	}
    82  }
    83  
    84  // TestConfigSchema returns a ConfigSchemaFn that statically returns the
    85  // TestSpec
    86  func TestConfigSchema() ConfigSchemaFn {
    87  	return StaticConfigSchema(TestSpec)
    88  }
    89  
    90  // NoopSetConfig is a noop implementation of set config
    91  func NoopSetConfig() SetConfigFn {
    92  	return func(_ *Config) error { return nil }
    93  }