github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/core/description/action_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package description
     5  
     6  import (
     7  	"time"
     8  
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  	"gopkg.in/yaml.v2"
    12  )
    13  
    14  type ActionSerializationSuite struct {
    15  	SliceSerializationSuite
    16  }
    17  
    18  var _ = gc.Suite(&ActionSerializationSuite{})
    19  
    20  func (s *ActionSerializationSuite) SetUpTest(c *gc.C) {
    21  	s.SliceSerializationSuite.SetUpTest(c)
    22  	s.importName = "actions"
    23  	s.sliceName = "actions"
    24  	s.importFunc = func(m map[string]interface{}) (interface{}, error) {
    25  		return importActions(m)
    26  	}
    27  	s.testFields = func(m map[string]interface{}) {
    28  		m["actions"] = []interface{}{}
    29  	}
    30  }
    31  
    32  func (s *ActionSerializationSuite) TestNewAction(c *gc.C) {
    33  	args := ActionArgs{
    34  		Id:         "foo",
    35  		Receiver:   "bar",
    36  		Name:       "bam",
    37  		Parameters: map[string]interface{}{"foo": 3, "bar": "bam"},
    38  		Enqueued:   time.Now(),
    39  		Started:    time.Now(),
    40  		Completed:  time.Now(),
    41  		Status:     "happy",
    42  		Message:    "a message",
    43  		Results:    map[string]interface{}{"the": 3, "thing": "bam"},
    44  	}
    45  	action := newAction(args)
    46  	c.Check(action.Id(), gc.Equals, args.Id)
    47  	c.Check(action.Receiver(), gc.Equals, args.Receiver)
    48  	c.Check(action.Name(), gc.Equals, args.Name)
    49  	c.Check(action.Parameters(), jc.DeepEquals, args.Parameters)
    50  	c.Check(action.Enqueued(), gc.Equals, args.Enqueued)
    51  	c.Check(action.Started(), gc.Equals, args.Started)
    52  	c.Check(action.Completed(), gc.Equals, args.Completed)
    53  	c.Check(action.Status(), gc.Equals, args.Status)
    54  	c.Check(action.Message(), gc.Equals, args.Message)
    55  	c.Check(action.Results(), jc.DeepEquals, args.Results)
    56  }
    57  
    58  func (s *ActionSerializationSuite) TestParsingSerializedData(c *gc.C) {
    59  	initial := actions{
    60  		Version: 1,
    61  		Actions_: []*action{
    62  			newAction(ActionArgs{
    63  				Id:         "foo",
    64  				Receiver:   "bar",
    65  				Name:       "bam",
    66  				Parameters: map[string]interface{}{"foo": 3, "bar": "bam"},
    67  				Enqueued:   time.Now().UTC(),
    68  				Started:    time.Now().UTC(),
    69  				Completed:  time.Now().UTC(),
    70  				Status:     "happy",
    71  				Message:    "a message",
    72  				Results:    map[string]interface{}{"the": 3, "thing": "bam"},
    73  			}),
    74  			newAction(ActionArgs{
    75  				Name:       "bing",
    76  				Enqueued:   time.Now().UTC(),
    77  				Parameters: map[string]interface{}{"bop": 4, "beep": "fish"},
    78  				Results:    map[string]interface{}{"eggs": 5, "spam": "wow"},
    79  			}),
    80  		},
    81  	}
    82  
    83  	bytes, err := yaml.Marshal(initial)
    84  	c.Assert(err, jc.ErrorIsNil)
    85  
    86  	var source map[string]interface{}
    87  	err = yaml.Unmarshal(bytes, &source)
    88  	c.Assert(err, jc.ErrorIsNil)
    89  
    90  	actions, err := importActions(source)
    91  	c.Assert(err, jc.ErrorIsNil)
    92  
    93  	c.Assert(actions, jc.DeepEquals, initial.Actions_)
    94  }