/
unitcachehook.go
83 lines (69 loc) · 1.91 KB
/
unitcachehook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2020 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package jujuctesting
import (
"sync"
)
type UnitCharmState struct {
values map[string]string
mu sync.Mutex
}
// ContextUnitCharmState is a test double for jujuc.unitCharmStateContext.
type ContextUnitCharmState struct {
contextBase
info *UnitCharmState
}
func (u *UnitCharmState) SetCharmState(newCharmState map[string]string) {
u.mu.Lock()
defer u.mu.Unlock()
u.values = newCharmState
}
// GetCharmState implements jujuc.unitCharmStateContext.
func (c *ContextUnitCharmState) GetCharmState() (map[string]string, error) {
c.stub.AddCall("GetCharmState")
_ = c.stub.NextErr()
c.info.mu.Lock()
defer c.info.mu.Unlock()
c.ensureValues()
if len(c.info.values) == 0 {
return nil, nil
}
retVal := make(map[string]string, len(c.info.values))
for k, v := range c.info.values {
retVal[k] = v
}
return retVal, nil
}
// GetCharmStateValue implements jujuc.unitCharmStateContext.
func (c *ContextUnitCharmState) GetCharmStateValue(key string) (string, error) {
c.stub.AddCall("GetSCharmStateValue")
c.info.mu.Lock()
defer c.info.mu.Unlock()
c.ensureValues()
return c.info.values[key], nil
}
// DeleteCharmStateValue implements jujuc.unitCharmStateContext.
func (c *ContextUnitCharmState) DeleteCharmStateValue(key string) error {
c.stub.AddCall("DeleteCharmStateValue")
_ = c.stub.NextErr()
c.info.mu.Lock()
defer c.info.mu.Unlock()
c.ensureValues()
delete(c.info.values, key)
return nil
}
// SetCharmStateValue implements jujuc.unitCharmStateContext.
func (c *ContextUnitCharmState) SetCharmStateValue(key string, value string) error {
c.stub.AddCall("SetCharmStateValue")
_ = c.stub.NextErr()
c.info.mu.Lock()
defer c.info.mu.Unlock()
c.ensureValues()
c.info.values[key] = value
return nil
}
func (c *ContextUnitCharmState) ensureValues() {
if c.info.values == nil {
c.info.values = make(map[string]string)
}
}