github.com/kiegroup/kie-tools/packages/kn-plugin-workflow@v0.32.0/it-tests/run_test.go (about) 1 //go:build it_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 it_tests 23 24 import ( 25 "fmt" 26 "os" 27 "os/exec" 28 "path/filepath" 29 "sync" 30 "testing" 31 "time" 32 33 "github.com/kiegroup/kie-tools/packages/kn-plugin-workflow/pkg/command" 34 "github.com/kiegroup/kie-tools/packages/kn-plugin-workflow/pkg/common" 35 "github.com/stretchr/testify/require" 36 ) 37 38 var cfgTestInputPrepareCreateRun = CfgTestInputCreate{ 39 input: command.CreateCmdConfig{ProjectName: "new-project"}, 40 } 41 42 type cfgTestInputRun struct { 43 input command.RunCmdConfig 44 } 45 46 var cfgTestInputRun_Success = []cfgTestInputRun{ 47 {input: command.RunCmdConfig{PortMapping: "8081", OpenDevUI: false}}, 48 {input: command.RunCmdConfig{}}, 49 } 50 51 func transformRunCmdCfgToArgs(cfg command.RunCmdConfig) []string { 52 args := []string{"run"} 53 if !cfg.OpenDevUI { 54 args = append(args, "--open-dev-ui=false") 55 } 56 if cfg.PortMapping != "" { 57 args = append(args, "--port", cfg.PortMapping) 58 } 59 return args 60 } 61 62 func getRunProjectPort(t *testing.T, config cfgTestInputRun) string { 63 if config.input.PortMapping != "" { 64 return config.input.PortMapping 65 } else { 66 projectDefaultPort, err := LookupFlagDefaultValue("port", command.NewRunCommand()) 67 require.NoErrorf(t, err, "Error: %v", err) 68 return projectDefaultPort 69 } 70 } 71 72 func TestRunCommand(t *testing.T) { 73 for testIndex, test := range cfgTestInputRun_Success { 74 t.Run(fmt.Sprintf("Test run project success index: %d", testIndex), func(t *testing.T) { 75 defer CleanUpAndChdirTemp(t) 76 RunRunTest(t, cfgTestInputPrepareCreateRun, test) 77 }) 78 } 79 } 80 81 func RunRunTest(t *testing.T, cfgTestInputPrepareCreate CfgTestInputCreate, test cfgTestInputRun) string { 82 var err error 83 84 // Create the project 85 RunCreateTest(t, cfgTestInputPrepareCreate) 86 87 projectName := GetCreateProjectName(t, cfgTestInputPrepareCreateRun) 88 projectDir := filepath.Join(TempTestsPath, projectName) 89 err = os.Chdir(projectDir) 90 require.NoErrorf(t, err, "Expected nil error, got %v", err) 91 92 cmd := exec.Command(KnExecutable) 93 94 var wg sync.WaitGroup 95 wg.Add(1) 96 97 // Run the `run` command 98 go func() { 99 defer wg.Done() 100 _, err = ExecuteKnWorkflowWithCmd(cmd, transformRunCmdCfgToArgs(test.input)...) 101 require.Truef(t, err == nil || IsSignalInterrupt(err), "Expected nil error or signal interrupt, got %v", err) 102 }() 103 104 // Check if the project is successfully run and accessible within a specified time limit. 105 readyCheckURL := fmt.Sprintf("http://localhost:%s/q/health/ready", getRunProjectPort(t, test)) 106 pollInterval := 5 * time.Second 107 timeout := 2 * time.Minute 108 ready := make(chan bool) 109 t.Logf("Checking if project is ready at %s", readyCheckURL) 110 go common.PollReadyCheckURL(readyCheckURL, pollInterval, ready) 111 select { 112 case <-ready: 113 cmd.Process.Signal(os.Interrupt) 114 case <-time.After(timeout): 115 t.Fatalf("Test case timed out after %s. The project was not ready within the specified time.", timeout) 116 cmd.Process.Signal(os.Interrupt) 117 } 118 119 wg.Wait() 120 121 return projectName 122 }