Skip to content

Commit

Permalink
rollback to mock
Browse files Browse the repository at this point in the history
  • Loading branch information
treblereel committed Dec 19, 2024
1 parent 05d8ff7 commit a2b2075
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 28,371 deletions.
28,290 changes: 0 additions & 28,290 deletions packages/kn-plugin-workflow/e2e-tests/crd/operator.yaml

This file was deleted.

47 changes: 20 additions & 27 deletions packages/kn-plugin-workflow/e2e-tests/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import (
"os"
"path/filepath"
"testing"
"time"

"github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/command"
"github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/common"
"github.com/stretchr/testify/require"
)

Expand All @@ -48,41 +48,34 @@ func transformDeployCmdCfgToArgs(cfg command.DeployUndeployCmdConfig) []string {
func TestDeployProjectSuccess(t *testing.T) {
dir, err := os.Getwd()
require.NoError(t, err)

var originalCheckCrds = command.CheckCRDs
defer func() { command.CheckCRDs = originalCheckCrds }()

command.CheckCRDs = func(crds []string, typeName string) error {
return nil
}

var executeApplyOriginal = common.ExecuteApply
defer func() { common.ExecuteApply = executeApplyOriginal }()

common.ExecuteApply = func(crd, namespace string) error {
return nil
}

defer os.Chdir(dir)
for testIndex, test := range cfgTestInputDeploy_Success {
for testIndex := range cfgTestInputDeploy_Success {
t.Run(fmt.Sprintf("Test deploy project success index: %d", testIndex), func(t *testing.T) {
// Run `create` command
RunCreateTest(t, CfgTestInputCreate_Success[0])
// change dir to created project
RunCreateTest(t, CfgTestInputCreate_Success[testIndex])
projectName := GetCreateProjectName(t, CfgTestInputCreate_Success[0])
projectDir := filepath.Join(TempTestsPath, projectName)

// delete the project directory after the test
defer os.RemoveAll(projectDir)

require.NoError(t, err)
err = os.Chdir(projectDir)
require.NoErrorf(t, err, "Expected nil error, got %v", err)

// Run `deploy` command
_, err = ExecuteKnWorkflow(transformDeployCmdCfgToArgs(test.input)...)
require.NoErrorf(t, err, "Expected nil error, got: %v", err)

deployed := make(chan bool)
errCan := make(chan error)
defer close(deployed)
defer close(errCan)

go command.PollGetDeploymentStatus("default", "hello", 5*time.Second, 5*time.Minute, deployed, errCan)

select {
case <-deployed:
fmt.Printf(" - ✅ Deployment of %s is completed\n", "hello")
case err := <-errCan:
t.Fatalf(" - ❌ Deployment of %s failed: %v\n", "default", err)
}

err = command.NewUndeployCommand().Execute()
cmd := command.NewDeployCommand()
err = cmd.Execute()
require.NoError(t, err)
})
}
Expand Down
50 changes: 0 additions & 50 deletions packages/kn-plugin-workflow/e2e-tests/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
package e2e_tests

import (
"context"
"flag"
"fmt"
"os"
Expand All @@ -31,13 +30,6 @@ import (
"strings"
"testing"
"time"

"github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/common/k8sclient"

"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/dynamic"
)

var parentPath string
Expand All @@ -52,8 +44,6 @@ func TestMain(m *testing.M) {

// Create temp directory for tests and switch inside it
workingPath, _ := os.Getwd()
// we have to setup the CRDs before we can run the tests
setupCrds(workingPath)
parentPath = filepath.Dir(workingPath)
tempDirName := "temp-tests"
if fileExists(tempDirName) {
Expand All @@ -75,46 +65,6 @@ func TestMain(m *testing.M) {
os.Exit(exitCode)
}

func setupCrds(current string) {

client, err := k8sclient.DynamicClient()
if err != nil {
fmt.Println("❌ ERROR: Failed to create dynamic client: ", err)
os.Exit(1)
}
crdPath := filepath.Join(current, "crd", operatorCRD)
if resources, err := k8sclient.ParseYamlFile(crdPath); err != nil {
fmt.Printf("❌ ERROR: Failed to parse YAML file: %v\n", err)
os.Exit(1)
} else {
for _, resource := range resources {
gvk := resource.GroupVersionKind()
gvr, _ := meta.UnsafeGuessKindToResource(gvk)

baseResource := client.Resource(gvr)

ns := resource.GetNamespace()
var resourceInterface dynamic.ResourceInterface

if ns != "" {
resourceInterface = baseResource.Namespace(ns)
} else {
resourceInterface = baseResource
}

_, err := resourceInterface.Create(context.Background(), &resource, metav1.CreateOptions{})
if err != nil {
if errors.IsAlreadyExists(err) {
continue
} else {
fmt.Println("❌ ERROR: Failed to create resource: ", err)
os.Exit(1)
}
}
}
}
}

func setUpTempDir(tempDirName string) {
var err error
err = os.Mkdir(tempDirName, 0750)
Expand Down
2 changes: 1 addition & 1 deletion packages/kn-plugin-workflow/pkg/command/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ func runDeployCmdConfig(cmd *cobra.Command) (cfg DeployUndeployCmdConfig, err er
return cfg, nil
}

func CheckCRDs(crds []string, typeName string) error {
var CheckCRDs = func(crds []string, typeName string) error {
for _, crd := range crds {
err := common.CheckCrdExists(crd)
if err != nil {
Expand Down
12 changes: 10 additions & 2 deletions packages/kn-plugin-workflow/pkg/common/k8sclient/goapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,12 +352,20 @@ var GetNamespace = func() (string, error) {

config, err := KubeApiConfig()
if err != nil {
fmt.Println("❌ ERROR: Failed to get current k8s namespace: %w", err)
return "", fmt.Errorf("❌ ERROR: Failed to get current k8s namespace: %w", err)
}
namespace := config.Contexts[config.CurrentContext].Namespace

if len(namespace) == 0 {
var namespace string

if contextes, ok := config.Contexts[config.CurrentContext]; !ok {
namespace = "default"
} else {
if len(contextes.Namespace) == 0 {
namespace = "default"
} else {
namespace = contextes.Namespace
}
}
fmt.Printf(" - ✅ k8s current namespace: %s\n", namespace)
return namespace, nil
Expand Down
2 changes: 1 addition & 1 deletion packages/kn-plugin-workflow/pkg/common/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func GetNamespace() (string, error) {
return Current.GetNamespace()
}

func ExecuteApply(crd, namespace string) error {
var ExecuteApply = func(crd, namespace string) error {
return Current.ExecuteApply(crd, namespace)
}

Expand Down

0 comments on commit a2b2075

Please sign in to comment.