11package task_metadata
22
33import (
4- "fmt"
5- "strings"
64 "testing"
75
86 . "github.com/onsi/gomega"
9- "github.com/stretchr/testify/assert"
107
118 bctx "github.com/flant/shell-operator/pkg/hook/binding_context"
129 htypes "github.com/flant/shell-operator/pkg/hook/types"
13- "github.com/flant/shell-operator/pkg/metric"
1410 "github.com/flant/shell-operator/pkg/task"
15- "github.com/flant/shell-operator/pkg/task/queue"
1611)
1712
1813func Test_HookMetadata_Access (t * testing.T ) {
@@ -38,74 +33,3 @@ func Test_HookMetadata_Access(t *testing.T) {
3833 g .Expect (hm .BindingContext [0 ].Binding ).Should (Equal ("each_1_min" ))
3934 g .Expect (hm .BindingContext [1 ].Binding ).Should (Equal ("each_5_min" ))
4035}
41-
42- func Test_HookMetadata_QueueDump_Task_Description (t * testing.T ) {
43- g := NewWithT (t )
44-
45- logLabels := map [string ]string {
46- "hook" : "hook1.sh" ,
47- }
48-
49- metricStorage := metric .NewStorageMock (t )
50- metricStorage .HistogramObserveMock .Set (func (metric string , value float64 , labels map [string ]string , buckets []float64 ) {
51- assert .Equal (t , metric , "{PREFIX}tasks_queue_action_duration_seconds" )
52- assert .NotZero (t , value )
53- assert .Equal (t , map [string ]string {
54- "queue_action" : "AddLast" ,
55- "queue_name" : "" ,
56- }, labels )
57- assert .Nil (t , buckets )
58- })
59-
60- q := queue .NewTasksQueue ().WithMetricStorage (metricStorage )
61-
62- q .AddLast (task .NewTask (EnableKubernetesBindings ).
63- WithMetadata (HookMetadata {
64- HookName : "hook1.sh" ,
65- Binding : string (EnableKubernetesBindings ),
66- }))
67-
68- q .AddLast (task .NewTask (HookRun ).
69- WithMetadata (HookMetadata {
70- HookName : "hook1.sh" ,
71- BindingType : htypes .OnKubernetesEvent ,
72- Binding : "monitor_pods" ,
73- }).
74- WithLogLabels (logLabels ).
75- WithQueueName ("main" ))
76-
77- q .AddLast (task .NewTask (HookRun ).
78- WithMetadata (HookMetadata {
79- HookName : "hook1.sh" ,
80- BindingType : htypes .Schedule ,
81- AllowFailure : true ,
82- Binding : "every 1 sec" ,
83- Group : "monitor_pods" ,
84- }).
85- WithLogLabels (logLabels ).
86- WithQueueName ("main" ))
87-
88- queueDump := taskQueueToText (q )
89-
90- g .Expect (queueDump ).Should (ContainSubstring ("hook1.sh" ), "Queue dump should reveal a hook name." )
91- g .Expect (queueDump ).Should (ContainSubstring ("EnableKubernetesBindings" ), "Queue dump should reveal EnableKubernetesBindings." )
92- g .Expect (queueDump ).Should (ContainSubstring (":kubernetes:" ), "Queue dump should show kubernetes binding." )
93- g .Expect (queueDump ).Should (ContainSubstring (":schedule:" ), "Queue dump should show schedule binding." )
94- g .Expect (queueDump ).Should (ContainSubstring ("group=monitor_pods" ), "Queue dump should show group name." )
95- }
96-
97- func taskQueueToText (q * queue.TaskQueue ) string {
98- var buf strings.Builder
99- buf .WriteString (fmt .Sprintf ("Queue '%s': length %d, status: '%s'\n " , q .Name , q .Length (), q .Status ))
100- buf .WriteString ("\n " )
101-
102- index := 1
103- q .Iterate (func (task task.Task ) {
104- buf .WriteString (fmt .Sprintf ("%2d. " , index ))
105- buf .WriteString (task .GetDescription ())
106- buf .WriteString ("\n " )
107- index ++
108- })
109-
110- return buf .String ()
111- }
0 commit comments