|
| 1 | +// Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +// license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright |
| 4 | +// ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +// the Apache License, Version 2.0 (the "License"); you may |
| 6 | +// not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package runner |
| 19 | + |
| 20 | +import ( |
| 21 | + "errors" |
| 22 | + "net/http" |
| 23 | + "net/url" |
| 24 | + "reflect" |
| 25 | + "testing" |
| 26 | + |
| 27 | + "github.com/elastic/cloud-sdk-go/pkg/api" |
| 28 | + "github.com/elastic/cloud-sdk-go/pkg/api/mock" |
| 29 | + "github.com/elastic/cloud-sdk-go/pkg/models" |
| 30 | + multierror "github.com/hashicorp/go-multierror" |
| 31 | +) |
| 32 | + |
| 33 | +func TestResync(t *testing.T) { |
| 34 | + type args struct { |
| 35 | + params ResyncParams |
| 36 | + } |
| 37 | + tests := []struct { |
| 38 | + name string |
| 39 | + args args |
| 40 | + wantErr error |
| 41 | + }{ |
| 42 | + { |
| 43 | + name: "Fails due to parameter validation (Cluster ID)", |
| 44 | + args: args{}, |
| 45 | + wantErr: &multierror.Error{Errors: []error{ |
| 46 | + errors.New("id field cannot be empty"), |
| 47 | + errors.New("api reference is required for command"), |
| 48 | + }}, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "Fails due to parameter validation (API)", |
| 52 | + args: args{params: ResyncParams{ |
| 53 | + ID: "d324608c97154bdba2dff97511d40368", |
| 54 | + }}, |
| 55 | + wantErr: &multierror.Error{Errors: []error{ |
| 56 | + errors.New("api reference is required for command"), |
| 57 | + }}, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "Fails due to unknown API response", |
| 61 | + args: args{params: ResyncParams{ |
| 62 | + ID: "2c221bd86b7f48959a59ee3128d5c5e8", |
| 63 | + Params: Params{ |
| 64 | + API: api.NewMock(mock.Response{Response: http.Response{ |
| 65 | + StatusCode: http.StatusForbidden, |
| 66 | + Body: mock.NewStringBody(`{"error": "some forbidden error"}`), |
| 67 | + }}), |
| 68 | + }, |
| 69 | + }}, |
| 70 | + wantErr: errors.New(`{"error": "some forbidden error"}`), |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "Fails due to API error", |
| 74 | + args: args{params: ResyncParams{ |
| 75 | + ID: "2c221bd86b7f48959a59ee3128d5c5e8", |
| 76 | + Params: Params{ |
| 77 | + API: api.NewMock(mock.Response{ |
| 78 | + Error: errors.New("error with API"), |
| 79 | + }), |
| 80 | + }, |
| 81 | + }}, |
| 82 | + wantErr: &url.Error{ |
| 83 | + Op: "Post", |
| 84 | + URL: "https://mock-host/mock-path/platform/infrastructure/runners/2c221bd86b7f48959a59ee3128d5c5e8/_resync", |
| 85 | + Err: errors.New("error with API"), |
| 86 | + }, |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "Succeeds to resynchronize Kibana instance without errors", |
| 90 | + args: args{params: ResyncParams{ |
| 91 | + ID: "d324608c97154bdba2dff97511d40368", |
| 92 | + Params: Params{ |
| 93 | + API: api.NewMock(mock.Response{Response: http.Response{ |
| 94 | + StatusCode: http.StatusOK, |
| 95 | + Body: mock.NewStringBody(`{}`), |
| 96 | + }}), |
| 97 | + }, |
| 98 | + }}, |
| 99 | + }, |
| 100 | + } |
| 101 | + |
| 102 | + for _, tt := range tests { |
| 103 | + t.Run(tt.name, func(t *testing.T) { |
| 104 | + if err := Resync(tt.args.params); !reflect.DeepEqual(err, tt.wantErr) { |
| 105 | + t.Errorf("Resync() error = %v, wantErr %v", err, tt.wantErr) |
| 106 | + } |
| 107 | + }) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func TestResyncAll(t *testing.T) { |
| 112 | + type args struct { |
| 113 | + params Params |
| 114 | + } |
| 115 | + tests := []struct { |
| 116 | + name string |
| 117 | + args args |
| 118 | + wantErr error |
| 119 | + want *models.ModelVersionIndexSynchronizationResults |
| 120 | + }{ |
| 121 | + { |
| 122 | + name: "Fails due to parameter validation (API)", |
| 123 | + args: args{params: Params{}}, |
| 124 | + wantErr: errors.New("api reference is required for command"), |
| 125 | + }, |
| 126 | + { |
| 127 | + name: "Fails due to unknown API response", |
| 128 | + args: args{params: Params{ |
| 129 | + API: api.NewMock(mock.Response{Response: http.Response{ |
| 130 | + StatusCode: http.StatusForbidden, |
| 131 | + Body: mock.NewStringBody(`{"error": "some forbidden error"}`), |
| 132 | + }}), |
| 133 | + }}, |
| 134 | + wantErr: errors.New(`{"error": "some forbidden error"}`), |
| 135 | + }, |
| 136 | + { |
| 137 | + name: "Fails due to API error", |
| 138 | + args: args{params: Params{ |
| 139 | + API: api.NewMock(mock.Response{ |
| 140 | + Error: errors.New("error with API"), |
| 141 | + }), |
| 142 | + }}, |
| 143 | + wantErr: &url.Error{ |
| 144 | + Op: "Post", |
| 145 | + URL: "https://mock-host/mock-path/platform/infrastructure/runners/_resync?skip_matching_version=true", |
| 146 | + Err: errors.New("error with API"), |
| 147 | + }, |
| 148 | + }, |
| 149 | + { |
| 150 | + name: "Succeeds to re-synchronize all Kibana instances without errors", |
| 151 | + args: args{params: Params{ |
| 152 | + API: api.NewMock(mock.Response{Response: http.Response{ |
| 153 | + StatusCode: http.StatusAccepted, |
| 154 | + Body: mock.NewStringBody(`{}`), |
| 155 | + }}), |
| 156 | + }}, |
| 157 | + want: &models.ModelVersionIndexSynchronizationResults{}, |
| 158 | + }, |
| 159 | + } |
| 160 | + |
| 161 | + for _, tt := range tests { |
| 162 | + t.Run(tt.name, func(t *testing.T) { |
| 163 | + got, err := ResyncAll(tt.args.params) |
| 164 | + if !reflect.DeepEqual(tt.wantErr, err) { |
| 165 | + t.Errorf("ResyncAll() error = %v, wantErr %v", err, tt.wantErr) |
| 166 | + return |
| 167 | + } |
| 168 | + if !reflect.DeepEqual(got, tt.want) { |
| 169 | + t.Errorf("ResyncAll() = %v, want %v", got, tt.want) |
| 170 | + } |
| 171 | + }) |
| 172 | + } |
| 173 | +} |
0 commit comments