Skip to content

Commit 92db46a

Browse files
committed
chore: fix store cov being 0% && skip http round-trip, calculate instead
1 parent d9cf04f commit 92db46a

3 files changed

Lines changed: 142 additions & 11 deletions

File tree

pkg/controller/replicaset.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ func (c *ReplicaSetController) reconcile(rs types.ReplicaSet) error {
6262
return err
6363
}
6464
}
65+
current += diff
6566
}
6667

6768
if current > desired {
@@ -71,14 +72,10 @@ func (c *ReplicaSetController) reconcile(rs types.ReplicaSet) error {
7172
return err
7273
}
7374
}
75+
current -= diff
7476
}
7577

76-
// TODO while this is more robust than calculating (pods mightve silently failed), there's probably a more optimal way
77-
updatedPods, err := c.getMatchingPods(rs)
78-
if err != nil {
79-
return err
80-
}
81-
rs.CurrentCount = uint(len(updatedPods))
78+
rs.CurrentCount = current
8279
return c.client.UpdateReplicaSet(rs.Name, rs)
8380
}
8481
func (c *ReplicaSetController) getMatchingPods(rs types.ReplicaSet) ([]types.Pod, error) {

pkg/store/mem_bench_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ import (
55
"testing"
66
)
77

8-
type testItem struct {
9-
Name string
10-
Value int
11-
}
12-
138
func BenchmarkMemStorePut(b *testing.B) {
149
store := NewMemStore[testItem]()
1510
item := testItem{Name: "test", Value: 42}

pkg/store/store_test.go

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package store
2+
3+
import (
4+
"path/filepath"
5+
"testing"
6+
7+
bolt "go.etcd.io/bbolt"
8+
)
9+
10+
type testItem struct {
11+
Name string
12+
Value int
13+
}
14+
15+
// storeFactory creates a fresh store for testing.
16+
type storeFactory func(t *testing.T) Store[testItem]
17+
18+
func memFactory(t *testing.T) Store[testItem] {
19+
t.Helper()
20+
return NewMemStore[testItem]()
21+
}
22+
23+
func boltFactory(t *testing.T) Store[testItem] {
24+
t.Helper()
25+
db, err := bolt.Open(filepath.Join(t.TempDir(), "test.db"), 0o600, nil)
26+
if err != nil {
27+
t.Fatal(err)
28+
}
29+
t.Cleanup(func() { _ = db.Close() })
30+
return NewBoltStore[testItem](db, "test")
31+
}
32+
33+
func runStoreTests(t *testing.T, name string, factory storeFactory) {
34+
t.Run(name, func(t *testing.T) {
35+
t.Run("PutAndGet", func(t *testing.T) {
36+
s := factory(t)
37+
s.Put("a", testItem{Name: "a", Value: 1})
38+
39+
got, ok := s.Get("a")
40+
if !ok {
41+
t.Fatal("expected item to be found")
42+
}
43+
if got.Name != "a" || got.Value != 1 {
44+
t.Errorf("got %+v, want {a 1}", got)
45+
}
46+
})
47+
48+
t.Run("GetMissing", func(t *testing.T) {
49+
s := factory(t)
50+
_, ok := s.Get("missing")
51+
if ok {
52+
t.Error("expected not found")
53+
}
54+
})
55+
56+
t.Run("PutOverwrite", func(t *testing.T) {
57+
s := factory(t)
58+
s.Put("a", testItem{Name: "a", Value: 1})
59+
s.Put("a", testItem{Name: "a", Value: 2})
60+
61+
got, ok := s.Get("a")
62+
if !ok {
63+
t.Fatal("expected item to be found")
64+
}
65+
if got.Value != 2 {
66+
t.Errorf("got Value=%d, want 2", got.Value)
67+
}
68+
})
69+
70+
t.Run("Delete", func(t *testing.T) {
71+
s := factory(t)
72+
s.Put("a", testItem{Name: "a", Value: 1})
73+
s.Delete("a")
74+
75+
_, ok := s.Get("a")
76+
if ok {
77+
t.Error("expected item to be deleted")
78+
}
79+
})
80+
81+
t.Run("DeleteMissing", func(t *testing.T) {
82+
s := factory(t)
83+
s.Delete("nope") // should not panic
84+
})
85+
86+
t.Run("ListEmpty", func(t *testing.T) {
87+
s := factory(t)
88+
items := s.List()
89+
if len(items) != 0 {
90+
t.Errorf("expected empty list, got %d items", len(items))
91+
}
92+
})
93+
94+
t.Run("ListMultiple", func(t *testing.T) {
95+
s := factory(t)
96+
s.Put("a", testItem{Name: "a", Value: 1})
97+
s.Put("b", testItem{Name: "b", Value: 2})
98+
s.Put("c", testItem{Name: "c", Value: 3})
99+
100+
items := s.List()
101+
if len(items) != 3 {
102+
t.Fatalf("expected 3 items, got %d", len(items))
103+
}
104+
105+
seen := map[string]bool{}
106+
for _, item := range items {
107+
seen[item.Name] = true
108+
}
109+
for _, name := range []string{"a", "b", "c"} {
110+
if !seen[name] {
111+
t.Errorf("missing item %q in list", name)
112+
}
113+
}
114+
})
115+
116+
t.Run("ListAfterDelete", func(t *testing.T) {
117+
s := factory(t)
118+
s.Put("a", testItem{Name: "a", Value: 1})
119+
s.Put("b", testItem{Name: "b", Value: 2})
120+
s.Delete("a")
121+
122+
items := s.List()
123+
if len(items) != 1 {
124+
t.Fatalf("expected 1 item, got %d", len(items))
125+
}
126+
if items[0].Name != "b" {
127+
t.Errorf("expected item b, got %s", items[0].Name)
128+
}
129+
})
130+
})
131+
}
132+
133+
func TestMemStore(t *testing.T) {
134+
runStoreTests(t, "MemStore", memFactory)
135+
}
136+
137+
func TestBoltStore(t *testing.T) {
138+
runStoreTests(t, "BoltStore", boltFactory)
139+
}

0 commit comments

Comments
 (0)