github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/state/status_util_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package state_test
     5  
     6  import (
     7  	"runtime"
     8  	"time"
     9  
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/juju/state"
    14  	"github.com/juju/juju/status"
    15  )
    16  
    17  type statusHistoryFunc func(int) ([]status.StatusInfo, error)
    18  
    19  type statusSetter interface {
    20  	SetStatus(status.StatusInfo) error
    21  }
    22  
    23  func primeStatusHistory(c *gc.C, entity statusSetter, statusVal status.Status, count int, nextData func(int) map[string]interface{}, delta time.Duration) {
    24  	info := ""
    25  	now := time.Now().Add(-delta)
    26  	for i := 0; i < count; i++ {
    27  		c.Logf("setting status for %v", entity)
    28  		data := nextData(i)
    29  		t := now.Add(time.Duration(i) * time.Second)
    30  		s := status.StatusInfo{
    31  			Status:  statusVal,
    32  			Message: info,
    33  			Data:    data,
    34  			Since:   &t,
    35  		}
    36  		err := entity.SetStatus(s)
    37  		c.Assert(err, jc.ErrorIsNil)
    38  		if runtime.GOOS == "windows" {
    39  			// The default clock tick on Windows is 15.6 ms.
    40  			time.Sleep(20 * time.Millisecond)
    41  		}
    42  	}
    43  }
    44  
    45  func checkInitialWorkloadStatus(c *gc.C, statusInfo status.StatusInfo) {
    46  	c.Check(statusInfo.Status, gc.Equals, status.Waiting)
    47  	c.Check(statusInfo.Message, gc.Equals, "waiting for machine")
    48  	c.Check(statusInfo.Data, gc.HasLen, 0)
    49  	c.Check(statusInfo.Since, gc.NotNil)
    50  }
    51  
    52  func primeUnitStatusHistory(c *gc.C, unit *state.Unit, count int, delta time.Duration) {
    53  	primeStatusHistory(c, unit, status.Active, count, func(i int) map[string]interface{} {
    54  		return map[string]interface{}{"$foo": i, "$delta": delta}
    55  	}, delta)
    56  }
    57  
    58  func checkPrimedUnitStatus(c *gc.C, statusInfo status.StatusInfo, expect int, expectDelta time.Duration) {
    59  	c.Check(statusInfo.Status, gc.Equals, status.Active)
    60  	c.Check(statusInfo.Message, gc.Equals, "")
    61  	c.Check(statusInfo.Data, jc.DeepEquals, map[string]interface{}{"$foo": expect, "$delta": int64(expectDelta)})
    62  	c.Check(statusInfo.Since, gc.NotNil)
    63  }
    64  
    65  func checkInitialUnitAgentStatus(c *gc.C, statusInfo status.StatusInfo) {
    66  	c.Check(statusInfo.Status, gc.Equals, status.Allocating)
    67  	c.Check(statusInfo.Message, gc.Equals, "")
    68  	c.Check(statusInfo.Data, gc.HasLen, 0)
    69  	c.Assert(statusInfo.Since, gc.NotNil)
    70  }
    71  
    72  func primeUnitAgentStatusHistory(c *gc.C, agent *state.UnitAgent, count int, delta time.Duration) {
    73  	primeStatusHistory(c, agent, status.Executing, count, func(i int) map[string]interface{} {
    74  		return map[string]interface{}{"$bar": i, "$delta": delta}
    75  	}, delta)
    76  }
    77  
    78  func checkPrimedUnitAgentStatus(c *gc.C, statusInfo status.StatusInfo, expect int, expectDelta time.Duration) {
    79  	c.Check(statusInfo.Status, gc.Equals, status.Executing)
    80  	c.Check(statusInfo.Message, gc.Equals, "")
    81  	c.Check(statusInfo.Data, jc.DeepEquals, map[string]interface{}{"$bar": expect, "$delta": int64(expectDelta)})
    82  	c.Check(statusInfo.Since, gc.NotNil)
    83  }