t.Fatalf("expected provider ReadStateBytes method to receive TypeName %q and StateId %q, instead got TypeName %q and StateId %q",
typeName,
stateId,
req.TypeName,
req.StateId)
}
returnproviders.ReadStateBytesResponse{
Bytes:[]byte(stateString),
// no diags
}
},
}
// This package will be consumed in a statemgr.Full, so we test using NewRemoteGRPC
// and invoke the method on that interface that uses Get.
c:=NewRemoteGRPC(&provider,typeName,stateId)
err:=c.RefreshState()// Calls Get
iferr!=nil{
t.Fatalf("unexpected error: %s",err)
}
if!provider.ReadStateBytesCalled{
t.Fatal("expected remote grpc state manager's RefreshState method to, via Get, call ReadStateBytes method on underlying provider, but it has not been called")
}
s:=c.State()
v,ok:=s.RootOutputValues["foo"]
if!ok{
t.Fatal("state manager doesn't contain the state returned by the mock")
}
ifv.Value.AsString()!="bar"{
t.Fatal("state manager doesn't contain the correct output value in the state")
}
})
t.Run("state manager made using grpcClient returns expected error from error diagnostic",func(t*testing.T){
// we don't expect state to accompany an error, but this test shows that
// if an error us present amy state returned is ignored.
Bytes:[]byte(stateString),
Diagnostics:diags,
}
},
}
// This package will be consumed in a statemgr.Full, so we test using NewRemoteGRPC
// and invoke the method on that interface that uses Get.
c:=NewRemoteGRPC(&provider,typeName,stateId)
err:=c.RefreshState()// Calls Get
iferr==nil{
t.Fatal("expected an error but got none")
}
if!provider.ReadStateBytesCalled{
t.Fatal("expected remote grpc state manager's RefreshState method to, via Get, call ReadStateBytes method on underlying provider, but it has not been called")
}
s:=c.State()
ifs!=nil{
t.Fatalf("expected refresh to fail due to error diagnostic, but state has been refreshed: %s",s.String())
}
})
}
// Testing grpcClient's Put method, via the state manager made using a grpcClient.
// The PersistState method on a state manager calls the Put method of the underlying client.
funcTest_grpcClient_Put(t*testing.T){
typeName:="foo_bar"// state store 'bar' in provider 'foo'
stateId:="production"
// State with 1 output
s:=states.NewState()
s.SetOutputValue(addrs.AbsOutputValue{
Module:addrs.RootModuleInstance,
OutputValue:addrs.OutputValue{Name:"foo"},
},cty.StringVal("bar"),false)
t.Run("state manager made using grpcClient writes the expected state",func(t*testing.T){
provider:=testing_provider.MockProvider{
// Mock a provider and internal state store that
// have both been configured
ConfigureProviderCalled:true,
ConfigureStateStoreCalled:true,
// Check values received by the provider from the Put method.