-
Notifications
You must be signed in to change notification settings - Fork 1
Cloud-SDK extraction B/C/D — PR 1: IaCStateBackend.Configure RPC + host wiring #679
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| package module | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "reflect" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| pb "github.com/GoCodeAlone/workflow/plugin/external/proto" | ||
| "google.golang.org/grpc" | ||
| "google.golang.org/grpc/codes" | ||
| "google.golang.org/grpc/status" | ||
| ) | ||
|
|
||
| // configureStateBackendClient is a pb.IaCStateBackendClient stub for the | ||
| // IaCModule.Init() Configure-wiring tests: it records the Configure request it | ||
| // received and can be told to fail. | ||
| type configureStateBackendClient struct { | ||
| gotConfigure *pb.ConfigureRequest | ||
| configureErr error | ||
| } | ||
|
|
||
| func (c *configureStateBackendClient) Configure(_ context.Context, r *pb.ConfigureRequest, _ ...grpc.CallOption) (*pb.ConfigureResponse, error) { | ||
| c.gotConfigure = r | ||
| if c.configureErr != nil { | ||
| return nil, c.configureErr | ||
| } | ||
| return &pb.ConfigureResponse{}, nil | ||
| } | ||
| func (*configureStateBackendClient) GetState(context.Context, *pb.GetStateRequest, ...grpc.CallOption) (*pb.GetStateResponse, error) { | ||
| return nil, nil | ||
| } | ||
| func (*configureStateBackendClient) SaveState(context.Context, *pb.SaveStateRequest, ...grpc.CallOption) (*pb.SaveStateResponse, error) { | ||
| return nil, nil | ||
| } | ||
| func (*configureStateBackendClient) ListStates(context.Context, *pb.ListStatesRequest, ...grpc.CallOption) (*pb.ListStatesResponse, error) { | ||
| return nil, nil | ||
| } | ||
| func (*configureStateBackendClient) DeleteState(context.Context, *pb.DeleteStateRequest, ...grpc.CallOption) (*pb.DeleteStateResponse, error) { | ||
| return nil, nil | ||
| } | ||
| func (*configureStateBackendClient) Lock(context.Context, *pb.LockRequest, ...grpc.CallOption) (*pb.LockResponse, error) { | ||
| return nil, nil | ||
| } | ||
| func (*configureStateBackendClient) Unlock(context.Context, *pb.UnlockRequest, ...grpc.CallOption) (*pb.UnlockResponse, error) { | ||
| return nil, nil | ||
| } | ||
| func (*configureStateBackendClient) ListBackendNames(context.Context, *pb.ListBackendNamesRequest, ...grpc.CallOption) (*pb.ListBackendNamesResponse, error) { | ||
| return nil, nil | ||
| } | ||
|
|
||
| // TestIaCModuleConfigureWiring asserts IaCModule.Init() calls the plugin | ||
| // backend's Configure RPC with the backend name and the JSON-encoded module | ||
| // config before the store becomes usable. | ||
| func TestIaCModuleConfigureWiring(t *testing.T) { | ||
| const backend = "azure_blob_configure_wiring_test" | ||
| fake := &configureStateBackendClient{} | ||
| if err := iacStateBackendRegistryInstance.register(backend, fake); err != nil { | ||
| t.Fatalf("register: %v", err) | ||
| } | ||
| defer func() { | ||
| iacStateBackendRegistryInstance.mu.Lock() | ||
| delete(iacStateBackendRegistryInstance.clients, backend) | ||
| iacStateBackendRegistryInstance.mu.Unlock() | ||
| }() | ||
|
|
||
| cfg := map[string]any{"backend": backend, "container": "tfstate", "account": "wf"} | ||
| m := NewIaCModule("iac-plugin", cfg) | ||
| if err := m.Init(NewMockApplication()); err != nil { | ||
| t.Fatalf("Init: %v", err) | ||
| } | ||
|
|
||
| if fake.gotConfigure == nil { | ||
| t.Fatal("Init did not call the backend's Configure RPC") | ||
| } | ||
| if fake.gotConfigure.BackendName != backend { | ||
| t.Fatalf("Configure BackendName = %q, want %q", fake.gotConfigure.BackendName, backend) | ||
| } | ||
| got, err := jsonBytesToMap(fake.gotConfigure.ConfigJson) | ||
| if err != nil { | ||
| t.Fatalf("Configure ConfigJson not valid JSON: %v", err) | ||
| } | ||
| if !reflect.DeepEqual(got, cfg) { | ||
| t.Fatalf("Configure ConfigJson = %+v, want %+v", got, cfg) | ||
| } | ||
| if _, ok := m.store.(*grpcIaCStateStore); !ok { | ||
| t.Fatalf("m.store is %T, want *grpcIaCStateStore", m.store) | ||
| } | ||
| } | ||
|
|
||
| // TestIaCModuleConfigureError asserts a Configure RPC failure aborts Init() with | ||
| // a wrapped error naming the module and the backend. | ||
| func TestIaCModuleConfigureError(t *testing.T) { | ||
| const backend = "azure_blob_configure_error_test" | ||
| sentinel := errors.New("plugin rejected config") | ||
| fake := &configureStateBackendClient{configureErr: sentinel} | ||
| if err := iacStateBackendRegistryInstance.register(backend, fake); err != nil { | ||
| t.Fatalf("register: %v", err) | ||
| } | ||
| defer func() { | ||
| iacStateBackendRegistryInstance.mu.Lock() | ||
| delete(iacStateBackendRegistryInstance.clients, backend) | ||
| iacStateBackendRegistryInstance.mu.Unlock() | ||
| }() | ||
|
|
||
| m := NewIaCModule("iac-plugin", map[string]any{"backend": backend}) | ||
| err := m.Init(NewMockApplication()) | ||
| if err == nil { | ||
| t.Fatal("Init must fail when the backend's Configure RPC errors") | ||
| } | ||
| if !errors.Is(err, sentinel) { | ||
| t.Fatalf("Init error must wrap the Configure error, got: %v", err) | ||
| } | ||
| if !strings.Contains(err.Error(), "iac-plugin") || !strings.Contains(err.Error(), backend) { | ||
| t.Fatalf("Init error must name the module and backend, got: %v", err) | ||
| } | ||
| if m.store != nil { | ||
| t.Fatalf("m.store must stay nil when Configure fails, got %T", m.store) | ||
| } | ||
| } | ||
|
|
||
| // TestIaCModuleConfigureUnimplemented asserts that when the backend plugin is an | ||
| // older build whose Configure RPC returns gRPC codes.Unimplemented, Init() fails | ||
| // with an actionable error telling the operator to upgrade the plugin. | ||
| func TestIaCModuleConfigureUnimplemented(t *testing.T) { | ||
| const backend = "azure_blob_configure_unimplemented_test" | ||
| unimpl := status.Error(codes.Unimplemented, "method Configure not implemented") | ||
| fake := &configureStateBackendClient{configureErr: unimpl} | ||
| if err := iacStateBackendRegistryInstance.register(backend, fake); err != nil { | ||
| t.Fatalf("register: %v", err) | ||
| } | ||
| defer func() { | ||
| iacStateBackendRegistryInstance.mu.Lock() | ||
| delete(iacStateBackendRegistryInstance.clients, backend) | ||
| iacStateBackendRegistryInstance.mu.Unlock() | ||
| }() | ||
|
|
||
| m := NewIaCModule("iac-plugin", map[string]any{"backend": backend}) | ||
| err := m.Init(NewMockApplication()) | ||
| if err == nil { | ||
| t.Fatal("Init must fail when the backend's Configure RPC is Unimplemented") | ||
| } | ||
| if !errors.Is(err, unimpl) { | ||
| t.Fatalf("Init error must wrap the Unimplemented error, got: %v", err) | ||
| } | ||
| if !strings.Contains(err.Error(), "iac-plugin") || !strings.Contains(err.Error(), backend) { | ||
| t.Fatalf("Init error must name the module and backend, got: %v", err) | ||
| } | ||
| if !strings.Contains(err.Error(), "upgrade") { | ||
| t.Fatalf("Unimplemented error must tell the operator to upgrade the plugin, got: %v", err) | ||
| } | ||
| if m.store != nil { | ||
| t.Fatalf("m.store must stay nil when Configure fails, got %T", m.store) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implemented in 6e17cb5 —
IaCModule.Init()now detectsstatus.Code(err) == codes.Unimplementedfrom the Configure call and returns an actionable "upgrade the backend plugin" error citing decisions/0036; other gRPC codes keep the generic path. NewTestIaCModuleConfigureUnimplementedcovers it.