github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/service/systemd/stubdbusapi_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package systemd
     5  
     6  import (
     7  	"github.com/coreos/go-systemd/dbus"
     8  	"github.com/juju/testing"
     9  )
    10  
    11  type StubDbusAPI struct {
    12  	*testing.Stub
    13  
    14  	Units     []dbus.UnitStatus
    15  	Props     map[string]interface{}
    16  	TypeProps map[string]interface{}
    17  }
    18  
    19  func (fda *StubDbusAPI) AddService(name, desc, status string) {
    20  	active := ""
    21  	load := "loaded"
    22  	if status == "error" {
    23  		load = status
    24  	} else {
    25  		active = status
    26  	}
    27  
    28  	unit := dbus.UnitStatus{
    29  		Name:        name + ".service",
    30  		Description: desc,
    31  		ActiveState: active,
    32  		LoadState:   load,
    33  	}
    34  	fda.Units = append(fda.Units, unit)
    35  }
    36  
    37  func (fda *StubDbusAPI) SetProperty(unitType, name string, value interface{}) {
    38  	if unitType == "" {
    39  		unitType = "Unit"
    40  	}
    41  
    42  	switch unitType {
    43  	case "Unit":
    44  		if fda.Props == nil {
    45  			fda.Props = make(map[string]interface{})
    46  		}
    47  		fda.Props[name] = value
    48  	default:
    49  		if fda.TypeProps == nil {
    50  			fda.TypeProps = make(map[string]interface{})
    51  		}
    52  		fda.TypeProps[name] = value
    53  	}
    54  }
    55  
    56  func (fda *StubDbusAPI) ListUnits() ([]dbus.UnitStatus, error) {
    57  	fda.Stub.AddCall("ListUnits")
    58  
    59  	return fda.Units, fda.NextErr()
    60  }
    61  
    62  func (fda *StubDbusAPI) StartUnit(name string, mode string, ch chan<- string) (int, error) {
    63  	fda.Stub.AddCall("StartUnit", name, mode, ch)
    64  
    65  	return 0, fda.NextErr()
    66  }
    67  
    68  func (fda *StubDbusAPI) StopUnit(name string, mode string, ch chan<- string) (int, error) {
    69  	fda.Stub.AddCall("StopUnit", name, mode, ch)
    70  
    71  	return 0, fda.NextErr()
    72  }
    73  
    74  func (fda *StubDbusAPI) LinkUnitFiles(files []string, runtime bool, force bool) ([]dbus.LinkUnitFileChange, error) {
    75  	fda.Stub.AddCall("LinkUnitFiles", files, runtime, force)
    76  
    77  	return nil, fda.NextErr()
    78  }
    79  
    80  func (fda *StubDbusAPI) EnableUnitFiles(files []string, runtime bool, force bool) (bool, []dbus.EnableUnitFileChange, error) {
    81  	fda.Stub.AddCall("EnableUnitFiles", files, runtime, force)
    82  
    83  	return false, nil, fda.NextErr()
    84  }
    85  
    86  func (fda *StubDbusAPI) DisableUnitFiles(files []string, runtime bool) ([]dbus.DisableUnitFileChange, error) {
    87  	fda.Stub.AddCall("DisableUnitFiles", files, runtime)
    88  
    89  	return nil, fda.NextErr()
    90  }
    91  
    92  func (fda *StubDbusAPI) GetUnitProperties(unit string) (map[string]interface{}, error) {
    93  	fda.Stub.AddCall("GetUnitProperties", unit)
    94  
    95  	return fda.Props, fda.NextErr()
    96  }
    97  
    98  func (fda *StubDbusAPI) GetUnitTypeProperties(unit, unitType string) (map[string]interface{}, error) {
    99  	fda.Stub.AddCall("GetUnitTypeProperties", unit, unitType)
   100  
   101  	return fda.TypeProps, fda.NextErr()
   102  }
   103  
   104  func (fda *StubDbusAPI) Reload() error {
   105  	fda.Stub.AddCall("Reload")
   106  
   107  	return fda.Stub.NextErr()
   108  }
   109  
   110  func (fda *StubDbusAPI) Close() {
   111  	fda.Stub.AddCall("Close")
   112  
   113  	fda.Stub.NextErr() // We don't return the error (just pop it off).
   114  }