github.com/iaas-resource-provision/iaas-rpc@v1.0.7-0.20211021023331-ed21f798c408/internal/moduletest/provider_test.go (about) 1 package moduletest 2 3 import ( 4 "testing" 5 6 "github.com/google/go-cmp/cmp" 7 "github.com/iaas-resource-provision/iaas-rpc/internal/providers" 8 "github.com/zclconf/go-cty-debug/ctydebug" 9 "github.com/zclconf/go-cty/cty" 10 ) 11 12 func TestProvider(t *testing.T) { 13 14 assertionConfig := cty.ObjectVal(map[string]cty.Value{ 15 "component": cty.StringVal("spline_reticulator"), 16 "equal": cty.MapVal(map[string]cty.Value{ 17 "match": cty.ObjectVal(map[string]cty.Value{ 18 "description": cty.StringVal("this should match"), 19 "got": cty.StringVal("a"), 20 "want": cty.StringVal("a"), 21 }), 22 "unmatch": cty.ObjectVal(map[string]cty.Value{ 23 "description": cty.StringVal("this should not match"), 24 "got": cty.StringVal("a"), 25 "want": cty.StringVal("b"), 26 }), 27 }), 28 "check": cty.MapVal(map[string]cty.Value{ 29 "pass": cty.ObjectVal(map[string]cty.Value{ 30 "description": cty.StringVal("this should pass"), 31 "condition": cty.True, 32 }), 33 "fail": cty.ObjectVal(map[string]cty.Value{ 34 "description": cty.StringVal("this should fail"), 35 "condition": cty.False, 36 }), 37 }), 38 }) 39 40 // The provider code expects to receive an object that was decoded from 41 // HCL using the schema, so to make sure we're testing a more realistic 42 // situation here we'll require the config to conform to the schema. If 43 // this fails, it's a bug in the configuration definition above rather 44 // than in the provider itself. 45 for _, err := range assertionConfig.Type().TestConformance(testAssertionsSchema.Block.ImpliedType()) { 46 t.Error(err) 47 } 48 49 p := NewProvider() 50 51 configureResp := p.ConfigureProvider(providers.ConfigureProviderRequest{ 52 Config: cty.EmptyObjectVal, 53 }) 54 if got, want := len(configureResp.Diagnostics), 1; got != want { 55 t.Fatalf("got %d Configure diagnostics, but want %d", got, want) 56 } 57 if got, want := configureResp.Diagnostics[0].Description().Summary, "The test provider is experimental"; got != want { 58 t.Fatalf("wrong diagnostic message\ngot: %s\nwant: %s", got, want) 59 } 60 61 validateResp := p.ValidateResourceConfig(providers.ValidateResourceConfigRequest{ 62 TypeName: "test_assertions", 63 Config: assertionConfig, 64 }) 65 if got, want := len(validateResp.Diagnostics), 0; got != want { 66 t.Fatalf("got %d ValidateResourceTypeConfig diagnostics, but want %d", got, want) 67 } 68 69 planResp := p.PlanResourceChange(providers.PlanResourceChangeRequest{ 70 TypeName: "test_assertions", 71 Config: assertionConfig, 72 PriorState: cty.NullVal(assertionConfig.Type()), 73 ProposedNewState: assertionConfig, 74 }) 75 if got, want := len(planResp.Diagnostics), 0; got != want { 76 t.Fatalf("got %d PlanResourceChange diagnostics, but want %d", got, want) 77 } 78 planned := planResp.PlannedState 79 if got, want := planned, assertionConfig; !want.RawEquals(got) { 80 t.Fatalf("wrong planned new value\n%s", ctydebug.DiffValues(want, got)) 81 } 82 83 gotComponents := p.TestResults() 84 wantComponents := map[string]*Component{ 85 "spline_reticulator": { 86 Assertions: map[string]*Assertion{ 87 "pass": { 88 Outcome: Pending, 89 Description: "this should pass", 90 }, 91 "fail": { 92 Outcome: Pending, 93 Description: "this should fail", 94 }, 95 "match": { 96 Outcome: Pending, 97 Description: "this should match", 98 }, 99 "unmatch": { 100 Outcome: Pending, 101 Description: "this should not match", 102 }, 103 }, 104 }, 105 } 106 if diff := cmp.Diff(wantComponents, gotComponents); diff != "" { 107 t.Fatalf("wrong test results after planning\n%s", diff) 108 } 109 110 applyResp := p.ApplyResourceChange(providers.ApplyResourceChangeRequest{ 111 TypeName: "test_assertions", 112 Config: assertionConfig, 113 PriorState: cty.NullVal(assertionConfig.Type()), 114 PlannedState: planned, 115 }) 116 if got, want := len(applyResp.Diagnostics), 0; got != want { 117 t.Fatalf("got %d ApplyResourceChange diagnostics, but want %d", got, want) 118 } 119 final := applyResp.NewState 120 if got, want := final, assertionConfig; !want.RawEquals(got) { 121 t.Fatalf("wrong new value\n%s", ctydebug.DiffValues(want, got)) 122 } 123 124 gotComponents = p.TestResults() 125 wantComponents = map[string]*Component{ 126 "spline_reticulator": { 127 Assertions: map[string]*Assertion{ 128 "pass": { 129 Outcome: Passed, 130 Description: "this should pass", 131 Message: "condition passed", 132 }, 133 "fail": { 134 Outcome: Failed, 135 Description: "this should fail", 136 Message: "condition failed", 137 }, 138 "match": { 139 Outcome: Passed, 140 Description: "this should match", 141 Message: "correct value\n got: \"a\"\n", 142 }, 143 "unmatch": { 144 Outcome: Failed, 145 Description: "this should not match", 146 Message: "wrong value\n got: \"a\"\n want: \"b\"\n", 147 }, 148 }, 149 }, 150 } 151 if diff := cmp.Diff(wantComponents, gotComponents); diff != "" { 152 t.Fatalf("wrong test results after applying\n%s", diff) 153 } 154 155 }