Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cr templating #51

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
12 changes: 7 additions & 5 deletions cmd/helm2go-operator-sdk/new/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,6 @@ func (hc *HelmChartClient) DoHelmGoConversion() (*resourcecache.ResourceCache, e
func scaffoldOverwrite(outputDir, kind, apiVersion string, rcache *resourcecache.ResourceCache) error {

ok := templating.OverwriteController(outputDir, kind, apiVersion, rcache)
if !ok {
fmt.Println(ok)
}
// create templates for writing to file
templates := templating.CacheTemplating(rcache, outputDir, kind, apiVersion)
// templates to files; outputDir is the parent directory where the operator scaffolding lives
Expand All @@ -152,7 +149,12 @@ func scaffoldOverwrite(outputDir, kind, apiVersion string, rcache *resourcecache
ok = templating.ResourceFileStructure(rcache, resDir)
ok = templating.TemplatesToFiles(templates, resDir)
if !ok {
return fmt.Errorf("Writing to File Error")
return fmt.Errorf("error writing to file")
}

//begin overwritting the types file
ok = templating.OverwriteKindTypes(outputDir, kind, apiVersion)
if !ok {
return fmt.Errorf("error overwriting kind types")
}
return nil
}
333 changes: 138 additions & 195 deletions cmd/helm2go-operator-sdk/new/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,122 +2,153 @@ package new

import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"

"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/redhat-nfvpe/helm2go-operator-sdk/internal/pathconfig"
"github.com/spf13/cobra"
)

var cwd, _ = os.Getwd()
var parent = filepath.Dir(filepath.Dir(filepath.Dir(cwd)))

func silencelog() {
log.SetOutput(ioutil.Discard)
}
func resetlog() {
log.SetOutput(os.Stdout)
func TestCommand(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Command")
}

func TestLoadChartGetsLocalChart(t *testing.T) {
//g := gomega.NewGomegaWithT(t)
silencelog()

var local = "/test/bitcoind"
var testLocal = parent + local

// load the chart
chartClient := NewChartClient()
chartClient.HelmChartRef = testLocal
err := chartClient.LoadChart()
if err != nil {
t.Fatal(err)
}
// verify that the chart loads the right thing
if chartClient.Chart.GetMetadata().Name != "bitcoind" {
resetlog()
t.Fatalf("Unexpected Chart Name!")
}
}
var _ = Describe("Load Chart", func() {
It("Gets Local Chart", func() {
var local = "/test/bitcoind"
var testLocal = parent + local
chartClient := NewChartClient()
chartClient.HelmChartRef = testLocal
err := chartClient.LoadChart()
Expect(err).ToNot(HaveOccurred())
Expect(chartClient.Chart.GetMetadata().Name).To(Equal("bitcoind"))
})
It("Gets External Charts From Valid Repos", func() {
var client HelmChartClient

func TestCommandFlagValidation(t *testing.T) {
// initiate flags for testing
var err error
helmChartRef = ""
silencelog()
if err = verifyFlags(); err == nil {
resetlog()
t.Logf("Error: %v", err)
t.Fatal("Expected Flag Validation Error: --helm-chart-ref")
silencelog()
}
helmChartRef = "./test/tomcat"
kind = "Tomcat"
apiVersion = ""
if err = verifyFlags(); err == nil {
resetlog()
t.Logf("Error: %v", err)
t.Fatal("Expected Flag Validation Error: --api-version")
silencelog()
}
apiVersion = "app.example/v1alpha1"
if err = verifyFlags(); err == nil {
resetlog()
t.Logf("Error: %v", err)
t.Fatal("expected flag validation error; api version does not match naming convention")
silencelog()
}
apiVersion = "app.example.com/v1alpha1"
if err = verifyFlags(); err != nil {
resetlog()
t.Logf("Error: %v", err)
t.Fatal("Unexpected Flag Validation Error")
silencelog()
}
}
gopathDir := os.Getenv("GOPATH")
pathConfig := pathconfig.NewConfig(filepath.Join(gopathDir, "src/github.com/redhat-nfvpe/helm2go-operator-sdk"))
client.PathConfig = pathConfig

func TestKindTypeValidation(t *testing.T) {
var err error
apiVersion = "app.example.com/v1alpha1"
helmChartRef = "./test/tomcat"
kind = ""
silencelog()
if err = verifyFlags(); err == nil {
resetlog()
t.Fatal("Expected Flag Validation Error: kind")
silencelog()
}
kind = "tensorflow-notebook"
if err = verifyFlags(); err == nil {
resetlog()
t.Fatalf("Expected Lowercase Flag Validation Error: --kind %v", err)
silencelog()
}
kind = "tensorflowNotebook"
if err = verifyFlags(); err == nil {
resetlog()
t.Fatal("Expected Lowercase Flag Validation Error: --kind")
t.Fatal(err)
silencelog()
}
kind = "Tensorflow-Notebook"
if err = verifyFlags(); err == nil {
resetlog()
t.Fatal("Expected Hyphen Flag Validation Error: --kind")
t.Fatal(err)
silencelog()
}
kind = "TensorflowNotebook"
if err = verifyFlags(); err != nil {
resetlog()
t.Fatal("Unexpected Flag Validation Error")
t.Fatal(err)
silencelog()
}
}
client.HelmChartRef = "auto-deploy-app"
client.HelmChartRepo = "https://charts.gitlab.io/"
err := client.LoadChart()
Expect(err).ToNot(HaveOccurred())

// cleanup auto-deploy-app

client.HelmChartRef = "not-a-chart"
client.HelmChartRepo = ""
err = client.LoadChart()
Expect(err).To(HaveOccurred())

client.HelmChartRepo = "invalidrepo.io"
err = client.LoadChart()
Expect(err).To(HaveOccurred())
})
})

var _ = Describe("Flag Validation", func() {
It("Verifies Command Flags", func() {
var err error
helmChartRef = ""
err = verifyFlags()
Expect(err).To(HaveOccurred())

helmChartRef = "./test/tomcat"
kind = "Tomcat"
apiVersion = ""
err = verifyFlags()
Expect(err).To(HaveOccurred())

apiVersion = "app.example/v1alpha1"
err = verifyFlags()
Expect(err).To(HaveOccurred())
apiVersion = "app.example.com/v1alpha1"
err = verifyFlags()
Expect(err).ToNot(HaveOccurred())
})
It("Verifies Kind Flag", func() {
var err error
apiVersion = "app.example.com/v1alpha1"
helmChartRef = "./test/tomcat"

kind = ""
err = verifyFlags()
Expect(err).To(HaveOccurred())

kind = "tensorflow-notebook"
err = verifyFlags()
Expect(err).To(HaveOccurred())

kind = "tensorflowNotebook"
err = verifyFlags()
Expect(err).To(HaveOccurred())

kind = "Tensorflow-Notebook"
err = verifyFlags()
Expect(err).To(HaveOccurred())

kind = "TensorflowNotebook"
err = verifyFlags()
Expect(err).ToNot(HaveOccurred())
})
It("Verifies API Version", func() {
var err error
helmChartRef = "./test/tomcat"
kind = "TensorflowNotebook"

apiVersion = "app.example.com/v1alpha1"
err = verifyFlags()
Expect(err).ToNot(HaveOccurred())

apiVersion = "web.k8s.io/v1"
err = verifyFlags()
Expect(err).ToNot(HaveOccurred())

apiVersion = "k8s.io"
err = verifyFlags()
Expect(err).To(HaveOccurred())

apiVersion = "app,s.k8s.io/v1beta1"
err = verifyFlags()
Expect(err).To(HaveOccurred())

apiVersion = "apps.example/v1beta1"
err = verifyFlags()
Expect(err).To(HaveOccurred())

})
})

var _ = Describe("Command Argument Validation", func() {
It("Fails On Invalid Arguments", func() {
var err error
err = createInvalidCommand().Execute()
Expect(err).To(HaveOccurred())

err = createInvalidOperatorName().Execute()
Expect(err).To(HaveOccurred())

err = createInvalidKindName().Execute()
Expect(err).To(HaveOccurred())
})
It("Passes On Valid Arguments", func() {
var err error
err = createValidCommand().Execute()
Expect(err).ToNot(HaveOccurred())
cleanupValidOperator()
})
})

//creates a command for use in the argument validation test
func createValidCommand() *cobra.Command {
Expand All @@ -138,6 +169,12 @@ func createValidCommand() *cobra.Command {
return cmd
}

func cleanupValidOperator() {
gopathDir := os.Getenv("GOPATH")
operatorDir := filepath.Join(gopathDir, "src", "redhat-nfvpe", "helm2go-operator-sdk", "cmd", "helm2go-operator-sdk", "new", "test-operator")
os.RemoveAll(operatorDir)
}

// creates an invalid command for use in the argument validation test
func createInvalidCommand() *cobra.Command {
var cmd *cobra.Command
Expand Down Expand Up @@ -190,97 +227,3 @@ func createInvalidKindName() *cobra.Command {
})
return cmd
}

func TestInvalidCommandArgumentValidation(t *testing.T) {
var err error
silencelog()
if err = createInvalidCommand().Execute(); err == nil {
resetlog()
t.Logf("Error: %v", err)
t.Fatal("Expected Error! Command Has No Operator Name Argument.")
silencelog()
}
if err = createInvalidOperatorName().Execute(); err == nil {
resetlog()
t.Logf("Error: %v", err)
t.Fatal("Expected Error! Command Has Invalid(empty) Operator Name Argument.")
silencelog()
}
if err = createInvalidKindName().Execute(); err == nil {
resetlog()
t.Logf("Error: %v", err)
t.Fatal("Expected Error! Command Has Invalid(Has Space) API Version Argument.")
silencelog()
}

}

func TestValidCommandArgument(t *testing.T) {
defer os.RemoveAll("./test-operator")
if err := createValidCommand().Execute(); err != nil {
resetlog()
t.Logf("Error: %v", err)
os.RemoveAll("./test-operator")
t.Fatal("Unexpected Error! Provided Correct Arguments and Flags.")
silencelog()
}

}

// clean up

func TestHelmChartDownload(t *testing.T) {
var client HelmChartClient

gopathDir := os.Getenv("GOPATH")
pathConfig := pathconfig.NewConfig(filepath.Join(gopathDir, "src/github.com/redhat-nfvpe/helm2go-operator-sdk"))
client.PathConfig = pathConfig

// defer func() {
// if r := recover(); r != nil {
// fmt.Printf("Panic: %+v\n", r)
// }
// }()

client.HelmChartRef = "auto-deploy-app"
client.HelmChartRepo = "https://charts.gitlab.io/"
err := client.LoadChart()
if err != nil {
resetlog()
t.Fatalf("Unexpected Error: %v\n", err)
silencelog()
}

client.HelmChartRef = "not-a-chart"
client.HelmChartRepo = ""
err = client.LoadChart()
if err == nil {
resetlog()
t.Fatalf("Expected Error Chart Does Not Exist!")
silencelog()
}

client.HelmChartRepo = "invalidrepo.io"
err = client.LoadChart()
if err == nil {
resetlog()
t.Fatalf("Expected Error Invalid Repo!")
silencelog()
}
}

func TestAPINamingConventionValidation(t *testing.T) {
testCases := map[string]bool{
"app.example.com/v1alpha1": true,
"web.k8s.io/v1": true,
"k8s.io": false,
"app,s.k8s.io/v1beta1": false,
"apps.example/v1beta1": false,
}

for v, ok := range testCases {
if m := apiVersionMatchesConvention(v); m != ok {
t.Fatalf("error validating: %v; expected %v got %v", v, ok, m)
}
}
}
2 changes: 0 additions & 2 deletions cmd/helm2go-operator-sdk/new/cmd_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ func matchVersion(cmdOut *[]byte) error {
pattern := regexp.MustCompile(`.*version\: +v(\d.\d.\d).*commit\: +(.*)`)
matches := pattern.FindStringSubmatch(string(*cmdOut))

fmt.Println(matches)

if l := len(matches); l != 2+1 {
return fmt.Errorf("expected three matches, received %d instead", l)
}
Expand Down
Loading