| 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("SetEnvironment", func(t *testing.T) { |
| t.Run("should store the ResultStore environment", func(t *testing.T) { |
| ctx, fxt := setup(t) |
| fxt.Assert(ctx.SetEnvironment(Production), "unable to set ResultStore environment") |
| }) |
| t.Run("should error if overwriting the ResultStore environment", func(t *testing.T) { |
| ctx, fxt := setup(t) |
| fxt.Assert(ctx.SetEnvironment(Production), "unable to set ResultStore environment") |
| // Same value should not error |
| fxt.Assert(ctx.SetEnvironment(Production), "unable to set ResultStore environment") |
| fxt.Assert(!ctx.SetEnvironment(Staging), "should not have been able to overwrite ResultStore environment") |
| }) |
| }) |
| |
| t.Run("Environment", func(t *testing.T) { |
| t.Run("should return the ResultStore environment", func(t *testing.T) { |
| ctx, fxt := setup(t) |
| |
| // Should start off empty. |
| environment := ctx.Environment() |
| fxt.Assert(environment == "", "environment should be empty. Got %s", environment) |
| fxt.Assert(ctx.SetEnvironment(Production), "unable to set ResultStore environment") |
| environment = ctx.Environment() |
| fxt.Assert(environment == Production, "got unexpected value for environment: %s", environment) |
| }) |
| }) |
| |
| 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("invocation_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("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("invocation_name"), "unable to set Invocation") |
| invocation = ctx.InvocationName() |
| fxt.Assert(invocation == "invocation_name", "got unexpected value for Invocation name: %s", 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("invocation_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("invocation_name"), "unable to set Invocation") |
| invocation = ctx.InvocationName() |
| fxt.Assert(invocation == "invocation_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("invocation_id"), "unable to set Invocation") |
| invocation = ctx.InvocationID() |
| fxt.Assert(invocation == "invocation_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("SetTestAction", func(t *testing.T) { |
| t.Run("should store the TestAction name", func(t *testing.T) { |
| ctx, fxt := setup(t) |
| fxt.Assert(ctx.SetTestAction("target_id", "config_id", "action"), "unable to set action") |
| }) |
| t.Run("should error if overwriting the TestAction name", func(t *testing.T) { |
| ctx, fxt := setup(t) |
| targetID := "target_id" |
| configID := "config_id" |
| fxt.Assert(ctx.SetTestAction(targetID, configID, "action"), "unable to set action") |
| // Same value should not error |
| fxt.Assert(ctx.SetTestAction(targetID, configID, "action"), "unable to set action") |
| fxt.Assert(!ctx.SetTestAction(targetID, configID, "new_action"), "should not have been able to overwrite the TestAction") |
| }) |
| }) |
| |
| t.Run("TestAction", func(t *testing.T) { |
| t.Run("should return the TestAction", func(t *testing.T) { |
| ctx, fxt := setup(t) |
| |
| // Should start off empty. |
| targetID := "target_id" |
| configID := "config_id" |
| name := ctx.TestAction(targetID, configID) |
| fxt.Assert(name == "", "TestAction should be empty. Got %s", name) |
| fxt.Assert(ctx.SetTestAction(targetID, configID, "action_name"), "unable to set action") |
| name = ctx.TestAction("target_id", "config_id") |
| fxt.Assert(name == "action_name", "got unexpected value for TestAction 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("invocation_name"), "unable to set set Invocation Name") |
| fxt.Assert(ctx.SetInvocationID("invocation_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.SetEnvironment(Production), "unable to set environment") |
| fxt.Assert(ctx.SetTestAction("target_id", "config_id", "action"), "unable to set TestAction") |
| |
| // 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. |
| |
| invocationName := ctx.InvocationName() |
| fxt.Assert(invocationName == "invocation_name", "expected 'invocation_name', got %s", invocationName) |
| |
| invocationID := ctx.InvocationID() |
| fxt.Assert(invocationID == "invocation_id", "expected 'invocation_id', got %s", invocationID) |
| |
| 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) |
| |
| action := ctx.TestAction("target_id", "config_id") |
| fxt.Assert(action == "action", "expected 'target_id', got %s", action) |
| |
| environment := ctx.Environment() |
| fxt.Assert(environment == Production, "expected '%s', got %s", Production, environment) |
| }) |
| } |