[tilo] Minor Context Changes
* Remove boolean return value from getters:
In client code, it's more convenient to just check whether
the value is empty to see if its set in the Context.
* Store both InvocationID and InvocationName
We need both when creating and updating entities.
* Fix formatting in test cases.
IN-699 #comment
Change-Id: Ica9f54a0349eb4f090a4787448bdddb712e3b25f
diff --git a/tilo/resultstore/context.go b/tilo/resultstore/context.go
index 7a93924..abcbb14 100644
--- a/tilo/resultstore/context.go
+++ b/tilo/resultstore/context.go
@@ -33,26 +33,36 @@
type Context interface {
// SetAuthToken stores the Invocation's auth token.
SetAuthToken(string) bool
- // Returns the Invocation's auth token.
- AuthToken() (string, bool)
- // Stores the name of the Invocation.
- SetInvocation(name string) bool
- // Returns the name of the Invocation.
- Invocation() (string, bool)
+ // AuthToken is the Invocation's auth token.
+ AuthToken() string
- // Stores a Target's name under its ID.
- // associated with a different name.
+ // SetInvocationID Stores the name of the Invocation.
+ SetInvocationID(id string) bool
+
+ // SetInvocationName Stores the name of the Invocation.
+ SetInvocationName(name string) bool
+
+ // InvocationID is the Invocation's ID.
+ InvocationID() string
+
+ // InvocationName is the Invocation's name.
+ InvocationName() string
+
+ // SetTarget stores a Target's name under its ID.
SetTarget(id, name string) bool
- // Returns a Target's name, given its ID.
- Target(id string) (string, bool)
- // Stores a ConfiguredTarget's name under its ID.
+ // Target returns a Target's name, given its ID.
+ Target(id string) string
+
+ // SetConfiguredTarget stores a ConfiguredTarget's name under its ID.
SetConfiguredTarget(targetID, configID string, name string) bool
- // Returns a ConfiguredTarget's name, given its ID.
- ConfiguredTarget(targetID, configID string) (string, bool)
+
+ // ConfiguredTarget returns a ConfiguredTarget's name, given its ID.
+ ConfiguredTarget(targetID, configID string) string
}
+// NewContext returns a new Context.
func NewContext() Context {
return &rsContext{}
}
@@ -60,7 +70,8 @@
// The default Context implementation.
type rsContext struct {
AuthTokenValue string `json:"auth_token"`
- InvocationName string `json:"invocation"`
+ InvocationNameValue string `json:"invocation_id"`
+ InvocationIDValue string `json:"invocation_id`
TargetNames map[string]string `json:"targets"`
ConfiguredTargetNames map[string]string `json:"configured_targets"`
}
@@ -73,54 +84,68 @@
return true
}
-func (c *rsContext) AuthToken() (string, bool) {
- return c.AuthTokenValue, c.AuthTokenValue != ""
+func (c *rsContext) AuthToken() string {
+ return c.AuthTokenValue
}
-func (c *rsContext) SetInvocation(name string) bool {
- if c.InvocationName != "" && c.InvocationName != name {
+func (c *rsContext) SetInvocationName(name string) bool {
+ if c.InvocationNameValue != "" && c.InvocationNameValue != name {
return false
}
- c.InvocationName = name
+ c.InvocationNameValue = name
return true
}
-func (c *rsContext) Invocation() (string, bool) {
- return c.InvocationName, c.InvocationName != ""
+func (c *rsContext) InvocationName() string {
+ return c.InvocationNameValue
+}
+
+func (c *rsContext) SetInvocationID(name string) bool {
+ if c.InvocationIDValue != "" && c.InvocationIDValue != name {
+ return false
+ }
+ c.InvocationIDValue = name
+ return true
+}
+
+func (c *rsContext) InvocationID() string {
+ return c.InvocationIDValue
}
func (c *rsContext) SetTarget(id, name string) bool {
c.ensureTargets()
- oldName, ok := c.Target(id)
- if ok && oldName != name {
+ oldName := c.Target(id)
+ if oldName != "" && oldName != name {
return false
}
+
c.TargetNames[id] = name
return true
}
-func (c *rsContext) Target(id string) (string, bool) {
+func (c *rsContext) Target(id string) string {
c.ensureTargets()
- val, ok := c.TargetNames[id]
- return val, ok
+ val, _ := c.TargetNames[id]
+ return val
}
func (c *rsContext) SetConfiguredTarget(targetID, configID string, name string) bool {
c.ensureConfiguredTargets()
- ID := c.createConfiguredTargetID(targetID, configID)
- oldName, ok := c.ConfiguredTarget(targetID, configID)
- if ok && oldName != name {
+ oldName := c.ConfiguredTarget(targetID, configID)
+ if oldName != "" && oldName != name {
return false
}
+
+ ID := c.createConfiguredTargetID(targetID, configID)
c.ConfiguredTargetNames[ID] = name
return true
}
-func (c *rsContext) ConfiguredTarget(targetID, configID string) (string, bool) {
+func (c *rsContext) ConfiguredTarget(targetID, configID string) string {
c.ensureConfiguredTargets()
ID := c.createConfiguredTargetID(targetID, configID)
- val, ok := c.ConfiguredTargetNames[ID]
- return val, ok
+ val, _ := c.ConfiguredTargetNames[ID]
+ return val
}
func (c *rsContext) ensureTargets() {
@@ -128,6 +153,7 @@
c.TargetNames = make(map[string]string)
}
}
+
func (c *rsContext) ensureConfiguredTargets() {
if c.ConfiguredTargetNames == nil {
c.ConfiguredTargetNames = make(map[string]string)
diff --git a/tilo/resultstore/context_test.go b/tilo/resultstore/context_test.go
index f16c829..e679039 100644
--- a/tilo/resultstore/context_test.go
+++ b/tilo/resultstore/context_test.go
@@ -22,9 +22,7 @@
fxt.Assert(ctx.SetAuthToken("token"), "could not set auth token")
// Same value should not error
fxt.Assert(ctx.SetAuthToken("token"), "could not set auth token")
- fxt.Assert(
- !ctx.SetAuthToken("new_token"),
- "should not have been able to overwrite the auth token")
+ fxt.Assert(!ctx.SetAuthToken("new_token"), "should not have been able to overwrite the auth token")
})
})
@@ -33,43 +31,65 @@
ctx, fxt := setup(t)
// Should start off empty.
- token, ok := ctx.AuthToken()
- fxt.Assert(!ok && token == "", "auth token should be empty. Got %s", token)
+ token := ctx.AuthToken()
+ fxt.Assert(token == "", "auth token should be empty. Got %s", token)
fxt.Assert(ctx.SetAuthToken("token"), "unable to set auth token")
- token, _ = ctx.AuthToken()
+ token = ctx.AuthToken()
fxt.Assert(token == "token", "got unexpected value for auth token: %s", token)
})
})
- t.Run("SetInvocation", func(t *testing.T) {
+ t.Run("SetInvocationName", func(t *testing.T) {
t.Run("should store the Invocation name", func(t *testing.T) {
ctx, fxt := setup(t)
- fxt.Assert(ctx.SetInvocation("invocation"), "unable to set Invocation")
+ fxt.Assert(ctx.SetInvocationName("inv_name"), "unable to set Invocation")
})
t.Run("should error if overwriting the Invocation name", func(t *testing.T) {
ctx, fxt := setup(t)
- fxt.Assert(ctx.SetInvocation("Invocation"), "unable to set Invocation")
+ fxt.Assert(ctx.SetInvocationName("Invocation"), "unable to set Invocation")
// Same value should not error
- fxt.Assert(ctx.SetInvocation("Invocation"), "unable to set Invocation")
- fxt.Assert(
- !ctx.SetInvocation("new_invocation"),
- "should not have been able to overwrite Invocation")
+ fxt.Assert(ctx.SetInvocationName("Invocation"), "unable to set Invocation")
+ fxt.Assert(!ctx.SetInvocationName("new_invocation"), "should not have been able to overwrite Invocation")
})
})
- t.Run("Invocation", func(t *testing.T) {
+ t.Run("SetInvocationID", func(t *testing.T) {
+ t.Run("should store the Invocation ID", func(t *testing.T) {
+ ctx, fxt := setup(t)
+ fxt.Assert(ctx.SetInvocationID("inv_id"), "unable to set Invocation")
+ })
+ t.Run("should error if overwriting the Invocation ID", func(t *testing.T) {
+ ctx, fxt := setup(t)
+ fxt.Assert(ctx.SetInvocationID("Invocation"), "unable to set Invocation")
+ // Same value should not error
+ fxt.Assert(ctx.SetInvocationID("Invocation"), "unable to set Invocation")
+ fxt.Assert(!ctx.SetInvocationID("new_invocation"), "should not have been able to overwrite Invocation")
+ })
+ })
+
+ t.Run("InvocationName", func(t *testing.T) {
t.Run("should return the Invocation name", func(t *testing.T) {
ctx, fxt := setup(t)
// Should start off empty.
- invocation, ok := ctx.Invocation()
- fxt.Assert(!ok && invocation == "",
- "Invocation should be empty. got %s", invocation)
- fxt.Assert(ctx.SetInvocation("invocation"), "unable to set Invocation")
- invocation, _ = ctx.Invocation()
- fxt.Assert(
- invocation == "invocation",
- "got unexpected value for Invocation name: %s", invocation)
+ invocation := ctx.InvocationName()
+ fxt.Assert(invocation == "", "Invocation should be empty. Got %s", invocation)
+ fxt.Assert(ctx.SetInvocationName("inv_name"), "unable to set Invocation")
+ invocation = ctx.InvocationName()
+ fxt.Assert(invocation == "inv_name", "got unexpected value for Invocation name: %s", invocation)
+ })
+ })
+
+ t.Run("InvocationID", func(t *testing.T) {
+ t.Run("should return the Invocation ID", func(t *testing.T) {
+ ctx, fxt := setup(t)
+
+ // Should start off empty.
+ invocation := ctx.InvocationID()
+ fxt.Assert(invocation == "", "Invocation should be empty. Got %s", invocation)
+ fxt.Assert(ctx.SetInvocationID("inv_id"), "unable to set Invocation")
+ invocation = ctx.InvocationID()
+ fxt.Assert(invocation == "inv_id", "got unexpected value for Invocation name: %s", invocation)
})
})
@@ -84,9 +104,7 @@
fxt.Assert(ctx.SetTarget(targetID, "target"), "unable to set target")
// Same value should not error
fxt.Assert(ctx.SetTarget(targetID, "target"), "unable to set target")
- fxt.Assert(
- !ctx.SetTarget(targetID, "new_target"),
- "should not have been able to overwrite the Target")
+ fxt.Assert(!ctx.SetTarget(targetID, "new_target"), "should not have been able to overwrite the Target")
})
})
@@ -96,10 +114,10 @@
// Should start off empty.
targetID := "target_id"
- name, ok := ctx.Target(targetID)
- fxt.Assert(!ok && name == "", "Target should be empty. Got %s", name)
+ name := ctx.Target(targetID)
+ fxt.Assert(name == "", "Target should be empty. Got %s", name)
fxt.Assert(ctx.SetTarget(targetID, "target_name"), "unable to set target")
- name, _ = ctx.Target("target_id")
+ name = ctx.Target("target_id")
fxt.Assert(name == "target_name", "got unexpected value for Target name: %s", name)
})
})
@@ -118,11 +136,8 @@
fxt.Assert(ctx.SetConfiguredTarget(targetID, configID, "ct"),
"unable to set ConfiguredTarget")
// Same value should not error
- fxt.Assert(ctx.SetConfiguredTarget(targetID, configID, "ct"),
- "unable to set ConfiguredTarget")
- fxt.Assert(
- !ctx.SetConfiguredTarget(targetID, configID, "new_ct"),
- "should not have been able to overwrite the ConfiguredTarget")
+ fxt.Assert(ctx.SetConfiguredTarget(targetID, configID, "ct"), "unable to set ConfiguredTarget")
+ fxt.Assert(!ctx.SetConfiguredTarget(targetID, configID, "new_ct"), "should not have been able to overwrite the ConfiguredTarget")
})
})
@@ -133,13 +148,11 @@
// Should start off empty.
targetID := "target_id"
configID := "config_id"
- name, ok := ctx.ConfiguredTarget(targetID, configID)
- fxt.Assert(!ok && name == "", "ConfiguredTarget name should be empty. Got %s", name)
- fxt.Assert(ctx.SetConfiguredTarget(targetID, configID, "ct"),
- "unable to set ConfiguredTarget")
- name, _ = ctx.ConfiguredTarget(targetID, configID)
- fxt.Assert(name == "ct",
- "got unexpected value for configured target name: %s", name)
+ name := ctx.ConfiguredTarget(targetID, configID)
+ fxt.Assert(name == "", "ConfiguredTarget name should be empty. Got %s", name)
+ fxt.Assert(ctx.SetConfiguredTarget(targetID, configID, "ct"), "unable to set ConfiguredTarget")
+ name = ctx.ConfiguredTarget(targetID, configID)
+ fxt.Assert(name == "ct", "got unexpected value for configured target name: %s", name)
})
})
@@ -148,10 +161,10 @@
// Fill the context.
fxt.Assert(ctx.SetAuthToken("token"), "unable to set set AuthToken")
- fxt.Assert(ctx.SetInvocation("invocation"), "unable to set set Invocation")
+ fxt.Assert(ctx.SetInvocationName("inv_name"), "unable to set set Invocation Name")
+ fxt.Assert(ctx.SetInvocationID("inv_id"), "unable to set Invocatino ID")
fxt.Assert(ctx.SetTarget("target_id", "target"), "unable to set set Target")
- fxt.Assert(ctx.SetConfiguredTarget("target_id", "config_id", "config_target"),
- "unable to set set ConfiguredTarget")
+ fxt.Assert(ctx.SetConfiguredTarget("target_id", "config_id", "config_target"), "unable to set set ConfiguredTarget")
// Marshal and unmarshal it.
bytes, err := json.Marshal(ctx)
@@ -161,16 +174,19 @@
// Verify all the values are still there.
- invocation, _ := ctx.Invocation()
- fxt.Assert(invocation == "invocation", "expected 'invocation', got %s", invocation)
+ invName := ctx.InvocationName()
+ fxt.Assert(invName == "inv_name", "expected 'inv_name', got %s", invName)
- token, _ := ctx.AuthToken()
+ invID := ctx.InvocationID()
+ fxt.Assert(invID == "inv_id", "expected 'inv_id', got %s", invID)
+
+ token := ctx.AuthToken()
fxt.Assert(token == "token", "expected 'token', got %s", token)
- target, _ := ctx.Target("target_id")
+ target := ctx.Target("target_id")
fxt.Assert(target == "target", "expected 'target', got %s", target)
- ct, _ := ctx.ConfiguredTarget("target_id", "config_id")
+ ct := ctx.ConfiguredTarget("target_id", "config_id")
fxt.Assert(ct == "config_target", "expected 'config_target', got %s", ct)
})
}