Skip to content

Commit

Permalink
WIP: failing test
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Hu committed Jun 27, 2018
1 parent 8624db0 commit 7b711bf
Show file tree
Hide file tree
Showing 2 changed files with 354 additions and 0 deletions.
88 changes: 88 additions & 0 deletions cmd/orb.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package cmd

import (
"context"
"fmt"
"io/ioutil"
"os"

"github.com/CircleCI-Public/circleci-cli/client"
"github.com/pkg/errors"
Expand All @@ -11,6 +14,20 @@ import (
"github.com/spf13/viper"
)

var orbPath string

type orbConfigResponse struct {
OrbConfig struct {
Valid bool
SourceYaml string
OutputYaml string

Errors []struct {
Message string
}
}
}

func newOrbCommand() *cobra.Command {

orbListCommand := &cobra.Command{
Expand All @@ -19,12 +36,20 @@ func newOrbCommand() *cobra.Command {
RunE: listOrbs,
}

orbValidateCommand := &cobra.Command{
Use: "validate",
Short: "validate an orb.yml",
RunE: validateOrb,
}

orbCommand := &cobra.Command{
Use: "orb",
Short: "Operate on orbs",
}

orbValidateCommand.PersistentFlags().StringVarP(&orbPath, "path", "p", "orb.yml", "path to orb config")
orbCommand.AddCommand(orbListCommand)
orbCommand.AddCommand(orbValidateCommand)

return orbCommand
}
Expand Down Expand Up @@ -97,3 +122,66 @@ query ListOrbs ($after: String!) {
return nil

}

func loadOrbYaml(path string) (string, error) {

config, err := ioutil.ReadFile(path)

fmt.Fprintln(os.Stderr, "******************** orb.go")
fmt.Fprintln(os.Stderr, path)
fmt.Fprintln(os.Stderr, err)
if err != nil {
return "", errors.Wrapf(err, "Could not load orb file at %s", path)
}

return string(config), nil
}

func orbValidateQuery(ctx context.Context) (*orbConfigResponse, error) {

query := `
query ValidateOrb ($orb: String!) {
orbConfig(configYaml: $orb) {
valid,
errors { message },
sourceYaml,
outputYaml
}
}`

fmt.Fprintln(os.Stderr, "********************asdfasdfasdfsa")
fmt.Fprintln(os.Stderr, orbPath)

config, err := loadOrbYaml(orbPath)
if err != nil {
return nil, err
}

variables := map[string]string{
"config": config,
}

var response orbConfigResponse
err = queryAPI(ctx, query, variables, &response)
if err != nil {
return nil, errors.Wrap(err, "Unable to validate config")
}

return &response, nil
}

func validateOrb(cmd *cobra.Command, args []string) error {
ctx := context.Background()
response, err := configQuery(ctx)

if err != nil {
return err
}

if !response.BuildConfig.Valid {
return response.processErrors()
}

Logger.Infof("Orb at %s is valid", orbPath)
return nil
}
266 changes: 266 additions & 0 deletions cmd/orb_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
package cmd_test

import (
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"github.com/onsi/gomega/gexec"
"github.com/onsi/gomega/ghttp"
)

// func appendPostHandler(server *ghttp.Server, authToken string, statusCode int, expectedRequestJson string, responseBody string) {
// server.AppendHandlers(
// ghttp.CombineHandlers(
// ghttp.VerifyRequest("POST", "/"),
// ghttp.VerifyHeader(http.Header{
// "Authorization": []string{authToken},
// }),
// ghttp.VerifyContentType("application/json; charset=utf-8"),
// // From Gomegas ghttp.VerifyJson to avoid the
// // VerifyContentType("application/json") check
// // that fails with "application/json; charset=utf-8"
// func(w http.ResponseWriter, req *http.Request) {
// body, err := ioutil.ReadAll(req.Body)
// req.Body.Close()
// Expect(err).ShouldNot(HaveOccurred())
// Expect(body).Should(MatchJSON(expectedRequestJson), "JSON Mismatch")
// },
// ghttp.RespondWith(statusCode, `{ "data": `+responseBody+`}`),
// ),
// )
// }

type orbYaml struct {
TempHome string
Path string
YamlFile *os.File
}

func openOrbYaml() (orbYaml, error) {
var (
orb orbYaml = orbYaml{}
err error
)

const (
orbDir = "myorb"
orbFile = "orb.yml"
)

tempHome, err := ioutil.TempDir("", "circleci-cli-test-")
if err != nil {
return orb, err
}

err = os.Mkdir(filepath.Join(tempHome, orbDir), 0700)
if err != nil {
return orb, err
}

orb.Path = filepath.Join(tempHome, orbDir, orbFile)

var file *os.File
file, err = os.OpenFile(
orb.Path,
os.O_RDWR|os.O_CREATE,
0600,
)
if err != nil {
return orb, err
}

orb.YamlFile = file

return orb, nil
}

var _ = Describe("Orb", func() {
Describe("with an api and orb.yml", func() {
var (
testServer *ghttp.Server
orb orbYaml
)

BeforeEach(func() {
var err error
orb, err = openOrbYaml()
Expect(err).ToNot(HaveOccurred())

testServer = ghttp.NewServer()
})

AfterEach(func() {
orb.YamlFile.Close()
os.RemoveAll(orb.TempHome)

testServer.Close()
})

Describe("when validating orb", func() {
var (
token string
command *exec.Cmd
)

BeforeEach(func() {
token = "testtoken"
fmt.Fprintln(os.Stderr, "******************** Path CLI")
fmt.Fprintln(os.Stderr, pathCLI)
fmt.Fprintln(os.Stderr, token)
fmt.Fprintln(os.Stderr, testServer.URL())
fmt.Fprintln(os.Stderr, orb.Path)
command = exec.Command(pathCLI,
"orb", "validate",
"-t", token,
"-e", testServer.URL(),
"-p", orb.Path,
)
})

FIt("works", func() {
_, err := orb.YamlFile.Write([]byte(`{}`))
Expect(err).ToNot(HaveOccurred())

gqlResponse := `{
"orbConfig": {
"sourceYaml": "{}",
"valid": true,
"errors": []
}
}`

expectedRequestJson := ` {
"query": "\n\t\tquery ValidateOrb ($orb: String!) {\n\t\t\torbConfig(orbYaml: $orb) {\n\t\t\t\tvalid,\n\t\t\t\terrors { message },\n\t\t\t\tsourceYaml,\n\t\t\t\toutputYaml\n\t\t\t}\n\t\t}",
"variables": {
"orb": "{}"
}
}`

appendPostHandler(testServer, token, http.StatusOK, expectedRequestJson, gqlResponse)

session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)

Expect(err).ShouldNot(HaveOccurred())
Eventually(session.Out).Should(gbytes.Say("Orb at myorb/orb.yml is valid"))
Eventually(session).Should(gexec.Exit(0))
})

// It("prints errors if invalid", func() {
// _, err := orb.YamlFile.Write([]byte(`some orb`))
// Expect(err).ToNot(HaveOccurred())

// gqlResponse := `{
// "buildConfig": {
// "sourceYaml": "hello world",
// "valid": false,
// "errors": [
// {"message": "invalid_orb"}
// ]
// }
// }`

// expectedRequestJson := ` {
// "query": "\n\t\tquery ValidateConfig ($orb: String!) {\n\t\t\tbuildConfig(orbYaml: $orb) {\n\t\t\t\tvalid,\n\t\t\t\terrors { message },\n\t\t\t\tsourceYaml,\n\t\t\t\toutputYaml\n\t\t\t}\n\t\t}",
// "variables": {
// "orb": "some orb"
// }
// }`
// appendPostHandler(testServer, token, http.StatusOK, expectedRequestJson, gqlResponse)

// session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)

// Expect(err).ShouldNot(HaveOccurred())
// Eventually(session.Err).Should(gbytes.Say("Error:"))
// Eventually(session.Err).Should(gbytes.Say("-- invalid_orb"))
// Eventually(session).Should(gexec.Exit(255))

// })
})

// Describe("when expanding orb", func() {
// var (
// token string
// command *exec.Cmd
// )

// BeforeEach(func() {
// token = "testtoken"
// command = exec.Command(pathCLI,
// "orb", "expand",
// "-t", token,
// "-e", testServer.URL(),
// "-p", orb.Path,
// )
// })

// It("works", func() {
// _, err := orb.YamlFile.Write([]byte(`some orb`))
// Expect(err).ToNot(HaveOccurred())

// gqlResponse := `{
// "buildConfig": {
// "outputYaml": "hello world",
// "valid": true,
// "errors": []
// }
// }`

// expectedRequestJson := ` {
// "query": "\n\t\tquery ValidateConfig ($orb: String!) {\n\t\t\tbuildConfig(orbYaml: $orb) {\n\t\t\t\tvalid,\n\t\t\t\terrors { message },\n\t\t\t\tsourceYaml,\n\t\t\t\toutputYaml\n\t\t\t}\n\t\t}",
// "variables": {
// "orb": "some orb"
// }
// }`

// appendPostHandler(testServer, token, http.StatusOK, expectedRequestJson, gqlResponse)

// session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)

// Expect(err).ShouldNot(HaveOccurred())
// Eventually(session.Out).Should(gbytes.Say("hello world"))
// Eventually(session).Should(gexec.Exit(0))
// })

// It("prints errors if invalid", func() {
// _, err := orb.YamlFile.Write([]byte(`some orb`))
// Expect(err).ToNot(HaveOccurred())

// gqlResponse := `{
// "buildConfig": {
// "outputYaml": "hello world",
// "valid": false,
// "errors": [
// {"message": "error1"},
// {"message": "error2"}
// ]
// }
// }`

// expectedRequestJson := ` {
// "query": "\n\t\tquery ValidateConfig ($orb: String!) {\n\t\t\tbuildConfig(orbYaml: $orb) {\n\t\t\t\tvalid,\n\t\t\t\terrors { message },\n\t\t\t\tsourceYaml,\n\t\t\t\toutputYaml\n\t\t\t}\n\t\t}",
// "variables": {
// "orb": "some orb"
// }
// }`

// appendPostHandler(testServer, token, http.StatusOK, expectedRequestJson, gqlResponse)

// session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)

// Expect(err).ShouldNot(HaveOccurred())
// Eventually(session.Err).Should(gbytes.Say("Error:"))
// Eventually(session.Err).Should(gbytes.Say("-- error1,"))
// Eventually(session.Err).Should(gbytes.Say("-- error2,"))
// Eventually(session).Should(gexec.Exit(255))

// })
// })
})
})

0 comments on commit 7b711bf

Please sign in to comment.