-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support loading templates from network (HTTP)
- Loading branch information
Showing
8 changed files
with
695 additions
and
11 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package template | ||
|
||
const ( | ||
XPodlikeExtension = "x-podlike" | ||
ArgsProperty = "args" | ||
ServicesProperty = "services" | ||
|
||
TypeInline = "inline" | ||
TypeHttp = "http" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package template | ||
|
||
import ( | ||
"github.com/docker/cli/cli/compose/types" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestRender_File(t *testing.T) { | ||
tmplFile, err := ioutil.TempFile("", "template") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer os.Remove(tmplFile.Name()) | ||
|
||
if _, err := tmplFile.WriteString(` | ||
component: | ||
image: sample/{{ .Service.Labels.CompName }}:{{ .Args.TargetTag }} | ||
`); err != nil { | ||
t.Fatal(err) | ||
} | ||
if err := tmplFile.Close(); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
tmpl := podTemplate{ | ||
Template: tmplFile.Name(), | ||
} | ||
|
||
rendered := tmpl.render(&transformConfiguration{ | ||
Service: &types.ServiceConfig{ | ||
Labels: types.Labels{ | ||
"CompName": "testing", | ||
}, | ||
}, | ||
Args: map[string]interface{}{ | ||
"TargetTag": "0.1.2", | ||
}, | ||
Session: &transformSession{}, | ||
}) | ||
|
||
if comp, ok := rendered["component"]; !ok { | ||
t.Error("Root key not found") | ||
} else if mComp, ok := comp.(map[string]interface{}); !ok { | ||
t.Error("Invalid root key") | ||
} else if image, ok := mComp["image"]; !ok { | ||
t.Error("Image key not found") | ||
} else if image != "sample/testing:0.1.2" { | ||
t.Error("Invalid image value found") | ||
} | ||
} | ||
|
||
func TestRender_Inline(t *testing.T) { | ||
tmpl := podTemplate{ | ||
Template: ` | ||
component: | ||
image: sample/{{ .Service.Labels.CompName }}:{{ .Args.TargetTag }} | ||
`, | ||
Inline: true, | ||
} | ||
|
||
rendered := tmpl.render(&transformConfiguration{ | ||
Service: &types.ServiceConfig{ | ||
Labels: types.Labels{ | ||
"CompName": "testing", | ||
}, | ||
}, | ||
Args: map[string]interface{}{ | ||
"TargetTag": "0.1.2", | ||
}, | ||
Session: &transformSession{}, | ||
}) | ||
|
||
if comp, ok := rendered["component"]; !ok { | ||
t.Error("Root key not found") | ||
} else if mComp, ok := comp.(map[string]interface{}); !ok { | ||
t.Error("Invalid root key") | ||
} else if image, ok := mComp["image"]; !ok { | ||
t.Error("Image key not found") | ||
} else if image != "sample/testing:0.1.2" { | ||
t.Error("Invalid image value found") | ||
} | ||
} | ||
|
||
func TestRender_Http(t *testing.T) { | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if r.RequestURI != "/template/testing" { | ||
t.Fatal("Invalid template request") | ||
} | ||
|
||
w.WriteHeader(200) | ||
w.Write([]byte(` | ||
component: | ||
image: sample/{{ .Service.Labels.CompName }}:{{ .Args.TargetTag }} | ||
`)) | ||
})) | ||
defer server.Close() | ||
|
||
tmpl := podTemplate{ | ||
Template: server.URL + "/template/testing", | ||
Http: true, | ||
} | ||
|
||
rendered := tmpl.render(&transformConfiguration{ | ||
Service: &types.ServiceConfig{ | ||
Labels: types.Labels{ | ||
"CompName": "testing", | ||
}, | ||
}, | ||
Args: map[string]interface{}{ | ||
"TargetTag": "0.1.2", | ||
}, | ||
Session: &transformSession{}, | ||
}) | ||
|
||
if comp, ok := rendered["component"]; !ok { | ||
t.Error("Root key not found") | ||
} else if mComp, ok := comp.(map[string]interface{}); !ok { | ||
t.Error("Invalid root key") | ||
} else if image, ok := mComp["image"]; !ok { | ||
t.Error("Image key not found") | ||
} else if image != "sample/testing:0.1.2" { | ||
t.Error("Invalid image value found") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters