github.com/cloudfoundry/postgres-release/src/acceptance-tests@v0.0.0-20240511030151-872bdd2e0dba/testing/helpers/config.go (about)

     1  package helpers
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"os"
     8  	"strings"
     9  
    10  	"gopkg.in/yaml.v2"
    11  )
    12  
    13  const MissingCertificateMsg = "missing `director_ca_cert` - specify BOSH director CA certificate"
    14  const IncorrectEnvMsg = "$PGATS_CONFIG %q does not specify an absolute path to test config file"
    15  
    16  type PgatsConfig struct {
    17  	Bosh              BOSHConfig      `yaml:"bosh"`
    18  	BoshCC            BOSHCloudConfig `yaml:"cloud_configs"`
    19  	PGReleaseVersion  string          `yaml:"postgres_release_version"`
    20  	PostgreSQLVersion string          `yaml:"postgresql_version"`
    21  	VersionsFile      string          `yaml:"versions_file"`
    22  }
    23  
    24  var DefaultPgatsConfig = PgatsConfig{
    25  	Bosh:              DefaultBOSHConfig,
    26  	BoshCC:            DefaultCloudConfig,
    27  	PGReleaseVersion:  "latest",
    28  	PostgreSQLVersion: "current",
    29  	VersionsFile:      "",
    30  }
    31  
    32  func LoadConfig(configFilePath string) (PgatsConfig, error) {
    33  	var config PgatsConfig
    34  	config = DefaultPgatsConfig
    35  
    36  	configFile, err := ioutil.ReadFile(configFilePath)
    37  	if err != nil {
    38  		return PgatsConfig{}, err
    39  	}
    40  	if err := yaml.Unmarshal(configFile, &config); err != nil {
    41  		return PgatsConfig{}, err
    42  	}
    43  
    44  	if config.Bosh.Credentials.CACert == "" {
    45  		return PgatsConfig{}, errors.New(MissingCertificateMsg)
    46  	}
    47  	return config, nil
    48  }
    49  
    50  func ConfigPath() (string, error) {
    51  	path := os.Getenv("PGATS_CONFIG")
    52  	if path == "" || !strings.HasPrefix(path, "/") {
    53  		return "", fmt.Errorf(IncorrectEnvMsg, path)
    54  	}
    55  
    56  	return path, nil
    57  }