Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,6 @@ func TestMeshsyncBinaryWithK8sClusterIntegration(t *testing.T) {
}
}

// need this as separate function to bring down cyclomatic complexity
// this one itself is also already too complicated :)
//
// TODO fix cyclop error
// integration-tests/k8s_cluster_integration_test.go:74:1: calculated cyclomatic complexity for function runWithMeshsyncBinaryAndk8sClusterMeshsyncBinaryTestCase is 11, max is 10 (cyclop)
//
//nolint:cyclop
func runMeshsyncBinaryWithK8sClusterTestCase(
br broker.Handler,
tcIndex int,
Expand Down Expand Up @@ -103,28 +96,14 @@ func runMeshsyncBinaryWithK8sClusterTestCase(
if err := cmd.Start(); err != nil {
t.Fatalf("error starting binary: %v", err)
}
errCh := make(chan error)
go func(cmd0 *exec.Cmd, errCh0 chan<- error) {
errCh0 <- cmd0.Wait()
}(cmd, errCh)

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

select {
case err := <-errCh:
if err != nil {
t.Fatalf("error running binary: %v", err)
}
case <-time.After(timeout):
if err := cmd.Process.Signal(syscall.SIGTERM); err != nil {
t.Fatalf("error terminating meshsync command: %v", err)
}
t.Logf("processing after timeout %d", timeout)
}
waitForMeshsync(t, cmd, timeout)

// Step 4: do final assertion, if any
if tc.finalHandler != nil {
Expand All @@ -135,7 +114,7 @@ func runMeshsyncBinaryWithK8sClusterTestCase(
}
}

// introduced this function to decrease cyclomatic complexity
// introduced these below function to decrease cyclomatic complexity
func withMeshsyncBinaryPrepareMeshsyncCMD(
t *testing.T,
tcIndex int,
Expand Down Expand Up @@ -166,3 +145,28 @@ func withMeshsyncBinaryPrepareMeshsyncCMD(

return cmd, deferFunc
}

func waitForMeshsync(
t *testing.T,
cmd *exec.Cmd,
timeout time.Duration,
) {
errCh := make(chan error)

go func() {
errCh <- cmd.Wait()
}()

select {
case err := <-errCh:
if err != nil {
t.Fatalf("error running binary: %v", err)
}
case <-time.After(timeout):
if err := cmd.Process.Signal(syscall.SIGTERM); err != nil {
t.Fatalf("error terminating meshsync command: %v", err)
}
t.Logf("processing after timeout %d", timeout)

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ func TestMeshsyncLibraryWithK8sClusterCustomBrokerIntegration(t *testing.T) {
}
}

// TODO fix cyclop error
// integration-tests/k8s_cluster_meshsync_as_library_integration_test.go:47:1: calculated cyclomatic complexity for function runWithMeshsyncLibraryAndk8sClusterTestCase is 15, max is 10 (cyclop)
//
//nolint:cyclop
func runWithMeshsyncLibraryAndk8sClusterCustomBrokerTestCase(
br broker.Handler,
tcIndex int,
Expand Down Expand Up @@ -86,46 +82,15 @@ func runWithMeshsyncLibraryAndk8sClusterCustomBrokerTestCase(
}

// Step 3: run meshsync library
go func(errCh0 chan<- error) {
runOptions := make([]libmeshsync.OptionsSetter, 0, len(tc.meshsyncRunOptions))
runOptions = append(runOptions, tc.meshsyncRunOptions...)
runOptions = append(runOptions, libmeshsync.WithBrokerHandler(br))

errCh0 <- libmeshsync.Run(
log,
runOptions...,
)
}(errCh)
runMeshsyncLibraryAsync(br, log, tc, errCh)

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

select {
case err := <-errCh:
if err != nil {
if !tc.expectError {
t.Fatal("must not end with error", err)
}
assert.ErrorContains(t, err, tc.expectedErrorMessage, "must end with expected error")
} else if tc.expectError {
if tc.expectedErrorMessage != "" {
t.Fatalf("must end with expected error message %s", tc.expectedErrorMessage)
}
t.Fatalf("must end with error")
}
case <-time.After(timeout):
self, err := os.FindProcess(os.Getpid())
if err != nil {
t.Fatalf("could not find self process: %v", err)
}
if err := self.Signal(syscall.SIGTERM); err != nil {
t.Fatalf("error terminating meshsync library: %v", err)
}
t.Logf("processing after timeout %d", timeout)
}
handleLibraryCompletion(t, errCh, timeout, tc)

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

// introduced these below function to decrease cyclomatic complexity
func withMeshsyncLibraryAndk8sClusterCustomBrokerPrepareMeshsyncLoggerOptions(
t *testing.T,
tcIndex int,
Expand Down Expand Up @@ -165,3 +131,61 @@ func withMeshsyncLibraryAndk8sClusterCustomBrokerPrepareMeshsyncLoggerOptions(

return options, deferFunc
}

func runMeshsyncLibraryAsync(
br broker.Handler,
log logger.Handler,
tc meshsyncLibraryWithK8SClusterCustomBrokerTestCaseStruct,
errCh chan<- error,
) {
go func() {
runOptions := make([]libmeshsync.OptionsSetter, 0, len(tc.meshsyncRunOptions)+1)
runOptions = append(runOptions, tc.meshsyncRunOptions...)
runOptions = append(runOptions, libmeshsync.WithBrokerHandler(br))

errCh <- libmeshsync.Run(log, runOptions...)
}()
}

func handleLibraryCompletion(
t *testing.T,
errCh <-chan error,
timeout time.Duration,
tc meshsyncLibraryWithK8SClusterCustomBrokerTestCaseStruct,
) {
select {
case err := <-errCh:
validateErrorOutcome(t, err, tc)
case <-time.After(timeout):
self, findErr := os.FindProcess(os.Getpid())
if findErr != nil {
t.Fatalf("could not find self process: %v", findErr)
}
if err := self.Signal(syscall.SIGTERM); err != nil {
t.Fatalf("error terminating meshsync library: %v", err)
}
t.Logf("processing after timeout %d", timeout)
}
}

func validateErrorOutcome(
t *testing.T,
err error,
tc meshsyncLibraryWithK8SClusterCustomBrokerTestCaseStruct,
) {
if err != nil {
if !tc.expectError {
t.Fatal("must not end with error", err)
}
assert.ErrorContains(t, err, tc.expectedErrorMessage, "must end with expected error")
return
}

// err == nil
if tc.expectError {
if tc.expectedErrorMessage != "" {
t.Fatalf("must end with expected error message %s", tc.expectedErrorMessage)
}
t.Fatalf("must end with error")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ var meshsyncLibraryWithK8SClusterCustomBrokerTestCaseData []meshsyncLibraryWithK
{
name: "output mode channel: number of messages received from meshsync is greater than zero",
meshsyncRunOptions: []libmeshsync.OptionsSetter{
// adding nil to options to test that meshsync can handle nil options setter
// must not fail when has nil in options setter
nil,
libmeshsync.WithOutputMode(config.OutputModeBroker),
libmeshsync.WithBrokerHandler(channel.NewChannelBrokerHandler()),
libmeshsync.WithStopAfterDuration(8 * time.Second),
Expand Down Expand Up @@ -50,18 +53,6 @@ var meshsyncLibraryWithK8SClusterCustomBrokerTestCaseData []meshsyncLibraryWithK
},
},
// TODO
// remove this as a separate test case,
// add nil to []libmeshsync.OptionsSetter in the previous test case with a comment
{
name: "output mode channel: must not fail when has nil in options setter",
meshsyncRunOptions: []libmeshsync.OptionsSetter{
nil,
libmeshsync.WithOutputMode(config.OutputModeBroker),
libmeshsync.WithBrokerHandler(channel.NewChannelBrokerHandler()),
libmeshsync.WithStopAfterDuration(0 * time.Second),
},
},
// TODO
// this is not an output mode test
// we do not need to run libmeshsync.Run, as we only test call to GetClusterID
// we still need k8s cluster in place;
Expand Down
Loading