@@ -2,14 +2,24 @@ package acceptance_test
2
2
3
3
import (
4
4
"context"
5
+ "encoding/json"
5
6
"fmt"
7
+ "io"
8
+ "os"
9
+ "strconv"
6
10
"testing"
11
+ "time"
7
12
13
+ awssdk "github.com/aws/aws-sdk-go/aws"
8
14
"github.com/google/uuid"
9
15
. "github.com/onsi/ginkgo/v2"
10
16
. "github.com/onsi/gomega"
17
+
11
18
corev1 "k8s.io/api/core/v1"
12
- "k8s.io/client-go/kubernetes/scheme"
19
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
20
+ "k8s.io/client-go/kubernetes"
21
+ "k8s.io/client-go/tools/clientcmd"
22
+ "k8s.io/kubectl/pkg/scheme"
13
23
capa "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
14
24
capi "sigs.k8s.io/cluster-api/api/v1beta1"
15
25
controllerruntime "sigs.k8s.io/controller-runtime"
@@ -19,6 +29,12 @@ import (
19
29
"github.com/aws-resolver-rules-operator/tests/acceptance/fixture"
20
30
)
21
31
32
+ const (
33
+ DefaultMaxLogLines = 300
34
+ DefaultPollingInterval = 5 * time .Second
35
+ DefaultTimeout = 3 * time .Minute
36
+ )
37
+
22
38
var (
23
39
k8sClient client.Client
24
40
@@ -28,50 +44,175 @@ var (
28
44
testFixture * fixture.Fixture
29
45
)
30
46
47
+ func LogCollectorFailHandler (message string , callerSkip ... int ) {
48
+ getPodLogs ()
49
+ Fail (message , callerSkip ... )
50
+ }
51
+
31
52
func TestAcceptance (t * testing.T ) {
32
- RegisterFailHandler (Fail )
53
+ RegisterFailHandler (LogCollectorFailHandler )
33
54
RunSpecs (t , "Acceptance Suite" )
34
55
}
35
56
36
- var _ = BeforeSuite (func () {
37
- tests .GetEnvOrSkip ("KUBECONFIG" )
57
+ var _ = SynchronizedBeforeSuite (func () []byte {
58
+ setupEventuallyParameters ()
59
+ k8sClient = setupKubeClient ()
38
60
39
- config , err := controllerruntime .GetConfig ()
61
+ fixtureConfig := getFixtureConfig ()
62
+ testFixture = fixture .NewFixture (k8sClient , fixtureConfig )
63
+
64
+ DeferCleanup (func () {
65
+ Expect (testFixture .Teardown ()).To (Succeed ())
66
+ })
67
+ err := testFixture .Setup ()
40
68
Expect (err ).NotTo (HaveOccurred ())
41
69
42
- err = capa .AddToScheme (scheme .Scheme )
70
+ data := fixture.Data {
71
+ Config : fixtureConfig ,
72
+ Network : testFixture .Network ,
73
+ }
74
+ encoded , err := json .Marshal (data )
43
75
Expect (err ).NotTo (HaveOccurred ())
44
76
45
- err = capi .AddToScheme (scheme .Scheme )
77
+ return encoded
78
+ }, func (jsonData []byte ) {
79
+ data := fixture.Data {}
80
+ err := json .Unmarshal (jsonData , & data )
46
81
Expect (err ).NotTo (HaveOccurred ())
47
82
48
- k8sClient , err = client .New (config , client.Options {Scheme : scheme .Scheme })
83
+ setupEventuallyParameters ()
84
+ k8sClient = setupKubeClient ()
85
+
86
+ testFixture = fixture .LoadFixture (k8sClient , data )
87
+ })
88
+
89
+ var _ = BeforeEach (func () {
90
+ namespace = uuid .New ().String ()
91
+ namespaceObj = & corev1.Namespace {}
92
+ namespaceObj .Name = namespace
93
+ Expect (k8sClient .Create (context .Background (), namespaceObj )).To (Succeed ())
94
+ })
95
+
96
+ var _ = AfterEach (func () {
97
+ Expect (k8sClient .Delete (context .Background (), namespaceObj )).To (Succeed ())
98
+ })
99
+
100
+ func getDurationFromEnvVar (env string , defaultDuration time.Duration ) time.Duration {
101
+ stringDuration := os .Getenv (env )
102
+ if stringDuration == "" {
103
+ return defaultDuration
104
+ }
105
+
106
+ duration , err := time .ParseDuration (stringDuration )
107
+ Expect (err ).NotTo (HaveOccurred ())
108
+
109
+ return duration
110
+ }
111
+
112
+ func getMaxLogLines () int64 {
113
+ maxLogLines := os .Getenv ("MAX_LOG_LINES" )
114
+ if maxLogLines == "" {
115
+ return DefaultMaxLogLines
116
+ }
117
+
118
+ maxLogLinesInt , err := strconv .Atoi (maxLogLines )
49
119
Expect (err ).NotTo (HaveOccurred ())
50
120
121
+ return int64 (maxLogLinesInt )
122
+ }
123
+
124
+ func getPodLogs () {
125
+ kubeConfigPath := tests .GetEnvOrSkip ("KUBECONFIG" )
126
+ maxLogLines := getMaxLogLines ()
127
+ config , err := clientcmd .BuildConfigFromFlags ("" , kubeConfigPath )
128
+ if err != nil {
129
+ fmt .Fprintf (GinkgoWriter , "Failed to build client config: %v" , err )
130
+ return
131
+ }
132
+
133
+ clientset , err := kubernetes .NewForConfig (config )
134
+ if err != nil {
135
+ fmt .Fprintf (GinkgoWriter , "Failed to build client: %v" , err )
136
+ return
137
+ }
138
+
139
+ ctx := context .Background ()
140
+ podsClient := clientset .CoreV1 ().Pods ("giantswarm" )
141
+ pods , err := podsClient .List (ctx , metav1.ListOptions {
142
+ LabelSelector : "app.kubernetes.io/name=aws-resolver-rules-operator" ,
143
+ })
144
+ if err != nil {
145
+ fmt .Fprintf (GinkgoWriter , "Failed to list pods: %v" , err )
146
+ return
147
+ }
148
+
149
+ if len (pods .Items ) == 0 {
150
+ fmt .Fprintf (GinkgoWriter , "No pods found: %v" , err )
151
+ return
152
+ }
153
+ pod := pods .Items [0 ]
154
+
155
+ logOptions := corev1.PodLogOptions {TailLines : awssdk .Int64 (maxLogLines )}
156
+ req := podsClient .GetLogs (pod .Name , & logOptions )
157
+ logStream , err := req .Stream (context .Background ())
158
+ if err != nil {
159
+ fmt .Fprintf (GinkgoWriter , "Failed to get log stream: %v" , err )
160
+ return
161
+ }
162
+ defer logStream .Close ()
163
+
164
+ logBytes , err := io .ReadAll (logStream )
165
+ if err != nil {
166
+ fmt .Fprintf (GinkgoWriter , "Failed to read logs: %v" , err )
167
+ return
168
+ }
169
+
170
+ fmt .Fprintf (GinkgoWriter ,
171
+ "\n \n ---------------- Last %d log lines for %q pod ----------------\n %s\n --------------------------------\n \n " ,
172
+ maxLogLines ,
173
+ pod .Name ,
174
+ string (logBytes ))
175
+ }
176
+
177
+ func getFixtureConfig () fixture.Config {
51
178
awsAccount := tests .GetEnvOrSkip ("MC_AWS_ACCOUNT" )
52
179
iamRoleID := tests .GetEnvOrSkip ("AWS_IAM_ROLE_ID" )
53
180
awsRegion := tests .GetEnvOrSkip ("AWS_REGION" )
54
181
managementClusterName := tests .GetEnvOrSkip ("MANAGEMENT_CLUSTER_NAME" )
55
182
managementClusterNamespace := tests .GetEnvOrSkip ("MANAGEMENT_CLUSTER_NAMESPACE" )
56
183
roleARN := fmt .Sprintf ("arn:aws:iam::%s:role/%s" , awsAccount , iamRoleID )
57
184
58
- fixtureConfig := fixture.Config {
185
+ return fixture.Config {
59
186
AWSAccount : awsAccount ,
60
187
AWSIAMRoleARN : roleARN ,
61
188
AWSRegion : awsRegion ,
62
189
ManagementClusterName : managementClusterName ,
63
190
ManagementClusterNamespace : managementClusterNamespace ,
64
191
}
65
- testFixture = fixture .NewFixture (k8sClient , fixtureConfig )
66
- })
192
+ }
67
193
68
- var _ = BeforeEach (func () {
69
- namespace = uuid .New ().String ()
70
- namespaceObj = & corev1.Namespace {}
71
- namespaceObj .Name = namespace
72
- Expect (k8sClient .Create (context .Background (), namespaceObj )).To (Succeed ())
73
- })
194
+ func setupEventuallyParameters () {
195
+ pollingInterval := getDurationFromEnvVar ("EVENTUALLY_POLLING_INTERVAL" , DefaultPollingInterval )
196
+ timeout := getDurationFromEnvVar ("EVENTUALLY_TIMEOUT" , DefaultTimeout )
74
197
75
- var _ = AfterEach (func () {
76
- Expect (k8sClient .Delete (context .Background (), namespaceObj )).To (Succeed ())
77
- })
198
+ SetDefaultEventuallyPollingInterval (pollingInterval )
199
+ SetDefaultEventuallyTimeout (timeout )
200
+ }
201
+
202
+ func setupKubeClient () client.Client {
203
+ tests .GetEnvOrSkip ("KUBECONFIG" )
204
+
205
+ config , err := controllerruntime .GetConfig ()
206
+ Expect (err ).NotTo (HaveOccurred ())
207
+
208
+ err = capa .AddToScheme (scheme .Scheme )
209
+ Expect (err ).NotTo (HaveOccurred ())
210
+
211
+ err = capi .AddToScheme (scheme .Scheme )
212
+ Expect (err ).NotTo (HaveOccurred ())
213
+
214
+ k8sClient , err = client .New (config , client.Options {Scheme : scheme .Scheme })
215
+ Expect (err ).NotTo (HaveOccurred ())
216
+
217
+ return k8sClient
218
+ }
0 commit comments