Skip to content

Commit

Permalink
(ref) #26 adds testing structure for config writer
Browse files Browse the repository at this point in the history
  • Loading branch information
benammann committed Aug 24, 2022
1 parent ed6abbf commit a834bfe
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 3 deletions.
2 changes: 2 additions & 0 deletions pkg/config/generic/repository_root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const TestFileBlankTwoContexts = "generic_repository_test-blank-two-contexts.jso
const TestFileBlankInvalidVersion = "generic_repository_test-invalid-version.json"
const TestFileConfigEntries = "generic_repository_test-config-entries.json"
const TestFileMissingEncryptionSecret = "generic_repository_test-missing-encryption-secret.json"
const TestFileRealWorld = "generic_repository_test-real-world.json"
const TestFileBlankDefaultRenderFilesMissingKey = "generic_repository_test-blank-render-files-missing-key.json"

func createTestRepository(fileName string, selectedContextName string) (*Repository, error) {
fileName = fmt.Sprintf("test_fs/%s", fileName)
Expand Down
3 changes: 2 additions & 1 deletion pkg/config/generic/schema_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
global_config "github.com/benammann/git-secrets/pkg/config/global"
"github.com/benammann/git-secrets/pkg/encryption"
"github.com/benammann/git-secrets/schema"
"github.com/spf13/afero"
"github.com/xeipuuv/gojsonschema"
"path/filepath"
"sort"
Expand Down Expand Up @@ -196,7 +197,7 @@ func ParseSchemaV1(jsonInput []byte, configFileUsed string, globalConfig *global
}
}

configWriter := NewV1Writer(Parsed, configFileUsed)
configWriter := NewV1Writer(afero.NewOsFs(), Parsed, configFileUsed)
repository := NewRepository(1, configFileUsed, configWriter)

