github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow@v0.28.1-0.20240311201729-34c6856b157f/e2e-tests/quarkus_build_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  	"testing"
    30  
    31  	"github.com/stretchr/testify/require"
    32  
    33  	"github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/command/quarkus"
    34  )
    35  
    36  var cfgTestInputPrepareQuarkusCreateBuild = CfgTestInputQuarkusCreate{
    37  	input: quarkus.CreateQuarkusProjectConfig{
    38  		ProjectName: "new-project",
    39  	},
    40  }
    41  
    42  type CfgTestInputQuarkusBuild struct {
    43  	input quarkus.BuildCmdConfig
    44  }
    45  
    46  var cfgTestInputQuarkusBuild_Success = []CfgTestInputQuarkusBuild{
    47  	{input: quarkus.BuildCmdConfig{
    48  		Image: "dev.local/my-project",
    49  	}},
    50  	{input: quarkus.BuildCmdConfig{
    51  		Image:      "my-user/my-project:1.0.0",
    52  		Repository: "other-user",
    53  		Tag:        "1.0.1",
    54  	}},
    55  	{input: quarkus.BuildCmdConfig{
    56  		Image: "dev.local/my-project",
    57  		Jib:   true,
    58  	}},
    59  	//{input: quarkus.BuildCmdConfig{
    60  	//	Image:     "dev.local/my-project",
    61  	//	JibPodman: true,
    62  	//}},
    63  	// {input: quarkus.BuildCmdConfig{
    64  	// 	Image: "dev.local/my-project",
    65  	// 	Jib:   true,
    66  	// 	Push:  true,
    67  	// }},
    68  	// {input: quarkus.BuildCmdConfig{
    69  	// 	Image: "dev.local/my-project",
    70  	// 	Push:  true,
    71  	// }},
    72  }
    73  
    74  func transformQuarkusBuildCmdCfgToArgs(cfg quarkus.BuildCmdConfig) []string {
    75  	args := []string{"build"}
    76  	if cfg.Image != "" {
    77  		args = append(args, "--image", cfg.Image)
    78  	}
    79  	if cfg.ImageName != "" {
    80  		args = append(args, "--image-name", cfg.ImageName)
    81  	}
    82  	if cfg.Registry != "" {
    83  		args = append(args, "--image-registry", cfg.Registry)
    84  	}
    85  	if cfg.Repository != "" {
    86  		args = append(args, "--image-repository", cfg.Repository)
    87  	}
    88  	if cfg.Tag != "" {
    89  		args = append(args, "--image-tag", cfg.Tag)
    90  	}
    91  	if cfg.Jib == true {
    92  		args = append(args, "--jib")
    93  	}
    94  	if cfg.JibPodman == true {
    95  		args = append(args, "--jib-podman")
    96  	}
    97  	if cfg.Push == true {
    98  		args = append(args, "--push")
    99  	}
   100  	if cfg.Test == true {
   101  		args = append(args, "--test")
   102  	}
   103  	return args
   104  }
   105  
   106  func TestQuarkusBuildCommand(t *testing.T) {
   107  	for testIndex, test := range cfgTestInputQuarkusBuild_Success {
   108  		t.Run(fmt.Sprintf("Test build project success index: %d", testIndex), func(t *testing.T) {
   109  			defer CleanUpAndChdirTemp(t)
   110  			RunQuarkusBuildTest(t, cfgTestInputPrepareQuarkusCreateBuild, test, true)
   111  		})
   112  	}
   113  }
   114  
   115  func RunQuarkusBuildTest(t *testing.T, cfgTestInputQuarkusCreate CfgTestInputQuarkusCreate, test CfgTestInputQuarkusBuild, cleanUp bool) string {
   116  	var err error
   117  
   118  	// Create the project
   119  	projectName := RunQuarkusCreateTest(t, cfgTestInputQuarkusCreate)
   120  	projectDir := filepath.Join(TempTestsPath, projectName)
   121  
   122  	err = os.Chdir(projectDir)
   123  	require.NoErrorf(t, err, "Expected nil error, got %v", err)
   124  
   125  	// Run `quarkus build` command
   126  	_, err = ExecuteKnWorkflowQuarkus(transformQuarkusBuildCmdCfgToArgs(test.input)...)
   127  	require.NoErrorf(t, err, "Expected nil error, got %v", err)
   128  
   129  	require.FileExists(t, filepath.Join("target", "kubernetes", "knative.yml"))
   130  
   131  	// Clean up images from docker and podman
   132  	if cleanUp {
   133  		CleanUpDockerPodman(t, test)
   134  	}
   135  
   136  	return projectName
   137  }
   138  
   139  func CleanUpDockerPodman(t *testing.T, test CfgTestInputQuarkusBuild) {
   140  	var err error
   141  	expectedImageName := ExpectedImageName(test.input)
   142  	var removeCmd *exec.Cmd
   143  	if test.input.JibPodman {
   144  		// Remove built image from podman
   145  		removeCmd = exec.Command("podman", "image", "rm", expectedImageName) // podman only takes `rm` for removing images
   146  	} else {
   147  		// Remove built image from docker
   148  		removeCmd = exec.Command("docker", "image", "rm", expectedImageName) // docker takes both `rm` and `remove` for removing images
   149  	}
   150  	t.Log("Removing image:", removeCmd.Args)
   151  	out, err := removeCmd.Output()
   152  	fmt.Print(string(out))
   153  	require.NoErrorf(t, err, "Error when removing image: %s. Expected nil error, got %v", expectedImageName, err)
   154  }