-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: support for go library use case (#7)
* refactor: support for go library use case Signed-off-by: Kaan Yagci <[email protected]> * test: add expected rendered Dockerfile Signed-off-by: Kaan Yagci <[email protected]> * test(core): add render tests Signed-off-by: Kaan Yagci <[email protected]> * chore: add test makefile target Signed-off-by: Kaan Yagci <[email protected]> * chore(gha): add a workflow to build and test Signed-off-by: Kaan Yagci <[email protected]> --------- Signed-off-by: Kaan Yagci <[email protected]>
- Loading branch information
Showing
8 changed files
with
280 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
name: Ensure version increment | ||
on: | ||
pull_request: | ||
branches: | ||
- main | ||
types: | ||
- opened | ||
- synchronize | ||
- reopened | ||
paths: | ||
- cli/*$ | ||
- core/** | ||
- docker-plugins/** | ||
- example/** | ||
concurrency: | ||
group: "${{ github.workflow_ref }} - ${{ github.ref }} - ${{ github.event_name }}" | ||
cancel-in-progress: true | ||
jobs: | ||
build_cli: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/[email protected] | ||
with: | ||
fetch-depth: 1 | ||
- name: Setup Go 1.22.x | ||
uses: actions/setup-go@v5 | ||
with: | ||
go-version: "1.22.x" | ||
- name: Build the CLI | ||
run: make build | ||
build_plugins: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/[email protected] | ||
with: | ||
fetch-depth: 1 | ||
- name: Setup Go 1.22.x | ||
uses: actions/setup-go@v5 | ||
with: | ||
go-version: "1.22.x" | ||
- name: Build Docker plugins | ||
run: make build-plugins | ||
test_docker_plugins: | ||
runs-on: ubuntu-latest | ||
needs: build_plugins | ||
steps: | ||
- uses: actions/[email protected] | ||
with: | ||
fetch-depth: 1 | ||
- name: Setup Go 1.22.x | ||
uses: actions/setup-go@v5 | ||
with: | ||
go-version: "1.22.x" | ||
- name: Create plugins directory | ||
run: mkdir -p ~/.docker/cli-plugins/ | ||
- name: Build Docker plugins | ||
run: make install-plugins | ||
test: | ||
runs-on: ubuntu-latest | ||
needs: [build_cli, build_plugins] | ||
steps: | ||
- uses: actions/[email protected] | ||
with: | ||
fetch-depth: 1 | ||
- name: Setup Go 1.22.x | ||
uses: actions/setup-go@v5 | ||
with: | ||
go-version: '1.22.x' | ||
- name: Test | ||
run: make test | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package wharf | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
const expectedRenderedDockerfile = ` | ||
# Use an official base image | ||
FROM ubuntu:latest | ||
# Set the exposed port | ||
EXPOSE 9091 | ||
# Set environment variables | ||
ENV DEBUG="false" | ||
ENV LOG_LEVEL="info" | ||
# Copy source code | ||
COPY . /app | ||
# Run command | ||
CMD echo Hello World | ||
` | ||
|
||
func TestRenderToString(t *testing.T) { | ||
var stringBuilder strings.Builder | ||
err := Render("../example/", "Dockerfile.template", "docker-values.yaml", &stringBuilder) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
generated := strings.TrimSpace(stringBuilder.String()) | ||
expected := strings.TrimSpace(expectedRenderedDockerfile) | ||
|
||
if generated != expected { | ||
t.Log("Strings are not equal") | ||
t.Logf("Generated length: %d", len(generated)) | ||
t.Logf("Expected length: %d", len(expected)) | ||
|
||
for i := 0; i < len(generated) && i < len(expected); i++ { | ||
if generated[i] != expected[i] { | ||
t.Errorf("Difference at char %d: '%c' != '%c'", i, generated[i], expected[i]) | ||
break | ||
} | ||
} | ||
|
||
if len(generated) != len(expected) { | ||
t.Error("Generated and expected strings have different lengths") | ||
} | ||
} | ||
} | ||
|
||
func TestRenderToOutputFile(t *testing.T) { | ||
// Render the template to a file | ||
// Compare result with the expected one | ||
file, err := os.CreateTemp(os.TempDir(), "Dockerfile") | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
defer func() { | ||
file.Close() | ||
os.Remove(file.Name()) | ||
}() | ||
err = Render("../example", "Dockerfile.template", "docker-values.yaml", file) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
equals, err := compareFiles(file, "../example/Dockerfile.expected") | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if !equals { | ||
t.Error("Files are not equals") | ||
} | ||
} | ||
|
||
// CompareFiles checks if the contents of two files are the same. | ||
func compareFiles(file1 *os.File, file2 string) (bool, error) { | ||
file1.Seek(0, io.SeekStart) | ||
f2, err := os.Open(file2) | ||
if err != nil { | ||
return false, err | ||
} | ||
defer f2.Close() | ||
|
||
const bufferSize = 4096 | ||
buf1 := make([]byte, bufferSize) | ||
buf2 := make([]byte, bufferSize) | ||
|
||
for { | ||
n1, err1 := file1.Read(buf1) | ||
n2, err2 := f2.Read(buf2) | ||
|
||
if n1 != n2 || err1 != nil || err2 != nil { | ||
if err1 == io.EOF && err2 == io.EOF { | ||
return true, nil // End of both files reached | ||
} | ||
return false, nil | ||
} | ||
|
||
if n1 == 0 && n2 == 0 { | ||
break // End of both files reached | ||
} | ||
|
||
if !compareChunks(buf1[:n1], buf2[:n2]) { | ||
return false, nil | ||
} | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
// compareChunks checks if two byte slices are equal. | ||
func compareChunks(chunk1, chunk2 []byte) bool { | ||
if len(chunk1) != len(chunk2) { | ||
return false | ||
} | ||
for i := range chunk1 { | ||
if chunk1[i] != chunk2[i] { | ||
return false | ||
} | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
package core | ||
package wharf | ||
|
||
const Version = "0.0.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Use an official base image | ||
FROM ubuntu:latest | ||
|
||
# Set the exposed port | ||
EXPOSE 9091 | ||
|
||
# Set environment variables | ||
|
||
ENV DEBUG="false" | ||
|
||
ENV LOG_LEVEL="info" | ||
|
||
|
||
# Copy source code | ||
COPY . /app | ||
|
||
# Run command | ||
CMD echo Hello World |