Unit tests/inject test clientset (#6874)

* Inject testClientset

* Test analyze CLI as unit test

* Replace odo analyze integration tests with unit tests
This commit is contained in:
Philippe Martin
2023-06-13 19:16:31 +02:00
committed by GitHub
parent 57da760b88
commit aabbded7e1
50 changed files with 404 additions and 191 deletions

View File

@@ -7,6 +7,7 @@ import (
"os"
"github.com/redhat-developer/odo/pkg/odo/cli"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions/clientset"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
@@ -173,11 +174,12 @@ func main() {
fmt.Print(command.Usage())
} else {
ctx := context.Background()
testClientset := clientset.Clientset{}
switch args[0] {
case "reference":
fmt.Print(referencePrinter(cli.NewCmdOdo(ctx, cli.OdoRecommendedName, cli.OdoRecommendedName), 0))
fmt.Print(referencePrinter(cli.NewCmdOdo(ctx, cli.OdoRecommendedName, cli.OdoRecommendedName, testClientset), 0))
case "structure":
fmt.Print(commandPrinter(cli.NewCmdOdo(ctx, cli.OdoRecommendedName, cli.OdoRecommendedName), 0))
fmt.Print(commandPrinter(cli.NewCmdOdo(ctx, cli.OdoRecommendedName, cli.OdoRecommendedName, testClientset), 0))
default:
fmt.Print(command.Usage())
}

118
cmd/odo/alizer_test.go Normal file
View File

@@ -0,0 +1,118 @@
package main
import (
"encoding/json"
"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"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions/clientset"
"github.com/redhat-developer/odo/pkg/testingutil/filesystem"
)
func TestOdoAlizer(t *testing.T) {
for _, tt := range []struct {
name string
clientset func() clientset.Clientset
args []string
wantErr string
wantStdout string
wantStderr string
checkJsonOutput func(t *testing.T, b []byte)
}{
{
name: "analyze without json output",
clientset: func() clientset.Clientset {
return clientset.Clientset{}
},
args: []string{"analyze"},
wantErr: "this command can be run with json output only, please use the flag: -o json",
},
{
name: "analyze with json output in an empty directory",
clientset: func() clientset.Clientset {
return clientset.Clientset{}
},
args: []string{"analyze", "-o", "json"},
wantErr: "No valid devfile found for project in",
},
{
name: "analyze with json output",
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,
}
},
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.Error(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{}
if tt.clientset != nil {
clientset = tt.clientset()
}
runCommand(t, tt.args, clientset, func(err error, stdout, stderr string) {
if (err != nil) != (tt.wantErr != "") {
t.Fatalf("errWanted: %v\nGot: %v", tt.wantErr != "", err != nil)
}
if tt.wantErr != "" {
if !strings.Contains(err.Error(), tt.wantErr) {
t.Fatalf("%q\nerror does not contain:\n%q", err.Error(), tt.wantErr)
}
}
if tt.wantStdout != "" {
if !strings.Contains(stdout, tt.wantStdout) {
t.Fatalf("%q\nstdout does not contain:\n%q", stdout, tt.wantStdout)
}
}
if tt.wantStderr != "" {
if !strings.Contains(stderr, tt.wantStderr) {
t.Fatalf("%q\nstderr does not contain:\n%q", stderr, tt.wantStderr)
}
}
if tt.checkJsonOutput != nil {
tt.checkJsonOutput(t, []byte(stdout))
}
})
})
}
}

77
cmd/odo/common_test.go Normal file
View File

@@ -0,0 +1,77 @@
package main
import (
"bytes"
"context"
"flag"
"os"
"testing"
"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/spf13/pflag"
"k8s.io/klog"
)
func resetGlobalFlags() {
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
pflag.CommandLine = pflag.NewFlagSet(os.Args[0], pflag.ExitOnError)
klog.InitFlags(nil)
}
func runCommand(
t *testing.T,
args []string,
clientset clientset.Clientset,
f func(err error, stdout, stderr string),
) {
// We are running the test on a new and empty directory (on real filesystem)
originWd, err := os.Getwd()
if err != nil {
t.Error(err)
}
defer func() {
_ = os.Chdir(originWd)
}()
cwd := t.TempDir()
err = os.Chdir(cwd)
if err != nil {
t.Error(err)
}
ctx := context.Background()
envConfig, err := config.GetConfiguration()
if err != nil {
t.Fatal(err)
}
ctx = envcontext.WithEnvConfig(ctx, *envConfig)
resetGlobalFlags()
var stdoutB, stderrB bytes.Buffer
clientset.Stdout = &stdoutB
clientset.Stderr = &stderrB
root := cli.NewCmdOdo(ctx, cli.OdoRecommendedName, cli.OdoRecommendedName, clientset)
root.SetOut(&stdoutB)
root.SetErr(&stderrB)
root.SetArgs(args)
err = root.ExecuteContext(ctx)
stdout := stdoutB.String()
stderr := stderrB.String()
f(err, stdout, stderr)
}
func checkEqual[T comparable](t *testing.T, a, b T) {
if a != b {
t.Errorf("Name should be \"%v\" but is \"%v\"", b, a)
}
}

View File

@@ -9,7 +9,7 @@ import (
"github.com/redhat-developer/odo/pkg/config"
envcontext "github.com/redhat-developer/odo/pkg/config/context"
"github.com/redhat-developer/odo/pkg/odo/cli"
"k8s.io/klog"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions/clientset"
)
var (
@@ -69,9 +69,10 @@ func TestOdoHelp(t *testing.T) {
t.Fatal(err)
}
ctx = envcontext.WithEnvConfig(ctx, *envConfig)
klog.InitFlags(nil)
root := cli.NewCmdOdo(ctx, cli.OdoRecommendedName, cli.OdoRecommendedName)
resetGlobalFlags()
root := cli.NewCmdOdo(ctx, cli.OdoRecommendedName, cli.OdoRecommendedName, clientset.Clientset{})
var stdoutB, stderrB bytes.Buffer
root.SetOut(&stdoutB)

View File

@@ -15,6 +15,7 @@ import (
"github.com/redhat-developer/odo/pkg/odo/cli"
"github.com/redhat-developer/odo/pkg/odo/cli/version"
odocontext "github.com/redhat-developer/odo/pkg/odo/context"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions/clientset"
"github.com/redhat-developer/odo/pkg/odo/util"
"github.com/redhat-developer/odo/pkg/odo/util/completion"
"github.com/redhat-developer/odo/pkg/preference"
@@ -37,7 +38,7 @@ func main() {
// create the complete command
klog.InitFlags(nil)
root := cli.NewCmdOdo(ctx, cli.OdoRecommendedName, cli.OdoRecommendedName)
root := cli.NewCmdOdo(ctx, cli.OdoRecommendedName, cli.OdoRecommendedName, clientset.Clientset{})
rootCmp := createCompletion(root)
cmp := complete.New("odo", rootCmp)