github.com/kevinklinger/open_terraform@v1.3.6/noninternal/command/views/json/output_test.go (about)

     1  package json
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/google/go-cmp/cmp"
     8  	"github.com/kevinklinger/open_terraform/noninternal/addrs"
     9  	"github.com/kevinklinger/open_terraform/noninternal/lang/marks"
    10  	"github.com/kevinklinger/open_terraform/noninternal/plans"
    11  	"github.com/kevinklinger/open_terraform/noninternal/states"
    12  	"github.com/zclconf/go-cty/cty"
    13  )
    14  
    15  func TestOutputsFromMap(t *testing.T) {
    16  	got, diags := OutputsFromMap(map[string]*states.OutputValue{
    17  		// Normal non-sensitive output
    18  		"boop": {
    19  			Value: cty.NumberIntVal(1234),
    20  		},
    21  		// Sensitive string output
    22  		"beep": {
    23  			Value:     cty.StringVal("horse-battery").Mark(marks.Sensitive),
    24  			Sensitive: true,
    25  		},
    26  		// Sensitive object output which is marked at the leaf
    27  		"blorp": {
    28  			Value: cty.ObjectVal(map[string]cty.Value{
    29  				"a": cty.ObjectVal(map[string]cty.Value{
    30  					"b": cty.ObjectVal(map[string]cty.Value{
    31  						"c": cty.StringVal("oh, hi").Mark(marks.Sensitive),
    32  					}),
    33  				}),
    34  			}),
    35  			Sensitive: true,
    36  		},
    37  		// Null value
    38  		"honk": {
    39  			Value: cty.NullVal(cty.Map(cty.Bool)),
    40  		},
    41  	})
    42  	if len(diags) > 0 {
    43  		t.Fatal(diags.Err())
    44  	}
    45  
    46  	want := Outputs{
    47  		"boop": {
    48  			Sensitive: false,
    49  			Type:      json.RawMessage(`"number"`),
    50  			Value:     json.RawMessage(`1234`),
    51  		},
    52  		"beep": {
    53  			Sensitive: true,
    54  			Type:      json.RawMessage(`"string"`),
    55  			Value:     json.RawMessage(`"horse-battery"`),
    56  		},
    57  		"blorp": {
    58  			Sensitive: true,
    59  			Type:      json.RawMessage(`["object",{"a":["object",{"b":["object",{"c":"string"}]}]}]`),
    60  			Value:     json.RawMessage(`{"a":{"b":{"c":"oh, hi"}}}`),
    61  		},
    62  		"honk": {
    63  			Sensitive: false,
    64  			Type:      json.RawMessage(`["map","bool"]`),
    65  			Value:     json.RawMessage(`null`),
    66  		},
    67  	}
    68  
    69  	if !cmp.Equal(want, got) {
    70  		t.Fatalf("unexpected result\n%s", cmp.Diff(want, got))
    71  	}
    72  }
    73  
    74  func TestOutputsFromChanges(t *testing.T) {
    75  	root := addrs.RootModuleInstance
    76  	num, err := plans.NewDynamicValue(cty.NumberIntVal(1234), cty.Number)
    77  	if err != nil {
    78  		t.Fatalf("unexpected error creating dynamic value: %v", err)
    79  	}
    80  	str, err := plans.NewDynamicValue(cty.StringVal("1234"), cty.String)
    81  	if err != nil {
    82  		t.Fatalf("unexpected error creating dynamic value: %v", err)
    83  	}
    84  
    85  	got := OutputsFromChanges([]*plans.OutputChangeSrc{
    86  		// Unchanged output "boop", value 1234
    87  		{
    88  			Addr: root.OutputValue("boop"),
    89  			ChangeSrc: plans.ChangeSrc{
    90  				Action: plans.NoOp,
    91  				Before: num,
    92  				After:  num,
    93  			},
    94  			Sensitive: false,
    95  		},
    96  		// New output "beep", value 1234
    97  		{
    98  			Addr: root.OutputValue("beep"),
    99  			ChangeSrc: plans.ChangeSrc{
   100  				Action: plans.Create,
   101  				Before: nil,
   102  				After:  num,
   103  			},
   104  			Sensitive: false,
   105  		},
   106  		// Deleted output "blorp", prior value 1234
   107  		{
   108  			Addr: root.OutputValue("blorp"),
   109  			ChangeSrc: plans.ChangeSrc{
   110  				Action: plans.Delete,
   111  				Before: num,
   112  				After:  nil,
   113  			},
   114  			Sensitive: false,
   115  		},
   116  		// Updated output "honk", prior value 1234, new value "1234"
   117  		{
   118  			Addr: root.OutputValue("honk"),
   119  			ChangeSrc: plans.ChangeSrc{
   120  				Action: plans.Update,
   121  				Before: num,
   122  				After:  str,
   123  			},
   124  			Sensitive: false,
   125  		},
   126  		// New sensitive output "secret", value "1234"
   127  		{
   128  			Addr: root.OutputValue("secret"),
   129  			ChangeSrc: plans.ChangeSrc{
   130  				Action: plans.Create,
   131  				Before: nil,
   132  				After:  str,
   133  			},
   134  			Sensitive: true,
   135  		},
   136  	})
   137  
   138  	want := Outputs{
   139  		"boop": {
   140  			Action:    "noop",
   141  			Sensitive: false,
   142  		},
   143  		"beep": {
   144  			Action:    "create",
   145  			Sensitive: false,
   146  		},
   147  		"blorp": {
   148  			Action:    "delete",
   149  			Sensitive: false,
   150  		},
   151  		"honk": {
   152  			Action:    "update",
   153  			Sensitive: false,
   154  		},
   155  		"secret": {
   156  			Action:    "create",
   157  			Sensitive: true,
   158  		},
   159  	}
   160  
   161  	if !cmp.Equal(want, got) {
   162  		t.Fatalf("unexpected result\n%s", cmp.Diff(want, got))
   163  	}
   164  }
   165  
   166  func TestOutputs_String(t *testing.T) {
   167  	outputs := Outputs{
   168  		"boop": {
   169  			Sensitive: false,
   170  			Type:      json.RawMessage(`"number"`),
   171  			Value:     json.RawMessage(`1234`),
   172  		},
   173  		"beep": {
   174  			Sensitive: true,
   175  			Type:      json.RawMessage(`"string"`),
   176  			Value:     json.RawMessage(`"horse-battery"`),
   177  		},
   178  	}
   179  	if got, want := outputs.String(), "Outputs: 2"; got != want {
   180  		t.Fatalf("unexpected value\n got: %q\nwant: %q", got, want)
   181  	}
   182  }