"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "internal/command/views/json_view_test.go" between
terraform-1.0.2.tar.gz and terraform-1.0.3.tar.gz

About: Terraform is a tool for building, changing, and versioning a datacenter infrastructure (written in Go).

json_view_test.go  (terraform-1.0.2):json_view_test.go  (terraform-1.0.3)
skipping to change at line 144 skipping to change at line 144
"resource_key": "boop", "resource_key": "boop",
"resource_name": "bar", "resource_name": "bar",
"resource_type": "test_instance", "resource_type": "test_instance",
}, },
}, },
}, },
} }
testJSONViewOutputEquals(t, done(t).Stdout(), want) testJSONViewOutputEquals(t, done(t).Stdout(), want)
} }
func TestJSONView_ResourceDrift(t *testing.T) {
streams, done := terminal.StreamsForTesting(t)
jv := NewJSONView(NewView(streams))
foo, diags := addrs.ParseModuleInstanceStr("module.foo")
if len(diags) > 0 {
t.Fatal(diags.Err())
}
managed := addrs.Resource{Mode: addrs.ManagedResourceMode, Type: "test_in
stance", Name: "bar"}
cs := &plans.ResourceInstanceChangeSrc{
Addr: managed.Instance(addrs.StringKey("boop")).Absolute(foo),
ChangeSrc: plans.ChangeSrc{
Action: plans.Update,
},
}
jv.ResourceDrift(viewsjson.NewResourceInstanceChange(cs))
want := []map[string]interface{}{
{
"@level": "info",
"@message": `module.foo.test_instance.bar["boop"]: Drift
detected (update)`,
"@module": "terraform.ui",
"type": "resource_drift",
"change": map[string]interface{}{
"action": "update",
"resource": map[string]interface{}{
"addr": `module.foo.test_inst
ance.bar["boop"]`,
"implied_provider": "test",
"module": "module.foo",
"resource": `test_instance.bar["b
oop"]`,
"resource_key": "boop",
"resource_name": "bar",
"resource_type": "test_instance",
},
},
},
}
testJSONViewOutputEquals(t, done(t).Stdout(), want)
}
func TestJSONView_ChangeSummary(t *testing.T) { func TestJSONView_ChangeSummary(t *testing.T) {
streams, done := terminal.StreamsForTesting(t) streams, done := terminal.StreamsForTesting(t)
jv := NewJSONView(NewView(streams)) jv := NewJSONView(NewView(streams))
jv.ChangeSummary(&viewsjson.ChangeSummary{ jv.ChangeSummary(&viewsjson.ChangeSummary{
Add: 1, Add: 1,
Change: 2, Change: 2,
Remove: 3, Remove: 3,
Operation: viewsjson.OperationApplied, Operation: viewsjson.OperationApplied,
}) })
skipping to change at line 266 skipping to change at line 306
func testJSONViewOutputEqualsFull(t *testing.T, output string, want []map[string ]interface{}) { func testJSONViewOutputEqualsFull(t *testing.T, output string, want []map[string ]interface{}) {
t.Helper() t.Helper()
// Remove final trailing newline // Remove final trailing newline
output = strings.TrimSuffix(output, "\n") output = strings.TrimSuffix(output, "\n")
// Split log into lines, each of which should be a JSON log message // Split log into lines, each of which should be a JSON log message
gotLines := strings.Split(output, "\n") gotLines := strings.Split(output, "\n")
if len(gotLines) != len(want) { if len(gotLines) != len(want) {
t.Fatalf("unexpected number of messages. got %d, want %d", len(go tLines), len(want)) t.Errorf("unexpected number of messages. got %d, want %d", len(go tLines), len(want))
} }
// Unmarshal each line and compare to the expected value // Unmarshal each line and compare to the expected value
for i := range gotLines { for i := range gotLines {
var gotStruct map[string]interface{} var gotStruct map[string]interface{}
if i >= len(want) {
t.Error("reached end of want messages too soon")
break
}
wantStruct := want[i] wantStruct := want[i]
if err := json.Unmarshal([]byte(gotLines[i]), &gotStruct); err != nil { if err := json.Unmarshal([]byte(gotLines[i]), &gotStruct); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if timestamp, ok := gotStruct["@timestamp"]; !ok { if timestamp, ok := gotStruct["@timestamp"]; !ok {
t.Errorf("message has no timestamp: %#v", gotStruct) t.Errorf("message has no timestamp: %#v", gotStruct)
} else { } else {
// Remove the timestamp value from the struct to allow co mparison // Remove the timestamp value from the struct to allow co mparison
delete(gotStruct, "@timestamp") delete(gotStruct, "@timestamp")
// Verify the timestamp format // Verify the timestamp format
if _, err := time.Parse("2006-01-02T15:04:05.000000Z07:00 ", timestamp.(string)); err != nil { if _, err := time.Parse("2006-01-02T15:04:05.000000Z07:00 ", timestamp.(string)); err != nil {
t.Fatalf("error parsing timestamp: %s", err) t.Errorf("error parsing timestamp on line %d: %s" , i, err)
} }
} }
if !cmp.Equal(wantStruct, gotStruct) { if !cmp.Equal(wantStruct, gotStruct) {
t.Fatalf("unexpected output on line %d:\n%s", i, cmp.Diff (wantStruct, gotStruct)) t.Errorf("unexpected output on line %d:\n%s", i, cmp.Diff (wantStruct, gotStruct))
} }
} }
} }
// testJSONViewOutputEquals skips the first line of output, since it ought to // testJSONViewOutputEquals skips the first line of output, since it ought to
// be a version message that we don't care about for most of our tests. // be a version message that we don't care about for most of our tests.
func testJSONViewOutputEquals(t *testing.T, output string, want []map[string]int erface{}) { func testJSONViewOutputEquals(t *testing.T, output string, want []map[string]int erface{}) {
t.Helper() t.Helper()
// Remove up to the first newline // Remove up to the first newline
 End of changes. 5 change blocks. 
3 lines changed or deleted 51 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)