Repository: opentofu/opentofu
File: internal/backend/testing.go (Go)

Code before:
				SchemaVersion: 0,
			},
			addrs.AbsProviderConfig{
				Provider: addrs.NewDefaultProvider("test"),
				Module:   addrs.RootModule,
			},
			addrs.NoKey,
		)

		// Lock before writing
		lockInfo := statemgr.NewLockInfo()
		lockInfo.Operation = "init"
		lockId, err := foo.Lock(t.Context(), lockInfo)
		if err != nil {
			t.Fatalf("error locking foo: %s", err)
		}

		// write a distinct known state to bar
		if err := bar.WriteState(barState); err != nil {
			t.Fatalf("bad: %s", err)
		}
		if err := bar.PersistState(t.Context(), nil); err != nil {
			t.Fatalf("bad: %s", err)
		}
		// Unlock the state after writing
		if err := foo.Unlock(t.Context(), lockId); err != nil {
			t.Fatalf("error unlocking foo: %s", err)
		}

		// verify that foo is unchanged with the existing state manager
		if err := foo.RefreshState(t.Context()); err != nil {
			t.Fatal("error refreshing foo:", err)
		}
		fooState = foo.State()
		if fooState.HasManagedResourceInstanceObjects() {
			t.Fatal("after writing a resource to bar, foo now has resources too")
		}

		// fetch foo again from the backend
		foo, err = b.StateMgr(t.Context(), "foo")
		if err != nil {
			t.Fatal("error re-fetching state:", err)
		}
		if err := foo.RefreshState(t.Context()); err != nil {
			t.Fatal("error refreshing foo:", err)
		}
		fooState = foo.State()
		if fooState.HasManagedResourceInstanceObjects() {
			t.Fatal("after writing a resource to bar and re-reading foo, foo now has resources too")
		}


Review comment (bug):
This feels a little bit weird to have it here. Please correct me if I am wrong.
Wouldn't be better to have the locking done right after creating `foo` state manager? And then the unlocking right after all the tests (aka before deleting workspaces)?

And another confusing part, this `foo` lock/unlock pair wraps operations on `bar`. Maybe I am confused for nothing, but doesn't look right to me 🤔 
Could you help understanding why is it needed this way?

Thanks

Code after:
		// We'll make "bar" different by adding a fake resource state to it.
		barState.SyncWrapper().SetResourceInstanceCurrent(
			addrs.ResourceInstance{
				Resource: addrs.Resource{
					Mode: addrs.ManagedResourceMode,
					Type: "test_thing",
					Name: "foo",
				},
			}.Absolute(addrs.RootModuleInstance),
			&states.ResourceInstanceObjectSrc{
				AttrsJSON:     []byte("{}"),
				Status:        states.ObjectReady,
				SchemaVersion: 0,
			},
			addrs.AbsProviderConfig{
				Provider: addrs.NewDefaultProvider("test"),
				Module:   addrs.RootModule,
			},
			addrs.NoKey,
		)

		// write a distinct known state to bar
		if err := bar.WriteState(barState); err != nil {
			t.Fatalf("bad: %s", err)
		}
		if err := bar.PersistState(t.Context(), nil); err != nil {
			t.Fatalf("bad: %s", err)
		}

		// verify that foo is unchanged with the existing state manager
		if err := foo.RefreshState(t.Context()); err != nil {
			t.Fatal("error refreshing foo:", err)
		}
		fooState = foo.State()
		if fooState.HasManagedResourceInstanceObjects() {
			t.Fatal("after writing a resource to bar, foo now has resources too")
		}

		// fetch foo again from the backend
		foo, err = b.StateMgr(t.Context(), "foo")
		if err != nil {
			t.Fatal("error re-fetching state:", err)
		}
		if err := foo.RefreshState(t.Context()); err != nil {
			t.Fatal("error refreshing foo:", err)
		}
		fooState = foo.State()
		if fooState.HasManagedResourceInstanceObjects() {
			t.Fatal("after writing a resource to bar and re-reading foo, foo now has resources too")
		}