...
1 package agent_test
2
3 import (
4 "fmt"
5 "sync"
6 "testing"
7
8 "github.com/datawire/ambassador/v2/pkg/agent"
9 "github.com/datawire/ambassador/v2/pkg/kates"
10 "github.com/google/uuid"
11 "github.com/stretchr/testify/assert"
12 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
13 "k8s.io/apimachinery/pkg/types"
14 )
15
16 func newGenericCallback(apiVersion, kind, name string, eventType agent.CallbackEventType) *agent.GenericCallback {
17 obj := &unstructured.Unstructured{}
18 id := uuid.New().String()
19 obj.SetUID(types.UID(id))
20
21 obj.SetAPIVersion(apiVersion)
22 obj.SetKind(kind)
23 obj.SetName(name)
24 obj.SetNamespace("default")
25 return &agent.GenericCallback{
26 EventType: eventType,
27 Obj: obj,
28 Sotw: []interface{}{},
29 }
30 }
31
32 func TestRolloutStore(t *testing.T) {
33 t.Run("will populate the rolloutstore successfully", func(t *testing.T) {
34
35 t.Parallel()
36 rs := agent.NewRolloutStore()
37 wg := sync.WaitGroup{}
38 wg.Add(10)
39
40
41 for i := 0; i < 10; i++ {
42 go func(i int) {
43 defer wg.Done()
44 name := fmt.Sprintf("Rollout%d", i)
45 callback := newGenericCallback("argoproj.io/v1alpha1", "Rollout", name, agent.CallbackEventAdded)
46 _, err := rs.FromCallback(callback)
47 assert.NoError(t, err)
48 }(i)
49 }
50 wg.Wait()
51
52
53 assert.Equal(t, 10, len(rs.Deltas()))
54 assert.Equal(t, "Rollout", rs.Deltas()[0].Kind)
55 assert.Equal(t, "argoproj.io/v1alpha1", rs.Deltas()[0].APIVersion)
56 assert.Equal(t, "default", rs.Deltas()[0].Namespace)
57 assert.Equal(t, kates.ObjectAdd, rs.Deltas()[0].DeltaType)
58 sotw := rs.StateOfWorld()
59 assert.Equal(t, 10, len(sotw))
60 })
61 }
62 func TestApplicationStore(t *testing.T) {
63 t.Run("will populate the rolloutstore successfully", func(t *testing.T) {
64
65 t.Parallel()
66 as := agent.NewApplicationStore()
67 wg := sync.WaitGroup{}
68 wg.Add(10)
69
70
71 for i := 0; i < 10; i++ {
72 go func(i int) {
73 defer wg.Done()
74 name := fmt.Sprintf("Application%d", i)
75 callback := newGenericCallback("argoproj.io/v1alpha1", "Application", name, agent.CallbackEventUpdated)
76 _, err := as.FromCallback(callback)
77 assert.NoError(t, err)
78 }(i)
79 }
80 wg.Wait()
81
82
83 assert.Equal(t, 10, len(as.Deltas()))
84 assert.Equal(t, "Application", as.Deltas()[0].Kind)
85 assert.Equal(t, "argoproj.io/v1alpha1", as.Deltas()[0].APIVersion)
86 assert.Equal(t, "default", as.Deltas()[0].Namespace)
87 assert.Equal(t, kates.ObjectUpdate, as.Deltas()[0].DeltaType)
88 sotw := as.StateOfWorld()
89 assert.Equal(t, 10, len(sotw))
90 })
91
92 }
93
View as plain text