Consul is a distributed, highly available, and data center aware solution to connect and configure applications across dynamic, distributed infrastructure.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.5 KiB

// Copyright (c) HashiCorp, Inc.
[COMPLIANCE] License changes (#18443) * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at <Blog URL>, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
1 year ago
// SPDX-License-Identifier: BUSL-1.1
//go:build !consulent
package agent
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/hashicorp/consul/agent/structs"
"github.com/stretchr/testify/require"
)
func TestOperator_Usage(t *testing.T) {
if testing.Short() {
t.Skip("too slow for testing.Short")
}
t.Parallel()
a := NewTestAgent(t, "")
defer a.Shutdown()
req, err := http.NewRequest("GET", "/v1/operator/usage", nil)
require.NoError(t, err)
// Register a few services
require.NoError(t, upsertTestService(a.RPC, "", "dc1", "web", "test-node", "", func(svc *structs.NodeService) {
svc.ID = "web1"
}))
require.NoError(t, upsertTestService(a.RPC, "", "dc1", "web", "test-node", "", func(svc *structs.NodeService) {
svc.ID = "web2"
}))
require.NoError(t, upsertTestService(a.RPC, "", "dc1", "db", "test-node", ""))
require.NoError(t, upsertTestService(a.RPC, "", "dc1", "web-proxy", "test-node", "", func(svc *structs.NodeService) {
svc.Kind = structs.ServiceKindConnectProxy
svc.Proxy = structs.ConnectProxyConfig{
DestinationServiceName: "web",
DestinationServiceID: "web1",
}
}))
// Add connect-native service to check that we include it in the billable service instances
require.NoError(t, upsertTestService(a.RPC, "", "dc1", "connect-native-app", "test-node", "", func(svc *structs.NodeService) {
svc.Connect.Native = true
}))
raw, err := a.srv.OperatorUsage(httptest.NewRecorder(), req)
require.NoError(t, err)
expected := map[string]structs.ServiceUsage{
"dc1": {
Services: 5,
ServiceInstances: 6,
ConnectServiceInstances: map[string]int{
"api-gateway": 0,
"connect-native": 1,
"connect-proxy": 1,
"ingress-gateway": 0,
"mesh-gateway": 0,
"terminating-gateway": 0,
},
// 4 = 6 total service instances - 1 connect proxy - 1 consul service
BillableServiceInstances: 4,
Nodes: 2,
},
}
require.Equal(t, expected, raw.(structs.Usage).Usage)
}
func upsertTestService(rpc rpcFn, secret, datacenter, name, node, partition string, modifyFuncs ...func(*structs.NodeService)) error {
req := structs.RegisterRequest{
Datacenter: datacenter,
Node: node,
SkipNodeUpdate: true,
Service: &structs.NodeService{
ID: name,
Service: name,
Port: 8080,
},
WriteRequest: structs.WriteRequest{Token: secret},
}
for _, modify := range modifyFuncs {
modify(req.Service)
}
var out struct{}
return rpc(context.Background(), "Catalog.Register", &req, &out)
}