Skip to content

Commit

Permalink
feat: test cases for unhappy and edge cases + allow file to not exist…
Browse files Browse the repository at this point in the history
… + rename testdata asset files
  • Loading branch information
francardoso93 committed Oct 27, 2023
1 parent b1e55eb commit d45dcf9
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 25 deletions.
Empty file.
File renamed without changes.
File renamed without changes.
2 changes: 0 additions & 2 deletions assets/testdata/.env.secretref.invalid2

This file was deleted.

21 changes: 8 additions & 13 deletions src/services/k8s/knative.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package k8s
import (
"bytes"
"context"
"errors"
"fmt"
"os"
"path"
"strings"
"text/template"
Expand Down Expand Up @@ -143,22 +145,10 @@ func setSecretEnv(manifest *servingv1.Service, SecretRefEnvFile string, manifest
}

func validateSecretEnvVar(secretEnvVar corev1.EnvVar) error {
invalidChars := []string{
"\"",
"'",
}

// Mandatory char
if !strings.Contains(secretEnvVar.Value, "/") {
return fmt.Errorf("Invalid secret format for '%s'. Missing '/' char. Value must be in the format <secret-name>/<secret-key>, instead of '%s'", secretEnvVar.Name, secretEnvVar.Value)
}

// Invalid chars
for _, value := range invalidChars {
if strings.Contains(secretEnvVar.Value, value) {
return fmt.Errorf("Invalid secret format for '%s'. Invalid char found (\", ')", secretEnvVar.Name)
}
}
return nil
}

Expand All @@ -173,13 +163,18 @@ func setEnv(manifest *servingv1.Service, envFile string, manifestEnvVars map[str

func loadEnvFile(envFile string, manifestEnvVars map[string]string) ([]corev1.EnvVar, error) {
var envVarList []corev1.EnvVar

if _, err := os.Stat(envFile); errors.Is(err, os.ErrNotExist) {
return nil, nil
}

envVariables, err := godotenv.Read(envFile)
if err != nil {
return nil, fmt.Errorf("Error loading .env file. '%s' already set", err)
}

if len(envVariables) > 0 {
for key, value := range envVariables {
for key, value := range envVariables {
if manifestEnvVars[key] == "" {
manifestEnvVars[key] = value
envVar := corev1.EnvVar{
Expand Down
45 changes: 35 additions & 10 deletions src/services/k8s/knative_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path"
"strings"
"testing"

"gotest.tools/v3/assert"
Expand Down Expand Up @@ -118,16 +119,6 @@ func TestLoadManifestForPrivateService(t *testing.T) {
assert.Assert(t, labels[visibilityLabel] == visibilityLabelPrivateValue)
}

// TODO: New use cases:
// 1 -> All happy and working
// 2 -> Invalid chars
// 3 -> Mandatory '/' char
// 4 -> Conflicting env vars between files
// 5 -> Invalid dotenv format
// 6 -> Empty files
// 7 -> only .env is empty
// 8 -> only .env.secretref is empty

func TestLoadManifestEnvironmentVariables(t *testing.T) {
envVariablesFromFile, err := godotenv.Read(envTestFile)
if err != nil {
Expand All @@ -150,3 +141,37 @@ func TestLoadManifestEnvironmentVariables(t *testing.T) {
assert.Assert(t, len(envVariablesFromFile) == 0, "Missing environment variables: %s", envVariablesFromFile )
assert.Assert(t, len(secretRefEnvVariablesFromFile) == 0, "Missing secret environment variables: %s", secretRefEnvVariablesFromFile)
}

func TestLoadManifestEnvironmentVariablesInvalidFormat(t *testing.T) {
invalidEnvTestFile := path.Join(root, "assets/testdata/.env.initium.invalid")
serviceManifest, err := LoadManifest(namespace, commitSha, proj, dockerImage, invalidEnvTestFile, secretRefEnvTestFile)
assert.Assert(t, err != nil && strings.Contains(err.Error(), "Error loading .env file"), "There should be a validation error when missing a mandatory character" )
assert.Assert(t, serviceManifest == nil, "Expected nil manifest, got %v", serviceManifest)
}

func TestLoadManifestSecretRefEnvironmentMandatoryChars(t *testing.T) {
invalidSecretRefEnvTestFile := path.Join(root, "assets/testdata/.env.secretref.initium.invalid2")
serviceManifest, err := LoadManifest(namespace, commitSha, proj, dockerImage, envTestFile, invalidSecretRefEnvTestFile)
assert.Assert(t, err != nil && strings.Contains(err.Error(), "Value must be in the format <secret-name>/<secret-key>"), "There should be a validation error when missing a mandatory character" )
assert.Assert(t, serviceManifest == nil, "Expected nil manifest, got %v", serviceManifest)
}

func TestLoadManifestSecretRefEnvironmentConflict(t *testing.T) {
invalidSecretRefEnvTestFile := path.Join(root, "assets/testdata/.env.secretref.conflictingvar")
serviceManifest, err := LoadManifest(namespace, commitSha, proj, dockerImage, envTestFile, invalidSecretRefEnvTestFile)
assert.Assert(t, err != nil && strings.Contains(err.Error(), "Conflicting environment variable"), "There should be a validation error when missing a mandatory character" )
assert.Assert(t, serviceManifest == nil, "Expected nil manifest, got %v", serviceManifest)
}

func TestLoadManifestSecretEnvironmentVariablesFileDoesNotExist(t *testing.T) {
nonExistingEnvTestFile := "idontexist"
nonExistingSecretRefEnvTestFile := "idontexist"
serviceManifest, err := LoadManifest(namespace, commitSha, proj, dockerImage, nonExistingEnvTestFile, nonExistingSecretRefEnvTestFile)
assert.Assert(t, serviceManifest != nil, "Expected maifest to be created without issues. Dotenv files are optional. Err: %s", err)
}

func TestLoadManifestSecretEnvironmentVariablesEmptyFile(t *testing.T) {
emtpyFile := path.Join(root, "assets/testdata/.env.initium.empty")
serviceManifest, err := LoadManifest(namespace, commitSha, proj, dockerImage, emtpyFile, emtpyFile)
assert.Assert(t, serviceManifest != nil, "Expected maifest to be created without issues. Dotenv files are optional. Err: %s", err)
}

0 comments on commit d45dcf9

Please sign in to comment.