github.com/muratcelep/terraform@v1.1.0-beta2-not-internal-4/not-internal/command/test_test.go (about)

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