|
1 | 1 | package fleet |
2 | 2 |
|
3 | | -import "testing" |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "sync" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/coreos/fleet/machine" |
| 9 | + "github.com/coreos/fleet/schema" |
| 10 | +) |
| 11 | + |
| 12 | +type stubFleetClient struct { |
| 13 | + testUnits []*schema.Unit |
| 14 | + testUnitStates []*schema.UnitState |
| 15 | + testMachineStates []machine.MachineState |
| 16 | + unitStatesMutex *sync.Mutex |
| 17 | + unitsMutex *sync.Mutex |
| 18 | +} |
| 19 | + |
| 20 | +func (c *stubFleetClient) Machines() ([]machine.MachineState, error) { |
| 21 | + return c.testMachineStates, nil |
| 22 | +} |
| 23 | +func (c *stubFleetClient) Unit(name string) (*schema.Unit, error) { |
| 24 | + c.unitsMutex.Lock() |
| 25 | + defer c.unitsMutex.Unlock() |
| 26 | + |
| 27 | + for _, unit := range c.testUnits { |
| 28 | + if unit.Name == name { |
| 29 | + return unit, nil |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + return nil, fmt.Errorf("Unit %s not found", name) |
| 34 | +} |
| 35 | +func (c *stubFleetClient) Units() ([]*schema.Unit, error) { |
| 36 | + c.unitsMutex.Lock() |
| 37 | + defer c.unitsMutex.Unlock() |
| 38 | + |
| 39 | + return c.testUnits, nil |
| 40 | +} |
| 41 | +func (c *stubFleetClient) UnitStates() ([]*schema.UnitState, error) { |
| 42 | + c.unitStatesMutex.Lock() |
| 43 | + defer c.unitStatesMutex.Unlock() |
| 44 | + |
| 45 | + return c.testUnitStates, nil |
| 46 | +} |
| 47 | +func (c *stubFleetClient) SetUnitTargetState(name, target string) error { |
| 48 | + |
| 49 | + var activeState string |
| 50 | + var subState string |
| 51 | + |
| 52 | + if target == "loaded" { |
| 53 | + activeState = "inactive" |
| 54 | + subState = "dead" |
| 55 | + } else if target == "launched" { |
| 56 | + activeState = "active" |
| 57 | + subState = "running" |
| 58 | + } |
| 59 | + |
| 60 | + unit, err := c.Unit(name) |
| 61 | + |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + c.unitsMutex.Lock() |
| 67 | + unit.DesiredState = target |
| 68 | + c.unitsMutex.Unlock() |
| 69 | + |
| 70 | + c.unitStatesMutex.Lock() |
| 71 | + defer c.unitStatesMutex.Unlock() |
| 72 | + |
| 73 | + for _, unitState := range c.testUnitStates { |
| 74 | + if name == unitState.Name { |
| 75 | + unitState.SystemdSubState = subState |
| 76 | + unitState.SystemdActiveState = activeState |
| 77 | + return nil |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + c.testUnitStates = append(c.testUnitStates, &schema.UnitState{Name: name, SystemdSubState: subState, SystemdActiveState: activeState}) |
| 82 | + |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +func (c *stubFleetClient) CreateUnit(unit *schema.Unit) error { |
| 87 | + c.unitsMutex.Lock() |
| 88 | + c.testUnits = append(c.testUnits, unit) |
| 89 | + c.unitsMutex.Unlock() |
| 90 | + |
| 91 | + return nil |
| 92 | +} |
| 93 | + |
| 94 | +func (c *stubFleetClient) DestroyUnit(name string) error { |
| 95 | + c.unitsMutex.Lock() |
| 96 | + for i := len(c.testUnits) - 1; i >= 0; i-- { |
| 97 | + if c.testUnits[i].Name == name { |
| 98 | + c.testUnits = append(c.testUnits[:i], c.testUnits[i+1:]...) |
| 99 | + } |
| 100 | + } |
| 101 | + c.unitsMutex.Unlock() |
| 102 | + |
| 103 | + return nil |
| 104 | +} |
| 105 | + |
| 106 | +func logState(outchan chan string, errchan chan error, errOutput *string, mutex *sync.Mutex) { |
| 107 | + for { |
| 108 | + select { |
| 109 | + case _, ok := <-outchan: |
| 110 | + if !ok { |
| 111 | + outchan = nil |
| 112 | + } |
| 113 | + case err, ok := <-errchan: |
| 114 | + if !ok { |
| 115 | + errchan = nil |
| 116 | + } |
| 117 | + if err != nil { |
| 118 | + mutex.Lock() |
| 119 | + *errOutput += fmt.Sprintf("%v\n", err) |
| 120 | + mutex.Unlock() |
| 121 | + } |
| 122 | + } |
| 123 | + if outchan == nil && errchan == nil { |
| 124 | + break |
| 125 | + } |
| 126 | + } |
| 127 | +} |
4 | 128 |
|
5 | 129 | func TestNewClient(t *testing.T) { |
6 | 130 | // set required flags |
|
0 commit comments