github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/command/jsonchecks/checks_test.go (about)

     1  package jsonchecks
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/google/go-cmp/cmp"
     8  	"github.com/hashicorp/terraform/internal/addrs"
     9  	"github.com/hashicorp/terraform/internal/checks"
    10  	"github.com/hashicorp/terraform/internal/states"
    11  )
    12  
    13  func TestMarshalCheckStates(t *testing.T) {
    14  	resourceAAddr := addrs.ConfigCheckable(addrs.Resource{
    15  		Mode: addrs.ManagedResourceMode,
    16  		Type: "test",
    17  		Name: "a",
    18  	}.InModule(addrs.RootModule))
    19  	resourceAInstAddr := addrs.Checkable(addrs.Resource{
    20  		Mode: addrs.ManagedResourceMode,
    21  		Type: "test",
    22  		Name: "a",
    23  	}.Instance(addrs.StringKey("foo")).Absolute(addrs.RootModuleInstance))
    24  	moduleChildAddr := addrs.RootModuleInstance.Child("child", addrs.IntKey(0))
    25  	resourceBAddr := addrs.ConfigCheckable(addrs.Resource{
    26  		Mode: addrs.ManagedResourceMode,
    27  		Type: "test",
    28  		Name: "b",
    29  	}.InModule(moduleChildAddr.Module()))
    30  	resourceBInstAddr := addrs.Checkable(addrs.Resource{
    31  		Mode: addrs.ManagedResourceMode,
    32  		Type: "test",
    33  		Name: "b",
    34  	}.Instance(addrs.NoKey).Absolute(moduleChildAddr))
    35  	outputAAddr := addrs.ConfigCheckable(addrs.OutputValue{Name: "a"}.InModule(addrs.RootModule))
    36  	outputAInstAddr := addrs.Checkable(addrs.OutputValue{Name: "a"}.Absolute(addrs.RootModuleInstance))
    37  	outputBAddr := addrs.ConfigCheckable(addrs.OutputValue{Name: "b"}.InModule(moduleChildAddr.Module()))
    38  	outputBInstAddr := addrs.Checkable(addrs.OutputValue{Name: "b"}.Absolute(moduleChildAddr))
    39  
    40  	tests := map[string]struct {
    41  		Input *states.CheckResults
    42  		Want  any
    43  	}{
    44  		"empty": {
    45  			&states.CheckResults{},
    46  			[]any{},
    47  		},
    48  		"failures": {
    49  			&states.CheckResults{
    50  				ConfigResults: addrs.MakeMap(
    51  					addrs.MakeMapElem(resourceAAddr, &states.CheckResultAggregate{
    52  						Status: checks.StatusFail,
    53  						ObjectResults: addrs.MakeMap(
    54  							addrs.MakeMapElem(resourceAInstAddr, &states.CheckResultObject{
    55  								Status: checks.StatusFail,
    56  								FailureMessages: []string{
    57  									"Not enough boops.",
    58  									"Too many beeps.",
    59  								},
    60  							}),
    61  						),
    62  					}),
    63  					addrs.MakeMapElem(resourceBAddr, &states.CheckResultAggregate{
    64  						Status: checks.StatusFail,
    65  						ObjectResults: addrs.MakeMap(
    66  							addrs.MakeMapElem(resourceBInstAddr, &states.CheckResultObject{
    67  								Status: checks.StatusFail,
    68  								FailureMessages: []string{
    69  									"Splines are too pointy.",
    70  								},
    71  							}),
    72  						),
    73  					}),
    74  					addrs.MakeMapElem(outputAAddr, &states.CheckResultAggregate{
    75  						Status: checks.StatusFail,
    76  						ObjectResults: addrs.MakeMap(
    77  							addrs.MakeMapElem(outputAInstAddr, &states.CheckResultObject{
    78  								Status: checks.StatusFail,
    79  							}),
    80  						),
    81  					}),
    82  					addrs.MakeMapElem(outputBAddr, &states.CheckResultAggregate{
    83  						Status: checks.StatusFail,
    84  						ObjectResults: addrs.MakeMap(
    85  							addrs.MakeMapElem(outputBInstAddr, &states.CheckResultObject{
    86  								Status: checks.StatusFail,
    87  								FailureMessages: []string{
    88  									"Not object-oriented enough.",
    89  								},
    90  							}),
    91  						),
    92  					}),
    93  				),
    94  			},
    95  			[]any{
    96  				map[string]any{
    97  					"//": "EXPERIMENTAL: see docs for details",
    98  					"address": map[string]any{
    99  						"kind":       "output_value",
   100  						"module":     "module.child",
   101  						"name":       "b",
   102  						"to_display": "module.child.output.b",
   103  					},
   104  					"instances": []any{
   105  						map[string]any{
   106  							"address": map[string]any{
   107  								"module":     "module.child[0]",
   108  								"to_display": "module.child[0].output.b",
   109  							},
   110  							"problems": []any{
   111  								map[string]any{
   112  									"message": "Not object-oriented enough.",
   113  								},
   114  							},
   115  							"status": "fail",
   116  						},
   117  					},
   118  					"status": "fail",
   119  				},
   120  				map[string]any{
   121  					"//": "EXPERIMENTAL: see docs for details",
   122  					"address": map[string]any{
   123  						"kind":       "resource",
   124  						"mode":       "managed",
   125  						"module":     "module.child",
   126  						"name":       "b",
   127  						"to_display": "module.child.test.b",
   128  						"type":       "test",
   129  					},
   130  					"instances": []any{
   131  						map[string]any{
   132  							"address": map[string]any{
   133  								"module":     "module.child[0]",
   134  								"to_display": "module.child[0].test.b",
   135  							},
   136  							"problems": []any{
   137  								map[string]any{
   138  									"message": "Splines are too pointy.",
   139  								},
   140  							},
   141  							"status": "fail",
   142  						},
   143  					},
   144  					"status": "fail",
   145  				},
   146  				map[string]any{
   147  					"//": "EXPERIMENTAL: see docs for details",
   148  					"address": map[string]any{
   149  						"kind":       "output_value",
   150  						"name":       "a",
   151  						"to_display": "output.a",
   152  					},
   153  					"instances": []any{
   154  						map[string]any{
   155  							"address": map[string]any{
   156  								"to_display": "output.a",
   157  							},
   158  							"status": "fail",
   159  						},
   160  					},
   161  					"status": "fail",
   162  				},
   163  				map[string]any{
   164  					"//": "EXPERIMENTAL: see docs for details",
   165  					"address": map[string]any{
   166  						"kind":       "resource",
   167  						"mode":       "managed",
   168  						"name":       "a",
   169  						"to_display": "test.a",
   170  						"type":       "test",
   171  					},
   172  					"instances": []any{
   173  						map[string]any{
   174  							"address": map[string]any{
   175  								"to_display":   `test.a["foo"]`,
   176  								"instance_key": "foo",
   177  							},
   178  							"problems": []any{
   179  								map[string]any{
   180  									"message": "Not enough boops.",
   181  								},
   182  								map[string]any{
   183  									"message": "Too many beeps.",
   184  								},
   185  							},
   186  							"status": "fail",
   187  						},
   188  					},
   189  					"status": "fail",
   190  				},
   191  			},
   192  		},
   193  	}
   194  
   195  	for name, test := range tests {
   196  		t.Run(name, func(t *testing.T) {
   197  			gotBytes := MarshalCheckStates(test.Input)
   198  
   199  			var got any
   200  			err := json.Unmarshal(gotBytes, &got)
   201  			if err != nil {
   202  				t.Fatal(err)
   203  			}
   204  
   205  			if diff := cmp.Diff(test.Want, got); diff != "" {
   206  				t.Errorf("wrong result\n%s", diff)
   207  			}
   208  		})
   209  	}
   210  }