Do not return an error in odo analyze if current directory contains an invalid Devfile (#6905)

* Add unit test highliging the issue

* Fix 'delete' unit tests

* Pass the filesystem object where it is relevant

* Add a way for CLI commands to indicate whether of not they require a valid Devfile

For the 'analyze' command, this is not required,
so Devfile parsing will be ignored completely.

* Make the fake filesystem return an absolute current dir

Otherwise, some code will assume it is relative,
and try to prepend the current physical directory
This commit is contained in:
Armel Soro
2023-06-22 10:06:18 +02:00
committed by GitHub
parent a29253521c
commit 28ed064133
16 changed files with 159 additions and 55 deletions

View File

@@ -2,10 +2,12 @@ package main
import (
"encoding/json"
"path/filepath"
"strings"
"testing"
"github.com/golang/mock/gomock"
"github.com/redhat-developer/alizer/go/pkg/apis/model"
"github.com/redhat-developer/odo/pkg/alizer"
"github.com/redhat-developer/odo/pkg/api"
@@ -18,6 +20,7 @@ func TestOdoAlizer(t *testing.T) {
for _, tt := range []struct {
name string
clientset func() clientset.Clientset
fsPopulator func(fs filesystem.Filesystem) error
args []string
wantErr string
wantStdout string
@@ -46,7 +49,7 @@ func TestOdoAlizer(t *testing.T) {
ctrl := gomock.NewController(t)
fs := filesystem.NewFakeFs()
alizerClient := alizer.NewMockClient(ctrl)
path := "."
path := "/"
alizerClient.EXPECT().DetectFramework(gomock.Any(), path).
Return(
model.DevFileType{
@@ -80,15 +83,77 @@ func TestOdoAlizer(t *testing.T) {
checkEqual(t, output[0].ApplicationPorts[1], 3000)
},
},
{
name: "analyze should not error out even if there is an invalid Devfile in the current directory",
clientset: func() clientset.Clientset {
ctrl := gomock.NewController(t)
fs := filesystem.NewFakeFs()
alizerClient := alizer.NewMockClient(ctrl)
path := "/"
alizerClient.EXPECT().DetectFramework(gomock.Any(), path).
Return(
model.DevFileType{
Name: "framework-name",
},
"1.1.1",
api.Registry{
Name: "TheRegistryName",
},
nil,
)
alizerClient.EXPECT().DetectPorts(path).Return([]int{8080, 3000}, nil)
alizerClient.EXPECT().DetectName(path).Return("aName", nil)
return clientset.Clientset{
FS: fs,
AlizerClient: alizerClient,
}
},
fsPopulator: func(fs filesystem.Filesystem) error {
cwd, err := fs.Getwd()
if err != nil {
return err
}
err = fs.WriteFile(
filepath.Join(cwd, "main.go"),
[]byte(`package main
import "fmt"
func main() {
fmt.Println("Hello World")
}
`),
0644)
if err != nil {
return err
}
return fs.WriteFile(filepath.Join(cwd, "devfile.yaml"), []byte("some-invalid-content"), 0644)
},
args: []string{"analyze", "-o", "json"},
checkJsonOutput: func(t *testing.T, b []byte) {
var output []api.DetectionResult
err := json.Unmarshal(b, &output)
if err != nil {
t.Fatal(err)
}
checkEqual(t, output[0].Devfile, "framework-name")
checkEqual(t, output[0].DevfileRegistry, "TheRegistryName")
checkEqual(t, output[0].Name, "aName")
checkEqual(t, output[0].DevfileVersion, "1.1.1")
checkEqual(t, output[0].ApplicationPorts[0], 8080)
checkEqual(t, output[0].ApplicationPorts[1], 3000)
},
},
} {
t.Run(tt.name, func(t *testing.T) {
clientset := clientset.Clientset{}
cs := clientset.Clientset{}
if tt.clientset != nil {
clientset = tt.clientset()
cs = tt.clientset()
}
runCommand(t, tt.args, runOptions{}, clientset, nil, func(err error, stdout, stderr string) {
runCommand(t, tt.args, runOptions{}, cs, tt.fsPopulator, func(err error, stdout, stderr string) {
if (err != nil) != (tt.wantErr != "") {
t.Fatalf("errWanted: %v\nGot: %v", tt.wantErr != "", err != nil)
t.Fatalf("errWanted: %v\nGot: %v", tt.wantErr != "", err)
}
if tt.wantErr != "" {

View File

@@ -7,14 +7,15 @@ import (
"os"
"testing"
"github.com/sethvargo/go-envconfig"
"github.com/spf13/pflag"
"k8s.io/klog"
"github.com/redhat-developer/odo/pkg/config"
envcontext "github.com/redhat-developer/odo/pkg/config/context"
"github.com/redhat-developer/odo/pkg/odo/cli"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions/clientset"
"github.com/redhat-developer/odo/pkg/testingutil/filesystem"
"github.com/sethvargo/go-envconfig"
"github.com/spf13/pflag"
"k8s.io/klog"
)
func resetGlobalFlags() {
@@ -33,7 +34,7 @@ func runCommand(
args []string,
options runOptions,
clientset clientset.Clientset,
populateFS func(fs filesystem.Filesystem),
populateFS func(fs filesystem.Filesystem) error,
f func(err error, stdout, stderr string),
) {
@@ -52,7 +53,10 @@ func runCommand(
}
if populateFS != nil {
populateFS(clientset.FS)
err = populateFS(clientset.FS)
if err != nil {
t.Fatal(err)
}
}
ctx := context.Background()

View File

@@ -8,16 +8,17 @@ import (
"github.com/golang/mock/gomock"
"github.com/onsi/gomega"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime/schema"
"github.com/redhat-developer/odo/pkg/kclient"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions/clientset"
"github.com/redhat-developer/odo/pkg/podman"
"github.com/redhat-developer/odo/pkg/testingutil/filesystem"
"github.com/redhat-developer/odo/pkg/util"
"github.com/redhat-developer/odo/tests/helper"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime/schema"
)
// Context
@@ -82,12 +83,15 @@ var allPlatforms = map[string]platformFunc{
// FS content
type fscontentFunc func(fs filesystem.Filesystem)
type fscontentFunc func(fs filesystem.Filesystem) error
var noContentFscontent fscontentFunc = func(fs filesystem.Filesystem) {}
var noContentFscontent fscontentFunc = func(fs filesystem.Filesystem) error {
return nil
}
var nodeJsSourcesFsContent fscontentFunc = func(fs filesystem.Filesystem) {
var nodeJsSourcesFsContent fscontentFunc = func(fs filesystem.Filesystem) error {
helper.CopyExample(filepath.Join("source", "devfiles", "nodejs", "project"), ".")
return nil
}
type fsOptions struct {
@@ -96,7 +100,7 @@ type fsOptions struct {
}
var nodeJsSourcesAndDevfileFsContent = func(devfilePath string, options fsOptions) fscontentFunc {
return func(fs filesystem.Filesystem) {
return func(fs filesystem.Filesystem) error {
helper.CopyExample(filepath.Join("source", "devfiles", "nodejs", "project"), ".")
helper.CopyExampleDevFile(
devfilePath,
@@ -110,6 +114,7 @@ var nodeJsSourcesAndDevfileFsContent = func(devfilePath string, options fsOption
gomega.Expect(err).NotTo(gomega.HaveOccurred())
}
return nil
}
}