github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow@v0.28.1-0.20240311201729-34c6856b157f/e2e-tests/quarkus_run_test.go (about)

     1  //go:build e2e_tests
     2  
     3  /*
     4   * Licensed to the Apache Software Foundation (ASF) under one
     5   * or more contributor license agreements.  See the NOTICE file
     6   * distributed with this work for additional information
     7   * regarding copyright ownership.  The ASF licenses this file
     8   * to you under the Apache License, Version 2.0 (the
     9   * "License"); you may not use this file except in compliance
    10   * with the License.  You may obtain a copy of the License at
    11   *
    12   *  http://www.apache.org/licenses/LICENSE-2.0
    13   *
    14   * Unless required by applicable law or agreed to in writing,
    15   * software distributed under the License is distributed on an
    16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    17   * KIND, either express or implied.  See the License for the
    18   * specific language governing permissions and limitations
    19   * under the License.
    20   */
    21  
    22  package e2e_tests
    23  
    24  import (
    25  	"fmt"
    26  	"os"
    27  	"os/exec"
    28  	"path/filepath"
    29  	"sync"
    30  	"testing"
    31  	"time"
    32  
    33  	"github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/command"
    34  	"github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/command/quarkus"
    35  	"github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/common"
    36  	"github.com/stretchr/testify/require"
    37  )
    38  
    39  var cfgTestInputPrepareQuarkusCreateRun = CfgTestInputQuarkusCreate{
    40  	input: quarkus.CreateQuarkusProjectConfig{ProjectName: "new-project"},
    41  }
    42  
    43  var cfgTestInputPrepareQuarkusBuildRun = CfgTestInputQuarkusBuild{
    44  	input: quarkus.BuildCmdConfig{
    45  		Image: "dev.local/new-project",
    46  	},
    47  }
    48  
    49  type cfgTestInputQuarkusRun struct {
    50  	input quarkus.RunCmdConfig
    51  }
    52  
    53  var cfgTestInputQuarkusRun_Success = []cfgTestInputQuarkusRun{
    54  	{input: quarkus.RunCmdConfig{PortMapping: "8081", OpenDevUI: false}},
    55  	{input: quarkus.RunCmdConfig{OpenDevUI: true}},
    56  }
    57  
    58  func transformQuarkusRunCmdCfgToArgs(cfg quarkus.RunCmdConfig) []string {
    59  	args := []string{"run"}
    60  	if !cfg.OpenDevUI {
    61  		args = append(args, "--open-dev-ui=false")
    62  	}
    63  	if cfg.PortMapping != "" {
    64  		args = append(args, "--port", cfg.PortMapping)
    65  	}
    66  	return args
    67  }
    68  
    69  func getRunQuarkusProjectPort(t *testing.T, config cfgTestInputQuarkusRun) string {
    70  	if config.input.PortMapping != "" {
    71  		return config.input.PortMapping
    72  	} else {
    73  		projectDefaultPort, err := LookupFlagDefaultValue("port", command.NewRunCommand())
    74  		require.NoErrorf(t, err, "Error: %v", err)
    75  		return projectDefaultPort
    76  	}
    77  }
    78  
    79  func TestQuarkusRunCommand(t *testing.T) {
    80  	for testIndex, test := range cfgTestInputQuarkusRun_Success {
    81  		t.Run(fmt.Sprintf("Test quarkus run project success index: %d", testIndex), func(t *testing.T) {
    82  			defer CleanUpAndChdirTemp(t)
    83  			RunQuarkusRunTest(t, cfgTestInputPrepareQuarkusCreateRun, cfgTestInputPrepareQuarkusBuildRun, test)
    84  		})
    85  	}
    86  }
    87  
    88  func RunQuarkusRunTest(t *testing.T, cfgTestInputPrepareQuarkusCreateRun CfgTestInputQuarkusCreate, cfgTestInputPrepareQuarkusBuild CfgTestInputQuarkusBuild, test cfgTestInputQuarkusRun) string {
    89  	var err error
    90  
    91  	// Create and build the quarkus project
    92  	projectName := RunQuarkusCreateTest(t, cfgTestInputPrepareQuarkusCreateRun)
    93  	projectDir := filepath.Join(TempTestsPath, projectName)
    94  	err = os.Chdir(projectDir)
    95  	require.NoErrorf(t, err, "Expected nil error, got %v", err)
    96  
    97  	cmd := exec.Command(KnExecutable)
    98  
    99  	var wg sync.WaitGroup
   100  	wg.Add(1)
   101  
   102  	// Run the `quarkus run` command
   103  	go func() {
   104  		defer wg.Done()
   105  		_, err = ExecuteKnWorkflowQuarkusWithCmd(cmd, transformQuarkusRunCmdCfgToArgs(test.input)...)
   106  		require.Truef(t, err == nil || IsSignalInterrupt(err), "Expected nil error or signal interrupt, got %v", err)
   107  	}()
   108  
   109  	// Check if the project is successfully run and accessible within a specified time limit.
   110  	readyCheckURL := fmt.Sprintf("http://localhost:%s/q/health/ready", getRunQuarkusProjectPort(t, test))
   111  	pollInterval := 5 * time.Second
   112  	timeout := 4 * time.Minute
   113  	ready := make(chan bool)
   114  	t.Logf("Checking if project is ready at %s", readyCheckURL)
   115  	go common.PollReadyCheckURL(readyCheckURL, pollInterval, ready)
   116  	select {
   117  	case <-ready:
   118  		cmd.Process.Signal(os.Interrupt)
   119  	case <-time.After(timeout):
   120  		t.Fatalf("Test case timed out after %s. The project was not ready within the specified time.", timeout)
   121  		cmd.Process.Signal(os.Interrupt)
   122  	}
   123  
   124  	wg.Wait()
   125  
   126  	return projectName
   127  }