github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/api.indices.datastream_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  //
     3  // The OpenSearch Contributors require contributions made to
     4  // this file be licensed under the Apache-2.0 license or a
     5  // compatible open source license.
     6  //
     7  // Modifications Copyright OpenSearch Contributors. See
     8  // GitHub history for details.
     9  
    10  //go:build integration
    11  // +build integration
    12  
    13  package opensearchapi_test
    14  
    15  import (
    16  	"bytes"
    17  	"context"
    18  	"encoding/json"
    19  	"fmt"
    20  	"github.com/opensearch-project/opensearch-go/v2"
    21  	"github.com/opensearch-project/opensearch-go/v2/opensearchapi"
    22  	"github.com/stretchr/testify/require"
    23  	"io/ioutil"
    24  	"net/http"
    25  	"strings"
    26  	"testing"
    27  	"time"
    28  )
    29  
    30  type DataStreamRequest interface {
    31  	Do(context.Context, opensearchapi.Transport) (*opensearchapi.Response, error)
    32  }
    33  
    34  func TestIndicesDataStreams_Do(t *testing.T) {
    35  	name := fmt.Sprintf("demo-%s", time.Now().Format("2006-01-02-15-04-05"))
    36  
    37  	tests := []struct {
    38  		name     string
    39  		r        DataStreamRequest
    40  		want     *opensearchapi.Response
    41  		wantBody string
    42  		wantErr  bool
    43  	}{
    44  		{
    45  			name: "TestIndicesCreateDataStreamRequest_Do",
    46  			r: opensearchapi.IndicesCreateDataStreamRequest{
    47  				Name:       name,
    48  				Pretty:     true,
    49  				Human:      true,
    50  				ErrorTrace: true,
    51  				Header: map[string][]string{
    52  					"Content-Type": {"application/json"},
    53  				},
    54  			},
    55  			want: &opensearchapi.Response{
    56  				StatusCode: 200,
    57  				Header: http.Header{
    58  					"Content-Type": []string{"application/json; charset=UTF-8"},
    59  				},
    60  			},
    61  			wantBody: `{"acknowledged":true}`,
    62  			wantErr:  false,
    63  		},
    64  		{
    65  			name: "TestIndicesGetDataStreamRequest_Do",
    66  			r: opensearchapi.IndicesGetDataStreamRequest{
    67  				Name:       name,
    68  				Pretty:     true,
    69  				Human:      true,
    70  				ErrorTrace: true,
    71  				Header: map[string][]string{
    72  					"Content-Type": {"application/json"},
    73  				},
    74  			},
    75  			want: &opensearchapi.Response{
    76  				StatusCode: 200,
    77  				Header: http.Header{
    78  					"Content-Type": []string{"application/json; charset=UTF-8"},
    79  				},
    80  			},
    81  			wantErr: false,
    82  		},
    83  		{
    84  			name: "TestIndicesGetAllDataStreamsRequest_Do",
    85  			r: opensearchapi.IndicesGetDataStreamRequest{
    86  				Pretty:     true,
    87  				Human:      true,
    88  				ErrorTrace: true,
    89  				Header: map[string][]string{
    90  					"Content-Type": {"application/json"},
    91  				},
    92  			},
    93  			want: &opensearchapi.Response{
    94  				StatusCode: 200,
    95  				Header: http.Header{
    96  					"Content-Type": []string{"application/json; charset=UTF-8"},
    97  				},
    98  			},
    99  			wantErr: false,
   100  		},
   101  		{
   102  			name: "TestIndicesGetStatsDataStreamRequest_Do",
   103  			r: opensearchapi.IndicesGetDataStreamStatsRequest{
   104  				Name:       name,
   105  				Pretty:     true,
   106  				Human:      true,
   107  				ErrorTrace: true,
   108  				Header: map[string][]string{
   109  					"Content-Type": {"application/json"},
   110  				},
   111  			},
   112  			want: &opensearchapi.Response{
   113  				StatusCode: 200,
   114  				Header: http.Header{
   115  					"Content-Type": []string{"application/json; charset=UTF-8"},
   116  				},
   117  			},
   118  			wantBody: fmt.Sprintf(`{"_shards":{"total":2,"successful":1,"failed":0},"data_stream_count":1,"backing_indices":1,"total_store_size":"208b","total_store_size_bytes":208,"data_streams":[{"data_stream":"%s","backing_indices":1,"store_size":"208b","store_size_bytes":208,"maximum_timestamp":0}]}`, name),
   119  			wantErr:  false,
   120  		},
   121  		{
   122  			name: "TestIndicesDeleteDataStreamRequest_Do",
   123  			r: opensearchapi.IndicesDeleteDataStreamRequest{
   124  				Name:       name,
   125  				Pretty:     true,
   126  				Human:      true,
   127  				ErrorTrace: true,
   128  				Header: map[string][]string{
   129  					"Content-Type": {"application/json"},
   130  				},
   131  			},
   132  			want: &opensearchapi.Response{
   133  				StatusCode: 200,
   134  				Header: http.Header{
   135  					"Content-Type": []string{"application/json; charset=UTF-8"},
   136  				},
   137  			},
   138  			wantBody: `{"acknowledged":true}`,
   139  			wantErr:  false,
   140  		},
   141  	}
   142  
   143  	client, err := opensearch.NewDefaultClient()
   144  	require.NoError(t, err)
   145  
   146  	iPut := opensearchapi.IndicesPutIndexTemplateRequest{
   147  		Name:       fmt.Sprintf("demo-data-template"),
   148  		Pretty:     true,
   149  		Human:      true,
   150  		ErrorTrace: true,
   151  		Body:       strings.NewReader(fmt.Sprintf(`{"index_patterns": ["demo-*"], "data_stream": {}, "priority": 100} }`)),
   152  	}
   153  
   154  	iPutResponse, err := iPut.Do(context.Background(), client)
   155  	require.NoError(t, err)
   156  	require.Equalf(t, false, iPutResponse.IsError(),
   157  		"Error when creating index template: %s", iPutResponse.String())
   158  
   159  	for _, tt := range tests {
   160  		t.Run(tt.name, func(t *testing.T) {
   161  			got, err := tt.r.Do(context.Background(), client)
   162  			if (err != nil) != tt.wantErr {
   163  				t.Errorf("Do() error = %v, wantErr %v", err, tt.wantErr)
   164  				return
   165  			}
   166  
   167  			require.Equalf(t, got.IsError(), tt.wantErr, "Do() got = %v, want %v", got.IsError(), tt.wantErr)
   168  			require.Equalf(t, got.StatusCode, tt.want.StatusCode, "Do() got = %v, want %v", got.StatusCode, tt.want.StatusCode)
   169  
   170  			if tt.wantBody != "" {
   171  				require.Equalf(t, got.Header, tt.want.Header, "Do() got = %v, want %v", got.Header, tt.want.Header)
   172  
   173  				defer got.Body.Close()
   174  				body, err := ioutil.ReadAll(got.Body)
   175  				require.NoError(t, err)
   176  
   177  				buffer := new(bytes.Buffer)
   178  				if err := json.Compact(buffer, body); err != nil {
   179  					fmt.Println(err)
   180  				}
   181  
   182  				require.Equalf(t, buffer.String(), tt.wantBody, "Do() got = %v, want %v", got.String(), tt.wantBody)
   183  			}
   184  		})
   185  	}
   186  }