From 43130adcc8636bc6c287bb961279fd908a16c018 Mon Sep 17 00:00:00 2001 From: Marc O'Morain Date: Thu, 5 Jul 2018 00:51:32 +0100 Subject: [PATCH 1/2] Test build agent settings storage --- cmd/build.go | 22 ++++++++++++++++------ cmd/build_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 cmd/build_test.go diff --git a/cmd/build.go b/cmd/build.go index a4e9244fe..37331ea45 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -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 @@ -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") } @@ -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) @@ -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"} diff --git a/cmd/build_test.go b/cmd/build_test.go new file mode 100644 index 000000000..463f56658 --- /dev/null +++ b/cmd/build_test.go @@ -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")) + }) + + }) +}) From 5990828e241dde5f8c3d25741f22290f33312c51 Mon Sep 17 00:00:00 2001 From: Marc O'Morain Date: Thu, 5 Jul 2018 11:55:23 +0100 Subject: [PATCH 2/2] Use a specific lint docker image --- .circleci/build.yml | 21 +++++++-------------- .circleci/config.yml | 19 ++++--------------- api/api.go | 4 ++-- 3 files changed, 13 insertions(+), 31 deletions(-) diff --git a/.circleci/build.yml b/.circleci/build.yml index d9634b64a..33a83c637 100644 --- a/.circleci/build.yml +++ b/.circleci/build.yml @@ -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 diff --git a/.circleci/config.yml b/.circleci/config.yml index a350ac9c9..23c1a8526 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -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 diff --git a/api/api.go b/api/api.go index 5333c7c86..a21cb40e8 100644 --- a/api/api.go +++ b/api/api.go @@ -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")