Skip to content

Commit

Permalink
Test build agent settings storage
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomorain committed Jul 4, 2018
1 parent 5d55e9c commit 29b5905
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
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
37 changes: 37 additions & 0 deletions cmd/build_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package cmd

import (
"io/ioutil"
"os"

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

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

Describe("loading settings", func() {

var tempHome string

BeforeEach(func() {
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"))
})

})
})

0 comments on commit 29b5905

Please sign in to comment.