1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
package agent
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/module/modagent"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/pkg/agentcfg"
"go.uber.org/mock/gomock"
)
var (
testSupportedCRD = requiredFluxCRDs[0]
testSupportedCRDResourceName = testSupportedCRD.GroupResource().String()
testSupportedCRDVersion = testSupportedCRD.Version
)
var (
_ modagent.Module = &module{}
)
func TestModule_DefaultAndValidateConfiguration_WithoutFluxConfig(t *testing.T) {
// GIVEN
m := &module{}
cfg := &agentcfg.AgentConfiguration{}
// WHEN
err := m.DefaultAndValidateConfiguration(cfg)
// THEN
require.NoError(t, err)
assert.Equal(t, defaultServiceAPIBaseURL, cfg.Flux.WebhookReceiverUrl)
}
func TestModule_DefaultAndValidateConfiguration_WithoutWebhookReceiverUrlConfig(t *testing.T) {
// GIVEN
m := &module{}
cfg := &agentcfg.AgentConfiguration{
Flux: &agentcfg.FluxCF{},
}
// WHEN
err := m.DefaultAndValidateConfiguration(cfg)
// THEN
require.NoError(t, err)
assert.Equal(t, defaultServiceAPIBaseURL, cfg.Flux.WebhookReceiverUrl)
}
func TestModule_DefaultAndValidateConfiguration_WithWebhookReceiverUrlConfig(t *testing.T) {
// GIVEN
m := &module{}
cfg := &agentcfg.AgentConfiguration{
Flux: &agentcfg.FluxCF{
WebhookReceiverUrl: "https://example.com",
},
}
// WHEN
err := m.DefaultAndValidateConfiguration(cfg)
// THEN
require.NoError(t, err)
assert.Equal(t, "https://example.com", cfg.Flux.WebhookReceiverUrl)
}
func TestModule_DefaultAndValidateConfiguration_WithDefaultEnabledFluxModule(t *testing.T) {
// GIVEN
m := &module{}
cfg := &agentcfg.AgentConfiguration{
Flux: &agentcfg.FluxCF{},
}
// WHEN
err := m.DefaultAndValidateConfiguration(cfg)
// THEN
require.NoError(t, err)
assert.True(t, *cfg.Flux.Enabled)
}
func TestModule_DefaultAndValidateConfiguration_WithEnabledFluxModuleSetToFalse(t *testing.T) {
// GIVEN
m := &module{}
enabledFluxModule := false
cfg := &agentcfg.AgentConfiguration{
Flux: &agentcfg.FluxCF{
Enabled: &enabledFluxModule,
},
}
// WHEN
err := m.DefaultAndValidateConfiguration(cfg)
// THEN
require.NoError(t, err)
assert.False(t, *cfg.Flux.Enabled)
}
func TestModule_Run_NotStartedWhenNotEnabled(t *testing.T) {
// GIVEN
ctrl := gomock.NewController(t)
mockReconciliationRunner := NewMockreconciliationRunner(ctrl)
configChannel := make(chan *agentcfg.AgentConfiguration, 1)
m := &module{
runner: mockReconciliationRunner,
}
enabledFluxModule := false
cfg := &agentcfg.AgentConfiguration{
Flux: &agentcfg.FluxCF{
Enabled: &enabledFluxModule,
},
}
// THEN
mockReconciliationRunner.EXPECT().run(gomock.Any(), gomock.Any()).Times(0)
// WHEN
err := m.DefaultAndValidateConfiguration(cfg)
require.NoError(t, err)
configChannel <- cfg
close(configChannel)
m.Run(context.Background(), configChannel)
}
func TestModule_Run_StartedWhenEnabledNotGiven(t *testing.T) {
// GIVEN
ctrl := gomock.NewController(t)
mockReconciliationRunner := NewMockreconciliationRunner(ctrl)
configChannel := make(chan *agentcfg.AgentConfiguration, 1)
m := &module{
runner: mockReconciliationRunner,
}
cfg := &agentcfg.AgentConfiguration{
Flux: &agentcfg.FluxCF{},
}
// THEN
mockReconciliationRunner.EXPECT().run(gomock.Any(), gomock.Any()).Times(1)
// WHEN
err := m.DefaultAndValidateConfiguration(cfg)
require.NoError(t, err)
configChannel <- cfg
close(configChannel)
m.Run(context.Background(), configChannel)
}
|