Skip to content

Commit

Permalink
chore: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nehemming committed Aug 23, 2021
1 parent aa71760 commit 61355cf
Show file tree
Hide file tree
Showing 13 changed files with 1,243 additions and 268 deletions.
5 changes: 5 additions & 0 deletions pkg/resource/notfounderror.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package resource

import (
"fmt"
"strings"
)

// NotFoundError indicates a resource cannot be found.
Expand Down Expand Up @@ -47,6 +48,10 @@ func (nfe *NotFoundError) Resource() string {

// Error converts the errorinto a string.
func (nfe *NotFoundError) Error() string {
ce := nfe.cause.Error()
if strings.Contains(ce, nfe.resource) {
return ce
}
return fmt.Sprintf("%s %s", nfe.resource, nfe.cause)
}

Expand Down
10 changes: 6 additions & 4 deletions pkg/resource/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"testing"
)

const readmeOpeningLine = "<p"

func TestReadResourceZero(t *testing.T) {
ctx := context.Background()
b, err := ReadResource(ctx)
Expand Down Expand Up @@ -71,7 +73,7 @@ func TestReadResourceLocalTestFile(t *testing.T) {
func TestReadResourceRemoteWeb(t *testing.T) {
ctx := context.Background()
b, err := ReadResource(ctx, "https://raw.githubusercontent.com/nehemming/cirocket/master/README.md")
if err != nil || !strings.HasPrefix(string(b), "#") {
if err != nil || !strings.HasPrefix(string(b), readmeOpeningLine) {
t.Error("unexpected", err, string(b))
}
}
Expand All @@ -94,7 +96,7 @@ func TestReadResourceRemoteFileSplit(t *testing.T) {
b, err := ReadResource(ctx,
"https://raw.githubusercontent.com/nehemming",
"cirocket/master/README.md")
if err != nil || !strings.HasPrefix(string(b), "#") {
if err != nil || !strings.HasPrefix(string(b), readmeOpeningLine) {
t.Error("unexpected", err, string(b))
}
}
Expand All @@ -105,7 +107,7 @@ func TestReadResourceRemoteFileSplitMltioots(t *testing.T) {
"c:\\windows", "//www/data/me",
"https://raw.githubusercontent.com/nehemming",
"cirocket/master/README.md")
if err != nil || !strings.HasPrefix(string(b), "#") {
if err != nil || !strings.HasPrefix(string(b), readmeOpeningLine) {
t.Error("unexpected", err, string(b))
}
}
Expand Down Expand Up @@ -164,7 +166,7 @@ func TestOpenReadURLErrors(t *testing.T) {
defer f.Close()

b, err := io.ReadAll(f)
if err != nil || !strings.HasPrefix(string(b), "#") {
if err != nil || !strings.HasPrefix(string(b), readmeOpeningLine) {
t.Error("unexpected", err, string(b))
}
}
Expand Down
23 changes: 23 additions & 0 deletions pkg/rocket/assemble_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,26 @@ func TestAssembleOneSourcesPathMissionWithSpec(t *testing.T) {
t.Error("unexpected", err)
}
}

func TestAssembleRunbookErrors(t *testing.T) {
loggee.SetLogger(stdlog.New())
ctx := context.Background()
mc := NewMissionControl()
tt := &testTaskType{t: t}
mc.RegisterTaskTypes(tt)

sources := []string{"testdata"}

err := mc.Assemble(ctx, "blue_runbook", sources, "bad", nil)
if err == nil {
t.Error("error unexpected")
}
}

func TestLoadMapFromLocationDecodeFail(t *testing.T) {
ctx := context.Background()
m, n, err := loadMapFromLocation(ctx, Location{Inline: "bad"}, "")
if err == nil || n != "" || m != nil {
t.Error("error unexpected", err, n, m)
}
}
57 changes: 45 additions & 12 deletions pkg/rocket/capcomm.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"runtime"
"strconv"
"strings"
"sync"
"text/template"
"time"

Expand Down Expand Up @@ -102,6 +103,9 @@ type (
// CapComm handles all communication between mission control and the mission stages and tasks.
CapComm struct {
data TemplateData
dataVersiion uint
version uint
dataLock sync.Mutex
env Getter
entrustedParentEnv Getter
funcMap template.FuncMap
Expand Down Expand Up @@ -188,7 +192,6 @@ func (capComm *CapComm) Copy(noTrust bool) *CapComm {
// Create the new capComm from the source
// Do not copy data and d not seal
newCapComm := &CapComm{
sealed: false,
mission: capComm.mission,
additionalMissionData: capComm.additionalMissionData, // no copy, but safe as set once
funcMap: make(template.FuncMap),
Expand Down Expand Up @@ -227,6 +230,13 @@ func (capComm *CapComm) Seal() *CapComm {
return capComm
}

func (capComm *CapComm) setModified() *CapComm {
capComm.dataLock.Lock()
defer capComm.dataLock.Unlock()
capComm.version++
return capComm
}

// Log returns the cap com logger.
func (capComm *CapComm) Log() loggee.Logger {
return capComm.log
Expand All @@ -236,15 +246,18 @@ func (capComm *CapComm) Log() loggee.Logger {
func (capComm *CapComm) ExportVariable(key, value string) *CapComm {
// Save the value in the local and export lists
capComm.variables.Set(key, value)
capComm.exportTo.Set(key, value)
return capComm
if capComm.exportTo != nil {
capComm.exportTo.Set(key, value)
}
return capComm.setModified()
}

// SetLocalVariable sets a local capComm's variable.
func (capComm *CapComm) SetLocalVariable(key, value string) *CapComm {
// Save the value in the local and export lists
capComm.variables.Set(key, value)
return capComm
capComm.data = nil
return capComm.setModified()
}

// ExportVariables exports a list of variables if set in the capComm to aa parent. If the variable is not found
Expand All @@ -257,6 +270,7 @@ func (capComm *CapComm) ExportVariables(exports Exports) {
capComm.exportTo.Set(key, value)
}
}
capComm.setModified()
}

func (capComm *CapComm) getTemplateVariables() map[string]string {
Expand All @@ -278,8 +292,10 @@ func (capComm *CapComm) GetVariable(key string) (string, bool) {
if value, ok := capComm.variables.Get(key); ok {
return value, true
}
if value, ok := capComm.exportTo.Get(key); ok {
return value, true
if capComm.exportTo != nil {
if value, ok := capComm.exportTo.Get(key); ok {
return value, true
}
}
return "", false
}
Expand Down Expand Up @@ -416,7 +432,7 @@ func (capComm *CapComm) createProviderFromInputSpec(ctx context.Context, inputSp
}
rp, err = providers.NewURLProvider(v, time.Second*time.Duration(inputSpec.URLTimeout), inputSpec.Optional)
} else {
panic("validation bad input runbook")
panic("validation bad input spec")
}

return rp, err
Expand Down Expand Up @@ -636,6 +652,9 @@ func (capComm *CapComm) MergeParams(ctx context.Context, params []Param) error {
return errors.Wrapf(err, "parameter %s", p.Name)
}
kvg.kv[p.Name] = v

// mark modified within look as expandParam can use data just added
capComm.setModified()
}

return nil
Expand All @@ -655,6 +674,11 @@ func (capComm *CapComm) MergeTemplateEnvs(ctx context.Context, env VarMap) error
expanded[k] = v
}

if len(expanded) == 0 {
return nil
}

defer capComm.setModified()
kvg := capComm.env.(*KeyValueGetter)
for k, v := range expanded {
kvg.kv[k] = v
Expand Down Expand Up @@ -736,12 +760,22 @@ func (capComm *CapComm) GetExecEnv() []string {

// GetTemplateData gts the data collection supplied to a template.
func (capComm *CapComm) GetTemplateData(ctx context.Context) TemplateData {
if capComm.data != nil {
// quick check
data := capComm.data
if data != nil && capComm.dataVersiion == capComm.version {
return capComm.data
}

data := make(TemplateData)
capComm.dataLock.Lock()
defer capComm.dataLock.Unlock()

// recheck
data = capComm.data
if data != nil && capComm.dataVersiion == capComm.version {
return capComm.data
}

data = make(TemplateData)
// Add mission data
if capComm.additionalMissionData != nil {
data[AdditionalMissionTag] = capComm.additionalMissionData
Expand All @@ -768,9 +802,8 @@ func (capComm *CapComm) GetTemplateData(ctx context.Context) TemplateData {
data[BuildTag] = buildinfo.GetBuildInfo(ctx)

// Save cached values
if capComm.sealed {
capComm.data = data
}
capComm.dataVersiion = capComm.version
capComm.data = data

return data
}
Expand Down
Loading

0 comments on commit 61355cf

Please sign in to comment.