Skip to content

Commit

Permalink
break: disable reaper at config level (#941)
Browse files Browse the repository at this point in the history
* chore: move TestContainers config to a separate file

* chore: rename to readConfig

* break: rename struct to honour project's name

* chore: add names to test table

* chore: support for disabling Ryuk at the configuration layer

* chore: rename to doReadConfig

* chore: read tc config just once

* chore: deprecate SkipReaper

* chore: add a pipeline for tests without Ryuk

* docs: document Ryuk

* chore: use same message when disabling Ryuk

* chore: print reaper banner when reading properties for first time

* chore: expose reading TC config

* break: do not return the config when retrieving a Docker client

* break: do not return the Docker Host when retrieving a Docker client

It comes in the TC config

* chore: simplify reaper-off pipeline

* chore: skip reaper test when it's disabled

* fix: update tests

* fix: default for docker host

* fix: create the Docker provider properly

* fix: rename GH check for reaper-off

* chore: remove old static-analysis checks

* fix: remove containers in tests

* fix: remove more containers in tests

* chore: extract to constants

* chore: enable Ryuk using env vars at GH workflow

* fix: reset test environment

* fix: typo

* chore: log when the properties file is found

* fix: reset config's syncOnce in tests

* chore: proper config state in tests

* chore: do not reset the sync

* fix: remove containers in tests

* chore: remove duplicated test

* docs: document the config

* fix: remove paragraph

* fix: proper escaping message
  • Loading branch information
mdelapenya committed Mar 20, 2023
1 parent ee9ff41 commit 447099b
Show file tree
Hide file tree
Showing 18 changed files with 748 additions and 439 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/ci-reaper-off.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Reaper-Off pipeline

on: [push, pull_request]

concurrency:
group: "${{ github.workflow }}-${{ github.head_ref || github.sha }}"
cancel-in-progress: true

jobs:
test-reaper-off:
strategy:
matrix:
go-version: [1.19.x, 1.x]
runs-on: ubuntu-latest
env:
TESTCONTAINERS_RYUK_DISABLED: "true"

steps:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v3

- name: gotestsum
# only run tests on linux, there are a number of things that won't allow the tests to run on anything else
# many (maybe, all?) images used can only be build on Linux, they don't have Windows in their manifest, and
# we can't put Windows Server in "Linux Mode" in Github actions
# another, host mode is only available on Linux, and we have tests around that, do we skip them?
run: make test-unit

- name: Run checker
run: |
./scripts/check_environment.sh
- name: Test Summary
uses: test-summary/action@4ee9ece4bca777a38f05c8fc578ac2007fe266f7
with:
paths: "**/TEST-*.xml"
if: always()
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ jobs:
go-version: [1.19.x, 1.x]
platform: [ubuntu-latest, windows-latest, macos-latest]
runs-on: ${{ matrix.platform }}
env:
TESTCONTAINERS_RYUK_DISABLED: "false"
steps:

- name: Set up Go
Expand Down
101 changes: 101 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package testcontainers

import (
"fmt"
"os"
"path/filepath"
"strconv"
"sync"

"github.com/magiconair/properties"
)

var tcConfig TestcontainersConfig
var tcConfigOnce *sync.Once = new(sync.Once)

// TestcontainersConfig represents the configuration for Testcontainers
// testcontainersConfig {
type TestcontainersConfig struct {
Host string `properties:"docker.host,default="`
TLSVerify int `properties:"docker.tls.verify,default=0"`
CertPath string `properties:"docker.cert.path,default="`
RyukDisabled bool `properties:"ryuk.disabled,default=false"`
RyukPrivileged bool `properties:"ryuk.container.privileged,default=false"`
}

// }

// ReadConfig reads from testcontainers properties file, storing the result in a singleton instance
// of the TestcontainersConfig struct
func ReadConfig() TestcontainersConfig {
tcConfigOnce.Do(func() {
tcConfig = readConfig()

if tcConfig.RyukDisabled {
ryukDisabledMessage := `
**********************************************************************************************
Ryuk has been disabled for the current execution. This can cause unexpected behavior in your environment.
More on this: https://golang.testcontainers.org/features/garbage_collector/
**********************************************************************************************`
Logger.Printf(ryukDisabledMessage)
Logger.Printf("\n%+v", tcConfig)
}
})

return tcConfig
}

// readConfig reads from testcontainers properties file, if it exists
// it is possible that certain values get overridden when set as environment variables
func readConfig() TestcontainersConfig {
config := TestcontainersConfig{}

applyEnvironmentConfiguration := func(config TestcontainersConfig) TestcontainersConfig {
if dockerHostEnv := os.Getenv("DOCKER_HOST"); dockerHostEnv != "" {
config.Host = dockerHostEnv
}
if config.Host == "" {
config.Host = "unix:///var/run/docker.sock"
}

ryukDisabledEnv := os.Getenv("TESTCONTAINERS_RYUK_DISABLED")
if parseBool(ryukDisabledEnv) {
config.RyukDisabled = ryukDisabledEnv == "true"
}

ryukPrivilegedEnv := os.Getenv("TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED")
if parseBool(ryukPrivilegedEnv) {
config.RyukPrivileged = ryukPrivilegedEnv == "true"
}

return config
}

home, err := os.UserHomeDir()
if err != nil {
return applyEnvironmentConfiguration(config)
}

tcProp := filepath.Join(home, ".testcontainers.properties")
// init from a file
properties, err := properties.LoadFile(tcProp, properties.UTF8)
if err != nil {
return applyEnvironmentConfiguration(config)
}

if err := properties.Decode(&config); err != nil {
fmt.Printf("invalid testcontainers properties file, returning an empty Testcontainers configuration: %v\n", err)
return applyEnvironmentConfiguration(config)
}

fmt.Printf("Testcontainers properties file has been found: %s\n", tcProp)

return applyEnvironmentConfiguration(config)
}

func parseBool(input string) bool {
if _, err := strconv.ParseBool(input); err == nil {
return true
}
return false
}
Loading

0 comments on commit 447099b

Please sign in to comment.