Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RenderTemplateToFile writes file to node #11160

Merged
merged 1 commit into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 3 additions & 4 deletions test/helpers/cilium.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/cilium/cilium/pkg/logging"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/test/config"
"github.com/cilium/cilium/test/ginkgo-ext"
ginkgoext "github.com/cilium/cilium/test/ginkgo-ext"
"github.com/cilium/cilium/test/helpers/logutils"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -552,7 +552,7 @@ func (s *SSHMeta) PolicyImport(path string) error {
func (s *SSHMeta) PolicyRenderAndImport(policy string) (int, error) {
filename := fmt.Sprintf("policy_%s.json", MakeUID())
s.logger.Debugf("PolicyRenderAndImport: render policy to '%s'", filename)
err := RenderTemplateToFile(filename, policy, os.ModePerm)
err := s.RenderTemplateToFile(filename, policy, os.ModePerm)
if err != nil {
s.logger.Errorf("PolicyRenderAndImport: cannot create policy file on '%s'", filename)
return 0, fmt.Errorf("cannot render the policy: %s", err)
Expand Down Expand Up @@ -886,11 +886,10 @@ CILIUM_OPTS=--kvstore consul --kvstore-opt consul.address=127.0.0.1:8500 --debug
INITSYSTEM=SYSTEMD`

ciliumConfig := "cilium.conf.ginkgo"
err := RenderTemplateToFile(ciliumConfig, fmt.Sprintf(systemdTemplate, ciliumOpts), os.ModePerm)
err := s.RenderTemplateToFile(ciliumConfig, fmt.Sprintf(systemdTemplate, ciliumOpts), os.ModePerm)
if err != nil {
return err
}
defer os.Remove(ciliumConfig)

confPath := filepath.Join("/home/vagrant/go/src/github.com/cilium/cilium/test", ciliumConfig)
res := s.Exec(fmt.Sprintf("sudo cp %s /etc/sysconfig/cilium", confPath))
Expand Down
18 changes: 18 additions & 0 deletions test/helpers/local_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"strings"
Expand Down Expand Up @@ -47,6 +48,7 @@ type Executor interface {
ExecuteContext(ctx context.Context, cmd string, stdout io.Writer, stderr io.Writer) error
String() string
BasePath() string
RenderTemplateToFile(filename string, tmplt string, perm os.FileMode) error
setBasePath()

Logger() *logrus.Entry
Expand Down Expand Up @@ -251,3 +253,19 @@ func (s *LocalExecutor) ExecInBackground(ctx context.Context, cmd string, option
func (s *LocalExecutor) BasePath() string {
return s.basePath
}

// RenderTemplateToFile renders a text/template string into a target filename
// with specific persmisions. Returns an error if the template cannot be
// validated or the file cannot be created.
func (s *LocalExecutor) RenderTemplateToFile(filename string, tmplt string, perm os.FileMode) error {
content, err := RenderTemplate(tmplt)
if err != nil {
return err
}

err = ioutil.WriteFile(filename, content.Bytes(), perm)
if err != nil {
return err
}
return nil
}
20 changes: 19 additions & 1 deletion test/helpers/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ var (
SSHMetaLogs = ginkgoext.NewWriter(new(Buffer))
)

// SSHMeta contains metadata to SSH into a remote location to run tests
// SSHMeta contains metadata to SSH into a remote location to run tests,
// implements Executor interface
type SSHMeta struct {
sshClient *SSHClient
env []string
Expand Down Expand Up @@ -319,3 +320,20 @@ func (s *SSHMeta) ExecInBackground(ctx context.Context, cmd string, options ...E

return res
}

// RenderTemplateToFile renders a text/template string into a target filename
// with specific persmisions. Returns an error if the template cannot be
// validated or the file cannot be created.
func (s *SSHMeta) RenderTemplateToFile(filename string, tmplt string, perm os.FileMode) error {
content, err := RenderTemplate(tmplt)
if err != nil {
return err
}

cmd := fmt.Sprintf("install -m %o <(echo '%s') %s\n", perm, content, filepath.Join(s.basePath, filename))
res := s.Exec(cmd)
if !res.WasSuccessful() {
return fmt.Errorf("%s", res.CombineOutput())
}
return nil
}
6 changes: 3 additions & 3 deletions test/helpers/policygen/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func (t *Target) CreateApplyManifest(spec *TestSpec, base string) error {
if err != nil {
return fmt.Errorf("cannot render template: %s", err)
}
err = helpers.RenderTemplateToFile(t.GetManifestName(spec), data.String(), os.ModePerm)
err = spec.Kub.RenderTemplateToFile(t.GetManifestName(spec), data.String(), os.ModePerm)
if err != nil {
return err
}
Expand Down Expand Up @@ -286,7 +286,7 @@ func (t *Target) CreateApplyManifest(spec *TestSpec, base string) error {
return fmt.Errorf("cannot render template: %s", err)
}

err = helpers.RenderTemplateToFile(t.GetManifestName(spec), data.String(), os.ModePerm)
err = spec.Kub.RenderTemplateToFile(t.GetManifestName(spec), data.String(), os.ModePerm)
if err != nil {
return err
}
Expand Down Expand Up @@ -473,7 +473,7 @@ spec:
ports:
- containerPort: 80`

err := helpers.RenderTemplateToFile(
err := t.Kub.RenderTemplateToFile(
t.GetManifestName(),
fmt.Sprintf(manifest, t.Prefix, t.SrcPod, t.DestPod, constants.AlpineCurlImage, constants.HttpdImage),
os.ModePerm)
Expand Down
18 changes: 6 additions & 12 deletions test/helpers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,19 @@ func MakeUID() string {
return fmt.Sprintf("%08x", randGen.Uint32())
}

// RenderTemplateToFile renders a text/template string into a target filename
// with specific persmisions. Returns eturn an error if the template cannot be
// validated or the file cannot be created.
func RenderTemplateToFile(filename string, tmplt string, perm os.FileMode) error {
// RenderTemplate renders a text/template string into a buffer.
// Returns eturn an error if the template cannot be validated.
func RenderTemplate(tmplt string) (*bytes.Buffer, error) {
t, err := template.New("").Parse(tmplt)
if err != nil {
return err
return nil, err
}
content := new(bytes.Buffer)
err = t.Execute(content, nil)
if err != nil {
return err
}

err = ioutil.WriteFile(filename, content.Bytes(), perm)
if err != nil {
return err
return nil, err
}
return nil
return content, nil
}

// TimeoutConfig represents the configuration for the timeout of a command.
Expand Down
4 changes: 2 additions & 2 deletions test/runtime/Policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -1679,7 +1679,7 @@ var _ = Describe("RuntimePolicyImportTests", func() {
It("Invalid Policies", func() {

testInvalidPolicy := func(data string) {
err := helpers.RenderTemplateToFile(invalidJSON, data, 0777)
err := vm.RenderTemplateToFile(invalidJSON, data, 0777)
Expect(err).Should(BeNil())

path := vm.GetFilePath(invalidJSON)
Expand Down Expand Up @@ -1712,7 +1712,7 @@ var _ = Describe("RuntimePolicyImportTests", func() {
)

BeforeEach(func() {
err := helpers.RenderTemplateToFile(policyJSON, policy, 0777)
err := vm.RenderTemplateToFile(policyJSON, policy, 0777)
Expect(err).Should(BeNil())

path := vm.GetFilePath(policyJSON)
Expand Down
8 changes: 4 additions & 4 deletions test/runtime/fqdn.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,12 @@ var _ = Describe("RuntimeFQDNPolicies", func() {
}

bindConfig := fmt.Sprintf(bindCiliumTestTemplate, getMapValues(worldIps)...)
err := helpers.RenderTemplateToFile(bindDBCilium, bindConfig, os.ModePerm)
err := vm.RenderTemplateToFile(bindDBCilium, bindConfig, os.ModePerm)
Expect(err).To(BeNil(), "bind file can't be created")

// // Installed DNSSEC domain
bindConfig = fmt.Sprintf(bindDNSSECTestTemplate, getMapValues(worldIps)...)
err = helpers.RenderTemplateToFile(bindDBDNSSSEC, bindConfig, os.ModePerm)
err = vm.RenderTemplateToFile(bindDBDNSSSEC, bindConfig, os.ModePerm)
Expect(err).To(BeNil(), "bind file can't be created")

for name, image := range ciliumOutsideImages {
Expand All @@ -212,10 +212,10 @@ var _ = Describe("RuntimeFQDNPolicies", func() {
outsideIps[name] = ip.String()
}
bindConfig = fmt.Sprintf(bindOutsideTestTemplate, getMapValues(outsideIps)...)
err = helpers.RenderTemplateToFile(bindDBOutside, bindConfig, os.ModePerm)
err = vm.RenderTemplateToFile(bindDBOutside, bindConfig, os.ModePerm)
Expect(err).To(BeNil(), "bind file can't be created")

err = helpers.RenderTemplateToFile(bindNamedConf, bind9ZoneConfig, os.ModePerm)
err = vm.RenderTemplateToFile(bindNamedConf, bind9ZoneConfig, os.ModePerm)
Expect(err).To(BeNil(), "Bind named.conf local file can't be created")

vm.ExecWithSudo("mkdir -m777 -p /data")
Expand Down