github.com/pulumi/terraform@v1.4.0/pkg/command/test_test.go (about) 1 package command 2 3 import ( 4 "bytes" 5 "io/ioutil" 6 "strings" 7 "testing" 8 9 "github.com/google/go-cmp/cmp" 10 "github.com/pulumi/terraform/pkg/command/views" 11 "github.com/pulumi/terraform/pkg/terminal" 12 ) 13 14 // These are the main tests for the "terraform test" command. 15 func TestTest(t *testing.T) { 16 t.Run("passes", func(t *testing.T) { 17 td := t.TempDir() 18 testCopyDir(t, testFixturePath("test-passes"), td) 19 defer testChdir(t, td)() 20 21 streams, close := terminal.StreamsForTesting(t) 22 cmd := &TestCommand{ 23 Meta: Meta{ 24 Streams: streams, 25 View: views.NewView(streams), 26 }, 27 } 28 exitStatus := cmd.Run([]string{"-junit-xml=junit.xml", "-no-color"}) 29 outp := close(t) 30 if got, want := exitStatus, 0; got != want { 31 t.Fatalf("wrong exit status %d; want %d\nstderr:\n%s", got, want, outp.Stderr()) 32 } 33 34 gotStdout := strings.TrimSpace(outp.Stdout()) 35 wantStdout := strings.TrimSpace(` 36 Warning: The "terraform test" command is experimental 37 38 We'd like to invite adventurous module authors to write integration tests for 39 their modules using this command, but all of the behaviors of this command 40 are currently experimental and may change based on feedback. 41 42 For more information on the testing experiment, including ongoing research 43 goals and avenues for feedback, see: 44 https://www.terraform.io/docs/language/modules/testing-experiment.html 45 `) 46 if diff := cmp.Diff(wantStdout, gotStdout); diff != "" { 47 t.Errorf("wrong stdout\n%s", diff) 48 } 49 50 gotStderr := strings.TrimSpace(outp.Stderr()) 51 wantStderr := strings.TrimSpace(` 52 Success! All of the test assertions passed. 53 `) 54 if diff := cmp.Diff(wantStderr, gotStderr); diff != "" { 55 t.Errorf("wrong stderr\n%s", diff) 56 } 57 58 gotXMLSrc, err := ioutil.ReadFile("junit.xml") 59 if err != nil { 60 t.Fatal(err) 61 } 62 gotXML := string(bytes.TrimSpace(gotXMLSrc)) 63 wantXML := strings.TrimSpace(` 64 <testsuites> 65 <errors>0</errors> 66 <failures>0</failures> 67 <tests>1</tests> 68 <testsuite> 69 <name>hello</name> 70 <tests>1</tests> 71 <skipped>0</skipped> 72 <errors>0</errors> 73 <failures>0</failures> 74 <testcase> 75 <name>output</name> 76 <classname>foo</classname> 77 </testcase> 78 </testsuite> 79 </testsuites> 80 `) 81 if diff := cmp.Diff(wantXML, gotXML); diff != "" { 82 t.Errorf("wrong JUnit XML\n%s", diff) 83 } 84 }) 85 t.Run("fails", func(t *testing.T) { 86 td := t.TempDir() 87 testCopyDir(t, testFixturePath("test-fails"), td) 88 defer testChdir(t, td)() 89 90 streams, close := terminal.StreamsForTesting(t) 91 cmd := &TestCommand{ 92 Meta: Meta{ 93 Streams: streams, 94 View: views.NewView(streams), 95 }, 96 } 97 exitStatus := cmd.Run([]string{"-junit-xml=junit.xml", "-no-color"}) 98 outp := close(t) 99 if got, want := exitStatus, 1; got != want { 100 t.Fatalf("wrong exit status %d; want %d\nstderr:\n%s", got, want, outp.Stderr()) 101 } 102 103 gotStdout := strings.TrimSpace(outp.Stdout()) 104 wantStdout := strings.TrimSpace(` 105 Warning: The "terraform test" command is experimental 106 107 We'd like to invite adventurous module authors to write integration tests for 108 their modules using this command, but all of the behaviors of this command 109 are currently experimental and may change based on feedback. 110 111 For more information on the testing experiment, including ongoing research 112 goals and avenues for feedback, see: 113 https://www.terraform.io/docs/language/modules/testing-experiment.html 114 `) 115 if diff := cmp.Diff(wantStdout, gotStdout); diff != "" { 116 t.Errorf("wrong stdout\n%s", diff) 117 } 118 119 gotStderr := strings.TrimSpace(outp.Stderr()) 120 wantStderr := strings.TrimSpace(` 121 ─── Failed: hello.foo.output (output "foo" value) ─────────────────────────── 122 wrong value 123 got: "foo value boop" 124 want: "foo not boop" 125 126 ───────────────────────────────────────────────────────────────────────────── 127 `) 128 if diff := cmp.Diff(wantStderr, gotStderr); diff != "" { 129 t.Errorf("wrong stderr\n%s", diff) 130 } 131 132 gotXMLSrc, err := ioutil.ReadFile("junit.xml") 133 if err != nil { 134 t.Fatal(err) 135 } 136 gotXML := string(bytes.TrimSpace(gotXMLSrc)) 137 wantXML := strings.TrimSpace(` 138 <testsuites> 139 <errors>0</errors> 140 <failures>1</failures> 141 <tests>1</tests> 142 <testsuite> 143 <name>hello</name> 144 <tests>1</tests> 145 <skipped>0</skipped> 146 <errors>0</errors> 147 <failures>1</failures> 148 <testcase> 149 <name>output</name> 150 <classname>foo</classname> 151 <failure> 152 <message>wrong value
 got: "foo value boop"
 want: "foo not boop"
</message> 153 </failure> 154 </testcase> 155 </testsuite> 156 </testsuites> 157 `) 158 if diff := cmp.Diff(wantXML, gotXML); diff != "" { 159 t.Errorf("wrong JUnit XML\n%s", diff) 160 } 161 }) 162 163 }