Skip to content

Commit 87ad6fb

Browse files
committed
tests: config tests
1 parent d7f94f5 commit 87ad6fb

File tree

7 files changed

+132
-12
lines changed

7 files changed

+132
-12
lines changed

.github/workflows/main.yml

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
name: Main
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
9+
env:
10+
GO_VERSION: 1.23
11+
CGO_ENABLED: 0
12+
13+
jobs:
14+
lint:
15+
name: Go Lint
16+
runs-on: ubuntu-latest
17+
env:
18+
GOLANGCI_LINT_VERSION: v2.5.0
19+
defaults:
20+
run:
21+
working-directory: ${{ github.workspace }}/go/src/github.com/${{ github.repository }}
22+
23+
steps:
24+
# https://github.com/marketplace/actions/setup-go-environment
25+
- name: Set up Go ${{ env.GO_VERSION }}
26+
uses: actions/setup-go@bfdd3570ce990073878bf10f6b2d79082de49492 # v2
27+
with:
28+
go-version: ${{ env.GO_VERSION }}
29+
30+
# https://github.com/marketplace/actions/checkout
31+
- name: Check out code
32+
uses: actions/checkout@ee0669bd1cc54295c223e0bb666b733df41de1c5 # v2
33+
with:
34+
path: go/src/github.com/${{ github.repository }}
35+
fetch-depth: 0
36+
37+
# https://golangci-lint.run/usage/install#other-ci
38+
- name: Install golangci-lint ${{ env.GOLANGCI_LINT_VERSION }}
39+
run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin ${GOLANGCI_LINT_VERSION}
40+
41+
- name: Setup GOPATH
42+
run: go env -w GOPATH=${{ github.workspace }}/go
43+
44+
- name: gofmt
45+
run: |
46+
if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then
47+
gofmt -s -d .
48+
exit 1
49+
fi
50+
51+
- name: Golangci-lint
52+
run: make lint
53+
54+
test:
55+
name: Go Tests
56+
runs-on: ubuntu-latest
57+
defaults:
58+
run:
59+
working-directory: ${{ github.workspace }}/go/src/github.com/${{ github.repository }}
60+
61+
steps:
62+
- name: Set up Go ${{ env.GO_VERSION }}
63+
uses: actions/setup-go@bfdd3570ce990073878bf10f6b2d79082de49492 # v2
64+
with:
65+
go-version: ${{ env.GO_VERSION }}
66+
67+
- name: Check out code
68+
uses: actions/checkout@ee0669bd1cc54295c223e0bb666b733df41de1c5 # v2
69+
with:
70+
path: go/src/github.com/${{ github.repository }}
71+
fetch-depth: 0
72+
73+
- name: Cache Go modules
74+
uses: actions/cache@6f8efc29b200d32929f49075959781ed54ec270c # v3
75+
with:
76+
path: ${{ github.workspace }}/go/pkg/mod
77+
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
78+
restore-keys: |
79+
${{ runner.os }}-go-
80+
81+
- name: Setup GOPATH
82+
run: go env -w GOPATH=${{ github.workspace }}/go
83+
84+
- name: Run tests
85+
run: make test
86+
87+
- name: Upload coverage reports to Codecov
88+
uses: codecov/codecov-action@671740ac38dd9b0130fbe1cec585b89eea48d3de # v5
89+
with:
90+
token: ${{ secrets.CODECOV_TOKEN }}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
coverage.out
2+
13
lib/
24
containers/
35
currunt

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ default: build run
55
build:
66
CGO_ENABLED=0 go build
77

8+
test:
9+
go test -v -cover -coverprofile=coverage.out ./...
10+
11+
lint:
12+
golangci-lint run
13+
814
run:
915
sudo ./currunt run -image ghcr.io/holysoles/ginrcon@sha256:f1a0a09ad95ae203006e2d57a38e5bf2e257a7fdbd449d81ff32796c06cc5871 -interactive -tty -entrypoint "/bin/sh"
1016

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func main() {
7171
os.Exit(1)
7272
}
7373

74-
config, err := config.Load()
74+
config, err := config.Load("")
7575
if err != nil {
7676
fmt.Printf("error loading config: %s\n", err)
7777
os.Exit(1)

pkg/config/config.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
)
88

99
const (
10-
// DOCKER_CONFIG_PATH = "/root/.docker/config.json"
1110
DOCKER_CONFIG_PATH = "/home/patrick/.docker/config.json"
1211
IMAGE_LIB = "./lib"
1312
)
@@ -17,21 +16,25 @@ type AuthConfig struct {
1716
}
1817
type CurruntConfig struct {
1918
Auths map[string]AuthConfig `json:"auths"`
20-
LibraryPath string `json:library_path`
19+
LibraryPath string `json:"library_path"`
2120
}
2221

23-
func Load() (CurruntConfig, error) {
24-
cPath := DOCKER_CONFIG_PATH
25-
22+
// Load returns the Currunt Configuration, loaded from the DOCKER_CONFIG_PATH constant.
23+
func Load(path string) (CurruntConfig, error) {
2624
var c CurruntConfig
2725
c.LibraryPath = IMAGE_LIB
28-
_, err := os.Stat(cPath)
29-
// no config file is okay
30-
if err != nil {
31-
return c, nil
26+
27+
if path == "" {
28+
path = DOCKER_CONFIG_PATH
29+
} else {
30+
// should exist if specified
31+
_, err := os.Stat(path)
32+
if err != nil {
33+
return c, nil
34+
}
3235
}
3336

34-
cF, err := os.Open(cPath)
37+
cF, err := os.OpenFile(path, os.O_CREATE|os.O_RDONLY, 0666)
3538
if err != nil {
3639
return c, err
3740
}

pkg/config/config_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package config
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestLoadDefault(t *testing.T) {
8+
_, err := Load("")
9+
if err != nil {
10+
t.Errorf("unexpected error loading configuration file: %v", err)
11+
}
12+
}
13+
14+
func TestLoadBadPath(t *testing.T) {
15+
_, err := Load("/notexist.config")
16+
if err != nil {
17+
t.Errorf("unexpected error loading configuration file: %v", err)
18+
}
19+
}

pkg/oci/image.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ func PullImage(i string, config config.CurruntConfig) (string, error) {
404404
// check if we were provided an index digest. If so, get the image digest
405405
_, err = getManifest(&client, &headers, image, iPath, regInfo)
406406
if err != nil {
407-
fmt.Println("DEBUG: looking up image index, provided digest apparently not manifest: %v", err)
407+
fmt.Printf("DEBUG: looking up image index, provided digest apparently not manifest: %v\n", err)
408408
mIdx, err := getManifestIndex(&client, &headers, &image, &regInfo)
409409
if err != nil {
410410
return imageID, err

0 commit comments

Comments
 (0)