Skip to content

Commit

Permalink
Merge pull request #101 from asteris-llc/feature/shell-module
Browse files Browse the repository at this point in the history
Port shell resource and tests
  • Loading branch information
BrianHicks committed Jul 21, 2016
2 parents 78787e0 + f1032de commit 217b5e9
Show file tree
Hide file tree
Showing 6 changed files with 286 additions and 7 deletions.
26 changes: 26 additions & 0 deletions helpers/fakerenderer/fakerenderer.go
@@ -0,0 +1,26 @@
// Copyright © 2016 Asteris, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package fakerenderer

type FakeRenderer struct {
}

func (fr *FakeRenderer) Value() (string, bool) {
return "", true
}

func (fr *FakeRenderer) Render(name, content string) (string, error) {
return content, nil
}
29 changes: 29 additions & 0 deletions helpers/fakerenderer/fakerenderer_test.go
@@ -0,0 +1,29 @@
// Copyright © 2016 Asteris, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package fakerenderer_test

import (
"testing"

"github.com/asteris-llc/converge/helpers/fakerenderer"
"github.com/asteris-llc/converge/resource"
"github.com/stretchr/testify/assert"
)

func TestFakeRendererInterface(t *testing.T) {
t.Parallel()

assert.Implements(t, (*resource.Renderer)(nil), new(fakerenderer.FakeRenderer))
}
55 changes: 52 additions & 3 deletions resource/shell/preparer.go
Expand Up @@ -14,7 +14,11 @@

package shell

import "github.com/asteris-llc/converge/resource"
import (
"os/exec"

"github.com/asteris-llc/converge/resource"
)

// Preparer for Shell tasks
type Preparer struct {
Expand All @@ -23,6 +27,51 @@ type Preparer struct {
}

// Prepare a new task
func (p *Preparer) Prepare(resource.Renderer) (resource.Task, error) {
return &Shell{}, nil
func (p *Preparer) Prepare(render resource.Renderer) (resource.Task, error) {
check, err := render.Render("check", p.Check)
if err != nil {
return nil, err
}

if err := p.validateScriptSyntax(check); err != nil {
return nil, err
}

apply, err := render.Render("apply", p.Apply)
if err != nil {
return nil, err
}

if err := p.validateScriptSyntax(apply); err != nil {
return nil, err
}

return &Shell{check, apply}, nil
}

func (p *Preparer) validateScriptSyntax(script string) error {
command := exec.Command("sh", "-n")

in, err := command.StdinPipe()
if err != nil {
return err
}

if err := command.Start(); err != nil {
return err
}

if _, err := in.Write([]byte(script)); err != nil {
return err
}

if err := in.Close(); err != nil {
return err
}

if err := command.Wait(); err != nil {
return err
}

return nil
}
42 changes: 42 additions & 0 deletions resource/shell/preparer_test.go
Expand Up @@ -17,6 +17,7 @@ package shell_test
import (
"testing"

"github.com/asteris-llc/converge/helpers/fakerenderer"
"github.com/asteris-llc/converge/resource"
"github.com/asteris-llc/converge/resource/shell"
"github.com/stretchr/testify/assert"
Expand All @@ -27,3 +28,44 @@ func TestPreparerInterface(t *testing.T) {

assert.Implements(t, (*resource.Resource)(nil), new(shell.Preparer))
}

func TestPreparerValidateValid(t *testing.T) {
t.Parallel()

sp := &shell.Preparer{
Check: "echo test",
Apply: "echo test",
}

_, err := sp.Prepare(&fakerenderer.FakeRenderer{})

assert.NoError(t, err)
}

func TestPreparerValidateInvalidCheck(t *testing.T) {
t.Parallel()

sp := &shell.Preparer{
Check: "if do then; esac",
}

_, err := sp.Prepare(&fakerenderer.FakeRenderer{})

if assert.Error(t, err) {
assert.EqualError(t, err, "exit status 2")
}
}

func TestPreparerValidateInvalidApply(t *testing.T) {
t.Parallel()

sp := &shell.Preparer{
Apply: "if do then; esac",
}

_, err := sp.Prepare(&fakerenderer.FakeRenderer{})

if assert.Error(t, err) {
assert.EqualError(t, err, "exit status 2")
}
}
59 changes: 55 additions & 4 deletions resource/shell/shell.go
Expand Up @@ -14,16 +14,67 @@

package shell

import (
"bytes"
"fmt"
"os/exec"
"syscall"
)

// Shell task
type Shell struct {
CheckStmt string `prepare:"Check"`
ApplyStmt string `prepare:"Apply"`
CheckStmt string
ApplyStmt string
}

func (s *Shell) Check() (status string, willChange bool, err error) {
return
out, code, err := s.exec(s.CheckStmt)
return out, code != 0, err
}

func (s *Shell) Apply() (err error) {
return
out, code, err := s.exec(s.ApplyStmt)
if code != 0 {
return fmt.Errorf("exit code %d, output: %q", code, out)
}
return err
}

func (s *Shell) exec(script string) (out string, code uint32, err error) {
command := exec.Command("sh")
stdin, err := command.StdinPipe()
if err != nil {
return "", 0, err
}

// TODO: does this create a race condition?
var sink bytes.Buffer
command.Stdout = &sink
command.Stderr = &sink

if err := command.Start(); err != nil {
return "", 0, err
}

if _, err := stdin.Write([]byte(script)); err != nil {
return "", 0, err
}

if err := stdin.Close(); err != nil {
return "", 0, err
}

err = command.Wait()
if _, ok := err.(*exec.ExitError); !ok && err != nil {
return "", 0, err
}

switch result := command.ProcessState.Sys().(type) {
case syscall.WaitStatus:
code = uint32(result)
default:
panic(fmt.Sprintf("unknown type %+v", result))
}

return sink.String(), code, nil
}
82 changes: 82 additions & 0 deletions resource/shell/shell_test.go
@@ -0,0 +1,82 @@
// Copyright © 2016 Asteris, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package shell_test

import (
"testing"

"github.com/asteris-llc/converge/resource"
"github.com/asteris-llc/converge/resource/shell"
"github.com/stretchr/testify/assert"
)

func TestShellInterface(t *testing.T) {
t.Parallel()

assert.Implements(t, (*resource.Task)(nil), new(shell.Shell))
}

func TestShellTaskCheckNeedsChange(t *testing.T) {
t.Parallel()

s := shell.Shell{
CheckStmt: "echo test && exit 1",
}

current, change, err := s.Check()
assert.Equal(t, "test\n", current)
assert.True(t, change)
assert.Nil(t, err)
}

func TestShellCheckNoChange(t *testing.T) {
t.Parallel()

s := shell.Shell{
CheckStmt: "echo test",
}

current, change, err := s.Check()
assert.Equal(t, "test\n", current)
assert.False(t, change)
assert.Nil(t, err)
}

func TestShellApplySuccess(t *testing.T) {
t.Parallel()

s := shell.Shell{
ApplyStmt: "echo test",
}

assert.NoError(t, s.Apply())
}

func TestShellTaskApplyError(t *testing.T) {
t.Parallel()

s := shell.Shell{
ApplyStmt: "echo bad && exit 1",
}

err := s.Apply()
if assert.Error(t, err) {
assert.EqualError(
t,
err,
`exit code 256, output: "bad\n"`,
)
}
}

0 comments on commit 217b5e9

Please sign in to comment.