Skip to content

Commit

Permalink
Improve tests for Maskinporten HTTP client and config (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinothamar committed Jun 20, 2024
1 parent ab4ea56 commit 05742c5
Show file tree
Hide file tree
Showing 6 changed files with 233 additions and 68 deletions.
12 changes: 10 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ type MaskinportenApiConfig struct {
Scope string `koanf:"scope" validate:"required"`
}

func GetConfig(operatorContext *operatorcontext.Context) (*Config, error) {
func GetConfig(operatorContext *operatorcontext.Context, configFilePath string) (*Config, error) {
var cfg *Config
var err error
if operatorContext.IsLocal() {
cfg, err = loadFromKoanf(operatorContext)
cfg, err = loadFromKoanf(operatorContext, configFilePath)
} else {
cfg, err = loadFromAzureKeyVault()
}
Expand All @@ -39,3 +39,11 @@ func GetConfig(operatorContext *operatorcontext.Context) (*Config, error) {

return cfg, nil
}

func GetConfigOrDie(operatorContext *operatorcontext.Context, configFilePath string) *Config {
cfg, err := GetConfig(operatorContext, configFilePath)
if err != nil {
panic(err)
}
return cfg
}
50 changes: 50 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package config

import (
"os"
"reflect"
"testing"

operatorcontext "github.com/altinn/altinn-k8s-operator/internal/operator_context"
"github.com/go-playground/validator/v10"
. "github.com/onsi/gomega"
)

func TestConfigMissingValuesFail(t *testing.T) {
RegisterTestingT(t)

file, err := os.CreateTemp(os.TempDir(), "*.env")
Expect(err).NotTo(HaveOccurred())
defer func() {
err := file.Close()
Expect(err).NotTo(HaveOccurred())
}()
defer func() {
err := os.Remove(file.Name())
Expect(err).NotTo(HaveOccurred())
}()

_, err = file.WriteString("maskinporten_api.url=https://example.com")
Expect(err).NotTo(HaveOccurred())

operatorContext := operatorcontext.DiscoverOrDie()
cfg, err := GetConfig(operatorContext, file.Name())
Expect(cfg).To(BeNil())
Expect(err).To(HaveOccurred())
_, ok := err.(validator.ValidationErrors)
errType := reflect.TypeOf(err)
Expect(errType.String()).To(Equal("validator.ValidationErrors"))
Expect(ok).To(BeTrue())
}

func TestConfigTestEnvLoadsOk(t *testing.T) {
RegisterTestingT(t)

operatorContext := operatorcontext.DiscoverOrDie()
cfg, err := GetConfig(operatorContext, "")
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())
Expect(cfg.MaskinportenApi.ClientId).To(Equal("64d8055d-bf0c-4ee2-979e-d2bbe996a9f5"))
Expect(cfg.MaskinportenApi.Url).To(Equal("https://maskinporten.dev"))
Expect(cfg.MaskinportenApi.Jwk).NotTo(BeNil())
}
25 changes: 14 additions & 11 deletions internal/config/koanf.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ var (
parser = dotenv.ParserEnv("", ".", func(s string) string { return s })
)

func loadFromKoanf(operatorContext *operatorcontext.Context) (*Config, error) {
func loadFromKoanf(operatorContext *operatorcontext.Context, configFilePath string) (*Config, error) {
tryFindProjectRoot()

filePath := "local.env"
if configFilePath == "" {
configFilePath = "local.env"
}

if !operatorContext.IsLocal() {
return nil, fmt.Errorf("loading config from koanf is only supported for local environment")
}
Expand All @@ -28,26 +31,26 @@ func loadFromKoanf(operatorContext *operatorcontext.Context) (*Config, error) {
if err != nil {
return nil, err
}
if _, err := os.Stat(filePath); os.IsNotExist(err) {
if path.IsAbs(filePath) {
return nil, fmt.Errorf("env file does not exist: '%s'", filePath)
if _, err := os.Stat(configFilePath); os.IsNotExist(err) {
if path.IsAbs(configFilePath) {
return nil, fmt.Errorf("env file does not exist: '%s'", configFilePath)
} else {
return nil, fmt.Errorf("env file does not exist in '%s': '%s'", currentDir, filePath)
return nil, fmt.Errorf("env file does not exist in '%s': '%s'", currentDir, configFilePath)
}
}

if !path.IsAbs(filePath) {
filePath = path.Join(currentDir, filePath)
if !path.IsAbs(configFilePath) {
configFilePath = path.Join(currentDir, configFilePath)
}

if err := k.Load(file.Provider(filePath), parser); err != nil {
return nil, fmt.Errorf("error loading config '%s': %w", filePath, err)
if err := k.Load(file.Provider(configFilePath), parser); err != nil {
return nil, fmt.Errorf("error loading config '%s': %w", configFilePath, err)
}

var cfg Config

if err := k.Unmarshal("", &cfg); err != nil {
return nil, fmt.Errorf("error unmarshalling config '%s': %w", filePath, err)
return nil, fmt.Errorf("error unmarshalling config '%s': %w", configFilePath, err)
}

return &cfg, nil
Expand Down
2 changes: 1 addition & 1 deletion internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func NewRuntime() (rt.Runtime, error) {
return nil, err
}

cfg, err := config.GetConfig(operatorContext)
cfg, err := config.GetConfig(operatorContext, "")
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 05742c5

Please sign in to comment.