github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow@v0.28.1-0.20240311201729-34c6856b157f/pkg/command/quarkus/build_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  )
    31  
    32  type testBuildImage struct {
    33  	input    BuildCmdConfig
    34  	expected string
    35  }
    36  
    37  var testsRunBuildImageSuccess = []testBuildImage{
    38  	{input: BuildCmdConfig{Image: "test", Jib: true}, expected: "test:latest"},
    39  	{input: BuildCmdConfig{Image: "docker.io/test:latest", JibPodman: true}, expected: "docker.io/test:latest"},
    40  	{input: BuildCmdConfig{Image: "docker.io/repo/test:latest", Push: true}, expected: "docker.io/repo/test:latest"},
    41  	{input: BuildCmdConfig{Image: "quay.io/repo/test:0.0.0", Test: true}, expected: "quay.io/repo/test:0.0.0"},
    42  
    43  	{input: BuildCmdConfig{Image: "test", ImageName: "abcd"}, expected: "abcd:latest"},
    44  	{input: BuildCmdConfig{Image: "docker.io/test", Registry: "quay.io"}, expected: "quay.io/test:latest"},
    45  	{input: BuildCmdConfig{Image: "test", Repository: "myuser"}, expected: "myuser/test:latest"},
    46  	{input: BuildCmdConfig{Image: "quay.io/test", Repository: "myuser"}, expected: "quay.io/myuser/test:latest"},
    47  	{input: BuildCmdConfig{Image: "test:0.0.0", Tag: "0.0.1"}, expected: "test:0.0.1"},
    48  }
    49  
    50  var testsRunBuildImageFail = []testBuildImage{
    51  	{input: BuildCmdConfig{Image: ""}, expected: ""},
    52  	{input: BuildCmdConfig{Image: "1"}, expected: ""},
    53  	{input: BuildCmdConfig{Image: "-test"}, expected: ""},
    54  	{input: BuildCmdConfig{Image: "test.abc"}, expected: ""},
    55  	{input: BuildCmdConfig{Image: "myuser/1"}, expected: ""},
    56  	{input: BuildCmdConfig{Image: "test", ImageName: "1"}, expected: ""},
    57  }
    58  
    59  func fakeRunBuildImage(testIndex int) func(command string, args ...string) *exec.Cmd {
    60  	return func(command string, args ...string) *exec.Cmd {
    61  		cs := []string{"-test.run=TestHelperRunBuildImage", "--", command}
    62  		cs = append(cs, args...)
    63  		cmd := exec.Command(os.Args[0], cs...)
    64  		cmd.Env = []string{fmt.Sprintf("GO_TEST_HELPER_RUN_BUILDER_IMAGE=%d", testIndex)}
    65  		return cmd
    66  	}
    67  }
    68  
    69  func TestHelperRunBuildImage(t *testing.T) {
    70  	testIndex, err := strconv.Atoi(os.Getenv("GO_TEST_HELPER_RUN_BUILDER_IMAGE"))
    71  	if err != nil {
    72  		return
    73  	}
    74  	fmt.Fprintf(os.Stdout, testsRunBuildImageSuccess[testIndex].expected)
    75  	os.Exit(0)
    76  }
    77  
    78  func TestRunBuildImage_Success(t *testing.T) {
    79  	for testIndex, test := range testsRunBuildImageSuccess {
    80  		common.ExecCommand = fakeRunBuildImage(testIndex)
    81  		defer func() { common.ExecCommand = exec.Command }()
    82  
    83  		out, err := runBuildImage(test.input)
    84  		if err != nil {
    85  			t.Errorf("Expected nil error, got %#v", err)
    86  		}
    87  
    88  		if string(out) != test.expected {
    89  			t.Errorf("Expected %q, got %q", test.expected, out)
    90  		}
    91  	}
    92  }
    93  
    94  func TestRunBuildImage_Fail(t *testing.T) {
    95  	for testIndex, test := range testsRunBuildImageFail {
    96  		common.ExecCommand = fakeRunBuildImage(testIndex)
    97  		defer func() { common.ExecCommand = exec.Command }()
    98  
    99  		out, err := runBuildImage(test.input)
   100  		if err == nil {
   101  			t.Errorf("Expected error, got %#v", out)
   102  
   103  		}
   104  	}
   105  }