-
-
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.
* ci: remove go test stage in Dockerfile * feat: add tests * fix: decrease required docker api version to v1.40 * fix: stream test logs * ci: remove permissions * ci: run build workflow only on tags * fix: add app setting to specify docker api version * fix: adjust docker api version setting * fix: adjust docker api version setting * fix: adjust docker api version setting * fix: add config validator * test: adjust test command * test: add coverage generate * test: adjust docker compose tests * test: adjust docker compose tests * test: add git tests * test: adjust Makefile coverage command * test: add app config tests * docs: update wiki * test: add logger tests * docs: update wiki * refactor: go mod tidy * refactor: move verifyProviderSecret function to security.go * refactor: move verifyProviderSecret function to security.go * test: add parse tests * test: remove parse tests
- Loading branch information
Showing
20 changed files
with
677 additions
and
50 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 |
---|---|---|
|
@@ -4,9 +4,6 @@ on: | |
push: | ||
tags: | ||
- 'v*' | ||
pull_request: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
build: | ||
|
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,25 @@ | ||
name: Tests | ||
|
||
on: | ||
workflow_dispatch: | ||
push: | ||
paths-ignore: | ||
- 'README.md' | ||
- 'docs/**' | ||
|
||
jobs: | ||
test: | ||
name: Run Acceptance Tests | ||
runs-on: ubuntu-latest | ||
timeout-minutes: 10 | ||
steps: | ||
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 | ||
- uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # v5.0.2 | ||
with: | ||
go-version-file: 'go.mod' | ||
cache: true | ||
- run: go mod download | ||
- run: go test -v -cover ./... -timeout 5m | ||
timeout-minutes: 5 | ||
env: | ||
WEBHOOK_SECRET: test_Secret1 |
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 |
---|---|---|
|
@@ -24,3 +24,6 @@ go.work.sum | |
|
||
*.env | ||
.env | ||
|
||
cover.out | ||
cover.html |
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
Submodule docs
updated
from 159384 to 681b02
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 |
---|---|---|
@@ -1,25 +1,47 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
"github.com/caarlos0/env/v11" | ||
"gopkg.in/validator.v2" | ||
"strings" | ||
) | ||
|
||
// AppConfig is used to configure this application | ||
type AppConfig struct { | ||
LogLevel string `env:"LOG_LEVEL,required" envDefault:"info"` // LogLevel is the log level for the application | ||
HttpPort uint16 `env:"HTTP_PORT,required" envDefault:"80"` // HttpPort is the port the HTTP server will listen on | ||
WebhookSecret string `env:"WEBHOOK_SECRET,required"` // WebhookSecret is the secret used to authenticate the webhook | ||
GitAccessToken string `env:"GIT_ACCESS_TOKEN"` // GitAccessToken is the access token used to authenticate with the Git server (e.g. GitHub) for private repositories | ||
AuthType string `env:"AUTH_TYPE" envDefault:"oauth2"` // AuthType is the type of authentication to use when cloning repositories | ||
SkipTLSVerification bool `env:"SKIP_TLS_VERIFICATION" envDefault:"false"` // SkipTLSVerification skips the TLS verification when cloning repositories. | ||
LogLevel string `env:"LOG_LEVEL,required" envDefault:"info"` // LogLevel is the log level for the application | ||
HttpPort uint16 `env:"HTTP_PORT,required" envDefault:"80" validate:"min=1,max=65535"` // HttpPort is the port the HTTP server will listen on | ||
WebhookSecret string `env:"WEBHOOK_SECRET,required"` // WebhookSecret is the secret used to authenticate the webhook | ||
GitAccessToken string `env:"GIT_ACCESS_TOKEN"` // GitAccessToken is the access token used to authenticate with the Git server (e.g. GitHub) for private repositories | ||
AuthType string `env:"AUTH_TYPE" envDefault:"oauth2"` // AuthType is the type of authentication to use when cloning repositories | ||
SkipTLSVerification bool `env:"SKIP_TLS_VERIFICATION" envDefault:"false"` // SkipTLSVerification skips the TLS verification when cloning repositories. | ||
DockerAPIVersion string `env:"DOCKER_API_VERSION" envDefault:"v1.40" validate:"regexp=^v[0-9]+\\.[0-9]+$"` // DockerAPIVersion is the version of the Docker API to use | ||
} | ||
|
||
var ( | ||
ErrInvalidLogLevel = validator.TextErr{Err: errors.New("invalid log level, must be one of debug, info, warn, error")} | ||
ErrInvalidDockerAPIVersion = validator.TextErr{Err: errors.New("invalid Docker API version format, must be e.g. v1.40")} | ||
) | ||
|
||
// GetAppConfig returns the configuration | ||
func GetAppConfig() (*AppConfig, error) { | ||
cfg := AppConfig{} | ||
if err := env.Parse(&cfg); err != nil { | ||
return nil, err | ||
} | ||
|
||
logLvl := strings.ToLower(cfg.LogLevel) | ||
if logLvl != "debug" && logLvl != "info" && logLvl != "warn" && logLvl != "error" { | ||
return nil, ErrInvalidLogLevel | ||
} | ||
|
||
if err := validator.Validate(cfg); err != nil { | ||
if strings.Contains(err.Error(), "DockerAPIVersion") { | ||
return nil, ErrInvalidDockerAPIVersion | ||
} | ||
|
||
return nil, err | ||
} | ||
|
||
return &cfg, nil | ||
} |
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 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestGetAppConfig(t *testing.T) { | ||
// Set up test cases | ||
tests := []struct { | ||
name string | ||
envVars map[string]string | ||
expectedErr error | ||
}{ | ||
{ | ||
name: "valid config", | ||
envVars: map[string]string{ | ||
"LOG_LEVEL": "info", | ||
"HTTP_PORT": "8080", | ||
"WEBHOOK_SECRET": "secret", | ||
"AUTH_TYPE": "oauth2", | ||
"DOCKER_API_VERSION": "v1.40", | ||
"GIT_ACCESS_TOKEN": "token", | ||
"SKIP_TLS_VERIFICATION": "false", | ||
}, | ||
expectedErr: nil, | ||
}, | ||
{ | ||
name: "invalid log level", | ||
envVars: map[string]string{ | ||
"LOG_LEVEL": "invalid", | ||
"WEBHOOK_SECRET": "secret", | ||
}, | ||
expectedErr: ErrInvalidLogLevel, | ||
}, | ||
{ | ||
name: "invalid API version", | ||
envVars: map[string]string{ | ||
"LOG_LEVEL": "info", | ||
"WEBHOOK_SECRET": "secret", | ||
"DOCKER_API_VERSION": "1.40", | ||
}, | ||
expectedErr: ErrInvalidDockerAPIVersion, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
// Set up the environment | ||
for k, v := range tt.envVars { | ||
if err := os.Setenv(k, v); err != nil { | ||
t.Fatalf("failed to set environment variable: %v", err) | ||
} | ||
} | ||
|
||
// Run the test | ||
_, err := GetAppConfig() | ||
if !errors.Is(err, tt.expectedErr) { | ||
t.Errorf("expected error to be '%v', got '%v'", tt.expectedErr, err) | ||
} | ||
if err == nil { | ||
// Clean up the environment | ||
for k := range tt.envVars { | ||
if err := os.Unsetenv(k); err != nil { | ||
t.Fatalf("failed to unset environment variable: %v", err) | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} |
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,112 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
var projectName = "test" | ||
|
||
func createTestFile(fileName string, content string) error { | ||
err := os.WriteFile(fileName, []byte(content), 0o644) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func createTmpDir(t *testing.T) string { | ||
dirName, err := os.MkdirTemp(os.TempDir(), "test-*") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
return dirName | ||
} | ||
|
||
func TestGetDeployConfig(t *testing.T) { | ||
fileName := ".compose-deploy.yaml" | ||
reference := "refs/heads/test" | ||
workingDirectory := "/test" | ||
composeFiles := []string{"test.compose.yaml"} | ||
|
||
deployConfig := fmt.Sprintf(`name: %s | ||
reference: %s | ||
working_dir: %s | ||
compose_files: | ||
- %s | ||
`, projectName, reference, workingDirectory, composeFiles[0]) | ||
|
||
dirName := createTmpDir(t) | ||
defer func(path string) { | ||
err := os.RemoveAll(path) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
}(dirName) | ||
|
||
filePath := filepath.Join(dirName, fileName) | ||
|
||
err := createTestFile(filePath, deployConfig) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
config, err := GetDeployConfig(dirName, projectName) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if config.Name != projectName { | ||
t.Errorf("expected name to be %v, got %s", projectName, config.Name) | ||
} | ||
|
||
if config.Reference != reference { | ||
t.Errorf("expected reference to be %v, got %s", reference, config.Reference) | ||
} | ||
|
||
if config.WorkingDirectory != workingDirectory { | ||
t.Errorf("expected working directory to be '%v', got '%s'", workingDirectory, config.WorkingDirectory) | ||
} | ||
|
||
if !reflect.DeepEqual(config.ComposeFiles, composeFiles) { | ||
t.Errorf("expected compose files to be %v, got %v", composeFiles, config.ComposeFiles) | ||
} | ||
} | ||
|
||
func TestGetDeployConfig_Default(t *testing.T) { | ||
defaultConfig := DefaultDeployConfig(projectName) | ||
|
||
dirName := createTmpDir(t) | ||
defer func(path string) { | ||
err := os.RemoveAll(path) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
}(dirName) | ||
|
||
config, err := GetDeployConfig(dirName, projectName) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if config.Name != projectName { | ||
t.Errorf("expected name to be %v, got %s", projectName, config.Name) | ||
} | ||
|
||
if config.Reference != defaultConfig.Reference { | ||
t.Errorf("expected reference to be %s, got %s", defaultConfig.Reference, config.Reference) | ||
} | ||
|
||
if config.WorkingDirectory != defaultConfig.WorkingDirectory { | ||
t.Errorf("expected working directory to be %s, got %s", defaultConfig.WorkingDirectory, config.WorkingDirectory) | ||
} | ||
|
||
if !reflect.DeepEqual(config.ComposeFiles, defaultConfig.ComposeFiles) { | ||
t.Errorf("expected compose files to be %v, got %v", defaultConfig.ComposeFiles, config.ComposeFiles) | ||
} | ||
} |
Oops, something went wrong.