@@ -84,89 +84,75 @@ func (suite *k8sSuite) Test00UpAndRunning() {
8484 ctx := context .Background ()
8585
8686 suite .Run ("agent pods are ready and not restarting" , func () {
87- suite .EventuallyWithTf (func (collect * assert.CollectT ) {
87+ suite .EventuallyWithTf (func (c * assert.CollectT ) {
8888
8989 linuxNodes , err := suite .K8sClient .CoreV1 ().Nodes ().List (ctx , metav1.ListOptions {
9090 LabelSelector : fields .OneTermEqualSelector ("kubernetes.io/os" , "linux" ).String (),
9191 })
92- if err != nil {
93- collect . Errorf ( "Failed to list Linux nodes: %w" , err )
92+ // Can be replaced by require.NoErrorf(…) once https://github.com/stretchr/testify/pull/1481 is merged
93+ if ! assert . NoErrorf ( c , err , "Failed to list Linux nodes" ) {
9494 return
9595 }
9696
9797 windowsNodes , err := suite .K8sClient .CoreV1 ().Nodes ().List (ctx , metav1.ListOptions {
9898 LabelSelector : fields .OneTermEqualSelector ("kubernetes.io/os" , "windows" ).String (),
9999 })
100- if err != nil {
101- collect . Errorf ( "Failed to list Windows nodes: %w" , err )
100+ // Can be replaced by require.NoErrorf(…) once https://github.com/stretchr/testify/pull/1481 is merged
101+ if ! assert . NoErrorf ( c , err , "Failed to list Windows nodes" ) {
102102 return
103103 }
104104
105105 linuxPods , err := suite .K8sClient .CoreV1 ().Pods ("datadog" ).List (ctx , metav1.ListOptions {
106106 LabelSelector : fields .OneTermEqualSelector ("app" , suite .AgentLinuxHelmInstallName + "-datadog" ).String (),
107107 })
108- if err != nil {
109- collect . Errorf ( "Failed to list Linux datadog agent pods: %w" , err )
108+ // Can be replaced by require.NoErrorf(…) once https://github.com/stretchr/testify/pull/1481 is merged
109+ if ! assert . NoErrorf ( c , err , "Failed to list Linux datadog agent pods" ) {
110110 return
111111 }
112112
113113 windowsPods , err := suite .K8sClient .CoreV1 ().Pods ("datadog" ).List (ctx , metav1.ListOptions {
114114 LabelSelector : fields .OneTermEqualSelector ("app" , suite .AgentWindowsHelmInstallName + "-datadog" ).String (),
115115 })
116- if err != nil {
117- collect . Errorf ( "Failed to list Windows datadog agent pods: %w" , err )
116+ // Can be replaced by require.NoErrorf(…) once https://github.com/stretchr/testify/pull/1481 is merged
117+ if ! assert . NoErrorf ( c , err , "Failed to list Windows datadog agent pods" ) {
118118 return
119119 }
120120
121121 clusterAgentPods , err := suite .K8sClient .CoreV1 ().Pods ("datadog" ).List (ctx , metav1.ListOptions {
122122 LabelSelector : fields .OneTermEqualSelector ("app" , suite .AgentLinuxHelmInstallName + "-datadog-cluster-agent" ).String (),
123123 })
124- if err != nil {
125- collect . Errorf ( "Failed to list datadog cluster agent pods: %w" , err )
124+ // Can be replaced by require.NoErrorf(…) once https://github.com/stretchr/testify/pull/1481 is merged
125+ if ! assert . NoErrorf ( c , err , "Failed to list datadog cluster agent pods" ) {
126126 return
127127 }
128128
129129 clusterChecksPods , err := suite .K8sClient .CoreV1 ().Pods ("datadog" ).List (ctx , metav1.ListOptions {
130130 LabelSelector : fields .OneTermEqualSelector ("app" , suite .AgentLinuxHelmInstallName + "-datadog-clusterchecks" ).String (),
131131 })
132- if err != nil {
133- collect . Errorf ( "Failed to list datadog cluster checks runner pods: %w" , err )
132+ // Can be replaced by require.NoErrorf(…) once https://github.com/stretchr/testify/pull/1481 is merged
133+ if ! assert . NoErrorf ( c , err , "Failed to list datadog cluster checks runner pods" ) {
134134 return
135135 }
136136
137- dogstatsdStandalonePods , err := suite .K8sClient .CoreV1 ().Pods ("dogstatsd-standalone" ).List (ctx , metav1.ListOptions {
137+ dogstatsdPods , err := suite .K8sClient .CoreV1 ().Pods ("dogstatsd-standalone" ).List (ctx , metav1.ListOptions {
138138 LabelSelector : fields .OneTermEqualSelector ("app" , "dogstatsd-standalone" ).String (),
139139 })
140- if err != nil {
141- collect . Errorf ( "Failed to list dogstatsd standalone pods: %w" , err )
140+ // Can be replaced by require.NoErrorf(…) once https://github.com/stretchr/testify/pull/1481 is merged
141+ if ! assert . NoErrorf ( c , err , "Failed to list dogstatsd standalone pods" ) {
142142 return
143143 }
144144
145- if len (linuxPods .Items ) != len (linuxNodes .Items ) {
146- collect .Errorf ("There is only %d Linux datadog agent pods for %d Linux nodes." , len (linuxPods .Items ), len (linuxNodes .Items ))
147- }
148- if len (windowsPods .Items ) != len (windowsNodes .Items ) {
149- collect .Errorf ("There is only %d Windows datadog agent pods for %d Windows nodes." , len (windowsPods .Items ), len (windowsNodes .Items ))
150- }
151- if len (clusterAgentPods .Items ) == 0 {
152- collect .Errorf ("There isn’t any cluster agent pod." )
153- }
154- if len (clusterChecksPods .Items ) == 0 {
155- collect .Errorf ("There isn’t any cluster checks worker pod." )
156- }
157- if len (dogstatsdStandalonePods .Items ) != len (linuxNodes .Items ) {
158- collect .Errorf ("There is only %d dogstatsd standalone pods for %d Linux nodes." , len (dogstatsdStandalonePods .Items ), len (linuxNodes .Items ))
159- }
145+ assert .Len (c , linuxPods .Items , len (linuxNodes .Items ))
146+ assert .Len (c , windowsPods .Items , len (windowsNodes .Items ))
147+ assert .NotEmpty (c , clusterAgentPods .Items )
148+ assert .NotEmpty (c , clusterChecksPods .Items )
149+ assert .Len (c , dogstatsdPods .Items , len (linuxNodes .Items ))
160150
161- for _ , podList := range []* corev1.PodList {linuxPods , windowsPods , clusterAgentPods , clusterChecksPods } {
151+ for _ , podList := range []* corev1.PodList {linuxPods , windowsPods , clusterAgentPods , clusterChecksPods , dogstatsdPods } {
162152 for _ , pod := range podList .Items {
163153 for _ , containerStatus := range append (pod .Status .InitContainerStatuses , pod .Status .ContainerStatuses ... ) {
164- if ! containerStatus .Ready {
165- collect .Errorf ("Container %s of pod %s isn’t ready." , containerStatus .Name , pod .Name )
166- }
167- if containerStatus .RestartCount > 0 {
168- collect .Errorf ("Container %s of pod %s has restarted %d times." , containerStatus .Name , pod .Name , containerStatus .RestartCount )
169- }
154+ assert .Truef (c , containerStatus .Ready , "Container %s of pod %s isn’t ready" , containerStatus .Name , pod .Name )
155+ assert .Zerof (c , containerStatus .RestartCount , "Container %s of pod %s has restarted" , containerStatus .Name , pod .Name )
170156 }
171157 }
172158 }
@@ -209,7 +195,7 @@ func (suite *k8sSuite) Test00UpAndRunning() {
209195 if suite .NoError (err ) && len (linuxPods .Items ) >= 1 {
210196 stdout , stderr , err := suite .podExec ("datadog" , linuxPods .Items [0 ].Name , tt .container , []string {"agent" , "version" })
211197 if suite .NoError (err ) {
212- suite .Equalf (stderr , "" , "Standard error of `agent version` should be empty," )
198+ suite .Emptyf (stderr , "Standard error of `agent version` should be empty," )
213199 match := versionExtractor .FindStringSubmatch (stdout )
214200 if suite .Equalf (2 , len (match ), "'Commit' not found in the output of `agent version`." ) {
215201 if len (GitCommit ) == 10 && len (match [1 ]) == 7 {
@@ -289,6 +275,10 @@ func (suite *k8sSuite) TestNginx() {
289275 `^kube_deployment:nginx$` ,
290276 `^kube_namespace:workload-nginx$` ,
291277 },
278+ Value : & testMetricExpectValueArgs {
279+ Max : 5 ,
280+ Min : 1 ,
281+ },
292282 },
293283 })
294284
@@ -345,6 +335,10 @@ func (suite *k8sSuite) TestRedis() {
345335 `^kube_deployment:redis$` ,
346336 `^kube_namespace:workload-redis$` ,
347337 },
338+ Value : & testMetricExpectValueArgs {
339+ Max : 5 ,
340+ Min : 1 ,
341+ },
348342 },
349343 })
350344
@@ -638,21 +632,20 @@ func (suite *k8sSuite) testHPA(namespace, deployment string) {
638632 }
639633 }()
640634
641- suite .EventuallyWithTf (func (collect * assert.CollectT ) {
635+ suite .EventuallyWithTf (func (c * assert.CollectT ) {
642636 metrics , err := suite .Fakeintake .FilterMetrics (
643637 "kubernetes_state.deployment.replicas_available" ,
644638 fakeintake.WithTags [* aggregator.MetricSeries ]([]string {
645639 "kube_namespace:" + namespace ,
646640 "kube_deployment:" + deployment ,
647641 }),
648642 )
649- if err != nil {
650- collect . Errorf ( "%w" , err )
643+ // Can be replaced by require.NoErrorf(…) once https://github.com/stretchr/testify/pull/1481 is merged
644+ if ! assert . NoErrorf ( c , err , "Failed to query fake intake" ) {
651645 return
652646 }
653- if len (metrics ) == 0 {
654- collect .Errorf ("No `kubernetes_state.deployment.replicas_available{kube_namespace:%s,kube_deployment:%s}` metrics yet" , namespace , deployment )
655- sendEvent ("error" , fmt .Sprintf ("No `kubernetes_state.deployment.replicas_available{kube_namespace:%s,kube_deployment:%s}` metrics yet" , namespace , deployment ), nil )
647+ if ! assert .NotEmptyf (c , metrics , "No `kubernetes_state.deployment.replicas_available{kube_namespace:%s,kube_deployment:%s}` metrics yet" , namespace , deployment ) {
648+ sendEvent ("warning" , fmt .Sprintf ("No `kubernetes_state.deployment.replicas_available{kube_namespace:%s,kube_deployment:%s}` metrics yet" , namespace , deployment ), nil )
656649 return
657650 }
658651
@@ -669,13 +662,13 @@ func (suite *k8sSuite) testHPA(namespace, deployment string) {
669662 continue
670663 }
671664
672- if point .Value > prevValue + 0.5 {
665+ if ! scaleUp && point .Value > prevValue + 0.5 {
673666 scaleUp = true
674667 sendEvent ("success" , "Scale up detected." , pointer .Ptr (int (point .Timestamp )))
675668 if scaleDown {
676669 break out
677670 }
678- } else if point .Value < prevValue - 0.5 {
671+ } else if ! scaleDown && point .Value < prevValue - 0.5 {
679672 scaleDown = true
680673 sendEvent ("success" , "Scale down detected." , pointer .Ptr (int (point .Timestamp )))
681674 if scaleUp {
@@ -685,12 +678,8 @@ func (suite *k8sSuite) testHPA(namespace, deployment string) {
685678 prevValue = point .Value
686679 }
687680 }
688- if ! scaleUp {
689- collect .Errorf ("No scale up detected" )
690- }
691- if ! scaleDown {
692- collect .Errorf ("No scale down detected" )
693- }
681+ assert .Truef (c , scaleUp , "No scale up detected" )
682+ assert .Truef (c , scaleDown , "No scale down detected" )
694683 }, 20 * time .Minute , 10 * time .Second , "Failed to witness scale up and scale down of %s.%s" , namespace , deployment )
695684 })
696685}
0 commit comments