github.com/kubeshop/testkube@v1.17.23/contrib/executor/k6/pkg/k6detector/k6_test.go (about)

     1  package k6detector
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	apiClient "github.com/kubeshop/testkube/pkg/api/v1/client"
     9  	"github.com/kubeshop/testkube/pkg/api/v1/testkube"
    10  )
    11  
    12  const validK6Script = `import http from 'k6/http';
    13  import { sleep, check } from 'k6';
    14  import { Counter } from 'k6/metrics';
    15  
    16  // A simple counter for http requests
    17  
    18  export const requests = new Counter('http_reqs');
    19  
    20  // you can specify stages of your test (ramp up/down patterns) through the options object
    21  // target is the number of VUs you are aiming for
    22  
    23  export const options = {
    24    stages: [
    25      { target: 20, duration: '1m' },
    26      { target: 15, duration: '1m' },
    27      { target: 0, duration: '1m' },
    28    ],
    29    thresholds: {
    30      requests: ['count < 100'],
    31    },
    32  };
    33  
    34  export default function () {
    35    // our HTTP request, note that we are saving the response to res, which can be accessed later
    36  
    37    const res = http.get('http://test.k6.io');
    38  
    39    sleep(1);
    40  
    41    const checkRes = check(res, {
    42      'status is 200': (r) => r.status === 200,
    43      'response body': (r) => r.body.indexOf('Feel free to browse') !== -1,
    44    });
    45  }`
    46  
    47  const invalidK6Script = `describe('The Home Page', () => {
    48  	it('Go to dashboard', () => {
    49  	  cy.visit('https://demo.testkube.io/apiEndpoint?apiEndpoint=https://demo.testkube.io/results/v1');
    50  	});
    51    
    52  	it('Test suites should be shown as default view', () => {
    53  	  cy.get('h1').should('have.text', 'Test Suites ');
    54  	});
    55    });`
    56  
    57  func TestK6Adapter(t *testing.T) {
    58  
    59  	t.Run("detect valid k6 script", func(t *testing.T) {
    60  		// given
    61  		a := Detector{}
    62  
    63  		// when
    64  		name, ok := a.Is(apiClient.UpsertTestOptions{
    65  			Content: testkube.NewStringTestContent(validK6Script),
    66  		})
    67  
    68  		// then
    69  		assert.True(t, ok, "K6Adapter should detect valid script")
    70  		assert.Equal(t, "k6/script", name)
    71  	})
    72  
    73  	t.Run("ignore invalid k6 script", func(t *testing.T) {
    74  		// given
    75  		a := Detector{}
    76  
    77  		// when
    78  		name, ok := a.Is(apiClient.UpsertTestOptions{
    79  			Content: testkube.NewStringTestContent(invalidK6Script),
    80  		})
    81  
    82  		// then
    83  		assert.False(t, ok, "K6Adapter should detect valid script")
    84  		assert.Empty(t, name)
    85  	})
    86  }