grim/convey

closing merged branch
hostnames
2017-10-13, Gary Kramlich
33eae19fcbbe
closing merged branch
/*
* Convey
* Copyright 2016-2017 Gary Kramlich <grim@reaperworld.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package intrinsic
import (
"bitbucket.org/rw_grim/convey/environment"
"bitbucket.org/rw_grim/convey/state"
"bitbucket.org/rw_grim/convey/tasks"
"github.com/aphistic/gomol"
"github.com/aphistic/sweet"
. "github.com/onsi/gomega"
)
func (s *intrinsicSuite) TestExtendExecute(t sweet.T) {
var (
task = newMockTask()
fullEnv = []string{}
st = &state.State{Environment: []string{"bar=bonk", "quux=honk"}}
)
task.executeFn = func(name string, logger *gomol.LogAdapter, env []string, st *state.State) error {
fullEnv = environment.Merge(environment.Merge(env, []string{"x=1", "foo=quux"}), st.Environment)
return nil
}
c := &Extend{
InnerTask: task,
Environment: []string{"foo=bar", "bar=baz"},
}
Expect(c.Execute("task", nil, nil, st)).To(BeNil())
Expect(fullEnv).To(HaveLen(4))
Expect(fullEnv).To(ConsistOf("x=1", "foo=bar", "bar=bonk", "quux=honk"))
// Should revert state
Expect(st.Environment).To(HaveLen(2))
Expect(st.Environment).To(ConsistOf("bar=bonk", "quux=honk"))
}
func (s *intrinsicSuite) TestDependencies(t sweet.T) {
c := &Extend{Task: "test-task"}
Expect(c.Dependencies()).To(Equal([]string{"test-task"}))
}
func (s *intrinsicSuite) TestResolve(t sweet.T) {
task := newMockTask()
task.X = 3
task.Y = []string{"foo", "bar"}
task.Z = struct {
A string
B string
C string
}{
A: "wynken",
B: "blynken",
C: "nod",
}
c := &Extend{Task: "a"}
err := c.Resolve(map[string]tasks.Task{
"a": task,
})
Expect(err).To(BeNil())
// Must clone inner task
Expect(c.InnerTask).NotTo(BeNil())
Expect(c.InnerTask).NotTo(BeIdenticalTo(task))
Expect(c.InnerTask.(*mockTask).X).To(Equal(3))
Expect(c.InnerTask.(*mockTask).Y).To(Equal([]string{"foo", "bar"}))
Expect(c.InnerTask.(*mockTask).Z.A).To(Equal("wynken"))
Expect(c.InnerTask.(*mockTask).Z.B).To(Equal("blynken"))
Expect(c.InnerTask.(*mockTask).Z.C).To(Equal("nod"))
}
func (s *intrinsicSuite) TestResolveChain(t sweet.T) {
task := newMockTask()
task.X = 3
task.Y = []string{"foo", "bar"}
c1 := &Extend{Task: "a"}
c2 := &Extend{Task: "b"}
taskMapping := map[string]tasks.Task{
"a": task,
"b": c1,
}
Expect(c1.Resolve(taskMapping)).To(BeNil())
Expect(c2.Resolve(taskMapping)).To(BeNil())
// Must clone inner task
Expect(c2.InnerTask).NotTo(BeNil())
Expect(c2.InnerTask).NotTo(BeIdenticalTo(c1))
Expect(c2.InnerTask.(*Extend).InnerTask).NotTo(BeNil())
Expect(c2.InnerTask.(*Extend).InnerTask.(*mockTask).X).To(Equal(3))
Expect(c2.InnerTask.(*Extend).InnerTask.(*mockTask).Y).To(Equal([]string{"foo", "bar"}))
}
//
// Mocks
type mockTask struct {
X int
Y []string
Z struct {
A string
B string
C string
}
executeFn func(string, *gomol.LogAdapter, []string, *state.State) error
}
func newMockTask() *mockTask {
return &mockTask{
executeFn: func(string, *gomol.LogAdapter, []string, *state.State) error {
return nil
},
}
}
func (t *mockTask) Execute(name string, logger *gomol.LogAdapter, env []string, st *state.State) error {
return t.executeFn(name, logger, env, st)
}
func (t *mockTask) New() tasks.Task {
return &mockTask{}
}
func (t *mockTask) Valid() error {
return nil
}