for _, resultingContext := range contexts {
Expand Down
7 changes: 5 additions & 2 deletions pkg/config/generic/schema_v1_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ import (
"encoding/json"
"fmt"
config_const "github.com/benammann/git-secrets/pkg/config/const"
"github.com/spf13/afero"
"os"
)

type V1Writer struct {
schema V1Schema
configPath string
fs afero.Fs
}

func NewV1Writer(schema V1Schema, configPath string) *V1Writer {
func NewV1Writer(fs afero.Fs, schema V1Schema, configPath string) *V1Writer {
return &V1Writer{
fs: fs,
schema: schema,
configPath: configPath,
}
Expand Down Expand Up @@ -122,7 +125,7 @@ func (v *V1Writer) WriteConfig() error {

newConfig, _ := json.MarshalIndent(v.schema, "", " ")

f, err := os.OpenFile(v.configPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0664)
f, err := v.fs.OpenFile(v.configPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0664)
if err != nil {
return fmt.Errorf("could not open config: %s", err.Error())
}
Expand Down
112 changes: 112 additions & 0 deletions pkg/config/generic/schema_v1_writer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package config_generic

import (
"encoding/json"
"fmt"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"testing"
)

type GetSchemaFunc = func() V1Schema

func CreateGetSchemaFunc(t *testing.T, fs afero.Fs, configPath string) GetSchemaFunc {
return func() V1Schema {
fileBytes, errRead := afero.ReadFile(fs, configPath)
assert.NoError(t, errRead)

var Parsed V1Schema
errParse := json.Unmarshal(fileBytes, &Parsed)
assert.NoError(t, errParse)

return Parsed
}
}

func NewWrappedV1Writer(t *testing.T, inputFileName string) (writer *V1Writer, originalSchema V1Schema, getSchema GetSchemaFunc) {

fs := afero.NewMemMapFs()
configPath := ".git-secrets.json"

// copy file from embed fs to afero fs -> .git-secrets.json
wantedConfig, errRead := testFiles.ReadFile(fmt.Sprintf("test_fs/%s", inputFileName))

var wantedConfigParsed V1Schema
errParse := json.Unmarshal(wantedConfig, &wantedConfigParsed)
assert.NoError(t, errParse)

assert.NoError(t, errRead)
assert.NoError(t, afero.WriteFile(fs, configPath, wantedConfig, 0664))

return NewV1Writer(fs, wantedConfigParsed, configPath), wantedConfigParsed, CreateGetSchemaFunc(t, fs, configPath)

}

func TestNewV1Writer(t *testing.T) {
newWriter := NewV1Writer(afero.NewMemMapFs(), V1Schema{}, "")
assert.NotNil(t, newWriter)
assert.IsType(t, &V1Writer{}, newWriter)
}

func TestV1Writer_AddContext(t *testing.T) {

t.Run("add context if not exists", func(t *testing.T) {
writer, _, getSchema := NewWrappedV1Writer(t, TestFileBlankDefault)
assert.Nil(t, getSchema().Context["prod"])
assert.NoError(t, writer.AddContext("prod"))

newCtx := getSchema().Context["prod"]

assert.NotNil(t, newCtx)
assert.Nil(t, newCtx.Secrets)
assert.Nil(t, newCtx.Configs)
assert.Nil(t, newCtx.DecryptSecret)
})

t.Run("should not add context if already exists", func(t *testing.T) {
writer, _, getSchema := NewWrappedV1Writer(t, TestFileBlankTwoContexts)
assert.NotNil(t, getSchema().Context["prod"].Secrets)
assert.Error(t, writer.AddContext("prod"))
assert.NotNil(t, getSchema().Context["prod"].Secrets)
})

}

func TestV1Writer_AddFileToRender(t *testing.T) {

t.Run("create render files map if missing", func(t *testing.T) {
writer, original, getSchema := NewWrappedV1Writer(t, TestFileBlankDefault)
assert.Nil(t, original.RenderFiles)
assert.NoError(t, writer.AddFileToRender("env", "fileIn", "fileOut"))
newSchema := getSchema()
assert.NotNil(t, newSchema.RenderFiles)
assert.NotNil(t, newSchema.RenderFiles["env"])
assert.NotNil(t, newSchema.RenderFiles["env"].Files)
assert.Len(t, newSchema.RenderFiles["env"].Files, 1)
})

t.Run("create files entry if missing", func(t *testing.T) {

})

t.Run("should not add existing files to the same target", func(t *testing.T) {

})

t.Run("should add files to a target", func(t *testing.T) {

})

}

func TestV1Writer_SetConfig(t *testing.T) {

}

func TestV1Writer_SetSecret(t *testing.T) {

}

func TestV1Writer_WriteConfig(t *testing.T) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "https://raw.githubusercontent.com/benammann/git-secrets/dev-beta/schema/def/v1.json",
"version": 1,
"context": {
"default": {
"decryptSecret": {
"fromName": "gitsecretstest"
},
"secrets": {
"test": "l5sqnu8UkO+PdW2fZo7IMhfHng7lf6XNXEfRhQ/fvboP1HqcRFcu"
}
}
}
}
46 changes: 46 additions & 0 deletions pkg/config/generic/test_fs/generic_repository_test-real-world.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"$schema": "https://raw.githubusercontent.com/benammann/git-secrets/dev-beta/schema/def/v1.json",
"version": 1,
"context": {
"default": {
"decryptSecret": {
"fromName": "gitSecretsTest"
},
"secrets": {
"databasePassword": "prPy40oRzdeFelmL5xVhbadEWNV9puR3/aWTY+gTYXOrT2bksi5GS9lCTKi66A3ePYa0hbwMqXadlDZw"
},
"configs": {
"databaseHost": "database.svc.local",
"databasePort": "3306"
}
},
"prod": {
"secrets": {
"databasePassword": "g8C/GHbk8vCU4iTWDqOWenJWRevyS69vizTcSjKjR0h36l7Nobhdv3wK3L1S5yRkJJxzm+p+TT0bpWon"
},
"configs": {
"databaseHost": "database-prod.svc.cluster",
"databasePort": "3307"
}
},
"staging": {
"secrets": {
"databasePassword": "4Y2jUHEvsy+cYhamCz49qjkUPCCUNdvePb2WAptvlNg54wmzBBN6QvgJl7p/N602tC7zKNT6Vn52RcxN"
},
"configs": {
"databaseHost": "database-stg.svc.cluster",
"databasePort": "3307"
}
}
},
"renderFiles": {
"env": {
"files": [
{
"fileIn": "templates/.env.dist",
"fileOut": "templates/.env"
}
]
}
}
}
Empty file.
1 change: 1 addition & 0 deletions pkg/config/generic/test_fs/templates/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env

0 comments on commit a834bfe

Please sign in to comment.