2021-06-01 11:32:36 -07:00
|
|
|
// Copyright 2021 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package linode
|
|
|
|
|
|
|
|
import (
|
2024-04-05 01:31:59 -07:00
|
|
|
"encoding/json"
|
2021-06-01 11:32:36 -07:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2024-04-05 01:31:59 -07:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-06-01 11:32:36 -07:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2024-04-05 01:31:59 -07:00
|
|
|
const tokenID = "7b2c56dd51edd90952c1b94c472b94b176f20c5c777e376849edd8ad1c6c03bb"
|
|
|
|
|
2023-10-03 13:09:25 -07:00
|
|
|
// SDMock is the interface for the Linode mock.
|
2021-06-01 11:32:36 -07:00
|
|
|
type SDMock struct {
|
|
|
|
t *testing.T
|
|
|
|
Server *httptest.Server
|
|
|
|
Mux *http.ServeMux
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSDMock returns a new SDMock.
|
|
|
|
func NewSDMock(t *testing.T) *SDMock {
|
|
|
|
return &SDMock{
|
|
|
|
t: t,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-03 13:09:25 -07:00
|
|
|
// Endpoint returns the URI to the mock server.
|
2021-06-01 11:32:36 -07:00
|
|
|
func (m *SDMock) Endpoint() string {
|
|
|
|
return m.Server.URL + "/"
|
|
|
|
}
|
|
|
|
|
2023-10-03 13:09:25 -07:00
|
|
|
// Setup creates the mock server.
|
2021-06-01 11:32:36 -07:00
|
|
|
func (m *SDMock) Setup() {
|
|
|
|
m.Mux = http.NewServeMux()
|
|
|
|
m.Server = httptest.NewServer(m.Mux)
|
2024-04-05 01:31:59 -07:00
|
|
|
m.t.Cleanup(m.Server.Close)
|
|
|
|
m.SetupHandlers()
|
2021-06-01 11:32:36 -07:00
|
|
|
}
|
|
|
|
|
2024-04-05 01:31:59 -07:00
|
|
|
// SetupHandlers for endpoints of interest.
|
|
|
|
func (m *SDMock) SetupHandlers() {
|
|
|
|
for _, handler := range []string{"/v4/account/events", "/v4/linode/instances", "/v4/networking/ips", "/v4/networking/ipv6/ranges"} {
|
|
|
|
m.Mux.HandleFunc(handler, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Header.Get("Authorization") != fmt.Sprintf("Bearer %s", tokenID) {
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
xFilter := struct {
|
|
|
|
Region string `json:"region"`
|
|
|
|
}{}
|
|
|
|
json.Unmarshal([]byte(r.Header.Get("X-Filter")), &xFilter)
|
|
|
|
|
|
|
|
directory := "testdata/no_region_filter"
|
|
|
|
if xFilter.Region != "" { // Validate region filter matches test criteria.
|
|
|
|
directory = "testdata/" + xFilter.Region
|
|
|
|
}
|
|
|
|
if response, err := os.ReadFile(filepath.Join(directory, r.URL.Path+".json")); err == nil {
|
|
|
|
w.Header().Add("content-type", "application/json; charset=utf-8")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write(response)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
})
|
|
|
|
}
|
2021-08-04 03:05:49 -07:00
|
|
|
}
|