Skip to content

Commit

Permalink
Bugfix/no ports invalid config (#29)
Browse files Browse the repository at this point in the history
* wip

* Update bug_report.md

* fix invalid configuration bug when debugger mode enabled but no port were exposed

* add test for this bug
  • Loading branch information
moshebe authored Jul 25, 2020
1 parent 9fa3b43 commit ba8dc47
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 39 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
.idea
gebug
.gebug
coverage.txt
20 changes: 0 additions & 20 deletions codecov.yml

This file was deleted.

65 changes: 47 additions & 18 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,25 @@ expose_ports:
},
wantErr: false,
},
{
input: `invalid yaml`,
wantErr: true,
},
{
input: `name: {{.NotExists}}`,
wantErr: true,
},
}

for _, test := range tests {
c, err := Load([]byte(test.input))
assert.NoError(t, err)
assert.NotNil(t, c)
assert.Equal(t, test.expected, c)
if test.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.NotNil(t, c)
assert.Equal(t, test.expected, c)
}
}
}

Expand Down Expand Up @@ -127,7 +139,23 @@ func generateFile(t *testing.T, prefix string, generate func(*Config, io.Writer)
}

func TestConfig_Write(t *testing.T) {
expected := `name: my-app
tests := []struct {
input *Config
expected string
wantErr bool
}{
{
input: &Config{
Name: "my-app",
OutputBinaryPath: "/app",
BuildCommand: "go build -o /app -gcflags=\"all=-N-l\"",
RunCommand: "/app",
RuntimeImage: "golang:1.14",
DebuggerPort: 40000,
DebuggerEnabled: true,
ExposePorts: []string{"8080", "8081:8081"},
},
expected: `name: my-app
output_binary: /app
build_command: go build -o /app -gcflags="all=-N-l"
run_command: /app
Expand All @@ -139,22 +167,23 @@ expose_ports:
- 8081:8081
networks: []
environment: []
`
c := Config{
Name: "my-app",
OutputBinaryPath: "/app",
BuildCommand: "go build -o /app -gcflags=\"all=-N-l\"",
RunCommand: "/app",
RuntimeImage: "golang:1.14",
DebuggerPort: 40000,
DebuggerEnabled: true,
ExposePorts: []string{"8080", "8081:8081"},
`,
wantErr: false,
},
}

buf := bytes.NewBufferString("")
err := c.Write(buf)
assert.NoError(t, err)
assert.Equal(t, expected, buf.String())
for i, test := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
buf := bytes.NewBufferString("")
err := test.input.Write(buf)
if test.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, test.expected, buf.String())
}
})
}
}

func TestConfig_updateBuildCommand(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ services:
{{- end}}
volumes:
- ../:/src:ro
{{- if .ExposePorts}}
{{- if or .ExposePorts .DebuggerEnabled}}
ports:
{{- range $key, $value := .ExposePorts}}
- {{$value}}
Expand Down
12 changes: 12 additions & 0 deletions pkg/config/testdata/generate_docker_compose_4.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: '3'
services:
gebug-my-app:
build:
context: ..
dockerfile: .gebug/Dockerfile
cap_add:
- SYS_PTRACE
volumes:
- ../:/src:ro
ports:
- 40000:40000
10 changes: 10 additions & 0 deletions pkg/config/testdata/generate_docker_compose_4.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: my-app
output_binary: /app
build_command: go build -o {{.output_binary}}
run_command: '{{.output_binary}}'
runtime_image: golang:1.14
debugger_enabled: true
debugger_port: 40000
expose_ports: []
networks: []
environment: []

0 comments on commit ba8dc47

Please sign in to comment.