Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove ofcourse 4: put part 1 #88

Merged
merged 8 commits into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ RUN go mod download
#
COPY . .

RUN go test ./... && \
go install \
-ldflags "-w -X 'github.com/Pix4D/cogito/resource.buildinfo=$BUILD_INFO'" \
./cmd/cogito \
./cmd/out
RUN go test ./cogito ./cmd/cogito
RUN go install \
marco-m-pix4d marked this conversation as resolved.
Show resolved Hide resolved
-ldflags "-w -X 'github.com/Pix4D/cogito/cogito.buildinfo=$BUILD_INFO'" \
./cmd/cogito

#
# The final image
Expand All @@ -38,4 +37,5 @@ RUN mkdir -p /opt/resource
COPY --from=builder /root/go/bin/* /opt/resource/

RUN ln -s /opt/resource/cogito /opt/resource/check && \
ln -s /opt/resource/cogito /opt/resource/in
ln -s /opt/resource/cogito /opt/resource/in && \
ln -s /opt/resource/cogito /opt/resource/out
45 changes: 40 additions & 5 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ vars:
DOCKER_BASE_NAME: "{{.DOCKER_ORG}}/{{.DOCKER_IMAGE}}"
DOCKER_FULL_NAME: "{{.DOCKER_BASE_NAME}}:{{.DOCKER_TAG}}"
BUILD_INFO: "Tag: {{.DOCKER_TAG}}, commit: {{.COMMIT}}, build date: {{.DATE}}"
LDFLAGS: -w -X '{{.REPO}}/resource.buildinfo={{.BUILD_INFO}}'
LDFLAGS: -w -X '{{.REPO}}/cogito.buildinfo={{.BUILD_INFO}}'
#
SMOKE_INPUT: >
{
Expand Down Expand Up @@ -63,21 +63,56 @@ tasks:
desc: Run the unit tests.
cmds:
# One day I will understand how to use -coverpkg=./... :-(
- go test -count=1 -short -coverprofile=coverage.out ./...
# FIXME - go test -count=1 -short -coverprofile=coverage.out ./...
marco-m-pix4d marked this conversation as resolved.
Show resolved Hide resolved
- go test -count=1 -short -coverprofile=coverage.out ./cogito ./cmd/cogito

test:all:
desc: Run all the tests (unit + integration). Use this target to get total coverage.
cmds:
- go test -count=1 -coverprofile=coverage.out ./...
# FIXME - go test -count=1 -coverprofile=coverage.out ./...
- go test -count=1 -short -coverprofile=coverage.out ./cogito ./cmd/cogito
env: *test-env

test:smoke:
desc: Simple smoke test of the local executables.
cmds:
- task: build
- task: build:copydir
- task: test:buildinfo
- echo '{{.SMOKE_INPUT}}' | ./bin/check
- echo
- echo '{{.SMOKE_INPUT}}' | ./bin/in dummy-dir
- echo
- rm -rf /tmp/cogito-test
- mkdir -p /tmp/cogito-test
- >
./bin/copydir cogito/testdata/one-repo/a-repo /tmp/cogito-test --dot
--template repo_url=https://github.com/foo/bar head=dummyHead
branch_name=dummyBranch commit_sha=dummySha
Comment on lines +89 to +91
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marco-m-pix4d, this is actually the line that triggered my comment on renaming copydir. I didn't think of it being used as a function in go code where a std lib copy isn't available. Just renaming the binary would be enough, IMO.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good point. Suggestions for the name of this executable? Same as in the previous comment: templatedir or fleshoutdir?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I prefer templatedir.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@odormond
addressed in last PR of the series.

- echo '{{.PUT_INPUT}}' | ./bin/out /tmp/cogito-test
marco-m-pix4d marked this conversation as resolved.
Show resolved Hide resolved
- echo
vars:
PUT_INPUT: >
{
"source": {"owner": "foo", "repo": "bar", "access_token": "123", "log_level": "debug"},
"params": {"state": "success"}
}

test:buildinfo:
desc: Verify that the executable contains build information
# cogito: This is the Cogito GitHub status resource. unknown
# cogito: This is the Cogito GitHub status resource. Tag: buildinfo, commit: e9b36d0814, build date: 2022-07-26
cmds:
# "unknown" is the default value, printed when built without linker flags.
- 'echo {{.OUTPUT}} | grep -v unknown'
- 'echo {{.OUTPUT}} | grep Tag:'
- 'echo {{.OUTPUT}} | grep commit:'
vars:
INPUT: '{"source": {"owner": "foo", "repo": "bar", "access_token": "123"}}'
OUTPUT:
# We only want to capture stderr, because the Cogito resource protocol uses
# stderr for logging.
sh: echo '{{.INPUT}}' | ./bin/check 2>&1 1>/dev/null

browser:
desc: "Show code coverage in browser (usage: task test:<subtarget> browser)"
Expand All @@ -89,10 +124,10 @@ tasks:
dir: bin
cmds:
- go build -ldflags "{{.LDFLAGS}}" ../cmd/cogito
- go build -ldflags "{{.LDFLAGS}}" ../cmd/out
- go build -ldflags "{{.LDFLAGS}}" ../cmd/hello
# FIXME - go build -ldflags "{{.LDFLAGS}}" ../cmd/hello
- ln -sf cogito check
- ln -sf cogito in
- ln -sf cogito out

build:copydir:
desc: Build copydir (development helper, normally not needed).
Expand Down
7 changes: 4 additions & 3 deletions cmd/cogito/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"path"

"github.com/Pix4D/cogito/cogito"
"github.com/Pix4D/cogito/resource"
"github.com/hashicorp/go-hclog"
)

Expand All @@ -19,7 +18,7 @@ func main() {
// - stderr for logging
// See: https://concourse-ci.org/implementing-resource-types.html
if err := run(os.Stdin, os.Stdout, os.Stderr, os.Args); err != nil {
fmt.Fprintln(os.Stderr, err)
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
}
}
Expand All @@ -30,14 +29,16 @@ func run(in io.Reader, out io.Writer, logOut io.Writer, args []string) error {
Output: logOut,
DisableTime: true,
})
log.Info(resource.BuildInfo())
log.Info(cogito.BuildInfo())

command := path.Base(args[0])
switch command {
case "check":
return cogito.Check(log, in, out, args[1:])
case "in":
return cogito.Get(log, in, out, args[1:])
case "out":
return cogito.Put(log, in, out, args[1:])
default:
return fmt.Errorf(
"cogito: unexpected invocation as '%s'; want: one of 'check', 'in', 'out'; (command-line: %s)",
Expand Down
81 changes: 44 additions & 37 deletions cmd/cogito/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,66 @@ import (
"strings"
"testing"

"github.com/Pix4D/cogito/testhelp"
"gotest.tools/v3/assert"
)

func TestRunSmokeSuccess(t *testing.T) {
type testCase struct {
name string
args []string
in string
}

test := func(t *testing.T, tc testCase) {
in := strings.NewReader(tc.in)
var out bytes.Buffer
var logOut bytes.Buffer
func TestRunCheckSmokeSuccess(t *testing.T) {
in := strings.NewReader(`
{
"source": {
"owner": "the-owner",
"repo": "the-repo",
"access_token": "the-secret",
"log_level": "debug"
}
}`)
var out bytes.Buffer
var logOut bytes.Buffer

err := run(in, &out, &logOut, tc.args)
err := run(in, &out, &logOut, []string{"check"})

assert.NilError(t, err, "\nout: %s\nlogOut: %s", out.String(), logOut.String())
}
assert.NilError(t, err, "\nout: %s\nlogOut: %s", out.String(), logOut.String())
}

testCases := []testCase{
{
name: "check",
args: []string{"check"},
in: `
func TestRunGetSmokeSuccess(t *testing.T) {
in := strings.NewReader(`
{
"source": {
"owner": "the-owner",
"repo": "the-repo",
"access_token": "the-secret"
}
}`,
},
{
name: "get",
args: []string{"in", "dummy-dir"},
in: `
"access_token": "the-secret",
"log_level": "debug"
},
"version": {"ref": "pizza"}
}`)
var out bytes.Buffer
var logOut bytes.Buffer

err := run(in, &out, &logOut, []string{"in", "dummy-dir"})

assert.NilError(t, err, "\nout: %s\nlogOut: %s", out.String(), logOut.String())
}

func TestRunPutSmokeSuccess(t *testing.T) {
in := strings.NewReader(`
{
"source": {
"owner": "the-owner",
"repo": "the-repo",
"access_token": "the-secret"
"access_token": "the-secret",
"log_level": "debug"
},
"version": {"ref": "pizza"}
}`,
},
}
"params": {"state": "pending"}
}`)
var out bytes.Buffer
var logOut bytes.Buffer
inputDir := testhelp.MakeGitRepoFromTestdata(t, "../../cogito/testdata/one-repo/a-repo",
testhelp.HttpsRemote("the-owner", "the-repo"), "dummySHA", "dummyHead")

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
test(t, tc)
})
}
err := run(in, &out, &logOut, []string{"out", inputDir})

assert.NilError(t, err, "\nout: %s\nlogOut: %s", out.String(), logOut.String())
}

func TestRunSmokeFailure(t *testing.T) {
Expand Down
21 changes: 9 additions & 12 deletions cmd/copydir/main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Useful when developing help.CopyDir.
// Useful when developing testhelp.CopyDir.
marco-m-pix4d marked this conversation as resolved.
Show resolved Hide resolved
package main

import (
"fmt"
"os"
"strings"

arg "github.com/alexflint/go-arg"
"github.com/alexflint/go-arg"

"github.com/Pix4D/cogito/help"
"github.com/Pix4D/cogito/testhelp"
)

func main() {
Expand All @@ -24,26 +24,23 @@ func main() {
fmt.Println("error:", err)
os.Exit(1)
}
src := os.Args[1]
dst := os.Args[2]
fmt.Println("src:", src, "dst:", dst)

var renamer help.Renamer
var renamer testhelp.Renamer
if args.Dot {
renamer = help.DotRenamer
renamer = testhelp.DotRenamer
} else {
renamer = help.IdentityRenamer
renamer = testhelp.IdentityRenamer
}

if err := help.CopyDir(dst, src, renamer, templateData); err != nil {
if err := testhelp.CopyDir(args.Dst, args.Src, renamer, templateData); err != nil {
fmt.Println("error:", err)
os.Exit(1)
}
}

// Take a list of strings of the form "key=value" and convert them to map entries.
func makeTemplateData(keyvals []string) (help.TemplateData, error) {
data := help.TemplateData{}
func makeTemplateData(keyvals []string) (testhelp.TemplateData, error) {
data := testhelp.TemplateData{}
for _, keyval := range keyvals {
pos := strings.Index(keyval, "=")
if pos == -1 {
Expand Down
10 changes: 0 additions & 10 deletions cmd/out/main.go

This file was deleted.

11 changes: 11 additions & 0 deletions cogito/buildinfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package cogito
marco-m-pix4d marked this conversation as resolved.
Show resolved Hide resolved

// Baked in at build time with the linker. See the Taskfile and the Dockerfile.
var buildinfo = "unknown"

// BuildInfo returns human-readable build information (tag, git commit, date, ...).
// This is useful to understand in the Concourse UI and logs which resource it is, since log
// output in Concourse doesn't mention the name of the resource (or task) generating it.
func BuildInfo() string {
return "This is the Cogito GitHub status resource. " + buildinfo
}
2 changes: 1 addition & 1 deletion cogito/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func Check(log hclog.Logger, in io.Reader, out io.Writer, args []string) error {
// could not be empty. Now (2022-07) it seems that it could indeed be empty.
// For the time being we keep it as-is because this maintains the previous behavior.
// This will be investigated by PCI-2617.
versions := []Version{{Ref: "dummy"}}
versions := []Version{DummyVersion}
enc := json.NewEncoder(out)
if err := enc.Encode(versions); err != nil {
return fmt.Errorf("check: %s", err)
Expand Down
2 changes: 1 addition & 1 deletion cogito/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func Get(log hclog.Logger, in io.Reader, out io.Writer, args []string) error {
// and put there the requested version of the resource.
// In this resource we do nothing, but we still check for protocol conformance.
if len(args) == 0 {
return fmt.Errorf("get: missing output directory from arguments")
return fmt.Errorf("get: arguments: missing output directory")
}
log.Debug("", "output-directory", args[0])

Expand Down
3 changes: 1 addition & 2 deletions cogito/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func TestGetFailure(t *testing.T) {
source: baseSource,
version: cogito.Version{Ref: "dummy"},
writer: io.Discard,
wantErr: "get: missing output directory from arguments",
wantErr: "get: arguments: missing output directory",
},
{
name: "system write error",
Expand All @@ -127,7 +127,6 @@ func TestGetFailure(t *testing.T) {
},
{
name: "system read error",
source: cogito.Source{},
reader: iotest.ErrReader(errors.New("test read error")),
writer: io.Discard,
wantErr: "get: parsing JSON from stdin: test read error",
Expand Down
Loading