blob: e67903912990ba3f9a4f38a2b69bcfa13dc26f2d [file] [log] [blame]
package resultstore
import (
"encoding/json"
"testing"
"fuchsia.googlesource.com/infra/infra/fxtesting"
)
func TestContext(t *testing.T) {
setup := func(t *testing.T) (Context, fxtesting.T) {
return NewContext(), fxtesting.T{t}
}
t.Run("SetAuthToken", func(t *testing.T) {
t.Run("should store the auth token", func(t *testing.T) {
ctx, fxt := setup(t)
fxt.Assert(ctx.SetAuthToken("token"), "could not set auth token")
})
t.Run("should error if overwriting the auth token", func(t *testing.T) {
ctx, fxt := setup(t)
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")
})
})
t.Run("AuthToken", func(t *testing.T) {
t.Run("should return the auth token", func(t *testing.T) {
ctx, fxt := setup(t)
// Should start off empty.
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()
fxt.Assert(token == "token", "got unexpected value for auth token: %s", token)
})
})
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.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.SetInvocationName("Invocation"), "unable to set Invocation")
// Same value should not error
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("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 := 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)
})
})
t.Run("SetTarget", func(t *testing.T) {
t.Run("should store the Target name", func(t *testing.T) {
ctx, fxt := setup(t)
fxt.Assert(ctx.SetTarget("target_id", "target"), "unable to set target")
})
t.Run("should error if overwriting the Target name", func(t *testing.T) {
ctx, fxt := setup(t)
targetID := "target_id"
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")
})
})
t.Run("Target", func(t *testing.T) {
t.Run("should return the Target", func(t *testing.T) {
ctx, fxt := setup(t)
// Should start off empty.
targetID := "target_id"
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")
fxt.Assert(name == "target_name", "got unexpected value for Target name: %s", name)
})
})
t.Run("SetConfiguredTarget", func(t *testing.T) {
t.Run("should store the ConfiguredTarget name", func(t *testing.T) {
ctx, fxt := setup(t)
fxt.Assert(
ctx.SetConfiguredTarget("target_id", "config_id", "target"),
"unable to set ConfiguredTarget")
})
t.Run("should error if overwriting the ConfiguredTarget name", func(t *testing.T) {
ctx, fxt := setup(t)
targetID := "target_id"
configID := "config_id"
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")
})
})
t.Run("ConfiguredTarget", func(t *testing.T) {
t.Run("should return the ConfiguredTarget", func(t *testing.T) {
ctx, fxt := setup(t)
// Should start off empty.
targetID := "target_id"
configID := "config_id"
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)
})
})
t.Run("should retain values after serialization", func(t *testing.T) {
ctx, fxt := setup(t)
// Fill the context.
fxt.Assert(ctx.SetAuthToken("token"), "unable to set set AuthToken")
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")
// Marshal and unmarshal it.
bytes, err := json.Marshal(ctx)
fxt.AssertNil(err)
ctx = NewContext()
fxt.AssertNil(json.Unmarshal(bytes, ctx))
// Verify all the values are still there.
invName := ctx.InvocationName()
fxt.Assert(invName == "inv_name", "expected 'inv_name', got %s", invName)
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")
fxt.Assert(target == "target", "expected 'target', got %s", target)
ct := ctx.ConfiguredTarget("target_id", "config_id")
fxt.Assert(ct == "config_target", "expected 'config_target', got %s", ct)
})
}