Skip to content

Commit

Permalink
more unit tests with options
Browse files Browse the repository at this point in the history
Signed-off-by: odubajDT <[email protected]>
  • Loading branch information
odubajDT committed Nov 13, 2024
1 parent fc20bd7 commit fd0e1c2
Showing 1 changed file with 178 additions and 12 deletions.
190 changes: 178 additions & 12 deletions pkg/pdatatest/pprofiletest/profiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,181 @@ func TestCompareProfiles(t *testing.T) {
compareOptions []CompareProfilesOption
withoutOptions error
withOptions error
}{}
}{
{
name: "empty",
expected: func() pprofile.Profiles {
p := pprofile.NewProfiles()
return p
}(),
actual: func() pprofile.Profiles {
p := pprofile.NewProfiles()
return p
}(),
},
{
name: "equal",
expected: func() pprofile.Profiles {
p := pprofile.NewProfiles()
rl := p.ResourceProfiles().AppendEmpty()
rl.Resource().Attributes().PutStr("key1", "value1")
l := rl.ScopeProfiles().AppendEmpty().Profiles().AppendEmpty()
l.Attributes().PutStr("scope-attr1", "value1")
l.SetProfileID(pprofile.NewProfileIDEmpty())
return p
}(),
actual: func() pprofile.Profiles {
p := pprofile.NewProfiles()
rl := p.ResourceProfiles().AppendEmpty()
rl.Resource().Attributes().PutStr("key1", "value1")
l := rl.ScopeProfiles().AppendEmpty().Profiles().AppendEmpty()
l.Attributes().PutStr("scope-attr1", "value1")
l.SetProfileID(pprofile.NewProfileIDEmpty())
return p
}(),
},
{
name: "resource order",
expected: func() pprofile.Profiles {
p := pprofile.NewProfiles()
rl := p.ResourceProfiles().AppendEmpty()
rl.Resource().Attributes().PutStr("key1", "value1")
l := rl.ScopeProfiles().AppendEmpty().Profiles().AppendEmpty()
l.Attributes().PutStr("scope-attr1", "value1")
l.SetProfileID(pprofile.NewProfileIDEmpty())
rl2 := p.ResourceProfiles().AppendEmpty()
rl2.Resource().Attributes().PutStr("key2", "value2")
l2 := rl2.ScopeProfiles().AppendEmpty().Profiles().AppendEmpty()
l2.Attributes().PutStr("scope-attr2", "value2")
l2.SetProfileID(pprofile.NewProfileIDEmpty())
return p
}(),
actual: func() pprofile.Profiles {
p := pprofile.NewProfiles()
rl2 := p.ResourceProfiles().AppendEmpty()
rl2.Resource().Attributes().PutStr("key2", "value2")
l2 := rl2.ScopeProfiles().AppendEmpty().Profiles().AppendEmpty()
l2.Attributes().PutStr("scope-attr2", "value2")
l2.SetProfileID(pprofile.NewProfileIDEmpty())
rl := p.ResourceProfiles().AppendEmpty()
rl.Resource().Attributes().PutStr("key1", "value1")
l := rl.ScopeProfiles().AppendEmpty().Profiles().AppendEmpty()
l.Attributes().PutStr("scope-attr1", "value1")
l.SetProfileID(pprofile.NewProfileIDEmpty())
return p
}(),
withoutOptions: multierr.Combine(
errors.New(`resources are out of order: resource "map[key1:value1]" expected at index 0, found at index 1`),
errors.New(`resources are out of order: resource "map[key2:value2]" expected at index 1, found at index 0`),
),
compareOptions: []CompareProfilesOption{
IgnoreResourceProfilesOrder()},
},
{
name: "resource masked attribute",
expected: func() pprofile.Profiles {
p := pprofile.NewProfiles()
rl := p.ResourceProfiles().AppendEmpty()
rl.Resource().Attributes().PutStr("key1", "value1")
return p
}(),
actual: func() pprofile.Profiles {
p := pprofile.NewProfiles()
rl := p.ResourceProfiles().AppendEmpty()
rl.Resource().Attributes().PutStr("key1", "value2")
return p
}(),
withoutOptions: multierr.Combine(
errors.New(`missing expected resource: map[key1:value1]`),
errors.New(`unexpected resource: map[key1:value2]`),
),
compareOptions: []CompareProfilesOption{
IgnoreResourceAttributeValue("key1")},
},
{
name: "resource scope order",
expected: func() pprofile.Profiles {
p := pprofile.NewProfiles()
rl := p.ResourceProfiles().AppendEmpty()
rl.Resource().Attributes().PutStr("key1", "value1")
l := rl.ScopeProfiles().AppendEmpty().Scope()
l.SetName("scope1")
l.Attributes().PutStr("scope-attr1", "value1")
l2 := rl.ScopeProfiles().AppendEmpty().Scope()
l2.Attributes().PutStr("scope-attr2", "value2")
l2.SetName("scope2")
return p
}(),
actual: func() pprofile.Profiles {
p := pprofile.NewProfiles()
rl := p.ResourceProfiles().AppendEmpty()
rl.Resource().Attributes().PutStr("key1", "value1")
l2 := rl.ScopeProfiles().AppendEmpty().Scope()
l2.Attributes().PutStr("scope-attr2", "value2")
l2.SetName("scope2")
l := rl.ScopeProfiles().AppendEmpty().Scope()
l.Attributes().PutStr("scope-attr1", "value1")
l.SetName("scope1")
return p
}(),
withoutOptions: errors.New(`resource "map[key1:value1]": scopes are out of order: scope scope1 expected at index 0, found at index 1; resource "map[key1:value1]": scopes are out of order: scope scope2 expected at index 1, found at index 0`),
compareOptions: []CompareProfilesOption{
IgnoreScopeProfilesOrder()},
},
{
name: "mask scope attribute",
expected: func() pprofile.Profiles {
p := pprofile.NewProfiles()
rl := p.ResourceProfiles().AppendEmpty()
rl.Resource().Attributes().PutStr("key1", "value1")
l := rl.ScopeProfiles().AppendEmpty().Scope()
l.SetName("scope1")
l.Attributes().PutStr("scope-attr1", "value1")
l2 := rl.ScopeProfiles().AppendEmpty().Scope()
l2.Attributes().PutStr("scope-attr2", "value2")
l2.SetName("scope2")
return p
}(),
actual: func() pprofile.Profiles {
p := pprofile.NewProfiles()
rl := p.ResourceProfiles().AppendEmpty()
rl.Resource().Attributes().PutStr("key1", "value1")
l := rl.ScopeProfiles().AppendEmpty().Scope()
l.Attributes().PutStr("scope-attr1", "value12")
l.SetName("scope1")
l2 := rl.ScopeProfiles().AppendEmpty().Scope()
l2.Attributes().PutStr("scope-attr2", "value22")
l2.SetName("scope2")
return p
}(),
withoutOptions: errors.New(`resource "map[key1:value1]": scope "scope1": attributes don't match expected: map[scope-attr1:value1], actual: map[scope-attr1:value12]; resource "map[key1:value1]": scope "scope2": attributes don't match expected: map[scope-attr2:value2], actual: map[scope-attr2:value22]`),
compareOptions: []CompareProfilesOption{
IgnoreScopeAttributeValue("scope-attr2"),
IgnoreScopeAttributeValue("scope-attr1"),
},
},

// TODO
// ignore scope attribute value
// ignore container order

// ignore profilecontainer attribute value
// ignore profilecontainer attribute order
// ignore profilecontainer timestamp value

// ignore profile attribute value
// ignore profile attribute order
// ignore profile timestamp value

}

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
err := CompareProfiles(tc.expected, tc.actual)
if tc.withoutOptions == nil {
assert.NoError(t, err)
} else {
require.EqualError(t, tc.withoutOptions, err.Error())
require.EqualError(t, err, tc.withoutOptions.Error())
}

