Skip to content

Commit

Permalink
Merge pull request #14 from CircleCI-Public/refactor-common-code
Browse files Browse the repository at this point in the history
Test build agent settings storage
  • Loading branch information
marcomorain authored Jul 5, 2018
2 parents feadf79 + 5990828 commit 22af4f8
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 37 deletions.
21 changes: 7 additions & 14 deletions .circleci/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,14 @@ jobs:
- codecov/upload:
path: coverage.txt
lint:
executor: go
docker:
- image: supinf/gometalinter:latest
environment:
CGO_ENABLED: 0
working_directory: /go/src/github.com/CircleCI-Public/circleci-cli
steps:
- checkout
- restore_cache:
keys: [v1-gometalinter-]
- run:
name: Install
command: |
[[ -e /go/bin/gometalinter ]] && exit
go get -u github.com/alecthomas/gometalinter
gometalinter --install
- run: make lint
- save_cache:
key: v1-gometalinter-{{ checksum "/go/bin/gometalinter" }}
paths: [/go/bin/*]
- checkout
- run: gometalinter --deadline 60s --vendor ./...

deploy:
executor: go
Expand Down
19 changes: 4 additions & 15 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,14 @@ jobs:
&pr=$CIRCLE_PR_NUMBER"
lint:
docker:
- image: circleci/golang:1.10
- image: supinf/gometalinter:latest
environment:
CGO_ENABLED: 0
working_directory: /go/src/github.com/CircleCI-Public/circleci-cli
steps:
- checkout
- restore_cache:
keys:
- v1-gometalinter-
- run:
name: Install
command: |
[[ -e /go/bin/gometalinter ]] && exit
go get -u github.com/alecthomas/gometalinter
gometalinter --install
- run:
command: make lint
- save_cache:
key: v1-gometalinter-{{ checksum "/go/bin/gometalinter" }}
paths:
- /go/bin/*
command: gometalinter --deadline 60s --vendor ./...
deploy:
docker:
- image: circleci/golang:1.10
Expand Down
4 changes: 2 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ func buildAndOrbQuery(ctx context.Context, logger *logger.Logger, configPath str

request := client.NewAuthorizedRequest(viper.GetString("token"), query)
request.Var("config", config)
client := client.NewClient(viper.GetString("endpoint"), logger)
graphQLclient := client.NewClient(viper.GetString("endpoint"), logger)

err = client.Run(ctx, request, response)
err = graphQLclient.Run(ctx, request, response)

if err != nil {
return errors.Wrap(err, "Unable to validate config")
Expand Down
22 changes: 16 additions & 6 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ func newBuildCommand() *cobra.Command {
}

var picardRepo = "circleci/picard"
var circleCiDir = path.Join(settings.UserHomeDir(), ".circleci")
var buildAgentSettingsPath = path.Join(circleCiDir, "build_agent_settings.json")

func circleCiDir() string {
return path.Join(settings.UserHomeDir(), ".circleci")
}

func buildAgentSettingsPath() string {
return path.Join(circleCiDir(), "build_agent_settings.json")
}

type buildAgentSettings struct {
LatestSha256 string
Expand All @@ -54,7 +60,11 @@ func storeBuildAgentSha(sha256 string) error {
return errors.Wrap(err, "Failed to serialize build agent settings")
}

err = ioutil.WriteFile(buildAgentSettingsPath, settingsJSON, 0644)
if err = os.MkdirAll(circleCiDir(), 0700); err != nil {
return errors.Wrap(err, "Could not create settings directory")
}

err = ioutil.WriteFile(buildAgentSettingsPath(), settingsJSON, 0644)

return errors.Wrap(err, "Failed to write build agent settings file")
}
Expand Down Expand Up @@ -99,11 +109,11 @@ func findLatestPicardSha() (string, error) {
}

func loadCurrentBuildAgentSha() string {
if _, err := os.Stat(buildAgentSettingsPath); os.IsNotExist(err) {
if _, err := os.Stat(buildAgentSettingsPath()); os.IsNotExist(err) {
return ""
}

settingsJSON, err := ioutil.ReadFile(buildAgentSettingsPath)
settingsJSON, err := ioutil.ReadFile(buildAgentSettingsPath())

if err != nil {
Logger.Error("Faild to load build agent settings JSON", err)
Expand Down Expand Up @@ -165,7 +175,7 @@ func runBuild(cmd *cobra.Command, args []string) error {
arguments := []string{"docker", "run", "--interactive", "--tty", "--rm",
"--volume", "/var/run/docker.sock:/var/run/docker.sock",
"--volume", fmt.Sprintf("%s:%s", pwd, pwd),
"--volume", fmt.Sprintf("%s:/root/.circleci", circleCiDir),
"--volume", fmt.Sprintf("%s:/root/.circleci", circleCiDir()),
"--workdir", pwd,
image, "circleci", "build"}

Expand Down
43 changes: 43 additions & 0 deletions cmd/build_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cmd

import (
"io/ioutil"
"os"

"github.com/CircleCI-Public/circleci-cli/logger"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("build", func() {

Describe("loading settings", func() {

var tempHome string

BeforeEach(func() {

Logger = logger.NewLogger(false)
var err error
tempHome, err = ioutil.TempDir("", "circleci-cli-test-")

Expect(err).ToNot(HaveOccurred())
Expect(os.Setenv("HOME", tempHome)).To(Succeed())

})

AfterEach(func() {
Expect(os.RemoveAll(tempHome)).To(Succeed())
})

It("can load settings", func() {

Expect(storeBuildAgentSha("deipnosophist")).To(Succeed())
Expect(loadCurrentBuildAgentSha()).To(Equal("deipnosophist"))
image, err := picardImage()
Expect(err).ToNot(HaveOccurred())
Expect(image).To(Equal("circleci/picard@deipnosophist"))
})

})
})

0 comments on commit 22af4f8

Please sign in to comment.