Skip to content

Commit 8173672

Browse files
authored
Merge pull request #487 from CodexRaunak/cleanup-integration-tests
Reduce cyclomatic complexity and clean up in integration tests
2 parents e34c563 + 0abd562 commit 8173672

File tree

4 files changed

+161
-142
lines changed

4 files changed

+161
-142
lines changed

integration-tests/meshsync_as_binary_with_k8s_cluster_integration_test.go

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,6 @@ func TestMeshsyncBinaryWithK8sClusterIntegration(t *testing.T) {
5555
}
5656
}
5757

58-
// need this as separate function to bring down cyclomatic complexity
59-
// this one itself is also already too complicated :)
60-
//
61-
// TODO fix cyclop error
62-
// integration-tests/k8s_cluster_integration_test.go:74:1: calculated cyclomatic complexity for function runWithMeshsyncBinaryAndk8sClusterMeshsyncBinaryTestCase is 11, max is 10 (cyclop)
63-
//
64-
//nolint:cyclop
6558
func runMeshsyncBinaryWithK8sClusterTestCase(
6659
br broker.Handler,
6760
tcIndex int,
@@ -103,28 +96,14 @@ func runMeshsyncBinaryWithK8sClusterTestCase(
10396
if err := cmd.Start(); err != nil {
10497
t.Fatalf("error starting binary: %v", err)
10598
}
106-
errCh := make(chan error)
107-
go func(cmd0 *exec.Cmd, errCh0 chan<- error) {
108-
errCh0 <- cmd0.Wait()
109-
}(cmd, errCh)
11099

111100
// intentionally big timeout to wait till the cmd execution ended
112101
timeout := time.Duration(time.Hour * 24)
113102
if tc.waitMeshsyncTimeout > 0 {
114103
timeout = tc.waitMeshsyncTimeout
115104
}
116105

117-
select {
118-
case err := <-errCh:
119-
if err != nil {
120-
t.Fatalf("error running binary: %v", err)
121-
}
122-
case <-time.After(timeout):
123-
if err := cmd.Process.Signal(syscall.SIGTERM); err != nil {
124-
t.Fatalf("error terminating meshsync command: %v", err)
125-
}
126-
t.Logf("processing after timeout %d", timeout)
127-
}
106+
waitForMeshsync(t, cmd, timeout)
128107

129108
// Step 4: do final assertion, if any
130109
if tc.finalHandler != nil {
@@ -135,7 +114,7 @@ func runMeshsyncBinaryWithK8sClusterTestCase(
135114
}
136115
}
137116

138-
// introduced this function to decrease cyclomatic complexity
117+
// introduced these below function to decrease cyclomatic complexity
139118
func withMeshsyncBinaryPrepareMeshsyncCMD(
140119
t *testing.T,
141120
tcIndex int,
@@ -166,3 +145,28 @@ func withMeshsyncBinaryPrepareMeshsyncCMD(
166145

167146
return cmd, deferFunc
168147
}
148+
149+
func waitForMeshsync(
150+
t *testing.T,
151+
cmd *exec.Cmd,
152+
timeout time.Duration,
153+
) {
154+
errCh := make(chan error)
155+
156+
go func() {
157+
errCh <- cmd.Wait()
158+
}()
159+
160+
select {
161+
case err := <-errCh:
162+
if err != nil {
163+
t.Fatalf("error running binary: %v", err)
164+
}
165+
case <-time.After(timeout):
166+
if err := cmd.Process.Signal(syscall.SIGTERM); err != nil {
167+
t.Fatalf("error terminating meshsync command: %v", err)
168+
}
169+
t.Logf("processing after timeout %d", timeout)
170+
171+
}
172+
}

integration-tests/meshsync_as_library_with_k8s_cluster_custom_broker_integration_test.go

Lines changed: 61 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ func TestMeshsyncLibraryWithK8sClusterCustomBrokerIntegration(t *testing.T) {
3535
}
3636
}
3737

38-
// TODO fix cyclop error
39-
// integration-tests/k8s_cluster_meshsync_as_library_integration_test.go:47:1: calculated cyclomatic complexity for function runWithMeshsyncLibraryAndk8sClusterTestCase is 15, max is 10 (cyclop)
40-
//
41-
//nolint:cyclop
4238
func runWithMeshsyncLibraryAndk8sClusterCustomBrokerTestCase(
4339
br broker.Handler,
4440
tcIndex int,
@@ -86,46 +82,15 @@ func runWithMeshsyncLibraryAndk8sClusterCustomBrokerTestCase(
8682
}
8783

8884
// Step 3: run meshsync library
89-
go func(errCh0 chan<- error) {
90-
runOptions := make([]libmeshsync.OptionsSetter, 0, len(tc.meshsyncRunOptions))
91-
runOptions = append(runOptions, tc.meshsyncRunOptions...)
92-
runOptions = append(runOptions, libmeshsync.WithBrokerHandler(br))
93-
94-
errCh0 <- libmeshsync.Run(
95-
log,
96-
runOptions...,
97-
)
98-
}(errCh)
85+
runMeshsyncLibraryAsync(br, log, tc, errCh)
9986

10087
// intentionally big timeout to wait till the run execution ended
10188
timeout := time.Duration(time.Hour * 24)
10289
if tc.waitMeshsyncTimeout > 0 {
10390
timeout = tc.waitMeshsyncTimeout
10491
}
10592

106-
select {
107-
case err := <-errCh:
108-
if err != nil {
109-
if !tc.expectError {
110-
t.Fatal("must not end with error", err)
111-
}
112-
assert.ErrorContains(t, err, tc.expectedErrorMessage, "must end with expected error")
113-
} else if tc.expectError {
114-
if tc.expectedErrorMessage != "" {
115-
t.Fatalf("must end with expected error message %s", tc.expectedErrorMessage)
116-
}
117-
t.Fatalf("must end with error")
118-
}
119-
case <-time.After(timeout):
120-
self, err := os.FindProcess(os.Getpid())
121-
if err != nil {
122-
t.Fatalf("could not find self process: %v", err)
123-
}
124-
if err := self.Signal(syscall.SIGTERM); err != nil {
125-
t.Fatalf("error terminating meshsync library: %v", err)
126-
}
127-
t.Logf("processing after timeout %d", timeout)
128-
}
93+
handleLibraryCompletion(t, errCh, timeout, tc)
12994

13095
// Step 4: do final assertion, if any
13196
if tc.finalHandler != nil {
@@ -136,6 +101,7 @@ func runWithMeshsyncLibraryAndk8sClusterCustomBrokerTestCase(
136101
}
137102
}
138103

104+
// introduced these below function to decrease cyclomatic complexity
139105
func withMeshsyncLibraryAndk8sClusterCustomBrokerPrepareMeshsyncLoggerOptions(
140106
t *testing.T,
141107
tcIndex int,
@@ -165,3 +131,61 @@ func withMeshsyncLibraryAndk8sClusterCustomBrokerPrepareMeshsyncLoggerOptions(
165131

166132
return options, deferFunc
167133
}
134+
135+
func runMeshsyncLibraryAsync(
136+
br broker.Handler,
137+
log logger.Handler,
138+
tc meshsyncLibraryWithK8SClusterCustomBrokerTestCaseStruct,
139+
errCh chan<- error,
140+
) {
141+
go func() {
142+
runOptions := make([]libmeshsync.OptionsSetter, 0, len(tc.meshsyncRunOptions)+1)
143+
runOptions = append(runOptions, tc.meshsyncRunOptions...)
144+
runOptions = append(runOptions, libmeshsync.WithBrokerHandler(br))
145+
146+
errCh <- libmeshsync.Run(log, runOptions...)
147+
}()
148+
}
149+
150+
func handleLibraryCompletion(
151+
t *testing.T,
152+
errCh <-chan error,
153+
timeout time.Duration,
154+
tc meshsyncLibraryWithK8SClusterCustomBrokerTestCaseStruct,
155+
) {
156+
select {
157+
case err := <-errCh:
158+
validateErrorOutcome(t, err, tc)
159+
case <-time.After(timeout):
160+
self, findErr := os.FindProcess(os.Getpid())
161+
if findErr != nil {
162+
t.Fatalf("could not find self process: %v", findErr)
163+
}
164+
if err := self.Signal(syscall.SIGTERM); err != nil {
165+
t.Fatalf("error terminating meshsync library: %v", err)
166+
}
167+
t.Logf("processing after timeout %d", timeout)
168+
}
169+
}
170+
171+
func validateErrorOutcome(
172+
t *testing.T,
173+
err error,
174+
tc meshsyncLibraryWithK8SClusterCustomBrokerTestCaseStruct,
175+
) {
176+
if err != nil {
177+
if !tc.expectError {
178+
t.Fatal("must not end with error", err)
179+
}
180+
assert.ErrorContains(t, err, tc.expectedErrorMessage, "must end with expected error")
181+
return
182+
}
183+
184+
// err == nil
185+
if tc.expectError {
186+
if tc.expectedErrorMessage != "" {
187+
t.Fatalf("must end with expected error message %s", tc.expectedErrorMessage)
188+
}
189+
t.Fatalf("must end with error")
190+
}
191+
}

integration-tests/meshsync_as_library_with_k8s_cluster_custom_broker_test_cases_test.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ var meshsyncLibraryWithK8SClusterCustomBrokerTestCaseData []meshsyncLibraryWithK
2121
{
2222
name: "output mode channel: number of messages received from meshsync is greater than zero",
2323
meshsyncRunOptions: []libmeshsync.OptionsSetter{
24+
// adding nil to options to test that meshsync can handle nil options setter
25+
// must not fail when has nil in options setter
26+
nil,
2427
libmeshsync.WithOutputMode(config.OutputModeBroker),
2528
libmeshsync.WithBrokerHandler(channel.NewChannelBrokerHandler()),
2629
libmeshsync.WithStopAfterDuration(8 * time.Second),
@@ -50,18 +53,6 @@ var meshsyncLibraryWithK8SClusterCustomBrokerTestCaseData []meshsyncLibraryWithK
5053
},
5154
},
5255
// TODO
53-
// remove this as a separate test case,
54-
// add nil to []libmeshsync.OptionsSetter in the previous test case with a comment
55-
{
56-
name: "output mode channel: must not fail when has nil in options setter",
57-
meshsyncRunOptions: []libmeshsync.OptionsSetter{
58-
nil,
59-
libmeshsync.WithOutputMode(config.OutputModeBroker),
60-
libmeshsync.WithBrokerHandler(channel.NewChannelBrokerHandler()),
61-
libmeshsync.WithStopAfterDuration(0 * time.Second),
62-
},
63-
},
64-
// TODO
6556
// this is not an output mode test
6657
// we do not need to run libmeshsync.Run, as we only test call to GetClusterID
6758
// we still need k8s cluster in place;

0 commit comments

Comments
 (0)