if tc.compareOptions == nil {
Expand Down Expand Up @@ -61,15 +227,15 @@ func TestCompareResourceProfiles(t *testing.T) {
rl := pprofile.NewResourceProfiles()
rl.Resource().Attributes().PutStr("key1", "value1")
l := rl.ScopeProfiles().AppendEmpty().Profiles().AppendEmpty()
l.Attributes().PutStr("profile-attr1", "value1")
l.Attributes().PutStr("scope-attr1", "value1")
l.SetProfileID(pprofile.NewProfileIDEmpty())
return rl
}(),
actual: func() pprofile.ResourceProfiles {
rl := pprofile.NewResourceProfiles()
rl.Resource().Attributes().PutStr("key1", "value1")
l := rl.ScopeProfiles().AppendEmpty().Profiles().AppendEmpty()
l.Attributes().PutStr("profile-attr1", "value1")
l.Attributes().PutStr("scope-attr1", "value1")
l.SetProfileID(pprofile.NewProfileIDEmpty())
return rl
}(),
Expand Down Expand Up @@ -139,15 +305,15 @@ func TestCompareScopeProfiles(t *testing.T) {
sl := pprofile.NewScopeProfiles()
sl.Scope().SetName("scope-name")
l := sl.Profiles().AppendEmpty()
l.Attributes().PutStr("profile-attr1", "value1")
l.Attributes().PutStr("scope-attr1", "value1")
l.SetProfileID(pprofile.NewProfileIDEmpty())
return sl
}(),
actual: func() pprofile.ScopeProfiles {
sl := pprofile.NewScopeProfiles()
sl.Scope().SetName("scope-name")
l := sl.Profiles().AppendEmpty()
l.Attributes().PutStr("profile-attr1", "value1")
l.Attributes().PutStr("scope-attr1", "value1")
l.SetProfileID(pprofile.NewProfileIDEmpty())
return sl
}(),
Expand Down Expand Up @@ -234,26 +400,26 @@ func TestCompareScopeProfiles(t *testing.T) {
expected: func() pprofile.ScopeProfiles {
sl := pprofile.NewScopeProfiles()
l := sl.Profiles().AppendEmpty()
l.Attributes().PutStr("profile-attr1", "value1")
l.Attributes().PutStr("scope-attr1", "value1")
l.SetProfileID(pprofile.ProfileID([]byte("profileid1111111")))
l = sl.Profiles().AppendEmpty()
l.Attributes().PutStr("profile-attr2", "value2")
l.Attributes().PutStr("scope-attr2", "value2")
l.SetProfileID(pprofile.ProfileID([]byte("profileid1111112")))
return sl
}(),
actual: func() pprofile.ScopeProfiles {
sl := pprofile.NewScopeProfiles()
l := sl.Profiles().AppendEmpty()
l.Attributes().PutStr("profile-attr2", "value2")
l.Attributes().PutStr("scope-attr2", "value2")
l.SetProfileID(pprofile.ProfileID([]byte("profileid1111111")))
l = sl.Profiles().AppendEmpty()
l.Attributes().PutStr("profile-attr1", "value1")
l.Attributes().PutStr("scope-attr1", "value1")
l.SetProfileID(pprofile.ProfileID([]byte("profileid1111113")))
return sl
}(),
err: multierr.Combine(
errors.New(`profile containers are out of order: profile container "map[profile-attr1:value1]" expected at index 0, found at index 1`),
errors.New(`profile containers are out of order: profile container "map[profile-attr2:value2]" expected at index 1, found at index 0`),
errors.New(`profile containers are out of order: profile container "map[scope-attr1:value1]" expected at index 0, found at index 1`),
errors.New(`profile containers are out of order: profile container "map[scope-attr2:value2]" expected at index 1, found at index 0`),
),
},
}
Expand Down

0 comments on commit fd0e1c2

Please sign in to comment.