github.com/kubeshop/testkube@v1.17.23/contrib/executor/postman/pkg/runner/newman/newman_test.go (about)

     1  package newman
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"os"
     9  	"path/filepath"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/kubeshop/testkube/pkg/utils/test"
    14  
    15  	"github.com/kubeshop/testkube/pkg/envs"
    16  
    17  	"github.com/stretchr/testify/assert"
    18  
    19  	"github.com/kubeshop/testkube/pkg/api/v1/testkube"
    20  )
    21  
    22  // TestRun runs newman instance on top of example collection
    23  // creates temporary server and check if call to the server was done from newman
    24  func TestRun_Integration(t *testing.T) {
    25  	test.IntegrationTest(t)
    26  	t.Parallel()
    27  	// given
    28  	tempDir, err := os.MkdirTemp("", "*")
    29  	assert.NoErrorf(t, err, "failed to create temp dir: %v", err)
    30  	defer os.RemoveAll(tempDir)
    31  
    32  	runner, err := NewNewmanRunner(context.Background(), envs.Params{DataDir: tempDir})
    33  	assert.NoError(t, err)
    34  
    35  	// and test server for getting newman responses
    36  	requestCompleted := false
    37  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    38  		requestCompleted = true
    39  	}))
    40  
    41  	defer ts.Close()
    42  
    43  	parts := strings.Split(ts.URL, ":")
    44  	port := parts[2]
    45  
    46  	err = os.WriteFile(filepath.Join(tempDir, "test-content"), []byte(fmt.Sprintf(exampleCollection, port, port)), 0644)
    47  	if err != nil {
    48  		assert.FailNow(t, "Unable to write postman runner test content file")
    49  	}
    50  
    51  	execution := testkube.Execution{
    52  		Content: testkube.NewStringTestContent(""),
    53  		Command: []string{"newman"},
    54  		Args: []string{
    55  			"run",
    56  			"<runPath>",
    57  			"-e",
    58  			"<envFile>",
    59  			"--reporters",
    60  			"cli,json",
    61  			"--reporter-json-export",
    62  			"<reportFile>",
    63  		},
    64  	}
    65  
    66  	ctx := context.Background()
    67  
    68  	// when
    69  	result, err := runner.Run(ctx, execution)
    70  
    71  	// then
    72  	assert.NoError(t, err)
    73  	assert.Empty(t, result.ErrorMessage)
    74  	assert.Contains(t, result.Output, "Successful GET request")
    75  	assert.Equal(t, requestCompleted, true)
    76  }
    77  
    78  const exampleCollection = `
    79  {
    80  	"info": {
    81  		"_postman_id": "3d9a6be2-bd3e-4cf7-89ca-354103aab4a7",
    82  		"name": "testkube",
    83  		"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
    84  	},
    85  	"item": [
    86  		{
    87  			"name": "Test",
    88  			"event": [
    89  				{
    90  					"listen": "test",
    91  					"script": {
    92  						"exec": [
    93  							"    pm.test(\"Successful GET request\", function () {",
    94  							"        pm.expect(pm.response.code).to.be.oneOf([200, 201, 202]);",
    95  							"    });"
    96  						],
    97  						"type": "text/javascript"
    98  					}
    99  				}
   100  			],
   101  			"request": {
   102  				"method": "GET",
   103  				"header": [],
   104  				"url": {
   105  					"raw": "http://127.0.0.1:%s",
   106  					"protocol": "http",
   107  					"host": [
   108  						"127",
   109  						"0",
   110  						"0",
   111  						"1"
   112  					],
   113  					"port": "%s"
   114  	
   115  				},
   116  				"host": ["localhost"]
   117  			},
   118  			"response": []
   119  		}
   120  	]
   121  }
   122  `