Skip to content

Commit

Permalink
test: add e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Eileen-Yu committed Aug 15, 2023
1 parent 913791f commit b5a1d16
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ type Flag struct {
}

type PluginConfig struct {
Resources []ResourceData `json:"resources,omitempty"`
ProjectName string `json:"projectname,omitempty"`
Resources []ResourceData `json:"resources,omitempty"`
}

type ResourceData struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/spf13/pflag"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
"sigs.k8s.io/yaml"
// "sigs.k8s.io/kubebuilder/v3/pkg/plugin/external"
)

Expand Down Expand Up @@ -58,6 +59,40 @@ func ApiCmd(pr *external.PluginRequest) external.PluginResponse {
flags.Parse(pr.Args)
number, _ := flags.GetInt("number")

// Update the project config with GVK
cfg := external.PluginConfig{}
err := yaml.Unmarshal([]byte(pr.Config), &cfg)
if err != nil {
return external.PluginResponse{
Error: true,
ErrorMsgs: []string{
err.Error(),
},
}
}

// Create and append the new config info
newResource := external.ResourceData{
Group: "group",
Domain: "my.domain",
Version: "v1",
Kind: "Externalpluginsample",
}
cfg.Resources = append(cfg.Resources, newResource)

updatedConfig, err := yaml.Marshal(cfg)
if err != nil {
return external.PluginResponse{
Error: true,
ErrorMsgs: []string{
err.Error(),
},
}
}

// Update the PluginResponse with the modified config string
pluginResponse.Config = string(updatedConfig)

apiFile := api.NewApiFile(api.WithNumber(number))

// Phase 2 Plugins uses the concept of a "universe" to represent the filesystem for a plugin.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ limitations under the License.
package scaffolds

import (
"os"
"path/filepath"

"github.com/spf13/pflag"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
"sigs.k8s.io/yaml"
Expand Down Expand Up @@ -59,7 +62,7 @@ func InitCmd(pr *external.PluginRequest) external.PluginResponse {
flags.Parse(pr.Args)
domain, _ := flags.GetString("domain")

// Update the project config
// Update the project config with ProjectName
cfg := external.PluginConfig{}
err := yaml.Unmarshal([]byte(pr.Config), &cfg)
if err != nil {
Expand All @@ -71,14 +74,20 @@ func InitCmd(pr *external.PluginRequest) external.PluginResponse {
}
}

// Create and append the new config info
newResource := external.ResourceData{
Group: "group",
Domain: "my.domain",
Version: "v1",
Kind: "Externalpluginsample",
// Get current directory as the project name
cwd, err := os.Getwd()
if err != nil {
return external.PluginResponse{
Error: true,
ErrorMsgs: []string{
err.Error(),
},
}
}
cfg.Resources = append(cfg.Resources, newResource)

dirName := filepath.Base(cwd)

cfg.ProjectName = dirName

updatedConfig, err := yaml.Marshal(cfg)
if err != nil {
Expand Down
21 changes: 21 additions & 0 deletions test/e2e/externalplugin/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ func GenerateProject(kbc *utils.TestContext) {
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Check initFile.txt should return no error.")
ExpectWithOffset(1, initFileContainsExpr).To(BeTrue(), "The init file does not contain the expected expression.")

var initSubcommandConfigTmpl = "projectName"

initSubcommandConfigContainsExpr, err := pluginutil.HasFileContentWith(
filepath.Join(kbc.Dir, "PROJECT"), initSubcommandConfigTmpl)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Check PROJECT should return no error.")
//nolint:lll
ExpectWithOffset(1, initSubcommandConfigContainsExpr).To(BeTrue(), "The PROJECT file does not contain the expected config with the init subcommand.")

By("creating API definition")
err = kbc.CreateAPI(
"--plugins", "sampleexternalplugin/v1",
Expand All @@ -93,6 +101,19 @@ func GenerateProject(kbc *utils.TestContext) {
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Check apiFile.txt should return no error.")
ExpectWithOffset(1, apiFileContainsExpr).To(BeTrue(), "The api file does not contain the expected expression.")

var apiSubcommandConfigTmpl = `
resources:
- domain: my.domain
group: group
kind: Externalpluginsample
version: v1`

apiSubcommandConfigContainsExpr, err := pluginutil.HasFileContentWith(
filepath.Join(kbc.Dir, "PROJECT"), apiSubcommandConfigTmpl)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Check PROJECT should return no error.")
//nolint:lll
ExpectWithOffset(1, apiSubcommandConfigContainsExpr).To(BeTrue(), "The PROJECT file does not contain the expected config with the create api subcommand.")

By("scaffolding webhook")
err = kbc.CreateWebhook(
"--plugins", "sampleexternalplugin/v1",
Expand Down

0 comments on commit b5a1d16

Please sign in to comment.