Skip to content
This repository has been archived by the owner on Jan 13, 2021. It is now read-only.

Commit

Permalink
Merge pull request #64 from Snapkitchen/gomod-and-acceptance-tests
Browse files Browse the repository at this point in the history
fix acceptance tests and move to go modules
  • Loading branch information
Izabela Gomes authored Jun 30, 2020
2 parents daf2148 + 2de1a22 commit 6b82ccb
Show file tree
Hide file tree
Showing 198 changed files with 287 additions and 34,103 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/assets
Dockerfile
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/cmd/check/check
/cmd/in/in
/cmd/out/out

/assets
fly-bin

main
41 changes: 11 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,46 +175,28 @@ jobs:

### Prerequisites

* golang is *required* - version 1.9.x is tested; earlier versions may also
* golang is *required* - version 1.13.x is tested; earlier versions may also
work.
* docker is *required* - version 17.06.x is tested; earlier versions may also
work.

### Dependencies

Dependencies are vendored in the `vendor` directory, according to the
[golang 1.5 vendor experiment](https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwi7puWg7ZrLAhUN1WMKHeT4A7oQFggdMAA&url=https%3A%2F%2Fgolang.org%2Fs%2Fgo15vendor&usg=AFQjCNEPCAjj1lnni5apHdA7rW0crWs7Zw).
Dependencies are handled using [go modules](https://github.com/golang/go/wiki/Modules).

#### Updating dependencies

Install [gvt](https://github.com/FiloSottile/gvt) and make sure it is available
in your $PATH, e.g.:

```
go get -u github.com/FiloSottile/gvt
```

To add a new dependency:
go mod download
```
gvt fetch
```

To update an existing dependency to a specific version:
```
gvt delete <import_path>
gvt fetch -revision <revision_number> <import_path>
```
To add or update a specific dependency version, follow the go modules instructions for [Daily Workflow](https://github.com/golang/go/wiki/Modules#daily-workflow)
### Running the tests
Install the ginkgo executable with:

```
go get -u github.com/onsi/ginkgo/ginkgo
```
#### Using a local environment
The tests require a running Concourse configured with basic auth to test against.
The acceptance tests require a running Concourse configured with basic auth to test against.
Run the tests with the following command (optionally also setting `INSECURE=true`):
Expand All @@ -226,14 +208,13 @@ PASSWORD=my-basic-auth-password \
./bin/test
```
or with the `Dockerfile`...
#### Using a Dockerfile
The tests have been embedded with the `Dockerfile`; ensuring that the testing
environment is consistent across any `docker` enabled platform. When the docker
image builds, the test are run inside the docker container, on failure they
**Note**: the `Dockerfile` tests do not run the acceptance tests, but ensure a consistent environment across any `docker` enabled platform. When the docker
image builds, the tests run inside the docker container, and on failure they
will stop the build.
The tests needs to be run from one directory up from the directory of the repo. They will also need the fly
The tests need to be ran from one directory up from the directory of the repo. They will also need the fly
linux tarball (from https://github.com/concourse/concourse/releases) to be present in the `fly/` folder e.g:
```
Expand All @@ -260,4 +241,4 @@ docker build -t concourse-pipeline-resource -f concourse-pipeline-resource/docke

### Contributing

Please [ensure the tests pass locally](https://github.com/concourse/concourse-pipeline-resource#running-the-tests).
Please [ensure the tests pass locally](#running-the-tests).
63 changes: 63 additions & 0 deletions acceptance/acceptance_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package acceptance
import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"strconv"
"strings"

"github.com/concourse/concourse-pipeline-resource/fly"
"github.com/concourse/concourse-pipeline-resource/logger"
Expand Down Expand Up @@ -37,9 +39,67 @@ var (
password string
insecure bool

env map[string]string

flyCommand fly.Command
)

func CaptureEnvVars() map[string]string {
capturedEnv := make(map[string]string)
// get list of current Key=Value
currentEnv := os.Environ()
// iterate and split into "Key": "Value"
for _, envVarItem := range currentEnv {
// split into before and after the first =
envVarItemKeyValue := strings.SplitN(envVarItem, "=", 2)
envVarItemKey := envVarItemKeyValue[0]
envVarItemValue := envVarItemKeyValue[1]
capturedEnv[envVarItemKey] = envVarItemValue
}
return capturedEnv
}

func RestoreEnvVars() {
os.Clearenv()
var err error
for envVarKey, envVarValue := range env {
err = os.Setenv(envVarKey, envVarValue)
if err != nil {
fmt.Fprintln(GinkgoWriter, err.Error())
}
}
}

func CreateTestPipelineConfigFile(dirPath, pipelineName string) (string, error) {
var err error

pipelineConfig := `---
resources:
- name: concourse-pipeline-resource-repo
type: git
source:
uri: https://github.com/concourse/concourse-pipeline-resource.git
branch: master
jobs:
- name: get-concourse-pipeline-resource-repo
plan:
- get: concourse-pipeline-resource-repo
`

pipelineConfigFileName := fmt.Sprintf("%s.yml", pipelineName)
pipelineConfigFilePath := filepath.Join(dirPath, pipelineConfigFileName)
err = ioutil.WriteFile(pipelineConfigFilePath, []byte(pipelineConfig), os.ModePerm)
return pipelineConfigFilePath, err
}

func SetTestPipeline(pipelineName string, configFilePath string) error {
var err error
var setOutput []byte
setOutput, err = flyCommand.SetPipeline(pipelineName, configFilePath, nil, nil)
fmt.Fprintf(GinkgoWriter, "pipeline '%s' set; output:\n\n%s\n", pipelineName, string(setOutput))
return err
}

func TestAcceptance(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Acceptance Suite")
Expand All @@ -48,6 +108,9 @@ func TestAcceptance(t *testing.T) {
var _ = BeforeSuite(func() {
var err error

By("Capturing current environment variables")
env = CaptureEnvVars()

By("Getting target from environment variables")
target = os.Getenv("TARGET")
Expect(target).NotTo(BeEmpty(), "$TARGET must be provided")
Expand Down
67 changes: 54 additions & 13 deletions acceptance/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package acceptance
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"time"
Expand All @@ -26,6 +27,11 @@ var _ = Describe("Check", func() {
)

BeforeEach(func() {
var err error

By("Restoring environment variables")
RestoreEnvVars()

By("Creating command object")
command = exec.Command(checkPath)

Expand All @@ -45,27 +51,62 @@ var _ = Describe("Check", func() {
Version: concourse.Version{},
}

var err error
stdinContents, err = json.Marshal(checkRequest)
Expect(err).ShouldNot(HaveOccurred())
})

Describe("successful behavior", func() {
It("returns pipeline versions without error", func() {
By("Running the command")
session := run(command, stdinContents)
Context("with a test pipeline", func() {
var (
testPipelineDir string
testPipelineFilePath string
testPipelineName string
testPipelineCreated bool
)

By("Validating command exited without error")
Eventually(session, checkTimeout).Should(gexec.Exit(0))
BeforeEach(func() {
var err error

var resp concourse.CheckResponse
err := json.Unmarshal(session.Out.Contents(), &resp)
Expect(err).NotTo(HaveOccurred())
By("Creating temp directory")
testPipelineDir, err = ioutil.TempDir("", "concourse-pipeline-resource")
Expect(err).NotTo(HaveOccurred())

Expect(len(resp)).To(BeNumerically(">", 0))
for _, v := range resp {
Expect(v).NotTo(BeEmpty())
}
By("Creating random pipeline name")
testPipelineName = fmt.Sprintf("cp-resource-test-%d", time.Now().UnixNano())

By("Creating test pipeline config file")
testPipelineFilePath, err = CreateTestPipelineConfigFile(testPipelineDir, testPipelineName)
Expect(err).NotTo(HaveOccurred())

By("Creating a test pipeline")
err = SetTestPipeline(testPipelineName, testPipelineFilePath)
Expect(err).NotTo(HaveOccurred())
testPipelineCreated = true
})

AfterEach(func() {
if testPipelineCreated {
_, err := flyCommand.DestroyPipeline(testPipelineName)
Expect(err).NotTo(HaveOccurred())
}
})

It("returns pipeline versions without error", func() {
By("Running the command")
session := run(command, stdinContents)

By("Validating command exited without error")
Eventually(session, checkTimeout).Should(gexec.Exit(0))

var resp concourse.CheckResponse
err := json.Unmarshal(session.Out.Contents(), &resp)
Expect(err).NotTo(HaveOccurred())

Expect(len(resp)).To(BeNumerically(">", 0))
for _, v := range resp {
Expect(v).NotTo(BeEmpty())
}
})
})

Context("target not provided", func() {
Expand Down
84 changes: 70 additions & 14 deletions acceptance/in_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ var _ = Describe("In", func() {
BeforeEach(func() {
var err error

By("Restoring environment variables")
RestoreEnvVars()

By("Creating temp directory")
destDirectory, err = ioutil.TempDir("", "concourse-pipeline-resource")
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -62,20 +65,56 @@ var _ = Describe("In", func() {
})

Describe("successful behavior", func() {
It("downloads all pipeline configs to the target directory", func() {
By("Running the command")
session := run(command, stdinContents)
Context("with a test pipeline", func() {
var (
testPipelineDir string
testPipelineFilePath string
testPipelineName string
testPipelineCreated bool
)

Eventually(session, inTimeout).Should(gexec.Exit(0))
BeforeEach(func() {
var err error

files, err := ioutil.ReadDir(destDirectory)
Expect(err).NotTo(HaveOccurred())
By("Creating temp directory")
testPipelineDir, err = ioutil.TempDir("", "concourse-pipeline-resource")
Expect(err).NotTo(HaveOccurred())

Expect(len(files)).To(BeNumerically(">", 0))
for _, file := range files {
Expect(file.Name()).To(MatchRegexp(".*\\.yml"))
Expect(file.Size()).To(BeNumerically(">", 0))
}
By("Creating random pipeline name")
testPipelineName = fmt.Sprintf("cp-resource-test-%d", time.Now().UnixNano())

By("Creating test pipeline config file")
testPipelineFilePath, err = CreateTestPipelineConfigFile(testPipelineDir, testPipelineName)
Expect(err).NotTo(HaveOccurred())

By("Creating a test pipeline")
err = SetTestPipeline(testPipelineName, testPipelineFilePath)
Expect(err).NotTo(HaveOccurred())
testPipelineCreated = true
})

AfterEach(func() {
if testPipelineCreated {
_, err := flyCommand.DestroyPipeline(testPipelineName)
Expect(err).NotTo(HaveOccurred())
}
})

It("downloads all pipeline configs to the target directory", func() {
By("Running the command")
session := run(command, stdinContents)

Eventually(session, inTimeout).Should(gexec.Exit(0))

files, err := ioutil.ReadDir(destDirectory)
Expect(err).NotTo(HaveOccurred())

Expect(len(files)).To(BeNumerically(">", 0))
for _, file := range files {
Expect(file.Name()).To(MatchRegexp(".*\\.yml"))
Expect(file.Size()).To(BeNumerically(">", 0))
}
})
})

It("returns valid json", func() {
Expand All @@ -96,7 +135,7 @@ var _ = Describe("In", func() {
}
})

Context("target not provided", func() {
Context("target not provided by source", func() {
BeforeEach(func() {
var err error
err = os.Setenv("ATC_EXTERNAL_URL", inRequest.Source.Target)
Expand All @@ -108,15 +147,32 @@ var _ = Describe("In", func() {
Expect(err).ShouldNot(HaveOccurred())
})

It("uses ATC_EXTERNAL_URL instead", func() {
By("Running the command")
session := run(command, stdinContents)

By("Validating command ran successfully")
Eventually(session, inTimeout).Should(gexec.Exit(0))
})
})

Context("target not provided at all", func() {
BeforeEach(func() {
inRequest.Source.Target = ""

var err error
stdinContents, err = json.Marshal(inRequest)
Expect(err).ShouldNot(HaveOccurred())
})

It("exits with error", func() {
By("Running the command")
session := run(command, stdinContents)

By("Validating command exited with error")
Eventually(session, inTimeout).Should(gexec.Exit(1))
Expect(session.Err).Should(gbytes.Say(".*target.*specified"))
Expect(session.Err).Should(gbytes.Say("target must be provided in source"))
})

})
})

Expand Down
Loading

0 comments on commit 6b82ccb

Please sign in to comment.