Skip to content

Commit

Permalink
Revert "chore: Refactor unit-tests to use golden files (#1670)"
Browse files Browse the repository at this point in the history
This reverts commit d26f95d.
  • Loading branch information
TeodorSAP committed Jan 14, 2025
1 parent d1ff330 commit 3f95d1b
Show file tree
Hide file tree
Showing 4 changed files with 318 additions and 321 deletions.
117 changes: 42 additions & 75 deletions internal/resources/otelcollector/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,65 +19,41 @@ import (
)

func TestAgent_ApplyResources(t *testing.T) {
image := "opentelemetry/collector:dummy"
namespace := "kyma-system"
priorityClassName := "normal"

tests := []struct {
name string
sut *AgentApplierDeleter
goldenFilePath string
saveGoldenFile bool
}{
{
name: "metric agent",
sut: NewMetricAgentApplierDeleter(image, namespace, priorityClassName),
goldenFilePath: "testdata/metric-agent.yaml",
},
}
var objects []client.Object

for _, tt := range tests {
var objects []client.Object
scheme := runtime.NewScheme()
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(istiosecurityclientv1.AddToScheme(scheme))

scheme := runtime.NewScheme()
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(istiosecurityclientv1.AddToScheme(scheme))

fakeClient := fake.NewClientBuilder().WithInterceptorFuncs(interceptor.Funcs{
Create: func(_ context.Context, c client.WithWatch, obj client.Object, _ ...client.CreateOption) error {
objects = append(objects, obj)
// Nothing has to be created, just add created object to the list
return nil
},
}).Build()
client := fake.NewClientBuilder().WithInterceptorFuncs(interceptor.Funcs{
Create: func(_ context.Context, c client.WithWatch, obj client.Object, _ ...client.CreateOption) error {
objects = append(objects, obj)
// Nothing has to be created, just add created object to the list
return nil
},
}).Build()

t.Run(tt.name, func(t *testing.T) {
err := tt.sut.ApplyResources(context.Background(), fakeClient, AgentApplyOptions{
AllowedPorts: []int32{5555, 6666},
CollectorConfigYAML: "dummy",
})
require.NoError(t, err)
image := "opentelemetry/collector:dummy"
namespace := "kyma-system"
priorityClassName := "normal"
sut := NewMetricAgentApplierDeleter(image, namespace, priorityClassName)

if tt.saveGoldenFile {
testutils.SaveAsYAML(t, scheme, objects, tt.goldenFilePath)
}
err := sut.ApplyResources(context.Background(), client, AgentApplyOptions{
AllowedPorts: []int32{5555, 6666},
CollectorConfigYAML: "dummy",
})
require.NoError(t, err)

bytes, err := testutils.MarshalYAML(scheme, objects)
require.NoError(t, err)
bytes, err := testutils.MarshalYAML(scheme, objects)
require.NoError(t, err)

goldenFileBytes, err := os.ReadFile(tt.goldenFilePath)
require.NoError(t, err)
goldenFileBytes, err := os.ReadFile("testdata/metric-agent.yaml")
require.NoError(t, err)

require.Equal(t, string(goldenFileBytes), string(bytes))
})
}
require.Equal(t, string(goldenFileBytes), string(bytes))
}

func TestAgent_DeleteResources(t *testing.T) {
image := "opentelemetry/collector:dummy"
namespace := "kyma-system"
priorityClassName := "normal"

var created []client.Object

fakeClient := fake.NewClientBuilder().WithInterceptorFuncs(interceptor.Funcs{
Expand All @@ -87,32 +63,23 @@ func TestAgent_DeleteResources(t *testing.T) {
},
}).Build()

tests := []struct {
name string
sut *AgentApplierDeleter
}{
{
name: "metric agent",
sut: NewMetricAgentApplierDeleter(image, namespace, priorityClassName),
},
}
image := "opentelemetry/collector:dummy"
namespace := "kyma-system"
priorityClassName := "normal"
sut := NewMetricAgentApplierDeleter(image, namespace, priorityClassName)

err := sut.ApplyResources(context.Background(), fakeClient, AgentApplyOptions{
AllowedPorts: []int32{5555, 6666},
CollectorConfigYAML: "dummy",
})
require.NoError(t, err)

err = sut.DeleteResources(context.Background(), fakeClient)
require.NoError(t, err)

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.sut.ApplyResources(context.Background(), fakeClient, AgentApplyOptions{
AllowedPorts: []int32{5555, 6666},
CollectorConfigYAML: "dummy",
})
require.NoError(t, err)

err = tt.sut.DeleteResources(context.Background(), fakeClient)
require.NoError(t, err)

for i := range created {
// an update operation on a non-existent object should return a NotFound error
err = fakeClient.Get(context.Background(), client.ObjectKeyFromObject(created[i]), created[i])
require.True(t, apierrors.IsNotFound(err), "want not found, got %v: %#v", err, created[i])
}
})
for i := range created {
// an update operation on a non-existent object should return a NotFound error
err = fakeClient.Get(context.Background(), client.ObjectKeyFromObject(created[i]), created[i])
require.True(t, apierrors.IsNotFound(err), "want not found, got %v: %#v", err, created[i])
}
}
Loading

0 comments on commit 3f95d1b

Please sign in to comment.