Skip to content

Commit

Permalink
test(config): added config unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dharsanb committed May 8, 2024
1 parent b593dff commit e2107e4
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 55 deletions.
58 changes: 6 additions & 52 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package provider
import (
"context"
"crypto/tls"
"errors"
"net/http"
"os"

Expand All @@ -13,7 +12,6 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/humanitec/humanitec-go-autogen"
"sigs.k8s.io/yaml"
)

// Ensure HumanitecProvider satisfies various provider interfaces.
Expand Down Expand Up @@ -94,59 +92,15 @@ func (p *HumanitecProvider) Configure(ctx context.Context, req provider.Configur
return
}

var c Config

// Check for .humctl file generated by humctl command line tool
configFilePath := data.Config.ValueString()
if configFilePath == "" {
homeDir, err := os.UserHomeDir()
if err != nil {
resp.Diagnostics.AddWarning(
"Unable to determine home directory",
"While configuring the provider, terraform was unable "+
"to determine user's home directory to read config file.",
)
configFilePath = ""
} else {
configFilePath = homeDir + "/.humctl"
if _, err := os.Stat(configFilePath); errors.Is(err, os.ErrNotExist) {
configFilePath = ""
}
}

} else if _, err := os.Stat(configFilePath); errors.Is(err, os.ErrNotExist) {
resp.Diagnostics.AddError(
"Unable to read config file",
"Terraform was unable to read config file mentioned "+
"in the config attribute.",
)
config, diags := readConfig(data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

if configFilePath != "" {
file, err := os.ReadFile(configFilePath)
if err != nil {
resp.Diagnostics.AddError(
"Unable to read config file",
"Terraform was unable to read the yaml config file "+
"in "+configFilePath,
)
return
}

err = yaml.Unmarshal(file, &c)
if err != nil {
resp.Diagnostics.AddError(
"Unable to parse yaml from config file",
"Terraform was unable to parse yaml config "+
"file in "+configFilePath,
)
// Not returning early allows the logic to collect all errors.
}
}
apiPrefix := c.ApiPrefix
orgID := c.Org
token := c.Token
apiPrefix := config.ApiPrefix
orgID := config.Org
token := config.Token

// Environment variables have precedence over config file, if found
if hostOld := os.Getenv("HUMANITEC_HOST"); hostOld != "" {
Expand Down
68 changes: 65 additions & 3 deletions internal/provider/utils.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package provider

import (
"errors"
"os"

"github.com/hashicorp/terraform-plugin-framework/diag"
"sigs.k8s.io/yaml"
)

type Config struct {
ApiPrefix string `json:"apiPrefix,omitempty"`
Org string `json:"org,omitempty"`
Token string `json:"token,omitempty"`
ApiPrefix string `yaml:"apiPrefix,omitempty"`
Org string `yaml:"org,omitempty"`
Token string `yaml:"token,omitempty"`
}

func valueAtPath[T any](input map[string]interface{}, path []string) (T, bool) {
Expand Down Expand Up @@ -46,3 +54,57 @@ func findInSlicePtr[T any](in *[]T, f func(T) bool) (T, bool) {

return element, found
}

func readConfig(data HumanitecProviderModel) (config Config, diags diag.Diagnostics) {
diags = diag.Diagnostics{}
// Check for .humctl file generated by humctl command line tool
configFilePath := data.Config.ValueString()
if configFilePath == "" {
homeDir, err := os.UserHomeDir()
if err != nil {
diags.AddWarning(
"Unable to determine home directory",
"While configuring the provider, terraform was unable "+
"to determine user's home directory to read config file.",
)
configFilePath = ""
} else {
configFilePath = homeDir + "/.humctl"
if _, err := os.Stat(configFilePath); errors.Is(err, os.ErrNotExist) {
configFilePath = ""
}
}
} else if _, err := os.Stat(configFilePath); errors.Is(err, os.ErrNotExist) {
diags.AddError(
"Unable to read config file",
"Terraform was unable to read config file mentioned "+
"in the config attribute.",
)
return
}

if configFilePath == "" {
return
}

file, err := os.ReadFile(configFilePath)
if err != nil {
diags.AddError(
"Unable to read config file",
"Terraform was unable to read the yaml config file "+
"in "+configFilePath,
)
return
}

err = yaml.Unmarshal(file, &config)
if err != nil {
diags.AddError(
"Unable to parse yaml from config file",
"Terraform was unable to parse yaml config "+
"file in "+configFilePath,
)
return
}
return
}
52 changes: 52 additions & 0 deletions internal/provider/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package provider

import (
"os"
"testing"

"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/stretchr/testify/assert"
"sigs.k8s.io/yaml"
)

func TestValueAtPath(t *testing.T) {
Expand All @@ -21,3 +24,52 @@ func TestValueAtPath(t *testing.T) {
assert.True(ok)
assert.Equal([]string{"a", "b"}, value)
}

func TestReadConfig(t *testing.T) {
assert := assert.New(t)
configData := Config{
ApiPrefix: "https://test-api.humanitec.io/",
Org: "unittest-org",
Token: "unittest-token",
}

configBytes, err := yaml.Marshal(configData)
if err != nil {
t.Fatal(err)
}

configFile, err := os.CreateTemp(".", ".humctl")
if err != nil {
t.Fatal(err)
}
defer configFile.Close()

_, err = configFile.Write(configBytes)
if err != nil {
t.Fatal(err)
}

configPath := configFile.Name()
defer os.Remove(configPath)

config, diags := readConfig(HumanitecProviderModel{
Config: types.StringValue(configPath),
})
assert.Len(diags, 0)
assert.Equal(config, configData)
}

func TestReadConfigNonExistentFile(t *testing.T) {
assert := assert.New(t)
currentDirectory, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
configPath := currentDirectory + ".humctl"

_, diags := readConfig(HumanitecProviderModel{
Config: types.StringValue(configPath),
})
assert.Len(diags, 1)
assert.Equal("Unable to read config file", diags[0].Summary())
}

0 comments on commit e2107e4

Please sign in to comment.