Skip to content

Commit

Permalink
added tests and did some renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
yairsimantov20 committed Feb 8, 2024
1 parent 99d969e commit 4a3e0f1
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 23 deletions.
10 changes: 5 additions & 5 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func Init() {
}

func NewConfiguration() (*port.Config, error) {
overrides := &port.Config{
config := &port.Config{
StateKey: ApplicationConfig.StateKey,
EventListenerType: ApplicationConfig.EventListenerType,
CreateDefaultResources: ApplicationConfig.CreateDefaultResources,
Expand All @@ -61,15 +61,15 @@ func NewConfiguration() (*port.Config, error) {
if err != nil {
v = []byte("{}")
klog.Infof("Config file not found, using defaults")
return overrides, nil
return config, nil
}
klog.Infof("Config file found")
err = yaml.Unmarshal(v, &overrides)
err = yaml.Unmarshal(v, &config)
if err != nil {
return nil, fmt.Errorf("failed loading configuration: %w", err)
}

overrides.StateKey = strings.ToLower(overrides.StateKey)
config.StateKey = strings.ToLower(config.StateKey)

return overrides, nil
return config, nil
}
8 changes: 3 additions & 5 deletions pkg/config/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ type ApplicationConfiguration struct {
EventListenerType string
CreateDefaultResources bool
OverwriteConfigurationOnRestart bool
// Deprecated: use IntegrationAppConfig instead. Used for updating the Port integration config on startup.
Resources []port.Resource
// Deprecated: use IntegrationAppConfig instead. Used for updating the Port integration config on startup.
DeleteDependents bool `json:"deleteDependents,omitempty"`
// Deprecated: use IntegrationAppConfig instead. Used for updating the Port integration config on startup.
// These Configurations are used only for setting up the Integration on installation or when using OverwriteConfigurationOnRestart flag.
Resources []port.Resource
DeleteDependents bool `json:"deleteDependents,omitempty"`
CreateMissingRelatedEntities bool `json:"createMissingRelatedEntities,omitempty"`
}
50 changes: 48 additions & 2 deletions pkg/defaults/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func Test_InitIntegration_ExistingIntegration(t *testing.T) {
checkResourcesDoesNotExist(f, []string{"workload", "namespace", "cluster"}, []string{"workload_overview_dashboard", "availability_scorecard_dashboard"})
}

func Test_InitIntegration_DeprecatedResourcesConfiguration(t *testing.T) {
func Test_InitIntegration_LocalResourcesConfiguration(t *testing.T) {
f := NewFixture(t)
err := integration.CreateIntegration(f.portClient, f.stateKey, "", nil)
if err != nil {
Expand Down Expand Up @@ -233,7 +233,7 @@ func Test_InitIntegration_DeprecatedResourcesConfiguration(t *testing.T) {
checkResourcesDoesNotExist(f, []string{"workload", "namespace", "cluster"}, []string{"workload_overview_dashboard", "availability_scorecard_dashboard"})
}

func Test_InitIntegration_DeprecatedResourcesConfiguration_ExistingIntegration_EmptyConfiguration(t *testing.T) {
func Test_InitIntegration_LocalResourcesConfiguration_ExistingIntegration_EmptyConfiguration(t *testing.T) {
f := NewFixture(t)
err := integration.CreateIntegration(f.portClient, f.stateKey, "POLLING", nil)
if err != nil {
Expand All @@ -253,3 +253,49 @@ func Test_InitIntegration_DeprecatedResourcesConfiguration_ExistingIntegration_E

checkResourcesDoesNotExist(f, []string{"workload", "namespace", "cluster"}, []string{"workload_overview_dashboard", "availability_scorecard_dashboard"})
}

func Test_InitIntegration_LocalResourcesConfiguration_ExistingIntegration_WithConfiguration_WithOverwriteConfigurationOnRestartFlag(t *testing.T) {
f := NewFixture(t)
expectedResources := &port.IntegrationAppConfig{
Resources: []port.Resource{
{
Kind: "workload",
Port: port.Port{
Entity: port.EntityMappings{
Mappings: []port.EntityMapping{
{
Identifier: "\"workload\"",
Title: "\"Workload\"",
Blueprint: "\"workload\"",
Icon: "\"Microservice\"",
Properties: map[string]string{
"namespace": "\"default\"",
},
},
},
},
},
},
},
}
err := integration.CreateIntegration(f.portClient, f.stateKey, "POLLING", expectedResources)
if err != nil {
t.Errorf("Error creating Port integration: %s", err.Error())
}

expectedResources.Resources[0].Kind = "namespace"
e := InitIntegration(f.portClient, &port.Config{
StateKey: f.stateKey,
EventListenerType: "KAFKA",
Resources: expectedResources.Resources,
CreateDefaultResources: true,
OverwriteConfigurationOnRestart: true,
})
assert.Nil(t, e)

i, err := integration.GetIntegration(f.portClient, f.stateKey)
assert.Nil(t, err)
assert.Equal(t, expectedResources, i.Config.Resources)

checkResourcesDoesNotExist(f, []string{"workload", "namespace", "cluster"}, []string{"workload_overview_dashboard", "availability_scorecard_dashboard"})
}
20 changes: 9 additions & 11 deletions pkg/port/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,13 @@ type IntegrationAppConfig struct {
}

type Config struct {
ResyncInterval uint `yaml:"resyncInterval,omitempty" json:"resyncInterval,omitempty"`
StateKey string `yaml:"stateKey,omitempty" json:"stateKey,omitempty"`
EventListenerType string `yaml:"eventListenerType,omitempty" json:"eventListenerType,omitempty"`
CreateDefaultResources bool `yaml:"createDefaultResources,omitempty" json:"createDefaultResources,omitempty"`
OverwriteConfigurationOnRestart bool `yaml:"overwriteConfigurationOnRestart,omitempty" json:"overwriteConfigurationOnRestart,omitempty"`
// Deprecated: use IntegrationAppConfig instead. Used for updating the Port integration config on startup.
Resources []Resource `yaml:"resources,omitempty" json:"resources,omitempty"`
// Deprecated: use IntegrationAppConfig instead. Used for updating the Port integration config on startup.
DeleteDependents bool `yaml:"deleteDependents,omitempty" json:"deleteDependents,omitempty"`
// Deprecated: use IntegrationAppConfig instead. Used for updating the Port integration config on startup.
CreateMissingRelatedEntities bool `yaml:"createMissingRelatedEntities,omitempty" json:"createMissingRelatedEntities,omitempty"`
ResyncInterval uint `yaml:"resyncInterval,omitempty"`
StateKey string `yaml:"stateKey,omitempty"`
EventListenerType string `yaml:"eventListenerType,omitempty"`
CreateDefaultResources bool `yaml:"createDefaultResources,omitempty"`
OverwriteConfigurationOnRestart bool `yaml:"overwriteConfigurationOnRestart,omitempty"`
// These Configurations are used only for setting up the Integration on installation or when using OverwriteConfigurationOnRestart flag.
Resources []Resource `yaml:"resources,omitempty"`
DeleteDependents bool `yaml:"deleteDependents,omitempty"`
CreateMissingRelatedEntities bool `yaml:"createMissingRelatedEntities,omitempty"`
}

0 comments on commit 4a3e0f1

Please sign in to comment.