我有一個函數,它接受aws openidconnectprovider pulumi 資源的輸入並創建一個iam 角色,並附加一個包含來自該oidc 提供商的資訊的assumerolepolicy。
問題:我正在嘗試為此函數編寫測試並模擬 oidc 提供者以作為函數呼叫的輸入。我無法理解如何正確地模擬它,以便測試輸出顯示我所期望的內容,目前看來模擬的數據沒有像我預期的那樣出現。
看來我沒有正確使用模擬,但我在這裡放棄了範例
這裡有更多文件
package mypkg import ( "github.com/pulumi/pulumi-aws/sdk/v5/go/aws/iam" "github.com/pulumi/pulumi/sdk/v3/go/pulumi" ) func createmycustomrole(ctx *pulumi.context, name string, oidcprovider *iam.openidconnectprovider, opts ...pulumi.resourceoption) (*iam.role, error) { role := &iam.role{} componenturn := fmt.sprintf("%s-custom-role", name) err := ctx.registercomponentresource("pkg:aws:mycustomrole", componenturn, role, opts...) if err != nil { return nil, err } url := oidc.url.applyt(func(s string) string { return fmt.sprint(strings.replaceall(s, "https://", ""), ":sub") }).(pulumi.stringoutput) assumerolepolicy := pulumi.sprintf(`{ "version": "2012-10-17", "statement": [{ "effect": "allow", "principal": { "federated": "%s" }, "action": "sts:assumerolewithwebidentity", "condition": { "stringequals": { "%s": [ "system:serviceaccount:kube-system:*", "system:serviceaccount:kube-system:cluster-autoscaler" ] } } }] }`, oidcprovider.arn, url) roleurn := fmt.sprintf("%s-custom-role", name) role, err = iam.newrole(ctx, roleurn, &iam.roleargs{ name: pulumi.string(roleurn), description: pulumi.string("create custom role"), assumerolepolicy: assumerolepolicy, tags: pulumi.tostringmap(map[string]string{"project": "test"}), }) if err != nil { return nil, err } return role, nil }
package mypkg import ( "sync" "testing" "github.com/pulumi/pulumi-aws/sdk/v5/go/aws/iam" "github.com/pulumi/pulumi/sdk/v3/go/common/resource" "github.com/pulumi/pulumi/sdk/v3/go/pulumi" "github.com/stretchr/testify/assert" ) type mocks int func (mocks) newresource(args pulumi.mockresourceargs) (string, resource.propertymap, error) { return args.name + "_id", args.inputs, nil } func (mocks) call(args pulumi.mockcallargs) (resource.propertymap, error) { outputs := map[string]interface{}{} if args.token == "aws:iam/getopenidconnectprovider:getopenidconnectprovider" { outputs["arn"] = "arn:aws:iam::123:oidc-provider/oidc.eks.us-west-2.amazonaws.com/id/abc" outputs["id"] = "abc" outputs["url"] = "https://someurl" } return resource.newpropertymapfrommap(outputs), nil } func testcreatemycustomrole(t *testing.t) { err := pulumi.runerr(func(ctx *pulumi.context) error { // gets the mocked oidc provider to use as input for the createdefaultautoscalerrole oidc, err := iam.getopenidconnectprovider(ctx, "get-test-oidc-provider", pulumi.id("abc"), &iam.openidconnectproviderstate{}) assert.noerror(t, err) infra, err := createmycustomrole(ctx, "role1", oidc}) assert.noerror(t, err) var wg sync.waitgroup wg.add(1) // check 1: assume role policy is formatted correctly pulumi.all(infra.urn(), infra.assumerolepolicy).applyt(func(all []interface{}) error { urn := all[0].(pulumi.urn) assumerolepolicy := all[1].(string) assert.equal(t, `{ "version": "2012-10-17", "statement": [{ "effect": "allow", "principal": { "federated": "arn:aws:iam::123:oidc-provider/oidc.eks.us-west-2.amazonaws.com/id/abc" }, "action": "sts:assumerolewithwebidentity", "condition": { "stringequals": { "someurl:sub": [ "system:serviceaccount:kube-system:*", "system:serviceaccount:kube-system:cluster-autoscaler" ] } } }] }`, assumerolepolicy) wg.done() return nil }) wg.wait() return nil }, pulumi.withmocks("project", "stack", mocks(0))) assert.noerror(t, err) } output
diff: --- expected +++ actual @@ -4,3 +4,3 @@ "effect": "allow", - "principal": { "federated": "arn:aws:iam::123:oidc-provider/oidc.eks.us-west-2.amazonaws.com/id/abc" }, + "principal": { "federated": "" }, "action": "sts:assumerolewithwebidentity", @@ -8,3 +8,3 @@ "stringequals": { - "someurl:sub": [ + ":sub": [ "system:serviceaccount:kube-system:*", test: testcreatemycustomrole
原來我使用了 newresource
錯誤。
當在測試函數中呼叫 getopenidconnectprovider
時,它會讀取資源並建立一個新的資源輸出,從而觸發對mocks.newresource的呼叫
修復方法是使用模擬輸出在 newresource 函數呼叫中為 getopenidconnectprovider openidconnectprovider
傳回的資源類型定義一個 if 語句。
func (mocks) newresource(args pulumi.mockresourceargs) (string, resource.propertymap, error) { pulumi.printf(args.typetoken) outputs := args.inputs.mappable() if args.typetoken == "aws:iam/openidconnectprovider:openidconnectprovider" { outputs["arn"] = "arn:aws:iam::123:oidc-provider/oidc.eks.us-west-2.amazonaws.com/id/abc" outputs["id"] = "abc" outputs["url"] = "https://someurl" } return args.name + "_id", resource.newpropertymapfrommap(outputs), nil } func (mocks) call(args pulumi.mockcallargs) (resource.propertymap, error) { outputs := map[string]interface{}{} return resource.newpropertymapfrommap(outputs), nil }
下面的程式碼片段是我更改了 assert
以便它無法顯示與上面對 newresource 所做的更改相比現在的差異
Diff: --- Expected Actual @@ -8,3 +8,3 @@ "StringEquals": { - "b:sub": [ + "someurl:sub": [ "system:serviceaccount:kube-system:*",
以上是如何在 Go 單元測試中模擬 Pulumi 資源?的詳細內容。更多資訊請關注PHP中文網其他相關文章!