github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow@v0.28.1-0.20240311201729-34c6856b157f/pkg/command/quarkus/deploy_test.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one
     3   * or more contributor license agreements.  See the NOTICE file
     4   * distributed with this work for additional information
     5   * regarding copyright ownership.  The ASF licenses this file
     6   * to you under the Apache License, Version 2.0 (the
     7   * "License"); you may not use this file except in compliance
     8   * with the License.  You may obtain a copy of the License at
     9   *
    10   *  http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing,
    13   * software distributed under the License is distributed on an
    14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   * KIND, either express or implied.  See the License for the
    16   * specific language governing permissions and limitations
    17   * under the License.
    18   */
    19  
    20  package quarkus
    21  
    22  import (
    23  	"fmt"
    24  	"os"
    25  	"os/exec"
    26  	"strconv"
    27  	"testing"
    28  
    29  	"github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/common"
    30  	"github.com/spf13/afero"
    31  )
    32  
    33  type testDeploy struct {
    34  	input      DeployCmdConfig
    35  	expected   bool
    36  	createFile string
    37  }
    38  
    39  const defaultPath = "./target/kubernetes"
    40  
    41  var testRunDeploy = []testDeploy{
    42  	{input: DeployCmdConfig{Path: defaultPath}, expected: true, createFile: "kogito.yml"},
    43  	{input: DeployCmdConfig{Path: "./different/folders"}, expected: true, createFile: "kogito.yml"},
    44  	{input: DeployCmdConfig{Path: "different/folders"}, expected: true, createFile: "kogito.yml"},
    45  	{input: DeployCmdConfig{}, expected: false, createFile: "test"},
    46  	{input: DeployCmdConfig{}, expected: false},
    47  }
    48  
    49  func fakeRunDeploy(testIndex int) func(command string, args ...string) *exec.Cmd {
    50  	return func(command string, args ...string) *exec.Cmd {
    51  		cs := []string{"-test.run=TestHelperRunDeploy", "--", command}
    52  		cs = append(cs, args...)
    53  		cmd := exec.Command(os.Args[0], cs...)
    54  		cmd.Env = []string{fmt.Sprintf("GO_TEST_HELPER_RUN_DEPLOY_IMAGE=%d", testIndex)}
    55  		return cmd
    56  	}
    57  }
    58  
    59  func TestHelperRunDeploy(t *testing.T) {
    60  	testIndex, err := strconv.Atoi(os.Getenv("GO_TEST_HELPER_RUN_DEPLOY_IMAGE"))
    61  	if err != nil {
    62  		return
    63  	}
    64  	out := []string{"Test", strconv.Itoa(testIndex)}
    65  	if testRunDeploy[testIndex].createFile != "" {
    66  		out = append(out, "with creating", testRunDeploy[testIndex].createFile, "file")
    67  	}
    68  	fmt.Fprintf(os.Stdout, "%v", out)
    69  	os.Exit(0)
    70  }
    71  
    72  func TestRunDeploy(t *testing.T) {
    73  	common.FS = afero.NewMemMapFs()
    74  	for testIndex, test := range testRunDeploy {
    75  		common.ExecCommand = fakeRunDeploy(testIndex)
    76  		defer func() { common.ExecCommand = exec.Command }()
    77  
    78  		if test.createFile != "" {
    79  			if test.input.Path == "" {
    80  				test.input.Path = defaultPath
    81  			}
    82  			common.CreateFolderStructure(t, test.input.Path)
    83  			common.CreateFileInFolderStructure(t, test.input.Path, test.createFile)
    84  		}
    85  
    86  		out, err := deployKnativeServiceAndEventingBindings(test.input)
    87  		if err != nil {
    88  			t.Errorf("Expected nil error, got %#v", err)
    89  		}
    90  
    91  		if out != test.expected {
    92  			t.Errorf("Expected %v, got %v", test.expected, out)
    93  		}
    94  
    95  		if test.createFile != "" {
    96  			common.DeleteFolderStructure(t, test.input.Path)
    97  		}
    98  	}
    99  }