github.com/hashicorp/packer@v1.14.3/datasource/http/data_acc_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package http
     5  
     6  import (
     7  	_ "embed"
     8  	"fmt"
     9  	"io"
    10  	"os"
    11  	"os/exec"
    12  	"regexp"
    13  	"testing"
    14  
    15  	"github.com/hashicorp/packer-plugin-sdk/acctest"
    16  )
    17  
    18  //go:embed test-fixtures/basic.pkr.hcl
    19  var testDatasourceBasic string
    20  
    21  //go:embed test-fixtures/empty_url.pkr.hcl
    22  var testDatasourceEmptyUrl string
    23  
    24  //go:embed test-fixtures/404_url.pkr.hcl
    25  var testDatasource404Url string
    26  
    27  //go:embed test-fixtures/invalid_method.pkr.hcl
    28  var testDatasourceInvalidMethod string
    29  
    30  func TestHttpDataSource(t *testing.T) {
    31  	tests := []struct {
    32  		Name            string
    33  		Path            string
    34  		Error           bool
    35  		ExpectedOutputs map[string]string
    36  	}{
    37  		{
    38  			Name:  "basic_test",
    39  			Path:  testDatasourceBasic,
    40  			Error: false,
    41  			ExpectedOutputs: map[string]string{
    42  				"url": "url is https://www.google.com",
    43  				// Check that body is not empty
    44  				"body": "body is true",
    45  			},
    46  		},
    47  		{
    48  			Name:  "url_is_empty",
    49  			Path:  testDatasourceEmptyUrl,
    50  			Error: true,
    51  			ExpectedOutputs: map[string]string{
    52  				"error": "the `url` must be specified",
    53  			},
    54  		},
    55  		{
    56  			Name:  "method_is_invalid",
    57  			Path:  testDatasourceInvalidMethod,
    58  			Error: true,
    59  			ExpectedOutputs: map[string]string{
    60  				"error": "the `method` must be one of \\[HEAD GET POST PUT DELETE OPTIONS PATCH\\]",
    61  			},
    62  		},
    63  		{
    64  			Name:  "404_url",
    65  			Path:  testDatasource404Url,
    66  			Error: true,
    67  		},
    68  	}
    69  	for _, tt := range tests {
    70  		t.Run(tt.Name, func(t *testing.T) {
    71  			testCase := &acctest.PluginTestCase{
    72  				Name: tt.Name,
    73  				Setup: func() error {
    74  					return nil
    75  				},
    76  				Teardown: func() error {
    77  					return nil
    78  				},
    79  				Template: tt.Path,
    80  				Type:     "http",
    81  				Check: func(buildCommand *exec.Cmd, logfile string) error {
    82  					if buildCommand.ProcessState != nil {
    83  						if buildCommand.ProcessState.ExitCode() != 0 && !tt.Error {
    84  							return fmt.Errorf("Bad exit code. Logfile: %s", logfile)
    85  						}
    86  						if tt.Error && buildCommand.ProcessState.ExitCode() == 0 {
    87  							return fmt.Errorf("Expected Bad exit code.")
    88  						}
    89  					}
    90  
    91  					if tt.ExpectedOutputs != nil {
    92  						logs, err := os.Open(logfile)
    93  						if err != nil {
    94  							return fmt.Errorf("Unable find %s", logfile)
    95  						}
    96  						defer logs.Close()
    97  
    98  						logsBytes, err := io.ReadAll(logs)
    99  						if err != nil {
   100  							return fmt.Errorf("Unable to read %s", logfile)
   101  						}
   102  						logsString := string(logsBytes)
   103  
   104  						for key, val := range tt.ExpectedOutputs {
   105  							if matched, _ := regexp.MatchString(val+".*", logsString); !matched {
   106  								t.Fatalf(
   107  									"logs doesn't contain expected log %v with value %v in %q",
   108  									key,
   109  									val,
   110  									logsString)
   111  							}
   112  						}
   113  
   114  					}
   115  
   116  					return nil
   117  				},
   118  			}
   119  			acctest.TestPlugin(t, testCase)
   120  		})
   121  	}
   122  
   123  }