How to use CreateTestRun method of cloudapi Package

Best K6 code snippet using cloudapi.CreateTestRun

api_test.go

Source:api_test.go Github

copy

Full Screen

...39 n, err := fmt.Fprintf(w, format, a...)40 require.NoError(t, err)41 return n42}43func TestCreateTestRun(t *testing.T) {44 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {45 fprintf(t, w, `{"reference_id": "1", "config": {"aggregationPeriod": "2s"}}`)46 }))47 defer server.Close()48 client := NewClient(testutils.NewLogger(t), "token", server.URL, "1.0")49 tr := &TestRun{50 Name: "test",51 }52 resp, err := client.CreateTestRun(tr)53 assert.Nil(t, err)54 assert.Equal(t, resp.ReferenceID, "1")55 assert.NotNil(t, resp.ConfigOverride)56 assert.True(t, resp.ConfigOverride.AggregationPeriod.Valid)57 assert.Equal(t, types.Duration(2*time.Second), resp.ConfigOverride.AggregationPeriod.Duration)58 assert.False(t, resp.ConfigOverride.AggregationMinSamples.Valid)59}60func TestFinished(t *testing.T) {61 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {62 fprintf(t, w, "")63 }))64 defer server.Close()65 client := NewClient(testutils.NewLogger(t), "token", server.URL, "1.0")66 thresholds := map[string]map[string]bool{67 "threshold": {68 "max < 10": true,69 },70 }71 err := client.TestFinished("1", thresholds, true, 0)72 assert.Nil(t, err)73}74func TestAuthorizedError(t *testing.T) {75 called := 076 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {77 called++78 w.WriteHeader(http.StatusForbidden)79 fprintf(t, w, `{"error": {"code": 5, "message": "Not allowed"}}`)80 }))81 defer server.Close()82 client := NewClient(testutils.NewLogger(t), "token", server.URL, "1.0")83 resp, err := client.CreateTestRun(&TestRun{Name: "test"})84 assert.Equal(t, 1, called)85 assert.Nil(t, resp)86 assert.EqualError(t, err, "(403/E5) Not allowed")87}88func TestDetailsError(t *testing.T) {89 called := 090 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {91 called++92 w.WriteHeader(http.StatusForbidden)93 fprintf(t, w, `{"error": {"code": 0, "message": "Validation failed", "details": { "name": ["Shorter than minimum length 2."]}}}`)94 }))95 defer server.Close()96 client := NewClient(testutils.NewLogger(t), "token", server.URL, "1.0")97 resp, err := client.CreateTestRun(&TestRun{Name: "test"})98 assert.Equal(t, 1, called)99 assert.Nil(t, resp)100 assert.EqualError(t, err, "(403) Validation failed\n name: Shorter than minimum length 2.")101}102func TestClientRetry(t *testing.T) {103 t.Parallel()104 called := 0105 idempotencyKey := ""106 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {107 gotK6IdempotencyKey := r.Header.Get(k6IdempotencyKeyHeader)108 if idempotencyKey == "" {109 idempotencyKey = gotK6IdempotencyKey110 }111 assert.NotEmpty(t, gotK6IdempotencyKey)112 assert.Equal(t, idempotencyKey, gotK6IdempotencyKey)113 called++114 w.WriteHeader(500)115 }))116 defer server.Close()117 client := NewClient(testutils.NewLogger(t), "token", server.URL, "1.0")118 client.retryInterval = 1 * time.Millisecond119 resp, err := client.CreateTestRun(&TestRun{Name: "test"})120 assert.Equal(t, 3, called)121 assert.Nil(t, resp)122 assert.NotNil(t, err)123}124func TestClientRetrySuccessOnSecond(t *testing.T) {125 t.Parallel()126 called := 1127 idempotencyKey := ""128 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {129 gotK6IdempotencyKey := r.Header.Get(k6IdempotencyKeyHeader)130 if idempotencyKey == "" {131 idempotencyKey = gotK6IdempotencyKey132 }133 assert.NotEmpty(t, gotK6IdempotencyKey)134 assert.Equal(t, idempotencyKey, gotK6IdempotencyKey)135 called++136 if called == 2 {137 fprintf(t, w, `{"reference_id": "1"}`)138 return139 }140 w.WriteHeader(500)141 }))142 defer server.Close()143 client := NewClient(testutils.NewLogger(t), "token", server.URL, "1.0")144 client.retryInterval = 1 * time.Millisecond145 resp, err := client.CreateTestRun(&TestRun{Name: "test"})146 assert.Equal(t, 2, called)147 assert.NotNil(t, resp)148 assert.Nil(t, err)149}150func TestIdempotencyKey(t *testing.T) {151 const idempotencyKey = "xxx"152 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {153 gotK6IdempotencyKey := r.Header.Get(k6IdempotencyKeyHeader)154 switch r.Method {155 case http.MethodPost:156 assert.NotEmpty(t, gotK6IdempotencyKey)157 assert.Equal(t, idempotencyKey, gotK6IdempotencyKey)158 default:159 assert.Empty(t, gotK6IdempotencyKey)...

Full Screen

Full Screen

cloud.go

Source:cloud.go Github

copy

Full Screen

...31 Duration int64 `json:"duration"`32 ProcessThresholds bool `json:"process_thresholds"`33 Instances int32 `json:"instances"`34}35func CreateTestRun(opts InspectOutput, instances int32, host, token string, log logr.Logger) (string, error) {36 if len(opts.External.Loadimpact.Name) < 1 {37 opts.External.Loadimpact.Name = "k6-operator-test"38 }39 cloudConfig := cloudapi.NewConfig()40 if opts.External.Loadimpact.ProjectID > 0 {41 cloudConfig.ProjectID = null.NewInt(opts.External.Loadimpact.ProjectID, true)42 }43 logger := &logrus.Logger{44 Out: os.Stdout,45 Formatter: new(logrus.TextFormatter),46 Hooks: make(logrus.LevelHooks),47 Level: logrus.InfoLevel,48 }49 if opts.Thresholds == nil {50 opts.Thresholds = make(map[string][]string)51 }52 if len(host) == 0 {53 host = cloudConfig.Host.String54 }55 client = cloudapi.NewClient(logger, token, host, consts.Version, time.Duration(time.Minute))56 resp, err := createTestRun(client, host, &TestRun{57 Name: opts.External.Loadimpact.Name,58 ProjectID: cloudConfig.ProjectID.Int64,59 VUsMax: int64(opts.MaxVUs),60 Thresholds: opts.Thresholds,61 Duration: int64(opts.TotalDuration.TimeDuration().Seconds()),62 ProcessThresholds: true,63 Instances: instances,64 })65 if err != nil {66 return "", err67 }68 return resp.ReferenceID, nil69}70// We cannot use cloudapi.TestRun struct and cloudapi.Client.CreateTestRun call because they're not aware of71// process_thresholds argument; so let's use custom struct and function instead72func createTestRun(client *cloudapi.Client, host string, testRun *TestRun) (*cloudapi.CreateTestRunResponse, error) {73 url := host + "/v1/tests"74 req, err := client.NewRequest("POST", url, testRun)75 if err != nil {76 return nil, err77 }78 ctrr := cloudapi.CreateTestRunResponse{}79 err = client.Do(req, &ctrr)80 if err != nil {81 return nil, err82 }83 if ctrr.ReferenceID == "" {84 return nil, fmt.Errorf("failed to get a reference ID")85 }86 return &ctrr, nil87}88func FinishTestRun(refID string) error {89 return client.TestFinished(refID, cloudapi.ThresholdResult(90 map[string]map[string]bool{},91 ), false, lib.RunStatusFinished)92}...

Full Screen

Full Screen

CreateTestRun

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 cloud := cloudapi.New("username", "password")4 testRun, err := cloud.CreateTestRun("test", "test", "test")5 if err != nil {6 fmt.Println(err)7 }8 fmt.Println(testRun)9}10import (11func main() {12 cloud := cloudapi.New("username", "password")13 testRun, err := cloud.CreateTestRun("test", "test", "test")14 if err != nil {15 fmt.Println(err)16 }17 fmt.Println(testRun)18}19import (20func main() {21 cloud := cloudapi.New("username", "password")22 testRun, err := cloud.CreateTestRun("test", "test", "test")23 if err != nil {24 fmt.Println(err)25 }26 fmt.Println(testRun)27}28import (29func main() {30 cloud := cloudapi.New("username", "password")31 testRun, err := cloud.CreateTestRun("test", "test", "test")32 if err != nil {33 fmt.Println(err)34 }35 fmt.Println(testRun)36}37import (38func main() {39 cloud := cloudapi.New("username", "password")40 testRun, err := cloud.CreateTestRun("test", "test", "test")41 if err != nil {42 fmt.Println(err)43 }44 fmt.Println(testRun

Full Screen

Full Screen

CreateTestRun

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 runtime.GOMAXPROCS(runtime.NumCPU())4 cloudAPI := NewCloudAPI()5 err := cloudAPI.CreateTestRun("my test run", "my test run description")6 if err != nil {7 log.Fatal(err)8 }9 testRunID := cloudAPI.GetTestRunID()10 fmt.Println("Test run ID: ", testRunID)11 testRunURL := cloudAPI.GetTestRunURL()12 fmt.Println("Test run URL: ", testRunURL)13 testRunStatus := cloudAPI.GetTestRunStatus()14 fmt.Println("Test run status: ", testRunStatus)15 testRunStartTime := cloudAPI.GetTestRunStartTime()16 fmt.Println("Test run start time: ", testRunStartTime)17 testRunEndTime := cloudAPI.GetTestRunEndTime()18 fmt.Println("Test run end time: ", testRunEndTime)19 testRunDuration := cloudAPI.GetTestRunDuration()20 fmt.Println("Test run duration: ", testRunDuration)21 testRunTestCaseCount := cloudAPI.GetTestRunTestCaseCount()22 fmt.Println("Test run test case count: ", testRunTestCaseCount)23 testRunTestCaseExecutedCount := cloudAPI.GetTestRunTestCaseExecutedCount()24 fmt.Println("Test run test case executed count: ", testRunTestCaseExecutedCount)25 testRunTestCasePassedCount := cloudAPI.GetTestRunTestCasePassedCount()

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful