2
0

allocationmatcher_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package opencost
  2. import (
  3. "testing"
  4. "github.com/google/go-cmp/cmp"
  5. afilter "github.com/opencost/opencost/core/pkg/filter/allocation"
  6. "github.com/opencost/opencost/core/pkg/filter/ast"
  7. "github.com/opencost/opencost/core/pkg/filter/ops"
  8. )
  9. func TestAliasPass(t *testing.T) {
  10. labelConfig := &LabelConfig{
  11. DepartmentLabel: "keydepartment",
  12. EnvironmentLabel: "keyenvironment",
  13. OwnerLabel: "keyowner",
  14. ProductLabel: "keyproduct",
  15. TeamLabel: "keyteam",
  16. }
  17. cases := []struct {
  18. name string
  19. input ast.FilterNode
  20. expected ast.FilterNode
  21. }{
  22. {
  23. name: "department equal",
  24. input: &ast.EqualOp{
  25. Left: ast.Identifier{
  26. Field: ast.NewAliasField(afilter.AliasDepartment),
  27. },
  28. Right: "x",
  29. },
  30. expected: ops.Or(
  31. ops.And(
  32. ops.Contains(afilter.FieldLabel, "keydepartment"),
  33. ops.Eq(ops.WithKey(afilter.FieldLabel, "keydepartment"), "x"),
  34. ),
  35. ops.And(
  36. ops.Not(ops.Contains(afilter.FieldLabel, "keydepartment")),
  37. ops.And(
  38. ops.Contains(afilter.FieldAnnotation, "keydepartment"),
  39. ops.Eq(ops.WithKey(afilter.FieldAnnotation, "keydepartment"), "x"),
  40. ),
  41. ),
  42. ),
  43. },
  44. }
  45. for _, c := range cases {
  46. pass := NewAllocationAliasPass(*labelConfig)
  47. t.Run(c.name, func(t *testing.T) {
  48. result, err := pass.Exec(c.input)
  49. if err != nil {
  50. t.Fatalf("unexpected error: %s", err)
  51. }
  52. if diff := cmp.Diff(c.expected, result); len(diff) > 0 {
  53. t.Errorf("diff: %s", diff)
  54. }
  55. })
  56. }
  57. }