Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
keroxp committed Jul 4, 2024
1 parent 14d22d7 commit be89a35
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 84 deletions.
16 changes: 7 additions & 9 deletions env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func LoadServiceDefiniton(dir string) (*ecs.CreateServiceInput, error) {
if noSvc != nil {
return nil, xerrors.Errorf("roll out context specified at '%s' but no 'service.json' or 'task-definition.json'", dir)
}
if _, err := ReadAndUnmarshalJson(svcPath, &service); err != nil {
if err := ReadAndUnmarshalJson(svcPath, &service); err != nil {
return nil, xerrors.Errorf("failed to read and unmarshal service.json: %s", err)
}
return &service, nil
Expand All @@ -83,7 +83,7 @@ func LoadTaskDefiniton(dir string) (*ecs.RegisterTaskDefinitionInput, error) {
if noTd != nil {
return nil, xerrors.Errorf("roll out context specified at '%s' but no 'service.json' or 'task-definition.json'", dir)
}
if _, err := ReadAndUnmarshalJson(tdPath, &td); err != nil {
if err := ReadAndUnmarshalJson(tdPath, &td); err != nil {
return nil, xerrors.Errorf("failed to read and unmarshal task-definition.json: %s", err)
}
return &td, nil
Expand Down Expand Up @@ -113,15 +113,13 @@ func MergeEnvars(dest *Envars, src *Envars) {
}
}

func ReadAndUnmarshalJson(path string, dest interface{}) ([]byte, error) {
func ReadAndUnmarshalJson(path string, dest interface{}) error {
if d, err := ReadFileAndApplyEnvars(path); err != nil {
return d, err
} else {
if err := json.Unmarshal(d, dest); err != nil {
return d, err
}
return d, nil
return err
} else if err := json.Unmarshal(d, dest); err != nil {
return err
}
return nil
}

func ReadFileAndApplyEnvars(path string) ([]byte, error) {
Expand Down
38 changes: 37 additions & 1 deletion env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,46 @@ func TestMergeEnvars(t *testing.T) {
assert.Equal(t, e1.Service, "fuga")
}

func TestLoadServiceDefinition(t *testing.T) {
t.Run("basic", func(t *testing.T) {
d, err := env.LoadServiceDefiniton("./testdata")
if err != nil {
t.Fatalf(err.Error())
}
assert.Equal(t, *d.ServiceName, "service")
})
t.Run("should error if service.json is not found", func(t *testing.T) {
_, err := env.LoadServiceDefiniton("../")
assert.EqualError(t, err, "roll out context specified at '../' but no 'service.json' or 'task-definition.json'")
})
t.Run("should error if service.json is invalid", func(t *testing.T) {
_, err := env.LoadServiceDefiniton("./testdata/invalid")
assert.ErrorContains(t, err, "failed to read and unmarshal service.json:")
})
}

func TestLoadTaskDefinition(t *testing.T) {
t.Run("basic", func(t *testing.T) {
d, err := env.LoadTaskDefiniton("./testdata")
if err != nil {
t.Fatalf(err.Error())
}
assert.Equal(t, *d.Family, "test-task")
})
t.Run("should error if task-definition.json is not found", func(t *testing.T) {
_, err := env.LoadTaskDefiniton("../")
assert.EqualError(t, err, "roll out context specified at '../' but no 'service.json' or 'task-definition.json'")
})
t.Run("should error if task-definition.json is invalid", func(t *testing.T) {
_, err := env.LoadTaskDefiniton("./testdata/invalid")
assert.ErrorContains(t, err, "failed to read and unmarshal task-definition.json:")
})
}

func TestReadFileAndApplyEnvars(t *testing.T) {
os.Setenv("HOGE", "hogehoge")
os.Setenv("FUGA", "fugafuga")
d, err := env.ReadFileAndApplyEnvars("./fixtures/template.txt")
d, err := env.ReadFileAndApplyEnvars("./testdata/template.txt")
if err != nil {
t.Fatalf(err.Error())
}
Expand Down
1 change: 1 addition & 0 deletions env/testdata/invalid/service.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
1 change: 1 addition & 0 deletions env/testdata/invalid/task-definition.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -21,80 +21,6 @@
}
],
"essential": true,
"entryPoint": [""],
"command": [""],
"environment": [
{
"name": "",
"value": ""
}
],
"mountPoints": [
{
"sourceVolume": "",
"containerPath": "",
"readOnly": true
}
],
"volumesFrom": [
{
"sourceContainer": "",
"readOnly": true
}
],
"linuxParameters": {
"capabilities": {
"add": [""],
"drop": [""]
},
"devices": [
{
"hostPath": "",
"containerPath": "",
"permissions": ["mknod"]
}
],
"initProcessEnabled": true,
"sharedMemorySize": 0,
"tmpfs": [
{
"containerPath": "",
"size": 0,
"mountOptions": [""]
}
]
},
"hostname": "",
"user": "",
"workingDirectory": "",
"disableNetworking": true,
"privileged": true,
"readonlyRootFilesystem": true,
"dnsServers": [""],
"dnsSearchDomains": [""],
"extraHosts": [
{
"hostname": "",
"ipAddress": ""
}
],
"dockerSecurityOptions": [""],
"dockerLabels": {
"KeyName": ""
},
"ulimits": [
{
"name": "core",
"softLimit": 0,
"hardLimit": 0
}
],
"logConfiguration": {
"logDriver": "gelf",
"options": {
"KeyName": ""
}
},
"healthCheck": {
"command": [""],
"interval": 0,
Expand Down
File renamed without changes.

0 comments on commit be89a35

Please sign in to comment.