-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unit tests/inject test clientset (#6874)
* Inject testClientset * Test analyze CLI as unit test * Replace odo analyze integration tests with unit tests
- Loading branch information
Showing
50 changed files
with
404 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} | ||
}) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.