Skip to content

Commit

Permalink
Convert to using EnvVars type instead of array
Browse files Browse the repository at this point in the history
  • Loading branch information
gordon-klotho committed Feb 27, 2023
1 parent 5c952df commit 56c383a
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 43 deletions.
54 changes: 29 additions & 25 deletions pkg/env_var/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func Test_envVarPlugin(t *testing.T) {
type testResult struct {
resource core.CloudResource
envVars []core.EnvironmentVariable
envVars core.EnvironmentVariables
}
tests := []struct {
name string
Expand All @@ -34,12 +34,13 @@ func Test_envVarPlugin(t *testing.T) {
const a = 1`,
want: testResult{
resource: &core.Persist{Kind: core.PersistRedisNodeKind, Name: "myRedisNode"},
envVars: []core.EnvironmentVariable{{
Name: "REDIS_NODE_HOST",
Kind: "persist_redis_node",
ResourceID: "myRedisNode",
Value: "host",
},
envVars: core.EnvironmentVariables{
{
Name: "REDIS_NODE_HOST",
Kind: "persist_redis_node",
ResourceID: "myRedisNode",
Value: "host",
},
{
Name: "REDIS_NODE_PORT",
Kind: "persist_redis_node",
Expand All @@ -63,12 +64,13 @@ const a = 1`,
const a = 1`,
want: testResult{
resource: &core.Persist{Kind: core.PersistRedisClusterKind, Name: "myRedisCluster"},
envVars: []core.EnvironmentVariable{{
Name: "REDIS_HOST",
Kind: "persist_redis_cluster",
ResourceID: "myRedisCluster",
Value: "host",
},
envVars: core.EnvironmentVariables{
{
Name: "REDIS_HOST",
Kind: "persist_redis_cluster",
ResourceID: "myRedisCluster",
Value: "host",
},
{
Name: "REDIS_PORT",
Kind: "persist_redis_cluster",
Expand All @@ -91,12 +93,13 @@ const a = 1`,
const a = 1`,
want: testResult{
resource: &core.Persist{Kind: core.PersistORMKind, Name: "myOrm"},
envVars: []core.EnvironmentVariable{{
Name: "ORM_CONNECTION_STRING",
Kind: "persist_orm",
ResourceID: "myOrm",
Value: "connection_string",
},
envVars: core.EnvironmentVariables{
{
Name: "ORM_CONNECTION_STRING",
Kind: "persist_orm",
ResourceID: "myOrm",
Value: "connection_string",
},
},
},
},
Expand Down Expand Up @@ -215,12 +218,13 @@ func Test_parseDirectiveToEnvVars(t *testing.T) {
const a = 1`,
want: EnvironmentVariableDirectiveResult{
kind: string(core.PersistRedisNodeKind),
variables: []core.EnvironmentVariable{{
Name: "REDIS_NODE_HOST",
Kind: "persist_redis_node",
ResourceID: "myRedisNode",
Value: "host",
},
variables: core.EnvironmentVariables{
{
Name: "REDIS_NODE_HOST",
Kind: "persist_redis_node",
ResourceID: "myRedisNode",
Value: "host",
},
{
Name: "REDIS_NODE_PORT",
Kind: "persist_redis_node",
Expand Down
4 changes: 2 additions & 2 deletions pkg/exec_unit/plugin_exec_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func Test_environmentVarsAddedToUnit(t *testing.T) {
tests := []struct {
name string
envVars map[string]string
want []core.EnvironmentVariable
want core.EnvironmentVariables
wantExecUnit bool
}{
{
Expand All @@ -30,7 +30,7 @@ func Test_environmentVarsAddedToUnit(t *testing.T) {
"key1": "value1",
"key2": "value2",
},
want: []core.EnvironmentVariable{
want: core.EnvironmentVariables{
{
Name: "key1",
Value: "value1",
Expand Down
4 changes: 2 additions & 2 deletions pkg/infra/kubernetes/exec_unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ func (unit *HelmExecUnit) getServiceName() string {
return unit.Name
}

func (unit *HelmExecUnit) addEnvsVarToDeployment(envVars []core.EnvironmentVariable) ([]Value, error) {
func (unit *HelmExecUnit) addEnvsVarToDeployment(envVars core.EnvironmentVariables) ([]Value, error) {
values := []Value{}

log := zap.L().Sugar().With(logging.FileField(unit.Deployment), zap.String("unit", unit.Name))
Expand Down Expand Up @@ -376,7 +376,7 @@ func (unit *HelmExecUnit) addEnvsVarToDeployment(envVars []core.EnvironmentVaria
return values, nil
}

func (unit *HelmExecUnit) addEnvVarToPod(envVars []core.EnvironmentVariable) ([]Value, error) {
func (unit *HelmExecUnit) addEnvVarToPod(envVars core.EnvironmentVariables) ([]Value, error) {
values := []Value{}

log := zap.L().Sugar().With(logging.FileField(unit.Pod), zap.String("unit", unit.Name))
Expand Down
8 changes: 4 additions & 4 deletions pkg/infra/kubernetes/exec_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ func Test_addEnvVarToDeployment(t *testing.T) {
tests := []struct {
name string
file string
envVars []core.EnvironmentVariable
envVars core.EnvironmentVariables
want result
wantErr bool
}{
Expand All @@ -451,7 +451,7 @@ spec:
containers:
- name: nginx
image: nginx:1.14.2`,
envVars: []core.EnvironmentVariable{{Name: "SEQUELIZEDB_PERSIST_ORM_CONNECTION"}},
envVars: core.EnvironmentVariables{{Name: "SEQUELIZEDB_PERSIST_ORM_CONNECTION"}},
want: result{
values: []Value{
{
Expand Down Expand Up @@ -546,13 +546,13 @@ func Test_addEnvVarToPod(t *testing.T) {
tests := []struct {
name string
file string
envVars []core.EnvironmentVariable
envVars core.EnvironmentVariables
want result
wantErr bool
}{
{
name: "Basic Pod",
envVars: []core.EnvironmentVariable{{Name: "SEQUELIZEDB_PERSIST_ORM_CONNECTION"}},
envVars: core.EnvironmentVariables{{Name: "SEQUELIZEDB_PERSIST_ORM_CONNECTION"}},
file: `apiVersion: v1
kind: Pod
metadata:
Expand Down
4 changes: 2 additions & 2 deletions pkg/infra/kubernetes/persist.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func (unit *HelmExecUnit) handlePersistForExecUnit(deps *core.Dependencies) ([]V
return values, nil
}

func generateEnvVars(deps *core.Dependencies, name string) []core.EnvironmentVariable {
envVars := []core.EnvironmentVariable{}
func generateEnvVars(deps *core.Dependencies, name string) core.EnvironmentVariables {
envVars := core.EnvironmentVariables{}
for _, target := range deps.Downstream(core.ResourceKey{Name: name, Kind: core.ExecutionUnitKind}) {
switch target.Kind {
case string(core.PersistORMKind):
Expand Down
10 changes: 5 additions & 5 deletions pkg/infra/kubernetes/persist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,25 +284,25 @@ func Test_generateEnvVarsForPersist(t *testing.T) {
name string
unit *core.ExecutionUnit
resource core.CloudResource
values []core.EnvironmentVariable
values core.EnvironmentVariables
}{
{
name: "Wrong dependencies",
unit: &core.ExecutionUnit{Name: "main"},
resource: &core.Persist{Name: "file", Kind: core.PersistFileKind},
values: []core.EnvironmentVariable{},
values: core.EnvironmentVariables{},
},
{
name: "orm dependency",
unit: &core.ExecutionUnit{Name: "main"},
resource: &core.Persist{Name: "orm", Kind: core.PersistORMKind},
values: []core.EnvironmentVariable{{Name: "ORM_PERSIST_ORM_CONNECTION", Kind: "persist_orm", ResourceID: "orm", Value: string(core.CONNECTION_STRING)}},
values: core.EnvironmentVariables{{Name: "ORM_PERSIST_ORM_CONNECTION", Kind: "persist_orm", ResourceID: "orm", Value: string(core.CONNECTION_STRING)}},
},
{
name: "redis node dependency",
unit: &core.ExecutionUnit{Name: "main"},
resource: &core.Persist{Name: "redisNode", Kind: core.PersistRedisNodeKind},
values: []core.EnvironmentVariable{
values: core.EnvironmentVariables{
{Name: "REDISNODE_PERSIST_REDIS_HOST", Kind: "persist_redis_node", ResourceID: "redisNode", Value: string(core.HOST)},
{Name: "REDISNODE_PERSIST_REDIS_PORT", Kind: "persist_redis_node", ResourceID: "redisNode", Value: string(core.PORT)},
},
Expand All @@ -311,7 +311,7 @@ func Test_generateEnvVarsForPersist(t *testing.T) {
name: "redis cluster dependency",
unit: &core.ExecutionUnit{Name: "main"},
resource: &core.Persist{Name: "redisCluster", Kind: core.PersistRedisClusterKind},
values: []core.EnvironmentVariable{
values: core.EnvironmentVariables{
{Name: "REDISCLUSTER_PERSIST_REDIS_HOST", Kind: "persist_redis_cluster", ResourceID: "redisCluster", Value: string(core.HOST)},
{Name: "REDISCLUSTER_PERSIST_REDIS_PORT", Kind: "persist_redis_cluster", ResourceID: "redisCluster", Value: string(core.PORT)},
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/lang/python/plugin_persist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ engine = create_engine(os.environ.get("SQLALCHEMY_PERSIST_ORM_CONNECTION"))`,
}
assert.NoError(err)
assert.Equal(tt.want, string(newF.Program()))
assert.Equal([]core.EnvironmentVariable{
assert.Equal(core.EnvironmentVariables{
{
Name: "SQLALCHEMY_PERSIST_ORM_CONNECTION",
Kind: string(core.PersistORMKind),
Expand Down Expand Up @@ -1048,7 +1048,7 @@ client = RedisCluster(host=os.environ.get("REDIS_PERSIST_REDIS_HOST"), port=os.e
}
assert.NoError(err)
assert.Equal(tt.want, string(newF.Program()))
assert.Equal([]core.EnvironmentVariable{
assert.Equal(core.EnvironmentVariables{
{
Name: "REDIS_PERSIST_REDIS_HOST",
Kind: string(tt.redisType),
Expand Down
2 changes: 1 addition & 1 deletion pkg/provider/infra_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type (
Schedules []Schedule
HelmOptions config.HelmChartOptions
Params config.InfraParams
EnvironmentVariables []core.EnvironmentVariable
EnvironmentVariables core.EnvironmentVariables
DockerfilePath string
}

Expand Down

0 comments on commit 56c383a

Please sign in to comment.