github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/api/uniter/goal-state_test.go (about)

     1  // Copyright 2018 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package uniter_test
     5  
     6  import (
     7  	"time"
     8  
     9  	coretesting "github.com/juju/juju/testing"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  	"gopkg.in/juju/names.v2"
    13  
    14  	"github.com/juju/juju/api/base/testing"
    15  	"github.com/juju/juju/api/uniter"
    16  	"github.com/juju/juju/apiserver/params"
    17  	"github.com/juju/juju/core/application"
    18  )
    19  
    20  type goalStateSuite struct {
    21  	coretesting.BaseSuite
    22  }
    23  
    24  var _ = gc.Suite(&goalStateSuite{})
    25  
    26  var (
    27  	timestamp = time.Date(2200, time.November, 5, 0, 0, 0, 0, time.UTC)
    28  
    29  	paramsBaseGoalStateStatus = params.GoalStateStatus{
    30  		Status: "active",
    31  		Since:  &timestamp,
    32  	}
    33  	apiBaseGoalStateStatus = application.GoalStateStatus{
    34  		Status: "active",
    35  		Since:  &timestamp,
    36  	}
    37  )
    38  
    39  func (s *goalStateSuite) TestGoalStateOneUnit(c *gc.C) {
    40  	paramsOneUnit := params.GoalStateResults{
    41  		Results: []params.GoalStateResult{
    42  			{Result: &params.GoalState{
    43  				Units: params.UnitsGoalState{
    44  					"mysql/0": paramsBaseGoalStateStatus,
    45  				},
    46  			},
    47  			},
    48  		},
    49  	}
    50  	apiOneUnit := application.GoalState{
    51  		Units: application.UnitsGoalState{
    52  			"mysql/0": apiBaseGoalStateStatus,
    53  		},
    54  	}
    55  	s.testGoalState(c, paramsOneUnit, apiOneUnit)
    56  
    57  }
    58  
    59  func (s *goalStateSuite) TestGoalStateTwoRelatedUnits(c *gc.C) {
    60  	paramsTwoRelatedUnits := params.GoalStateResults{
    61  		Results: []params.GoalStateResult{
    62  			{Result: &params.GoalState{
    63  				Units: params.UnitsGoalState{
    64  					"mysql/0": paramsBaseGoalStateStatus,
    65  				},
    66  				Relations: map[string]params.UnitsGoalState{
    67  					"db": {
    68  						"wordpress/0": paramsBaseGoalStateStatus,
    69  					},
    70  				},
    71  			},
    72  			},
    73  		},
    74  	}
    75  	apiTwoRelatedUnits := application.GoalState{
    76  		Units: application.UnitsGoalState{
    77  			"mysql/0": apiBaseGoalStateStatus,
    78  		},
    79  		Relations: map[string]application.UnitsGoalState{
    80  			"db": {
    81  				"wordpress/0": apiBaseGoalStateStatus,
    82  			},
    83  		},
    84  	}
    85  	s.testGoalState(c, paramsTwoRelatedUnits, apiTwoRelatedUnits)
    86  }
    87  
    88  func (s *goalStateSuite) testGoalState(c *gc.C, facadeResult params.GoalStateResults, apiResult application.GoalState) {
    89  	var called bool
    90  	apiCaller := testing.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
    91  		c.Check(objType, gc.Equals, "Uniter")
    92  		c.Check(version, gc.Equals, expectedVersion)
    93  		c.Check(request, gc.Equals, "GoalStates")
    94  		c.Check(arg, gc.DeepEquals, params.Entities{
    95  			Entities: []params.Entity{{Tag: "unit-mysql-0"}},
    96  		})
    97  		c.Assert(result, gc.FitsTypeOf, &params.GoalStateResults{})
    98  		*(result.(*params.GoalStateResults)) = facadeResult
    99  		called = true
   100  		return nil
   101  	})
   102  
   103  	st := uniter.NewState(apiCaller, names.NewUnitTag("mysql/0"))
   104  	goalStateResult, err := st.GoalState()
   105  
   106  	c.Assert(err, jc.ErrorIsNil)
   107  	c.Assert(goalStateResult, jc.DeepEquals, apiResult)
   108  	c.Assert(called, jc.IsTrue)
   109  }