| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- package kubemodel
- import (
- "testing"
- "time"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- "github.com/opencost/opencost/core/pkg/model/kubemodel"
- "github.com/opencost/opencost/core/pkg/source"
- )
- func TestComputeDeployments(t *testing.T) {
- start := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)
- end := start.Add(time.Hour)
- tests := []struct {
- name string
- overrides map[string]any
- want map[string]*kubemodel.Deployment
- }{
- {
- name: "no data returns empty deployment map",
- overrides: map[string]any{},
- want: map[string]*kubemodel.Deployment{},
- },
- {
- name: "basic deployment info and uptime",
- overrides: map[string]any{
- source.QueryDeploymentInfo: []*source.DeploymentInfoResult{
- {UID: "dep-1", Deployment: "my-app", NamespaceUID: "ns-1"},
- },
- source.QueryDeploymentUptime: []*source.UptimeResult{
- {UID: "dep-1", First: start, Last: end},
- },
- },
- want: map[string]*kubemodel.Deployment{
- "dep-1": {
- UID: "dep-1",
- Name: "my-app",
- NamespaceUID: "ns-1",
- Start: start,
- End: end,
- },
- },
- },
- {
- name: "deployment without uptime is not registered",
- overrides: map[string]any{
- source.QueryDeploymentInfo: []*source.DeploymentInfoResult{
- {UID: "dep-1", Deployment: "my-app", NamespaceUID: "ns-1"},
- },
- },
- want: map[string]*kubemodel.Deployment{},
- },
- {
- name: "deployment without namespace uid is not registered",
- overrides: map[string]any{
- source.QueryDeploymentInfo: []*source.DeploymentInfoResult{
- {UID: "dep-1", Deployment: "my-app"},
- },
- source.QueryDeploymentUptime: []*source.UptimeResult{
- {UID: "dep-1", First: start, Last: end},
- },
- },
- want: map[string]*kubemodel.Deployment{},
- },
- {
- name: "deployment labels and annotations are attached",
- overrides: map[string]any{
- source.QueryDeploymentInfo: []*source.DeploymentInfoResult{
- {UID: "dep-1", Deployment: "my-app", NamespaceUID: "ns-1"},
- },
- source.QueryDeploymentUptime: []*source.UptimeResult{
- {UID: "dep-1", First: start, Last: end},
- },
- source.QueryDeploymentLabels: []*source.LabelsResult{
- {UID: "dep-1", Labels: map[string]string{"app": "web"}},
- },
- source.QueryDeploymentAnnotations: []*source.AnnotationsResult{
- {UID: "dep-1", Annotations: map[string]string{"team": "platform"}},
- },
- },
- want: map[string]*kubemodel.Deployment{
- "dep-1": {
- UID: "dep-1",
- Name: "my-app",
- NamespaceUID: "ns-1",
- Start: start,
- End: end,
- Labels: map[string]string{"app": "web"},
- Annotations: map[string]string{"team": "platform"},
- },
- },
- },
- {
- name: "deployment match labels are attached",
- overrides: map[string]any{
- source.QueryDeploymentInfo: []*source.DeploymentInfoResult{
- {UID: "dep-1", Deployment: "my-app", NamespaceUID: "ns-1"},
- },
- source.QueryDeploymentUptime: []*source.UptimeResult{
- {UID: "dep-1", First: start, Last: end},
- },
- source.QueryDeploymentMatchLabels: []*source.DeploymentLabelsResult{
- {UID: "dep-1", Labels: map[string]string{"app": "web", "tier": "frontend"}},
- },
- },
- want: map[string]*kubemodel.Deployment{
- "dep-1": {
- UID: "dep-1",
- Name: "my-app",
- NamespaceUID: "ns-1",
- Start: start,
- End: end,
- MatchLabels: map[string]string{"app": "web", "tier": "frontend"},
- },
- },
- },
- {
- name: "uptime for unknown deployment is ignored",
- overrides: map[string]any{
- source.QueryDeploymentInfo: []*source.DeploymentInfoResult{
- {UID: "dep-1", Deployment: "my-app", NamespaceUID: "ns-1"},
- },
- source.QueryDeploymentUptime: []*source.UptimeResult{
- {UID: "dep-1", First: start, Last: end},
- {UID: "unknown-dep", First: start, Last: end},
- },
- },
- want: map[string]*kubemodel.Deployment{
- "dep-1": {
- UID: "dep-1",
- Name: "my-app",
- NamespaceUID: "ns-1",
- Start: start,
- End: end,
- },
- },
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- ds := source.NewMockOpenCostDataSource()
- ds.ResolutionValue = 5 * time.Minute
- seedCluster(ds, start, end)
- for method, result := range tt.overrides {
- ds.Querier.SetOverride(method, result)
- }
- km, err := NewKubeModel(testClusterUID, false, ds)
- require.NoError(t, err)
- kms, err := km.ComputeKubeModelSet(start, end)
- require.NoError(t, err)
- assert.Equal(t, tt.want, kms.Deployments)
- })
- }
- }
|